initial commit

This commit is contained in:
2025-08-27 23:26:36 +03:00
commit ae70c229f7
22 changed files with 1087 additions and 0 deletions

51
database/database.go Normal file
View File

@@ -0,0 +1,51 @@
package database
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type config struct {
Host string `json:"host"`
User string `json:"user"`
Password string `json:"password"`
Database string `json:"database"`
}
var Database *gorm.DB
var conf *config
func loadConfig() {
file, err := os.Open("./config.json")
if err != nil {
log.Fatalln(err)
}
data, err := io.ReadAll(file)
if err != nil {
log.Fatalln(err)
}
conf = new(config)
err = json.Unmarshal(data, conf)
if err != nil {
log.Fatalln(err)
}
}
func getDSN() string {
return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", conf.User, conf.Password, conf.Host, conf.Database)
}
func Connect() {
var err error
loadConfig()
Database, err = gorm.Open(postgres.Open(getDSN()), new(gorm.Config))
if err != nil {
log.Fatalln(err)
}
}

13
database/fraction.go Normal file
View File

@@ -0,0 +1,13 @@
package database
import "github.com/shopspring/decimal"
type Fraction struct {
ID int
Name string
OwnerID int
Owner *User
Money decimal.Decimal
Exp int
Level int
}

20
database/groups.go Normal file
View File

@@ -0,0 +1,20 @@
package database
import "github.com/shopspring/decimal"
type Group struct {
ID int
Name string
IsAdmin bool
IsVip bool
IsTester bool
Multiplier decimal.Decimal
Sale decimal.Decimal
MaxWaifus int
}
// func GetGroupById(id int) (*Group, error) {
// group := new(Group)
// // err := Database.Get(group, "select * from groups where id=$1;", id)
// return group, err
// }

30
database/shop.go Normal file
View File

@@ -0,0 +1,30 @@
package database
import "github.com/shopspring/decimal"
type ShopAuto struct {
ID int
Name string
Price decimal.Decimal
}
type ShopBusiness struct {
ID int
Name string
Price decimal.Decimal
Income decimal.Decimal
}
type ShopMaid struct {
ID int
Name string
Price decimal.Decimal
Income decimal.Decimal
}
type ShopMiner struct {
ID int
Name string
Price decimal.Decimal
Income decimal.Decimal
}

93
database/users.go Normal file
View File

@@ -0,0 +1,93 @@
package database
import (
"database/sql"
"errors"
"math"
"time"
"github.com/shopspring/decimal"
"gorm.io/gorm"
)
type User struct {
ID int
TelegramID int `gorm:"column:tg_id"`
Balance decimal.Decimal
Name string
GroupID int
Group *Group
Level int
WorkID int
Work *Work
WorkTime time.Time
Exp int
AutoID int
Auto *ShopAuto
BusinessID int
Business *ShopBusiness
MaidID int
Maid *ShopMaid
MinerID int
Miner *ShopMiner
IncomeTime time.Time
BTC decimal.Decimal
Invested decimal.Decimal
PairID int
Pair *User
Greeting string
Donat int
FractionID sql.NullInt64
Fraction *Fraction
MoneyIncome decimal.Decimal
ExpIncome int
BtcIncome decimal.Decimal
WaifuSearchTime time.Time
}
func GetOrCreateUser(tgId int, name string) (*User, error) {
user, err := GetUser(tgId)
if errors.Is(err, gorm.ErrRecordNotFound) {
user, err = CreateUser(tgId, name)
}
return user, err
}
func CreateUser(tgId int, name string) (*User, error) {
user := &User{
TelegramID: tgId,
Name: name,
}
tx := Database.Create(user)
return user, tx.Error
}
func GetUser(telegramId int) (*User, error) {
user := new(User)
tx := Database.Joins("Group").Joins("Work").Joins("Auto").Joins("Business").Joins("Maid").Joins("Miner").Joins("Fraction").Preload("Pair").Take(user, "tg_id=?", telegramId)
return user, tx.Error
}
func GetAllUsers() ([]*User, error) {
users := make([]*User, 0)
tx := Database.Joins("Group").Joins("Work").Joins("Auto").Joins("Business").Joins("Maid").Joins("Miner").Joins("Fraction").Preload("Pair").Find(&users)
return users, tx.Error
}
func CountLevel(userXp int) (int, int) {
xp := 0
nextLevel := 2
for {
xp = int(math.Pow(float64(nextLevel), 3)) * (nextLevel * 3)
if xp > userXp {
break
}
if nextLevel == 200 {
break
}
nextLevel++
}
return nextLevel - 1, xp
}

53
database/waifus.go Normal file
View File

@@ -0,0 +1,53 @@
package database
import "github.com/shopspring/decimal"
type Waifu struct {
ID int
Name string
Rarity int
ExpBonus decimal.Decimal
MoneyBonus decimal.Decimal
MarketPrice decimal.Decimal
Fandom string
Image string
OwnerID int
Owner *User
}
func GetAllWaifus() ([]*Waifu, error) {
waifus := make([]*Waifu, 0)
tx := Database.Joins("Owner").Find(&waifus).Order("id")
return waifus, tx.Error
}
func GetUserWaifus(userId int) ([]*Waifu, error) {
waifus := make([]*Waifu, 0)
tx := Database.Find(&waifus, "owner_id = ?", userId).Order("id")
return waifus, tx.Error
}
func GetFreeWaifus() ([]*Waifu, error) {
waifus := make([]*Waifu, 0)
tx := Database.Find(&waifus, "owner_id is null").Order("id")
return waifus, tx.Error
}
func GetFreeWaifusCount() (int64, error) {
var count int64 = 0
tx := Database.Model(&Waifu{}).Where("owner_id is null").Count(&count)
return count, tx.Error
}
func GetFreeWaifusWithRarity(rarity int) ([]*Waifu, error) {
waifus := make([]*Waifu, 0)
tx := Database.Find(&waifus, "owner_id is null and rarity = ?", rarity)
return waifus, tx.Error
}
func GetWaifuById(id int) (*Waifu, error) {
waifu := new(Waifu)
tx := Database.Joins("Owner").Find(waifu, id)
return waifu, tx.Error
}

24
database/works.go Normal file
View File

@@ -0,0 +1,24 @@
package database
import "github.com/shopspring/decimal"
type Work struct {
ID int
Name string
RequiredLevel int
MoneyIncome decimal.Decimal
MinExp int
MaxExp int
}
func GetWorkById(id int) (*Work, error) {
work := new(Work)
tx := Database.First(work, id)
return work, tx.Error
}
func GetAllWorks() ([]*Work, error) {
works := make([]*Work, 0)
tx := Database.Order("id").Find(&works)
return works, tx.Error
}