implement start/reset
This commit is contained in:
parent
00d4b8667f
commit
84579af511
181
bot.go
181
bot.go
@ -140,7 +140,6 @@ func botUnpause(m *tb.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func botDelete(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]+)")
|
r := regexp.MustCompile("\\/delete (?P<CompanyID>[0-9]+)")
|
||||||
id := uint8(255)
|
id := uint8(255)
|
||||||
if r.MatchString(m.Text) {
|
if r.MatchString(m.Text) {
|
||||||
@ -190,6 +189,33 @@ func botDelete(m *tb.Message) {
|
|||||||
return
|
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) {
|
func botCompanies(m *tb.Message) {
|
||||||
str := "Companies :"
|
str := "Companies :"
|
||||||
for k, v := range srv.Status.Companies {
|
for k, v := range srv.Status.Companies {
|
||||||
@ -212,6 +238,11 @@ func botReset(m *tb.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !cfg.Game.Started {
|
||||||
|
bot.SendChat(m.Chat.ID, "Game is not started.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
b := make([]byte, 8)
|
b := make([]byte, 8)
|
||||||
_, err := rand.Read(b)
|
_, err := rand.Read(b)
|
||||||
logErrorDebug(err, "botReset : rand.Read")
|
logErrorDebug(err, "botReset : rand.Read")
|
||||||
@ -232,11 +263,48 @@ func botReset(m *tb.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func botStart(m *tb.Message) {
|
func botActuallyReset(m *tb.Message) {
|
||||||
if m.Sender.ID != int(cfg.Telegram.AdminID) {
|
r := regexp.MustCompile("^\\/reset_(?P<Ref>[a-f0-9]{16})$")
|
||||||
bot.SendChat(m.Chat.ID, "Only the admin can use this command.")
|
|
||||||
|
ref := r.ReplaceAllString(m.Text, "${Ref}")
|
||||||
|
a, ok := botActionMap[ref]
|
||||||
|
if !ok {
|
||||||
|
bot.SendChat(m.Chat.ID, "No corresponding request.")
|
||||||
return
|
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)
|
b := make([]byte, 8)
|
||||||
_, err := rand.Read(b)
|
_, err := rand.Read(b)
|
||||||
@ -258,7 +326,44 @@ func botStart(m *tb.Message) {
|
|||||||
return
|
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) {
|
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]
|
cc, ok := cfg.Clients[m.Sender.ID]
|
||||||
if !ok {
|
if !ok {
|
||||||
bot.SendChat(m.Chat.ID, "Player not registered")
|
bot.SendChat(m.Chat.ID, "Player not registered")
|
||||||
@ -277,9 +382,13 @@ func botReady(m *tb.Message) {
|
|||||||
|
|
||||||
if cc.Ready {
|
if cc.Ready {
|
||||||
cc.Ready = false
|
cc.Ready = false
|
||||||
|
ready--
|
||||||
|
waiting++
|
||||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("Player not ready anymore. Only %d players ready now.", ready))
|
bot.SendChat(m.Chat.ID, fmt.Sprintf("Player not ready anymore. Only %d players ready now.", ready))
|
||||||
} else {
|
} else {
|
||||||
cc.Ready = true
|
cc.Ready = true
|
||||||
|
ready++
|
||||||
|
waiting--
|
||||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("Player is now ready. Still waiting for %d players.", 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})$")
|
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}")
|
botActuallyDelete(m)
|
||||||
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r = regexp.MustCompile("^\\/reset_(?P<Ref>[a-f0-9]{16})$")
|
r = regexp.MustCompile("^\\/reset_(?P<Ref>[a-f0-9]{16})$")
|
||||||
if r.MatchString(m.Text) {
|
if r.MatchString(m.Text) {
|
||||||
ref := r.ReplaceAllString(m.Text, "${Ref}")
|
botActuallyReset(m)
|
||||||
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r = regexp.MustCompile("^\\/start_(?P<Ref>[a-f0-9]{16})$")
|
r = regexp.MustCompile("^\\/start_(?P<Ref>[a-f0-9]{16})$")
|
||||||
if r.MatchString(m.Text) {
|
if r.MatchString(m.Text) {
|
||||||
ref := r.ReplaceAllString(m.Text, "${Ref}")
|
botActuallyStart(m)
|
||||||
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
main.go
4
main.go
@ -45,7 +45,9 @@ func main() {
|
|||||||
|
|
||||||
srv = &ServerTTD{
|
srv = &ServerTTD{
|
||||||
Config: cfg.Server,
|
Config: cfg.Server,
|
||||||
Data: &ServerDataTTD{},
|
Data: &ServerDataTTD{
|
||||||
|
Started: cfg.Game.Started,
|
||||||
|
},
|
||||||
Status: &ServerStatusTTD{},
|
Status: &ServerStatusTTD{},
|
||||||
}
|
}
|
||||||
go srv.Start()
|
go srv.Start()
|
||||||
|
18
ttd.go
18
ttd.go
@ -54,6 +54,7 @@ type ServerDataTTD struct {
|
|||||||
LastClientCompute time.Time
|
LastClientCompute time.Time
|
||||||
Conn net.Conn
|
Conn net.Conn
|
||||||
Stop chan struct{}
|
Stop chan struct{}
|
||||||
|
Started bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var updateHeartBeat = 5 * time.Second
|
var updateHeartBeat = 5 * time.Second
|
||||||
@ -591,6 +592,20 @@ func (s *ServerTTD) PruneCompanies() {
|
|||||||
|
|
||||||
func (s *ServerTTD) ComputeClientTime() {
|
func (s *ServerTTD) ComputeClientTime() {
|
||||||
t := time.Now()
|
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)
|
daysNow := int(t.Sub(cfg.Game.StartDate).Hours() / 24)
|
||||||
daysLast := int(s.Data.LastClientCompute.Sub(cfg.Game.StartDate).Hours() / 24)
|
daysLast := int(s.Data.LastClientCompute.Sub(cfg.Game.StartDate).Hours() / 24)
|
||||||
if daysLast != daysNow {
|
if daysLast != daysNow {
|
||||||
@ -714,6 +729,9 @@ func (s *ServerTTD) NeedPause() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerTTD) NeedPauseReason() string {
|
func (s *ServerTTD) NeedPauseReason() string {
|
||||||
|
if !cfg.Game.Started {
|
||||||
|
return "game not started"
|
||||||
|
}
|
||||||
if !s.Status.Initialized {
|
if !s.Status.Initialized {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
@ -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 = "044eac9"
|
var githash = "00d4b86"
|
||||||
var buildstamp = "2021-12-04_04:49:04"
|
var buildstamp = "2021-12-04_05:24:10"
|
||||||
var commits = "211"
|
var commits = "212"
|
||||||
var version = "044eac9-b211 - 2021-12-04_04:49:04"
|
var version = "00d4b86-b212 - 2021-12-04_05:24:10"
|
||||||
|
Loading…
Reference in New Issue
Block a user