added actions table, makes it easier to add time based commands

reactivated bench with a 2400 limit
help command just outputs command structs because that's easier
lower all commands to account for caps mistakes
some hardcoded nick bits :\
removed disused db structs
This commit is contained in:
alex 2018-11-15 02:11:45 +00:00
parent 5c24c985d3
commit 7f72f6e3ec
2 changed files with 74 additions and 42 deletions

View File

@ -1,12 +1,12 @@
package wat package wat
import ( import (
"fmt"
"time" "time"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite" _ "github.com/jinzhu/gorm/dialects/sqlite"
) )
import "fmt"
type Player struct { type Player struct {
gorm.Model gorm.Model
@ -22,6 +22,12 @@ type Player struct {
CoinsLost uint64 CoinsLost uint64
} }
type Action struct {
PlayerId uint `gorm:"primary_key;auto_increment:false"`
Type ActionType `gorm:"primary_key;auto_increment:false"`
Performed int64
}
func (p *Player) LoseCoins(coins uint64) { func (p *Player) LoseCoins(coins uint64) {
p.Coins -= coins p.Coins -= coins
p.CoinsLost += coins p.CoinsLost += coins
@ -41,25 +47,6 @@ func (p *Player) Level(xp int64) int64 {
} }
} }
type Ledger struct {
PlayerId uint `gorm:"primary_key"`
Time int64
Balance int64
Log string
}
type Item struct {
PlayerId uint
Name string `gorm:"primary_key"`
Price int64
}
type PlayerItem struct {
PlayerId uint
ItemId int
Count int
}
type WatDb struct { type WatDb struct {
db *gorm.DB db *gorm.DB
} }
@ -71,7 +58,7 @@ func NewWatDb() *WatDb {
if err != nil { if err != nil {
panic(err) panic(err)
} }
w.db.AutoMigrate(&Player{}, &Ledger{}, &Item{}, &PlayerItem{}) w.db.AutoMigrate(&Action{}, &Player{})
return &w return &w
} }
@ -85,7 +72,6 @@ func (w *WatDb) User(nick, host string, create bool) Player {
player.Host = host player.Host = host
w.db.Create(&player) w.db.Create(&player)
w.db.First(&player, "nick = ? or host = ?", nick, host) w.db.First(&player, "nick = ? or host = ?", nick, host)
w.db.Create(&Ledger{player.Model.ID, time.Now().Unix(), 0, "creation"})
} }
return player return player
} }
@ -97,6 +83,30 @@ func (w *WatDb) Update(upd ...interface{}) {
} }
} }
const (
Action_Mine ActionType = 1
Action_Rest ActionType = 2
Action_Lift ActionType = 3
)
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()}
if w.db.First(&action, "type = ? AND player_id = ?", actionType, player.ID).RecordNotFound() {
w.db.Create(&action)
} else {
action.Performed = time.Now().Unix()
w.Update(&action)
}
}
func (w *WatDb) TopLost() []Player { func (w *WatDb) TopLost() []Player {
var user = make([]Player, 10) var user = make([]Player, 10)
w.db.Limit(10).Order("coins_lost desc").Find(&user) w.db.Limit(10).Order("coins_lost desc").Find(&user)
@ -105,6 +115,6 @@ func (w *WatDb) TopLost() []Player {
func (w *WatDb) TopTen() []Player { func (w *WatDb) TopTen() []Player {
var user = make([]Player, 10) var user = make([]Player, 10)
w.db.Limit(10).Order("coins desc").Find(&user) w.db.Where("nick != 'watt'").Limit(10).Order("coins desc").Find(&user)
return user return user
} }

View File

@ -19,6 +19,10 @@ type WatGame struct {
lifeCommands map[string](func(*Player, []string) string) lifeCommands map[string](func(*Player, []string) string)
} }
var currency = "watcoin"
var currencys = "watcoins"
var unconscious = "wat, your hands fumble and fail you. try resting, weakling."
func NewWatGame(bot *WatBot, db *WatDb) *WatGame { func NewWatGame(bot *WatBot, db *WatDb) *WatGame {
g := WatGame{bot, db, Player{}, nil, nil} g := WatGame{bot, db, Player{}, nil, nil}
g.me = g.db.User(bot.Nick, "tripsit/user/"+bot.Nick, true) g.me = g.db.User(bot.Nick, "tripsit/user/"+bot.Nick, true)
@ -46,25 +50,16 @@ func NewWatGame(bot *WatBot, db *WatDb) *WatGame {
return &g return &g
} }
var currency = "watcoin"
var currencys = "watcoins"
var unconscious = "wat, your hands fumble and fail you. try resting, weakling."
var helpText = fmt.Sprintf("coins <nick>, watch <nick>, topten, mine, send <nick> <%s>, roll <%s>, steal <nick> <%s>, frame <nick> <%s>, punch <nick>", currency, currency, currency, currency)
//var rules = "A new account is created with 5 hours time credit. Mining exchanges time credit for %s: 1-10h: 1 p/h; >10h: 10 p/h; >1 day: 50 p/h; >1 month: 1000 p/h."
// missing
// invent, create, give inventory
func (g *WatGame) Msg(m *irc.Message, player *Player, fields []string) { func (g *WatGame) Msg(m *irc.Message, player *Player, fields []string) {
command := strings.ToLower(fields[0])
reply := "" reply := ""
if g.commands[fields[0]] != nil { if g.commands[command] != nil {
reply = g.commands[fields[0]](player, fields) reply = g.commands[command](player, fields)
} else { } else {
// one liners // one liners
switch strings.ToLower(fields[0]) { switch strings.ToLower(command) {
case "help": case "help":
reply = helpText reply = g.help()
case "toplost": case "toplost":
reply = fmt.Sprintf("%s losers: %s", currency, g.TopLost()) reply = fmt.Sprintf("%s losers: %s", currency, g.TopLost())
case "topten": case "topten":
@ -75,16 +70,33 @@ func (g *WatGame) Msg(m *irc.Message, player *Player, fields []string) {
reply = "I LOVE BUTTS" reply = "I LOVE BUTTS"
} }
} }
if g.lifeCommands[fields[0]] != nil { if g.lifeCommands[command] != nil {
if !player.Conscious() { if !player.Conscious() {
reply = unconscious reply = unconscious
} else { } else {
reply = g.lifeCommands[fields[0]](player, fields) reply = g.lifeCommands[command](player, fields)
} }
} }
g.bot.reply(m, reply) g.bot.reply(m, reply)
} }
func (g *WatGame) help() string {
ret := ""
for cmd, _ := range g.commands {
if len(ret) > 0 {
ret += ", "
}
ret += cmd
}
for cmd, _ := range g.lifeCommands {
if len(ret) > 0 {
ret += ", "
}
ret += cmd
}
return ret
}
func (g *WatGame) RandInt(max int64) uint64 { func (g *WatGame) RandInt(max int64) uint64 {
i, _ := rand.Int(rand.Reader, big.NewInt(max)) i, _ := rand.Int(rand.Reader, big.NewInt(max))
return i.Uint64() return i.Uint64()
@ -193,19 +205,24 @@ func (g *WatGame) Punch(player *Player, fields []string) string {
chance := g.RandInt(6) + 1 chance := g.RandInt(6) + 1
dmg := g.RandInt(6) + 1 dmg := g.RandInt(6) + 1
ret := fmt.Sprintf("%s rolls a d6... %s ", player.Nick, player.Nick) ret := fmt.Sprintf("%s rolls a d6... %s ", player.Nick, player.Nick)
if chance > 3 {
dmg += uint64(player.Level(player.Anarchy)) dmg += uint64(player.Level(player.Anarchy))
if chance > 3 {
ret += fmt.Sprintf("hits %s for %d points of damage! ", target.Nick, dmg) ret += fmt.Sprintf("hits %s for %d points of damage! ", target.Nick, dmg)
target.Health -= int64(dmg) target.Health -= int64(dmg)
g.db.Update(target) g.db.Update(target)
if target.Health <= 0 { if target.Health <= 0 {
ret += target.Nick + " has fallen unconscious." ret += target.Nick + " has fallen unconscious."
} else {
ret += fmt.Sprintf("%s has %dHP left", target.Nick, target.Health)
} }
} else { } else {
ret += fmt.Sprintf("fumbles, and punches themselves in confusion! %d self-damage. ", dmg) ret += fmt.Sprintf("fumbles, and punches themselves in confusion! %d self-damage. ", dmg)
player.Health -= int64(dmg) player.Health -= int64(dmg * 2)
player.Anarchy -= 1
if player.Health <= 0 { if player.Health <= 0 {
ret += player.Nick + " has fallen unconscious." ret += player.Nick + " has fallen unconscious."
} else {
ret += fmt.Sprintf("%s has %dHP left", player.Nick, player.Health)
} }
g.db.Update(player) g.db.Update(player)
} }
@ -348,7 +365,11 @@ func (g *WatGame) Rest(player *Player, fields []string) string {
} }
func (g *WatGame) Bench(player *Player, fields []string) string { func (g *WatGame) Bench(player *Player, fields []string) string {
return "meh" delta := g.db.LastActed(player, Action_Lift)
minTime := int64(2400)
if delta != 0 && delta-time.Now().Unix() < minTime {
return "you're tired. no more lifting for now."
}
weight := g.RandInt(370) + 50 weight := g.RandInt(370) + 50
reps := g.RandInt(10) reps := g.RandInt(10)
value := int64(0) value := int64(0)
@ -364,6 +385,7 @@ func (g *WatGame) Bench(player *Player, fields []string) string {
value = 10 value = 10
reply += "four twenty blaze it bro! " reply += "four twenty blaze it bro! "
} }
g.db.Act(player, Action_Lift)
player.Anarchy += value player.Anarchy += value
g.db.Update(player) g.db.Update(player)
reply += fmt.Sprintf("ur %d stronger lol", value) reply += fmt.Sprintf("ur %d stronger lol", value)