save eco/stats and add save trigger

This commit is contained in:
shoopea 2021-12-05 15:41:47 +08:00
parent fc3fff65f3
commit 6533e87a36
5 changed files with 114 additions and 9 deletions

29
bot.go
View File

@ -73,7 +73,9 @@ func (b *Bot) BotHandlers() {
b.bot.Handle("/passwd", botPasswd)
b.bot.Handle("/say", botSay)
b.bot.Handle("/help", botHelp)
b.bot.Handle("/version", botVersion)
b.bot.Handle("/save", botSave)
b.bot.Handle("/reset", botReset)
b.bot.Handle("/ready", botReady)
@ -283,7 +285,9 @@ func botActuallyReset(m *tb.Message) {
if time.Now().Sub(a.Time) > time.Minute {
bot.SendChat(m.Chat.ID, "Request expired.")
} else {
cfg.Save("backup." + *configFlag)
cfg.Game.Started = false
cfg.Stats = make(map[uint8]map[string]*Stat)
for _, cc := range cfg.Clients {
cc.Ready = false
cc.CompanyID = 255
@ -511,6 +515,31 @@ func botVersion(m *tb.Message) {
return
}
func botSave(m *tb.Message) {
if m.Sender.ID != int(cfg.Telegram.AdminID) {
bot.SendChat(m.Chat.ID, "Only the admin can use this command.")
return
}
r := regexp.MustCompile("^\\/save @(?P<Filename>[a-zA-Z0-9._]+)")
if !r.MatchString(m.Text) {
bot.SendChat(m.Chat.ID, "Wrong usage.")
return
}
filename := r.ReplaceAllString(m.Text, "${Filename}")
err := cfg.Save(filename + ".json")
logErrorDebug(err, "botSave : Config.Save(%s)", filename+".json")
if err != nil {
bot.SendChat(m.Chat.ID, fmt.Sprintf("Error : %s", err))
} else {
bot.SendChat(m.Chat.ID, "Saved.")
}
return
}
func botTransfer(m *tb.Message) {
r := regexp.MustCompile("^\\/transfer @(?P<Username>[a-zA-Z0-9]+) (?P<Duration>[a-z0-9]+)")

View File

@ -44,10 +44,11 @@ type ClientConfig struct {
}
type Config struct {
Server *ServerConfig `json:"server"`
Telegram *TelegramConfig `json:"telegram"`
Game *GameConfig `json:"game"`
Clients map[int]*ClientConfig `json:"clients"`
Server *ServerConfig `json:"server"`
Telegram *TelegramConfig `json:"telegram"`
Game *GameConfig `json:"game"`
Clients map[int]*ClientConfig `json:"clients"`
Stats map[uint8]map[string]*Stat `json:"stats"`
}
// Init values for a config based on package defaults

28
stats.go Normal file
View File

@ -0,0 +1,28 @@
package main
import "time"
type Stat struct {
CompanyID uint8
Date time.Time
Trains int
Lorries int
Busses int
Planes int
Ships int
TrainStations int
LorryStations int
BusStops int
Airports int
Harbours int
Money int64
Loan int64
Income int64
DeliveredCargoThisQuarter int
CompanyValueLastQuarter int64
PerformanceLastQuarter int
DeliveredCargoLastQuarter int
CompanyValuePreviousQuarter int64
PerformancePreviousQuarter int
DeliveredCargoPreviousQuarter int
}

49
ttd.go
View File

@ -382,13 +382,60 @@ func (s *ServerTTD) Poll(stop chan struct{}) {
Packet: p,
}
sp.Read(buffer[:p.PLength])
logInfoDebug("Server.Poll() : PacketServerCompanyEconomy :\n- CompanyID: %d\n- M: %d\tL: %d\tI: %d\n- Delivered now: %d\tLast: %d\tPrevious: %d\n,- Performance last: %d\t Previous: %d\n- Value last: %d\t Previous: %d", sp.CompanyID, sp.Money, sp.Loan, sp.Income, sp.DeliveredCargoThisQuarter, sp.DeliveredCargoLastQuarter, sp.DeliveredCargoPreviousQuarter, sp.PerformanceLastQuarter, sp.PerformancePreviousQuarter, sp.CompanyValueLastQuarter, sp.CompanyValuePreviousQuarter)
logInfoDebug("Server.Poll() : PacketServerCompanyEconomy :\n- CompanyID: %d\n- M: %d\tL: %d\tI: %d\n- Delivered now: %d\tLast: %d\tPrevious: %d\n- Performance last: %d\t Previous: %d\n- Value last: %d\t Previous: %d", sp.CompanyID, sp.Money, sp.Loan, sp.Income, sp.DeliveredCargoThisQuarter, sp.DeliveredCargoLastQuarter, sp.DeliveredCargoPreviousQuarter, sp.PerformanceLastQuarter, sp.PerformancePreviousQuarter, sp.CompanyValueLastQuarter, sp.CompanyValuePreviousQuarter)
cStats, ok := cfg.Stats[sp.CompanyID]
if !ok {
cStats = make(map[string]*Stat)
cfg.Stats[sp.CompanyID] = cStats
}
dStats, ok := cStats[s.Status.GameDate.Format("20060102")]
if !ok {
dStats = &Stat{
CompanyID: sp.CompanyID,
Date: s.Status.GameDate,
}
cStats[s.Status.GameDate.Format("20060102")] = dStats
}
dStats.Money = int64(sp.Money)
dStats.Loan = int64(sp.Loan)
dStats.Income = sp.Income
dStats.DeliveredCargoThisQuarter = int(sp.DeliveredCargoThisQuarter)
dStats.DeliveredCargoLastQuarter = int(sp.DeliveredCargoLastQuarter)
dStats.DeliveredCargoPreviousQuarter = int(sp.DeliveredCargoPreviousQuarter)
dStats.PerformanceLastQuarter = int(sp.PerformanceLastQuarter)
dStats.PerformancePreviousQuarter = int(sp.PerformancePreviousQuarter)
dStats.CompanyValueLastQuarter = int64(sp.CompanyValueLastQuarter)
dStats.CompanyValuePreviousQuarter = int64(sp.CompanyValuePreviousQuarter)
case AdminPacketServerCompanyStats:
sp := PacketServerCompanyStats{
Packet: p,
}
sp.Read(buffer[:p.PLength])
logInfoDebug("Server.Poll() : PacketServerCompanyStats :\n- CompanyID: %d\n- Vehicles T: %d\tL: %d\tB: %d\tP: %d\tS: %d\n- Stations T: %d\tL: %d\tB: %d\tP: %d\tS: %d", sp.CompanyID, sp.Trains, sp.Lorries, sp.Busses, sp.Planes, sp.Ships, sp.TrainStations, sp.LorryStations, sp.BusStops, sp.Airports, sp.Harbours)
cStats, ok := cfg.Stats[sp.CompanyID]
if !ok {
cStats = make(map[string]*Stat)
cfg.Stats[sp.CompanyID] = cStats
}
dStats, ok := cStats[s.Status.GameDate.Format("20060102")]
if !ok {
dStats = &Stat{
CompanyID: sp.CompanyID,
Date: s.Status.GameDate,
}
cStats[s.Status.GameDate.Format("20060102")] = dStats
}
dStats.Trains = int(sp.Trains)
dStats.TrainStations = int(sp.TrainStations)
dStats.Busses = int(sp.Busses)
dStats.BusStops = int(sp.BusStops)
dStats.Lorries = int(sp.Lorries)
dStats.LorryStations = int(sp.LorryStations)
dStats.Planes = int(sp.Planes)
dStats.Airports = int(sp.Airports)
dStats.Ships = int(sp.Ships)
dStats.Harbours = int(sp.Harbours)
case AdminPacketServerChat:
sp := PacketServerChat{
Packet: p,

View File

@ -1,6 +1,6 @@
// Code generated by version.sh (@generated) DO NOT EDIT.
package main
var githash = "0a4b2f4"
var buildstamp = "2021-12-04_14:37:53"
var commits = "221"
var version = "0a4b2f4-b221 - 2021-12-04_14:37:53"
var githash = "fc3fff6"
var buildstamp = "2021-12-05_07:41:29"
var commits = "222"
var version = "fc3fff6-b222 - 2021-12-05_07:41:29"