less talkative and better cleanup of stalled clients

This commit is contained in:
shoopea 2021-11-22 16:58:03 +08:00
parent 37310c4d88
commit 440e45fc29
2 changed files with 38 additions and 19 deletions

45
ttd.go
View File

@ -383,12 +383,12 @@ func (s *ServerTTD) Poll(stop chan struct{}) {
} }
sp.Read(buffer[:p.PLength]) sp.Read(buffer[:p.PLength])
if sp.Message == "!unpause" { if sp.Message == "!unpause" || sp.Message == "unpause" {
logInfoDebug("Server.Poll() : AdminPacketServerChat : Unpausing") logInfoDebug("Server.Poll() : AdminPacketServerChat : Unpausing")
clt := s.Status.Clients[sp.ClientID] clt := s.Status.Clients[sp.ClientID]
clt.Paused = false clt.Paused = false
s.Unpause() s.Unpause()
} else if sp.Message == "!pause" { } else if sp.Message == "!pause" || sp.Message == "pause" {
logInfoDebug("Server.Poll() : AdminPacketServerChat : Pausing") logInfoDebug("Server.Poll() : AdminPacketServerChat : Pausing")
clt := s.Status.Clients[sp.ClientID] clt := s.Status.Clients[sp.ClientID]
clt.Paused = true clt.Paused = true
@ -475,17 +475,8 @@ func (s *ServerTTD) Poll(stop chan struct{}) {
sp.Read(buffer[:p.PLength]) sp.Read(buffer[:p.PLength])
switch sp.Command { switch sp.Command {
/*
case "clients": case "clients":
for k, v := range s.Status.Clients {
logInfoDebug("Server.Poll() : Client[%d] : %s - %d (%s)", k, v.Name, v.CompanyID, v.Address)
}
case "companies": case "companies":
for k, v := range s.Status.Companies {
logInfoDebug("Server.Poll() : Company[%d] : %s", k, v.Name)
}
*/
case "pause": case "pause":
case "unpause": case "unpause":
default: default:
@ -578,6 +569,12 @@ func (s *ServerTTD) PruneClients() {
for cltID, clt := range s.Status.Clients { for cltID, clt := range s.Status.Clients {
if clt.LastSeen.Add(2 * updateHeartBeat).Before(time.Now()) { if clt.LastSeen.Add(2 * updateHeartBeat).Before(time.Now()) {
logInfoDebug("ServerTTD.PruneClients : deleting client #%d", cltID) logInfoDebug("ServerTTD.PruneClients : deleting client #%d", cltID)
if clt.CompanyID != 255 && cfg.CompanyIsRegistered(clt.CompanyID) {
cc := cfg.GetCompanyClient(clt.CompanyID)
if cc.Online {
cc.Online = false
}
}
delete(s.Status.Clients, cltID) delete(s.Status.Clients, cltID)
} }
} }
@ -592,6 +589,28 @@ func (s *ServerTTD) PruneCompanies() {
} }
} }
func (s *ServerTTD) UpdateConfigClients() {
for _, cc := range cfg.Clients {
if cc.Online {
if cc.CompanyID != 255 {
for coID, co := range s.Status.Companies {
if coID == cc.CompanyID {
hasClient := false
for clID, _ := range s.Status.Clients {
if clID == co.ClientID {
hasClient = true
}
}
if !hasClient {
cc.Online = false
}
}
}
}
}
}
}
func (s *ServerTTD) ComputeClientTime() { func (s *ServerTTD) ComputeClientTime() {
t := time.Now() t := time.Now()
daysNow := int(t.Sub(cfg.Game.StartDate).Hours() / 24) daysNow := int(t.Sub(cfg.Game.StartDate).Hours() / 24)
@ -673,7 +692,7 @@ func (s *ServerTTD) Pause() {
err := s.Send(px.Bytes()) err := s.Send(px.Bytes())
s.ComputeClientTime() s.ComputeClientTime()
bot.SendChat(cfg.Telegram.ChatID, "Game is paused.") //bot.SendChat(cfg.Telegram.ChatID, "Game is paused.")
s.Status.Paused = true s.Status.Paused = true
logErrorDebug(err, "Server.Pause : Send()") logErrorDebug(err, "Server.Pause : Send()")
@ -695,7 +714,7 @@ func (s *ServerTTD) Unpause() {
err := s.Send(px.Bytes()) err := s.Send(px.Bytes())
s.ComputeClientTime() s.ComputeClientTime()
bot.SendChat(cfg.Telegram.ChatID, "Game is unpaused.") //bot.SendChat(cfg.Telegram.ChatID, "Game is unpaused.")
s.Status.Paused = false s.Status.Paused = false
logErrorDebug(err, "Server.Unpause : Send()") logErrorDebug(err, "Server.Unpause : Send()")

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 = "69a6416" var githash = "37310c4"
var buildstamp = "2021-11-13_06:38:27" var buildstamp = "2021-11-22_08:57:20"
var commits = "200" var commits = "201"
var version = "69a6416-b200 - 2021-11-13_06:38:27" var version = "37310c4-b201 - 2021-11-22_08:57:20"