From 00d4b8667f0aa70fd0fba8761027beaac03ce547 Mon Sep 17 00:00:00 2001 From: shoopea Date: Sat, 4 Dec 2021 12:49:20 +0800 Subject: [PATCH] add start/reset --- bot.go | 214 ++++++++++++++++++++++++++++++++++++++++++++++++----- config.go | 2 + version.go | 8 +- 3 files changed, 202 insertions(+), 22 deletions(-) diff --git a/bot.go b/bot.go index a9f478a..8069777 100644 --- a/bot.go +++ b/bot.go @@ -17,13 +17,14 @@ type Bot struct { Config *TelegramConfig } -type CompanyDeleteEntry struct { +type BotActionEntry struct { + Action string CompanyID uint8 UserID int Time time.Time } -var companyDeleteMap map[string]*CompanyDeleteEntry +var botActionMap map[string]*BotActionEntry func (b *Bot) Start() { var err error @@ -34,7 +35,7 @@ func (b *Bot) Start() { }) failError(err, "Bot.Start() : registering bot") - companyDeleteMap = make(map[string]*CompanyDeleteEntry) + botActionMap = make(map[string]*BotActionEntry) b.BotHandlers() } @@ -68,15 +69,21 @@ func (b *Bot) BotHandlers() { b.bot.Handle("/delete", botDelete) b.bot.Handle("/companies", botCompanies) b.bot.Handle("/clients", botClients) - b.bot.Handle("/players", botPlayers) - b.bot.Handle("/give", botGive) - b.bot.Handle("/take", botTake) + b.bot.Handle("/passwd", botPasswd) b.bot.Handle("/say", botSay) b.bot.Handle("/help", botHelp) - b.bot.Handle("/transfer", botTransfer) b.bot.Handle("/version", botVersion) + b.bot.Handle("/reset", botReset) + b.bot.Handle("/ready", botReady) + b.bot.Handle("/start", botStart) + b.bot.Handle("/players", botPlayers) + + b.bot.Handle("/give", botGive) + b.bot.Handle("/take", botTake) + b.bot.Handle("/transfer", botTransfer) + b.bot.Handle(tb.OnPhoto, botPhoto) b.bot.Handle(tb.OnChannelPost, botChannelPost) b.bot.Handle(tb.OnQuery, botQuery) @@ -168,12 +175,13 @@ func botDelete(m *tb.Message) { } c := hex.EncodeToString(b) - d := &CompanyDeleteEntry{ + d := &BotActionEntry{ + Action: "DeleteCompany", CompanyID: id, UserID: m.Sender.ID, Time: time.Now(), } - companyDeleteMap[c] = d + botActionMap[c] = d bot.SendChat(m.Chat.ID, fmt.Sprintf("Press /delete_%s to delete '%s'", c, co.Name)) } else { @@ -198,7 +206,96 @@ func botClients(m *tb.Message) { bot.SendChat(m.Chat.ID, str) } +func botReset(m *tb.Message) { + if m.Sender.ID != int(cfg.Telegram.AdminID) { + bot.SendChat(m.Chat.ID, "Only the admin can use this command.") + return + } + + b := make([]byte, 8) + _, err := rand.Read(b) + logErrorDebug(err, "botReset : rand.Read") + if err != nil { + bot.SendChat(m.Chat.ID, "internal error") + return + } + + c := hex.EncodeToString(b) + d := &BotActionEntry{ + Action: "ResetGame", + CompanyID: 0, + UserID: m.Sender.ID, + Time: time.Now(), + } + botActionMap[c] = d + bot.SendChat(m.Chat.ID, fmt.Sprintf("Press /reset_%s to reset the game", c)) + 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.") + return + } + + b := make([]byte, 8) + _, err := rand.Read(b) + logErrorDebug(err, "botStart : rand.Read") + if err != nil { + bot.SendChat(m.Chat.ID, "internal error") + return + } + + c := hex.EncodeToString(b) + d := &BotActionEntry{ + Action: "StartGame", + CompanyID: 0, + UserID: m.Sender.ID, + Time: time.Now(), + } + botActionMap[c] = d + bot.SendChat(m.Chat.ID, fmt.Sprintf("Press /start_%s to actually start game", c)) + return +} + +func botReady(m *tb.Message) { + cc, ok := cfg.Clients[m.Sender.ID] + if !ok { + bot.SendChat(m.Chat.ID, "Player not registered") + return + } + + ready := 0 + waiting := 0 + for _, cc2 := range cfg.Clients { + if cc2.Ready { + ready++ + } else { + waiting++ + } + } + + if cc.Ready { + cc.Ready = false + bot.SendChat(m.Chat.ID, fmt.Sprintf("Player not ready anymore. Only %d players ready now.", ready)) + } else { + cc.Ready = true + bot.SendChat(m.Chat.ID, fmt.Sprintf("Player is now ready. Still waiting for %d players.", waiting)) + } + + return +} + func botPlayers(m *tb.Message) { + if cfg.Game.Started { + botPlayersStarted(m) + } else { + botPlayersWaiting(m) + } + return +} + +func botPlayersStarted(m *tb.Message) { d1 := time.Now().Sub(cfg.Game.StartDate) days := int(time.Duration(d1.Hours()) / 24) d2 := time.Duration(days+1)*(time.Hour)*24 - d1 @@ -233,6 +330,28 @@ func botPlayers(m *tb.Message) { bot.SendChat(m.Chat.ID, str) } +func botPlayersWaiting(m *tb.Message) { + msg := "Player status :\r\n" + ready := "" + waiting := "" + for _, cc := range cfg.Clients { + if cc.Ready { + ready = ready + fmt.Sprintf(" - %s", cc.Username) + "\r\n" + } else { + waiting = waiting + fmt.Sprintf(" - %s", cc.Username) + "\r\n" + } + } + + if len(ready) > 0 { + msg = msg + "Players ready :\r\n" + ready + } + if len(waiting) > 0 { + msg = msg + "Waiting for :\r\n" + waiting + } + + bot.SendChat(m.Chat.ID, msg) +} + func botGive(m *tb.Message) { if m.Sender.ID != int(cfg.Telegram.AdminID) { bot.SendChat(m.Chat.ID, "Only the admin can use this command.") @@ -371,12 +490,15 @@ func botHelp(m *tb.Message) { msg = fmt.Sprintf("%s/delete - delete company\r\n", msg) msg = fmt.Sprintf("%s/companies - list companies\r\n", msg) msg = fmt.Sprintf("%s/clients - list clients\r\n", msg) - msg = fmt.Sprintf("%s/players - list players\r\n", msg) msg = fmt.Sprintf("%s/give - give time to player\r\n", msg) msg = fmt.Sprintf("%s/take - take time from player\r\n", msg) msg = fmt.Sprintf("%s/transfer - transfer time between players\r\n", msg) msg = fmt.Sprintf("%s/passwd - change passwd\r\n", msg) msg = fmt.Sprintf("%s/say - send msg to the game\r\n", msg) + msg = fmt.Sprintf("%s/reset - reset the game\r\n", msg) + msg = fmt.Sprintf("%s/start - start the game\r\n", msg) + msg = fmt.Sprintf("%s/ready - set player as ready\r\n", msg) + msg = fmt.Sprintf("%s/players - list players\r\n", msg) msg = fmt.Sprintf("%s/version - version\r\n", msg) msg = fmt.Sprintf("%s/help - this\r\n", msg) @@ -618,26 +740,82 @@ func botQuery(q *tb.Query) { } func botText(m *tb.Message) { + r := regexp.MustCompile("^\\/delete_(?P[a-f0-9]{16})$") if r.MatchString(m.Text) { ref := r.ReplaceAllString(m.Text, "${Ref}") - d, ok := companyDeleteMap[ref] + a, ok := botActionMap[ref] if !ok { - bot.SendChat(m.Chat.ID, "No corresponding deletion request.") + bot.SendChat(m.Chat.ID, "No corresponding request.") return } - if d.UserID != m.Sender.ID { + 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(d.Time) > time.Minute { + if time.Now().Sub(a.Time) > time.Minute { bot.SendChat(m.Chat.ID, "Request expired.") - return + } else { + srv.DeleteCompany(a.CompanyID) + bot.SendChat(m.Chat.ID, "Company deleted.") } - srv.DeleteCompany(d.CompanyID) - bot.SendChat(m.Chat.ID, "Company deleted.") - delete(companyDeleteMap, ref) + delete(botActionMap, ref) return } + + r = regexp.MustCompile("^\\/reset_(?P[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) + return + } + + r = regexp.MustCompile("^\\/start_(?P[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) + return + } + PrintText(m) } diff --git a/config.go b/config.go index 98f5ba5..d585c98 100644 --- a/config.go +++ b/config.go @@ -30,6 +30,7 @@ type GameConfig struct { StartingAllotment time.Duration `json:"starting_allotment"` Threshold time.Duration `json:"threshold"` StartDate time.Time `json:"start_date"` + Started bool `json:"started"` } type ClientConfig struct { @@ -39,6 +40,7 @@ type ClientConfig struct { Online bool `json:"online"` TimeLeft time.Duration `json:"time_left"` CompanyID uint8 `json:"company_id` + Ready bool `json:"ready"` } type Config struct { diff --git a/version.go b/version.go index f283e6c..3e821bc 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,6 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "3b878e0" -var buildstamp = "2021-11-28_13:40:44" -var commits = "210" -var version = "3b878e0-b210 - 2021-11-28_13:40:44" +var githash = "044eac9" +var buildstamp = "2021-12-04_04:49:04" +var commits = "211" +var version = "044eac9-b211 - 2021-12-04_04:49:04"