final start/reset touches

This commit is contained in:
shoopea 2021-12-04 13:39:15 +08:00
parent 84579af511
commit acf58c99ef
4 changed files with 17 additions and 19 deletions

14
bot.go
View File

@ -284,6 +284,12 @@ func botActuallyReset(m *tb.Message) {
bot.SendChat(m.Chat.ID, "Request expired.") bot.SendChat(m.Chat.ID, "Request expired.")
} else { } else {
cfg.Game.Started = false cfg.Game.Started = false
for _, cc := range cfg.Clients {
cc.Ready = false
cc.CompanyID = 255
cc.Online = false
cc.TimeLeft = 0
}
bot.SendChat(m.Chat.ID, "Game resetted.") bot.SendChat(m.Chat.ID, "Game resetted.")
} }
delete(botActionMap, ref) delete(botActionMap, ref)
@ -350,6 +356,7 @@ func botActuallyStart(m *tb.Message) {
cfg.Game.StartDate = time.Now() cfg.Game.StartDate = time.Now()
for _, cc := range cfg.Clients { for _, cc := range cfg.Clients {
cc.Ready = true cc.Ready = true
cc.TimeLeft = cfg.Game.StartingAllotment
} }
bot.SendChat(m.Chat.ID, "Game started !") bot.SendChat(m.Chat.ID, "Game started !")
} }
@ -745,6 +752,7 @@ func botRegister(m *tb.Message) {
Username: m.Sender.Username, Username: m.Sender.Username,
CompanyID: 255, CompanyID: 255,
TimeLeft: 0, TimeLeft: 0,
Ready: cfg.Game.Started,
} }
cfg.Clients[m.Sender.ID] = cc cfg.Clients[m.Sender.ID] = cc
} else { } else {
@ -809,11 +817,13 @@ func botRegister(m *tb.Message) {
if len(coList) == 1 { if len(coList) == 1 {
for id, _ := range coList { for id, _ := range coList {
cc.CompanyID = id cc.CompanyID = id
if cc.TimeLeft == 0 { if cc.TimeLeft == 0 && cfg.Game.Started {
days := int(time.Now().Sub(cfg.Game.StartDate).Hours() / 24) days := int(time.Now().Sub(cfg.Game.StartDate).Hours() / 24)
cc.TimeLeft = cfg.Game.StartingAllotment + cfg.Game.DailyAllotment*time.Duration(days) cc.TimeLeft = cfg.Game.StartingAllotment + cfg.Game.DailyAllotment*time.Duration(days)
}
bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s registered %s (with %s playable)", cc.Username, srv.Status.Companies[cc.CompanyID].Name, cc.TimeLeft.Round(time.Second))) bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s registered %s (with %s playable)", cc.Username, srv.Status.Companies[cc.CompanyID].Name, cc.TimeLeft.Round(time.Second)))
} else {
bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s registered %s", cc.Username, srv.Status.Companies[cc.CompanyID].Name))
}
return return
} }
} }

View File

@ -45,9 +45,7 @@ func main() {
srv = &ServerTTD{ srv = &ServerTTD{
Config: cfg.Server, Config: cfg.Server,
Data: &ServerDataTTD{ Data: &ServerDataTTD{},
Started: cfg.Game.Started,
},
Status: &ServerStatusTTD{}, Status: &ServerStatusTTD{},
} }
go srv.Start() go srv.Start()

10
ttd.go
View File

@ -54,7 +54,6 @@ type ServerDataTTD struct {
LastClientCompute time.Time LastClientCompute time.Time
Conn net.Conn Conn net.Conn
Stop chan struct{} Stop chan struct{}
Started bool
} }
var updateHeartBeat = 5 * time.Second var updateHeartBeat = 5 * time.Second
@ -594,15 +593,6 @@ func (s *ServerTTD) ComputeClientTime() {
t := time.Now() t := time.Now()
if !cfg.Game.Started { if !cfg.Game.Started {
s.Data.LastClientCompute = t s.Data.LastClientCompute = t
s.Data.Started = false
return
}
if !s.Data.Started {
for _, cc := range cfg.Clients {
cc.TimeLeft = cfg.Game.StartingAllotment
}
bot.SendChat(cfg.Telegram.ChatID, "Starting allotment credited.")
s.Data.Started = true
return return
} }

View File

@ -1,6 +1,6 @@
// Code generated by version.sh (@generated) DO NOT EDIT. // Code generated by version.sh (@generated) DO NOT EDIT.
package main package main
var githash = "00d4b86" var githash = "84579af"
var buildstamp = "2021-12-04_05:24:10" var buildstamp = "2021-12-04_05:35:59"
var commits = "212" var commits = "213"
var version = "00d4b86-b212 - 2021-12-04_05:24:10" var version = "84579af-b213 - 2021-12-04_05:35:59"