update register/deregister

This commit is contained in:
shoopea 2021-11-09 13:24:42 +08:00
parent 93de4325fd
commit a100fb6c68
2 changed files with 78 additions and 6 deletions

76
bot.go
View File

@ -52,6 +52,7 @@ func (b *Bot) BotHandlers() {
b.bot.Handle("/pause", botPause) b.bot.Handle("/pause", botPause)
b.bot.Handle("/unpause", botUnpause) b.bot.Handle("/unpause", botUnpause)
b.bot.Handle("/register", botRegister) b.bot.Handle("/register", botRegister)
b.bot.Handle("/deregister", botDeregister)
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)
@ -170,6 +171,35 @@ func botPlayers(m *tb.Message) {
bot.SendChat(m.Chat.ID, str) bot.SendChat(m.Chat.ID, str)
} }
func botDeregister(m *tb.Message) {
cc, ok := cfg.Clients[m.Sender.ID]
if !ok {
cc = &ClientConfig{
UserID: m.Sender.ID,
Username: m.Sender.Username,
CompanyID: 255,
TimeLeft: 0,
}
cfg.Clients[m.Sender.ID] = cc
bot.SendChat(m.Chat.ID, "User isn't registered.")
return
}
if cc.CompanyID != 255 {
for coID, co := range srv.Status.Companies {
if coID == cc.CompanyID {
cc.CompanyID = 255
bot.SendChat(m.Chat.ID, fmt.Sprintf("Deregistered from %s. %s playable left.", co.Name, cc.TimeLeft))
return
}
}
logInfoAlert("botRegister : %s : no such CompanyID : %d", cc.Username, cc.CompanyID)
cc.CompanyID = 255
bot.SendChat(m.Chat.ID, fmt.Sprintf("Registered company didn't exist anymore. %s playable left.", cc.TimeLeft))
return
}
return
}
func botRegister(m *tb.Message) { func botRegister(m *tb.Message) {
cc, ok := cfg.Clients[m.Sender.ID] cc, ok := cfg.Clients[m.Sender.ID]
if !ok { if !ok {
@ -177,9 +207,22 @@ func botRegister(m *tb.Message) {
UserID: m.Sender.ID, UserID: m.Sender.ID,
Username: m.Sender.Username, Username: m.Sender.Username,
CompanyID: 255, CompanyID: 255,
TimeLeft: 0,
} }
cfg.Clients[m.Sender.ID] = cc cfg.Clients[m.Sender.ID] = cc
} else {
if cc.CompanyID != 255 {
for coID, co := range srv.Status.Companies {
if coID == cc.CompanyID {
bot.SendChat(m.Chat.ID, fmt.Sprintf("Already registered %s. Please /deregister first.", co.Name))
return
}
}
logInfoAlert("botRegister : %s : no such CompanyID : %d", cc.Username, cc.CompanyID)
cc.CompanyID = 255
}
} }
coList := make(map[uint8]struct{}) coList := make(map[uint8]struct{})
for coID, _ := range srv.Status.Companies { for coID, _ := range srv.Status.Companies {
coList[coID] = struct{}{} coList[coID] = struct{}{}
@ -195,15 +238,44 @@ func botRegister(m *tb.Message) {
} }
} }
if len(coList) == 0 { if len(coList) == 0 {
bot.SendChat(m.Chat.ID, "No company to register") bot.SendChat(m.Chat.ID, "No company to register")
return return
} }
r := regexp.MustCompile("^\\/register( )+(?P<Company>[a-zA-Z\\.\\-0-9 ]+)$")
if r.MatchString(m.Text) {
// we have a parameter
coName := r.ReplaceAllString(m.Text, "${Company}")
for coID, co := range srv.Status.Companies {
if co.Name == coName {
for _, c := range cfg.Clients {
if c.CompanyID == coID {
bot.SendChat(m.Chat.ID, fmt.Sprintf("Company %s is already registered to @%s", coName, c.Username))
return
}
}
cc.CompanyID = coID
if cc.TimeLeft == 0 {
days := int(time.Now().Sub(cfg.Game.StartDate).Hours() / 24)
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))
return
}
}
bot.SendChat(m.Chat.ID, fmt.Sprintf("Can't find company %s", coName))
return
}
if len(coList) == 1 { if len(coList) == 1 {
for id, _ := range coList { for id, _ := range coList {
cc.CompanyID = id cc.CompanyID = id
days := int(time.Now().Sub(cfg.Game.StartDate).Hours() / 24) if cc.TimeLeft == 0 {
cc.TimeLeft = cfg.Game.StartingAllotment + cfg.Game.DailyAllotment*time.Duration(days) days := int(time.Now().Sub(cfg.Game.StartDate).Hours() / 24)
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)) bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s registered %s (with %s playable)", cc.Username, srv.Status.Companies[cc.CompanyID].Name, cc.TimeLeft))
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 = "38a1d8a" var githash = "93de432"
var buildstamp = "2021-11-08_14:54:17" var buildstamp = "2021-11-09_05:24:29"
var commits = "174" var commits = "175"
var version = "38a1d8a-b174 - 2021-11-08_14:54:17" var version = "93de432-b175 - 2021-11-09_05:24:29"