implement start/reset

This commit is contained in:
shoopea 2021-12-04 13:24:45 +08:00
parent 00d4b8667f
commit 84579af511
4 changed files with 141 additions and 70 deletions

181
bot.go
View File

@ -140,7 +140,6 @@ func botUnpause(m *tb.Message) {
}
func botDelete(m *tb.Message) {
logInfoDebug("[%d] %s(%d) | %s(%d) : delete : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
r := regexp.MustCompile("\\/delete (?P<CompanyID>[0-9]+)")
id := uint8(255)
if r.MatchString(m.Text) {
@ -190,6 +189,33 @@ func botDelete(m *tb.Message) {
return
}
func botActuallyDelete(m *tb.Message) {
r := regexp.MustCompile("^\\/delete_(?P<Ref>[a-f0-9]{16})$")
ref := r.ReplaceAllString(m.Text, "${Ref}")
a, ok := botActionMap[ref]
if !ok {
bot.SendChat(m.Chat.ID, "No corresponding request.")
return
}
if a.Action != "DeleteCompany" {
bot.SendChat(m.Chat.ID, "Not a delete request.")
return
}
if a.UserID != m.Sender.ID {
bot.SendChat(m.Chat.ID, "Requesting user has to confirm himself.")
return
}
if time.Now().Sub(a.Time) > time.Minute {
bot.SendChat(m.Chat.ID, "Request expired.")
} else {
srv.DeleteCompany(a.CompanyID)
bot.SendChat(m.Chat.ID, "Company deleted.")
}
delete(botActionMap, ref)
return
}
func botCompanies(m *tb.Message) {
str := "Companies :"
for k, v := range srv.Status.Companies {
@ -212,6 +238,11 @@ func botReset(m *tb.Message) {
return
}
if !cfg.Game.Started {
bot.SendChat(m.Chat.ID, "Game is not started.")
return
}
b := make([]byte, 8)
_, err := rand.Read(b)
logErrorDebug(err, "botReset : rand.Read")
@ -232,11 +263,48 @@ func botReset(m *tb.Message) {
return
}
func botStart(m *tb.Message) {
if m.Sender.ID != int(cfg.Telegram.AdminID) {
bot.SendChat(m.Chat.ID, "Only the admin can use this command.")
func botActuallyReset(m *tb.Message) {
r := regexp.MustCompile("^\\/reset_(?P<Ref>[a-f0-9]{16})$")
ref := r.ReplaceAllString(m.Text, "${Ref}")
a, ok := botActionMap[ref]
if !ok {
bot.SendChat(m.Chat.ID, "No corresponding request.")
return
}
if a.Action != "ResetGame" {
bot.SendChat(m.Chat.ID, "Not a reset request.")
return
}
if a.UserID != m.Sender.ID {
bot.SendChat(m.Chat.ID, "Requesting user has to confirm himself.")
return
}
if time.Now().Sub(a.Time) > time.Minute {
bot.SendChat(m.Chat.ID, "Request expired.")
} else {
cfg.Game.Started = false
bot.SendChat(m.Chat.ID, "Game resetted.")
}
delete(botActionMap, ref)
return
}
func botStart(m *tb.Message) {
if cfg.Game.Started {
bot.SendChat(m.Chat.ID, "Game already started.")
return
}
actuallyReady := true
for _, cc := range cfg.Clients {
if !cc.Ready {
actuallyReady = false
}
}
if m.Sender.ID != int(cfg.Telegram.AdminID) && !actuallyReady {
bot.SendChat(m.Chat.ID, "Not all players are ready. Only the admin can force the start.")
}
b := make([]byte, 8)
_, err := rand.Read(b)
@ -258,7 +326,44 @@ func botStart(m *tb.Message) {
return
}
func botActuallyStart(m *tb.Message) {
r := regexp.MustCompile("^\\/start_(?P<Ref>[a-f0-9]{16})$")
ref := r.ReplaceAllString(m.Text, "${Ref}")
a, ok := botActionMap[ref]
if !ok {
bot.SendChat(m.Chat.ID, "No corresponding request.")
return
}
if a.Action != "StartGame" {
bot.SendChat(m.Chat.ID, "Not a game start request.")
return
}
if a.UserID != m.Sender.ID {
bot.SendChat(m.Chat.ID, "Requesting user has to confirm himself.")
return
}
if time.Now().Sub(a.Time) > time.Minute {
bot.SendChat(m.Chat.ID, "Request expired.")
} else {
cfg.Game.Started = true
cfg.Game.StartDate = time.Now()
for _, cc := range cfg.Clients {
cc.Ready = true
}
bot.SendChat(m.Chat.ID, "Game started !")
}
delete(botActionMap, ref)
return
}
func botReady(m *tb.Message) {
if cfg.Game.Started {
bot.SendChat(m.Chat.ID, "Game is already started.")
return
}
cc, ok := cfg.Clients[m.Sender.ID]
if !ok {
bot.SendChat(m.Chat.ID, "Player not registered")
@ -277,9 +382,13 @@ func botReady(m *tb.Message) {
if cc.Ready {
cc.Ready = false
ready--
waiting++
bot.SendChat(m.Chat.ID, fmt.Sprintf("Player not ready anymore. Only %d players ready now.", ready))
} else {
cc.Ready = true
ready++
waiting--
bot.SendChat(m.Chat.ID, fmt.Sprintf("Player is now ready. Still waiting for %d players.", waiting))
}
@ -743,77 +852,19 @@ func botText(m *tb.Message) {
r := regexp.MustCompile("^\\/delete_(?P<Ref>[a-f0-9]{16})$")
if r.MatchString(m.Text) {
ref := r.ReplaceAllString(m.Text, "${Ref}")
a, ok := botActionMap[ref]
if !ok {
bot.SendChat(m.Chat.ID, "No corresponding request.")
return
}
if a.Action != "DeleteCompany" {
bot.SendChat(m.Chat.ID, "Not a delete request.")
return
}
if a.UserID != m.Sender.ID {
bot.SendChat(m.Chat.ID, "Requesting user has to confirm himself.")
return
}
if time.Now().Sub(a.Time) > time.Minute {
bot.SendChat(m.Chat.ID, "Request expired.")
} else {
srv.DeleteCompany(a.CompanyID)
bot.SendChat(m.Chat.ID, "Company deleted.")
}
delete(botActionMap, ref)
botActuallyDelete(m)
return
}
r = regexp.MustCompile("^\\/reset_(?P<Ref>[a-f0-9]{16})$")
if r.MatchString(m.Text) {
ref := r.ReplaceAllString(m.Text, "${Ref}")
a, ok := botActionMap[ref]
if !ok {
bot.SendChat(m.Chat.ID, "No corresponding request.")
return
}
if a.Action != "ResetGame" {
bot.SendChat(m.Chat.ID, "Not a reset request.")
return
}
if a.UserID != m.Sender.ID {
bot.SendChat(m.Chat.ID, "Requesting user has to confirm himself.")
return
}
if time.Now().Sub(a.Time) > time.Minute {
bot.SendChat(m.Chat.ID, "Request expired.")
} else {
bot.SendChat(m.Chat.ID, "Game resetted (to be implemented).")
}
delete(botActionMap, ref)
botActuallyReset(m)
return
}
r = regexp.MustCompile("^\\/start_(?P<Ref>[a-f0-9]{16})$")
if r.MatchString(m.Text) {
ref := r.ReplaceAllString(m.Text, "${Ref}")
a, ok := botActionMap[ref]
if !ok {
bot.SendChat(m.Chat.ID, "No corresponding request.")
return
}
if a.Action != "StartGame" {
bot.SendChat(m.Chat.ID, "Not a game start request.")
return
}
if a.UserID != m.Sender.ID {
bot.SendChat(m.Chat.ID, "Requesting user has to confirm himself.")
return
}
if time.Now().Sub(a.Time) > time.Minute {
bot.SendChat(m.Chat.ID, "Request expired.")
} else {
bot.SendChat(m.Chat.ID, "Game started (to be implemented).")
}
delete(botActionMap, ref)
botActuallyStart(m)
return
}

View File

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

18
ttd.go
View File

@ -54,6 +54,7 @@ type ServerDataTTD struct {
LastClientCompute time.Time
Conn net.Conn
Stop chan struct{}
Started bool
}
var updateHeartBeat = 5 * time.Second
@ -591,6 +592,20 @@ func (s *ServerTTD) PruneCompanies() {
func (s *ServerTTD) ComputeClientTime() {
t := time.Now()
if !cfg.Game.Started {
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
}
daysNow := int(t.Sub(cfg.Game.StartDate).Hours() / 24)
daysLast := int(s.Data.LastClientCompute.Sub(cfg.Game.StartDate).Hours() / 24)
if daysLast != daysNow {
@ -714,6 +729,9 @@ func (s *ServerTTD) NeedPause() bool {
}
func (s *ServerTTD) NeedPauseReason() string {
if !cfg.Game.Started {
return "game not started"
}
if !s.Status.Initialized {
return ""
}

View File

@ -1,6 +1,6 @@
// Code generated by version.sh (@generated) DO NOT EDIT.
package main
var githash = "044eac9"
var buildstamp = "2021-12-04_04:49:04"
var commits = "211"
var version = "044eac9-b211 - 2021-12-04_04:49:04"
var githash = "00d4b86"
var buildstamp = "2021-12-04_05:24:10"
var commits = "212"
var version = "00d4b86-b212 - 2021-12-04_05:24:10"