log middleware
This commit is contained in:
13
database/psql/fraction.go
Normal file
13
database/psql/fraction.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package psql
|
||||
|
||||
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/psql/groups.go
Normal file
20
database/psql/groups.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package psql
|
||||
|
||||
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
|
||||
// }
|
||||
10
database/psql/migrate.go
Normal file
10
database/psql/migrate.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package psql
|
||||
|
||||
import "kurumibot/database"
|
||||
|
||||
func Migrate() error {
|
||||
return database.PostgresDatabase.AutoMigrate(
|
||||
&User{}, &Fraction{}, &Group{}, &Waifu{}, &Work{},
|
||||
&ShopAuto{}, &ShopBusiness{}, &ShopMaid{}, &ShopMiner{},
|
||||
)
|
||||
}
|
||||
46
database/psql/shop.go
Normal file
46
database/psql/shop.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package psql
|
||||
|
||||
import "github.com/shopspring/decimal"
|
||||
|
||||
type ShopAuto struct {
|
||||
ID int
|
||||
Name string
|
||||
Price decimal.Decimal
|
||||
}
|
||||
|
||||
func (ShopAuto) TableName() string {
|
||||
return "shop_auto"
|
||||
}
|
||||
|
||||
type ShopBusiness struct {
|
||||
ID int
|
||||
Name string
|
||||
Price decimal.Decimal
|
||||
Income decimal.Decimal
|
||||
}
|
||||
|
||||
func (ShopBusiness) TableName() string {
|
||||
return "shop_business"
|
||||
}
|
||||
|
||||
type ShopMaid struct {
|
||||
ID int
|
||||
Name string
|
||||
Price decimal.Decimal
|
||||
Income decimal.Decimal
|
||||
}
|
||||
|
||||
func (ShopMaid) TableName() string {
|
||||
return "shop_maid"
|
||||
}
|
||||
|
||||
type ShopMiner struct {
|
||||
ID int
|
||||
Name string
|
||||
Price decimal.Decimal
|
||||
Income decimal.Decimal
|
||||
}
|
||||
|
||||
func (ShopMiner) TableName() string {
|
||||
return "shop_miner"
|
||||
}
|
||||
100
database/psql/users.go
Normal file
100
database/psql/users.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package psql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"kurumibot/database"
|
||||
"log"
|
||||
"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 `gorm:"default:1"`
|
||||
Group *Group
|
||||
Level int `gorm:"default:1"`
|
||||
Exp int `gorm:"default:0"`
|
||||
WorkID int `gorm:"default:1"`
|
||||
Work *Work
|
||||
WorkTime time.Time
|
||||
AutoID sql.NullInt64
|
||||
Auto *ShopAuto
|
||||
BusinessID sql.NullInt64
|
||||
Business *ShopBusiness
|
||||
MaidID sql.NullInt64
|
||||
Maid *ShopMaid
|
||||
MinerID sql.NullInt64
|
||||
Miner *ShopMiner
|
||||
IncomeTime time.Time
|
||||
BTC decimal.Decimal
|
||||
Invested decimal.Decimal
|
||||
PairID sql.NullInt64
|
||||
Pair *User
|
||||
Greeting string `gorm:"size:255,default:Привет"`
|
||||
Donat int `gorm:"default:0"`
|
||||
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)
|
||||
log.Println(errors.Is(err, gorm.ErrRecordNotFound))
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_, err = CreateUser(tgId, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return GetUser(tgId)
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func CreateUser(tgId int, name string) (*User, error) {
|
||||
user := &User{
|
||||
TelegramID: tgId,
|
||||
Name: name,
|
||||
}
|
||||
tx := database.PostgresDatabase.Create(user)
|
||||
return user, tx.Error
|
||||
}
|
||||
|
||||
func GetUser(telegramId int) (*User, error) {
|
||||
user := new(User)
|
||||
tx := database.PostgresDatabase.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.PostgresDatabase.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
|
||||
}
|
||||
57
database/psql/waifus.go
Normal file
57
database/psql/waifus.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package psql
|
||||
|
||||
import (
|
||||
"kurumibot/database"
|
||||
|
||||
"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.PostgresDatabase.Joins("Owner").Find(&waifus).Order("id")
|
||||
return waifus, tx.Error
|
||||
}
|
||||
|
||||
func GetUserWaifus(userId int) ([]*Waifu, error) {
|
||||
waifus := make([]*Waifu, 0)
|
||||
tx := database.PostgresDatabase.Find(&waifus, "owner_id = ?", userId).Order("id")
|
||||
return waifus, tx.Error
|
||||
}
|
||||
|
||||
func GetFreeWaifus() ([]*Waifu, error) {
|
||||
waifus := make([]*Waifu, 0)
|
||||
tx := database.PostgresDatabase.Find(&waifus, "owner_id is null").Order("id")
|
||||
return waifus, tx.Error
|
||||
}
|
||||
|
||||
func GetFreeWaifusCount() (int64, error) {
|
||||
var count int64 = 0
|
||||
tx := database.PostgresDatabase.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.PostgresDatabase.Find(&waifus, "owner_id is null and rarity = ?", rarity)
|
||||
return waifus, tx.Error
|
||||
}
|
||||
|
||||
func GetWaifuById(id int) (*Waifu, error) {
|
||||
waifu := new(Waifu)
|
||||
tx := database.PostgresDatabase.Joins("Owner").Find(waifu, id)
|
||||
return waifu, tx.Error
|
||||
}
|
||||
28
database/psql/works.go
Normal file
28
database/psql/works.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package psql
|
||||
|
||||
import (
|
||||
"kurumibot/database"
|
||||
|
||||
"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.PostgresDatabase.First(work, id)
|
||||
return work, tx.Error
|
||||
}
|
||||
|
||||
func GetAllWorks() ([]*Work, error) {
|
||||
works := make([]*Work, 0)
|
||||
tx := database.PostgresDatabase.Order("id").Find(&works)
|
||||
return works, tx.Error
|
||||
}
|
||||
Reference in New Issue
Block a user