crash fix for initialized map

This commit is contained in:
shoopea 2021-11-09 17:45:01 +08:00
parent a100fb6c68
commit 8d580181bf
4 changed files with 56 additions and 14 deletions

14
bot.go
View File

@ -57,6 +57,8 @@ func (b *Bot) BotHandlers() {
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("/players", botPlayers)
b.bot.Handle("/give", botGive)
b.bot.Handle("/take", botTake)
b.bot.Handle(tb.OnPhoto, botPhoto) b.bot.Handle(tb.OnPhoto, botPhoto)
b.bot.Handle(tb.OnChannelPost, botChannelPost) b.bot.Handle(tb.OnChannelPost, botChannelPost)
@ -151,13 +153,13 @@ func botPlayers(m *tb.Message) {
online := "" online := ""
for _, cc := range cfg.Clients { for _, cc := range cfg.Clients {
if cc.Online { if cc.Online {
online = online + fmt.Sprintf(" - @%s (%s) : %s", cc.Username, cc.TimeLeft, srv.Status.Companies[cc.CompanyID].Name) + "\r\n" online = online + fmt.Sprintf(" - %s (%s) : %s", cc.Username, cc.TimeLeft, srv.Status.Companies[cc.CompanyID].Name) + "\r\n"
} }
} }
offline := "" offline := ""
for _, cc := range cfg.Clients { for _, cc := range cfg.Clients {
if _, ok := srv.Status.Companies[cc.CompanyID]; ok && !cc.Online { if _, ok := srv.Status.Companies[cc.CompanyID]; ok && !cc.Online {
offline = offline + fmt.Sprintf(" - @%s (%s) : %s", cc.Username, cc.TimeLeft, srv.Status.Companies[cc.CompanyID].Name) + "\r\n" offline = offline + fmt.Sprintf(" - %s (%s) : %s", cc.Username, cc.TimeLeft, srv.Status.Companies[cc.CompanyID].Name) + "\r\n"
} }
} }
@ -171,6 +173,14 @@ func botPlayers(m *tb.Message) {
bot.SendChat(m.Chat.ID, str) bot.SendChat(m.Chat.ID, str)
} }
func botGive(m *tb.Message) {
PrintText(m)
}
func botTake(m *tb.Message) {
PrintText(m)
}
func botDeregister(m *tb.Message) { func botDeregister(m *tb.Message) {
cc, ok := cfg.Clients[m.Sender.ID] cc, ok := cfg.Clients[m.Sender.ID]
if !ok { if !ok {

View File

@ -380,14 +380,26 @@ func (p *PacketServerCompanyNew) Read(b []byte) {
} }
func (p *PacketServerCompanyInfo) Read(b []byte) { func (p *PacketServerCompanyInfo) Read(b []byte) {
var err error
r := bufio.NewReader(bytes.NewReader(b)) r := bufio.NewReader(bytes.NewReader(b))
r.Discard(3) r.Discard(3)
c, _ := r.ReadByte() c, _ := r.ReadByte()
p.CompanyID = uint8(c) p.CompanyID = uint8(c)
p.Name, _ = r.ReadString(0) p.Name, err = r.ReadString(0)
logErrorDebug(err, "PacketServerCompanyInfo.Read : Name")
if err != nil {
p.Name = "Error"
} else {
p.Name = p.Name[:len(p.Name)-1] p.Name = p.Name[:len(p.Name)-1]
p.President, _ = r.ReadString(0) }
p.President, err = r.ReadString(0)
logErrorDebug(err, "PacketServerCompanyInfo.Read : President")
if err != nil {
p.President = "Error"
} else {
p.President = p.President[:len(p.President)-1] p.President = p.President[:len(p.President)-1]
}
c, _ = r.ReadByte() c, _ = r.ReadByte()
p.Color = uint8(c) p.Color = uint8(c)
c, _ = r.ReadByte() c, _ = r.ReadByte()
@ -410,14 +422,25 @@ func (p *PacketServerCompanyInfo) Read(b []byte) {
} }
func (p *PacketServerCompanyUpdate) Read(b []byte) { func (p *PacketServerCompanyUpdate) Read(b []byte) {
var err error
r := bufio.NewReader(bytes.NewReader(b)) r := bufio.NewReader(bytes.NewReader(b))
r.Discard(3) r.Discard(3)
c, _ := r.ReadByte() c, _ := r.ReadByte()
p.CompanyID = uint8(c) p.CompanyID = uint8(c)
p.Name, _ = r.ReadString(0) p.Name, err = r.ReadString(0)
logErrorDebug(err, "PacketServerCompanyUpdate.Read : Name")
if err != nil {
p.Name = "Error"
} else {
p.Name = p.Name[:len(p.Name)-1] p.Name = p.Name[:len(p.Name)-1]
}
p.President, _ = r.ReadString(0) p.President, _ = r.ReadString(0)
logErrorDebug(err, "PacketServerCompanyUpdate.Read : President")
if err != nil {
p.President = "Error"
} else {
p.President = p.President[:len(p.President)-1] p.President = p.President[:len(p.President)-1]
}
c, _ = r.ReadByte() c, _ = r.ReadByte()
p.Color = uint8(c) p.Color = uint8(c)
c, _ = r.ReadByte() c, _ = r.ReadByte()

11
ttd.go
View File

@ -348,7 +348,16 @@ func (s *ServerTTD) Poll(stop chan struct{}) {
} }
sp.Read(buffer[:p.PLength]) sp.Read(buffer[:p.PLength])
logInfoDebug("Server.Poll() : PacketServerCompanyUpdate :\n- CompanyID: %d\n- Name: %s\n- President: %s\n- Protected: %t", sp.CompanyID, sp.Name, sp.President, sp.Protected) logInfoDebug("Server.Poll() : PacketServerCompanyUpdate :\n- CompanyID: %d\n- Name: %s\n- President: %s\n- Protected: %t", sp.CompanyID, sp.Name, sp.President, sp.Protected)
c := s.Status.Companies[sp.CompanyID] c := &CompanyTTD{
CompanyID: sp.CompanyID,
FirstSeen: time.Now(),
ClientID: 0,
}
if _, ok := s.Status.Companies[sp.CompanyID]; !ok {
s.Status.Companies[sp.CompanyID] = c
} else {
c = s.Status.Companies[sp.CompanyID]
}
c.Name = sp.Name c.Name = sp.Name
c.FirstSeen = time.Now() c.FirstSeen = time.Now()
c.LastSeen = time.Now() c.LastSeen = time.Now()

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 = "93de432" var githash = "a100fb6"
var buildstamp = "2021-11-09_05:24:29" var buildstamp = "2021-11-09_09:44:42"
var commits = "175" var commits = "176"
var version = "93de432-b175 - 2021-11-09_05:24:29" var version = "a100fb6-b176 - 2021-11-09_09:44:42"