implement start/reset
This commit is contained in:
181
bot.go
181
bot.go
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user