2018-10-21 13:57:23 +02:00
|
|
|
package wat
|
|
|
|
|
|
|
|
import (
|
2018-11-15 03:11:45 +01:00
|
|
|
"fmt"
|
2018-10-21 13:57:23 +02:00
|
|
|
"time"
|
2018-11-08 11:40:08 +01:00
|
|
|
|
2021-03-10 15:03:48 +01:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/driver/sqlite"
|
2018-10-21 13:57:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Player struct {
|
|
|
|
gorm.Model
|
2018-11-08 11:40:08 +01:00
|
|
|
Nick string
|
|
|
|
Host string
|
|
|
|
Watting int64
|
|
|
|
Anarchy int64
|
|
|
|
Trickery int64
|
2021-03-10 15:03:48 +01:00
|
|
|
Coins uint64 `gorm:"default:100"`
|
2018-11-08 11:40:08 +01:00
|
|
|
Health int64
|
|
|
|
LastMined int64
|
2018-10-21 13:57:23 +02:00
|
|
|
LastRested int64
|
2018-11-14 23:38:32 +01:00
|
|
|
CoinsLost uint64
|
2018-12-04 00:21:11 +01:00
|
|
|
Bankrupcy int64 // how many times someone has been bankrupt
|
2018-10-26 13:40:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 03:11:45 +01:00
|
|
|
type Action struct {
|
|
|
|
PlayerId uint `gorm:"primary_key;auto_increment:false"`
|
|
|
|
Type ActionType `gorm:"primary_key;auto_increment:false"`
|
|
|
|
Performed int64
|
|
|
|
}
|
|
|
|
|
2018-11-14 23:38:32 +01:00
|
|
|
func (p *Player) LoseCoins(coins uint64) {
|
2018-10-26 13:40:02 +02:00
|
|
|
p.Coins -= coins
|
|
|
|
p.CoinsLost += coins
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Conscious() bool {
|
2018-10-23 01:15:28 +02:00
|
|
|
return (p.Health > 0)
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Level(xp int64) int64 {
|
|
|
|
if xp < 100 {
|
2018-11-08 11:40:08 +01:00
|
|
|
return xp / 10
|
2018-10-21 13:57:23 +02:00
|
|
|
} else if xp < 900 {
|
2018-11-08 11:40:08 +01:00
|
|
|
return 10 + (xp / 100)
|
2018-10-21 13:57:23 +02:00
|
|
|
} else {
|
|
|
|
return 99
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type WatDb struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
2024-10-10 00:04:16 +02:00
|
|
|
func NewWatDb(dbpath string) *WatDb {
|
2018-10-21 13:57:23 +02:00
|
|
|
w := WatDb{}
|
2021-03-10 15:03:48 +01:00
|
|
|
var err error
|
2024-10-10 00:04:16 +02:00
|
|
|
w.db, err = gorm.Open(sqlite.Open(dbpath), &gorm.Config{})
|
2018-10-21 13:57:23 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-10 15:03:48 +01:00
|
|
|
w.db.AutoMigrate(&Action{}, &Player{})
|
2018-10-21 13:57:23 +02:00
|
|
|
return &w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WatDb) User(nick, host string, create bool) Player {
|
|
|
|
var player Player
|
2024-10-02 22:30:06 +02:00
|
|
|
query := "nick = ?"
|
|
|
|
if host != "" {
|
|
|
|
query = query + " or host = ?"
|
|
|
|
}
|
2018-10-21 13:57:23 +02:00
|
|
|
// Try and get a user
|
2024-10-02 22:30:06 +02:00
|
|
|
if err := w.db.First(&player, query, nick, host).Error; err != nil && create {
|
2018-10-21 13:57:23 +02:00
|
|
|
fmt.Printf("Creating user: %s\n", err.Error())
|
|
|
|
// No user, make another
|
|
|
|
player.Nick = nick
|
|
|
|
player.Host = host
|
|
|
|
w.db.Create(&player)
|
|
|
|
w.db.First(&player, "nick = ? or host = ?", nick, host)
|
|
|
|
}
|
|
|
|
return player
|
|
|
|
}
|
|
|
|
|
2018-11-08 11:40:08 +01:00
|
|
|
func (w *WatDb) Update(upd ...interface{}) {
|
|
|
|
for _, u := range upd {
|
2018-12-04 00:21:11 +01:00
|
|
|
//fmt.Printf("Updating %+v\n", u)
|
2018-11-08 11:40:08 +01:00
|
|
|
w.db.Save(u)
|
|
|
|
}
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 03:11:45 +01:00
|
|
|
const (
|
2019-01-04 10:20:39 +01:00
|
|
|
Action_Mine ActionType = 1
|
|
|
|
Action_Rest ActionType = 2
|
|
|
|
Action_Lift ActionType = 3
|
|
|
|
Action_Riot ActionType = 4
|
|
|
|
Action_Bankrupt ActionType = 5
|
2018-11-15 03:11:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ActionType int
|
|
|
|
|
|
|
|
func (w *WatDb) LastActed(player *Player, actionType ActionType) int64 {
|
|
|
|
action := Action{}
|
|
|
|
w.db.First(&action, "type = ? AND player_id = ?", actionType, player.Model.ID)
|
|
|
|
return action.Performed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WatDb) Act(player *Player, actionType ActionType) {
|
|
|
|
action := Action{player.Model.ID, actionType, time.Now().Unix()}
|
2021-03-10 15:03:48 +01:00
|
|
|
if err := w.db.First(&action, "type = ? AND player_id = ?", actionType, player.Model.ID).Error; err != nil {
|
2018-11-15 03:11:45 +01:00
|
|
|
w.db.Create(&action)
|
|
|
|
} else {
|
|
|
|
action.Performed = time.Now().Unix()
|
|
|
|
w.Update(&action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:20:39 +01:00
|
|
|
func (w *WatDb) Strongest() []Player {
|
2019-06-28 19:54:32 +02:00
|
|
|
return w.GetTopColumn("anarchy")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WatDb) GetTopColumn(tipe string) []Player {
|
2019-01-04 10:20:39 +01:00
|
|
|
var user = make([]Player, 10)
|
2019-06-28 19:54:32 +02:00
|
|
|
w.db.Limit(10).Order(tipe + " desc").Find(&user)
|
2019-01-04 10:20:39 +01:00
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2019-06-28 19:54:32 +02:00
|
|
|
func (w *WatDb) Healthiest() []Player {
|
|
|
|
return w.GetTopColumn("health")
|
|
|
|
}
|
|
|
|
|
2018-11-14 23:38:32 +01:00
|
|
|
func (w *WatDb) TopLost() []Player {
|
2019-06-28 19:54:32 +02:00
|
|
|
return w.GetTopColumn("coins_lost")
|
2018-11-14 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2019-07-10 17:29:44 +02:00
|
|
|
func (w *WatDb) Bankruptest() []Player {
|
|
|
|
return w.GetTopColumn("bankrupcy")
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:57:23 +02:00
|
|
|
func (w *WatDb) TopTen() []Player {
|
|
|
|
var user = make([]Player, 10)
|
2018-11-15 03:11:45 +01:00
|
|
|
w.db.Where("nick != 'watt'").Limit(10).Order("coins desc").Find(&user)
|
2018-10-21 13:57:23 +02:00
|
|
|
return user
|
|
|
|
}
|