add start/reset

This commit is contained in:
shoopea 2021-12-04 12:49:20 +08:00
parent 044eac9a77
commit 00d4b8667f
3 changed files with 202 additions and 22 deletions

214
bot.go
View File

@ -17,13 +17,14 @@ type Bot struct {
Config *TelegramConfig Config *TelegramConfig
} }
type CompanyDeleteEntry struct { type BotActionEntry struct {
Action string
CompanyID uint8 CompanyID uint8
UserID int UserID int
Time time.Time Time time.Time
} }
var companyDeleteMap map[string]*CompanyDeleteEntry var botActionMap map[string]*BotActionEntry
func (b *Bot) Start() { func (b *Bot) Start() {
var err error var err error
@ -34,7 +35,7 @@ func (b *Bot) Start() {
}) })
failError(err, "Bot.Start() : registering bot") failError(err, "Bot.Start() : registering bot")
companyDeleteMap = make(map[string]*CompanyDeleteEntry) botActionMap = make(map[string]*BotActionEntry)
b.BotHandlers() b.BotHandlers()
} }
@ -68,15 +69,21 @@ func (b *Bot) BotHandlers() {
b.bot.Handle("/delete", botDelete) b.bot.Handle("/delete", botDelete)
b.bot.Handle("/companies", botCompanies) b.bot.Handle("/companies", botCompanies)
b.bot.Handle("/clients", botClients) 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("/passwd", botPasswd)
b.bot.Handle("/say", botSay) b.bot.Handle("/say", botSay)
b.bot.Handle("/help", botHelp) b.bot.Handle("/help", botHelp)
b.bot.Handle("/transfer", botTransfer)
b.bot.Handle("/version", botVersion) 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.OnPhoto, botPhoto)
b.bot.Handle(tb.OnChannelPost, botChannelPost) b.bot.Handle(tb.OnChannelPost, botChannelPost)
b.bot.Handle(tb.OnQuery, botQuery) b.bot.Handle(tb.OnQuery, botQuery)
@ -168,12 +175,13 @@ func botDelete(m *tb.Message) {
} }
c := hex.EncodeToString(b) c := hex.EncodeToString(b)
d := &CompanyDeleteEntry{ d := &BotActionEntry{
Action: "DeleteCompany",
CompanyID: id, CompanyID: id,
UserID: m.Sender.ID, UserID: m.Sender.ID,
Time: time.Now(), 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)) bot.SendChat(m.Chat.ID, fmt.Sprintf("Press /delete_%s to delete '%s'", c, co.Name))
} else { } else {
@ -198,7 +206,96 @@ func botClients(m *tb.Message) {
bot.SendChat(m.Chat.ID, str) 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) { 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) d1 := time.Now().Sub(cfg.Game.StartDate)
days := int(time.Duration(d1.Hours()) / 24) days := int(time.Duration(d1.Hours()) / 24)
d2 := time.Duration(days+1)*(time.Hour)*24 - d1 d2 := time.Duration(days+1)*(time.Hour)*24 - d1
@ -233,6 +330,28 @@ func botPlayers(m *tb.Message) {
bot.SendChat(m.Chat.ID, str) 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) { func botGive(m *tb.Message) {
if m.Sender.ID != int(cfg.Telegram.AdminID) { if m.Sender.ID != int(cfg.Telegram.AdminID) {
bot.SendChat(m.Chat.ID, "Only the admin can use this command.") 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/delete - delete company\r\n", msg)
msg = fmt.Sprintf("%s/companies - list companies\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/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/give - give time to player\r\n", msg)
msg = fmt.Sprintf("%s/take - take time from 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/transfer - transfer time between players\r\n", msg)
msg = fmt.Sprintf("%s/passwd - change passwd\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/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/version - version\r\n", msg)
msg = fmt.Sprintf("%s/help - this\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) { func botText(m *tb.Message) {
r := regexp.MustCompile("^\\/delete_(?P<Ref>[a-f0-9]{16})$") r := regexp.MustCompile("^\\/delete_(?P<Ref>[a-f0-9]{16})$")
if r.MatchString(m.Text) { if r.MatchString(m.Text) {
ref := r.ReplaceAllString(m.Text, "${Ref}") ref := r.ReplaceAllString(m.Text, "${Ref}")
d, ok := companyDeleteMap[ref] a, ok := botActionMap[ref]
if !ok { if !ok {
bot.SendChat(m.Chat.ID, "No corresponding deletion request.") bot.SendChat(m.Chat.ID, "No corresponding request.")
return 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.") bot.SendChat(m.Chat.ID, "Requesting user has to confirm himself.")
return return
} }
if time.Now().Sub(d.Time) > time.Minute { if time.Now().Sub(a.Time) > time.Minute {
bot.SendChat(m.Chat.ID, "Request expired.") bot.SendChat(m.Chat.ID, "Request expired.")
return } else {
srv.DeleteCompany(a.CompanyID)
bot.SendChat(m.Chat.ID, "Company deleted.")
} }
srv.DeleteCompany(d.CompanyID) delete(botActionMap, ref)
bot.SendChat(m.Chat.ID, "Company deleted.")
delete(companyDeleteMap, ref)
return 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)
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)
return
}
PrintText(m) PrintText(m)
} }

View File

@ -30,6 +30,7 @@ type GameConfig struct {
StartingAllotment time.Duration `json:"starting_allotment"` StartingAllotment time.Duration `json:"starting_allotment"`
Threshold time.Duration `json:"threshold"` Threshold time.Duration `json:"threshold"`
StartDate time.Time `json:"start_date"` StartDate time.Time `json:"start_date"`
Started bool `json:"started"`
} }
type ClientConfig struct { type ClientConfig struct {
@ -39,6 +40,7 @@ type ClientConfig struct {
Online bool `json:"online"` Online bool `json:"online"`
TimeLeft time.Duration `json:"time_left"` TimeLeft time.Duration `json:"time_left"`
CompanyID uint8 `json:"company_id` CompanyID uint8 `json:"company_id`
Ready bool `json:"ready"`
} }
type Config struct { type Config struct {

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 = "3b878e0" var githash = "044eac9"
var buildstamp = "2021-11-28_13:40:44" var buildstamp = "2021-12-04_04:49:04"
var commits = "210" var commits = "211"
var version = "3b878e0-b210 - 2021-11-28_13:40:44" var version = "044eac9-b211 - 2021-12-04_04:49:04"