add daily stats and color

This commit is contained in:
shoopea 2021-12-11 10:38:14 +08:00
parent 15203263e6
commit 0bfa4461ba
4 changed files with 46 additions and 14 deletions

View File

@ -41,6 +41,7 @@ type ClientConfig struct {
TimeLeft time.Duration `json:"time_left"`
CompanyID uint8 `json:"company_id`
Ready bool `json:"ready"`
Color string `json:"color"`
}
type Config struct {

View File

@ -30,11 +30,13 @@ type StatMonthly struct {
}
type StatDaily struct {
Money int64
Loan int64
Value int64
Trains int
Road int
Planes int
Ships int
CompanyID uint8
Date time.Time
Money int64
Loan int64
Value int64
Train int
Road int
Plane int
Ship int
}

35
ttd.go
View File

@ -25,7 +25,6 @@ type CompanyTTD struct {
CompanyID uint8
CompanyExtlID int64
Name string
Value int64
Protected bool
ClientID uint32
FirstSeen time.Time
@ -526,15 +525,45 @@ func (s *ServerTTD) Poll(stop chan struct{}) {
Packet: p,
}
sp.Read(buffer[:p.PLength])
r := regexp.MustCompile("#:(?P<CompanyID>[0-9]+)\\(.*\\) Company Name: '(?P<CompanyName>.+)'.*Value: (?P<CompanyValue>[0-9]+)( )+.*$")
r := regexp.MustCompile("#:(?P<CompanyID>[0-9]+)\\((?P<Color>[a-zA-Z]+)\\) Company Name: '(?P<CompanyName>.+)'.*Money: (?P<Money>[0-9]+).*Loan: (?P<Loan>[0-9]*).*Value: (?P<Value>[0-9]+).* \\(T:(?P<Train>[0-9]+), R:(?P<Road>[0-9]+), P:(?P<Plane>[0-9]+), S:(?P<Ship>[0-9]+)\\).*$")
if r.MatchString(sp.Output) {
coID := uint8(0)
for _, co := range srv.Status.Companies {
if co.Name == r.ReplaceAllString(sp.Output, "${CompanyName}") {
co.CompanyExtlID, _ = strconv.ParseInt(r.ReplaceAllString(sp.Output, "${CompanyID}"), 10, 64)
co.Value, _ = strconv.ParseInt(r.ReplaceAllString(sp.Output, "${CompanyValue}"), 10, 64)
coID = co.CompanyID
break
}
}
if cfg.StatsDaily == nil {
cfg.StatsDaily = make(map[int]map[string]*StatDaily)
}
if cfg.CompanyIsRegistered(coID) {
cc := cfg.GetCompanyClient(coID)
cc.Color = r.ReplaceAllString(sp.Output, "${Color}")
_, ok := cfg.StatsDaily[cc.UserID]
if !ok {
cfg.StatsDaily[cc.UserID] = make(map[string]*StatDaily)
}
stats, ok := cfg.StatsDaily[cc.UserID][s.Status.GameDate.Format("20060102")]
if !ok {
stats = &StatDaily{
CompanyID: coID,
Date: s.Status.GameDate,
}
cfg.StatsDaily[cc.UserID][s.Status.GameDate.Format("20060102")] = stats
}
stats.Money, _ = strconv.ParseInt(r.ReplaceAllString(sp.Output, "${Money}"), 10, 64)
stats.Loan, _ = strconv.ParseInt(r.ReplaceAllString(sp.Output, "${Loan}"), 10, 64)
stats.Value, _ = strconv.ParseInt(r.ReplaceAllString(sp.Output, "${Value}"), 10, 64)
stats.Train, _ = strconv.Atoi(r.ReplaceAllString(sp.Output, "${Train}"))
stats.Road, _ = strconv.Atoi(r.ReplaceAllString(sp.Output, "${Road}"))
stats.Plane, _ = strconv.Atoi(r.ReplaceAllString(sp.Output, "${Plane}"))
stats.Ship, _ = strconv.Atoi(r.ReplaceAllString(sp.Output, "${Ship}"))
}
}
r = regexp.MustCompile("Client #(?P<ClientID>[0-9]+).*name: '(?P<ClientName>.+)'.*company: (?P<CompanyID>[0-9]+)( )+IP: .*$")
if r.MatchString(sp.Output) {

View File

@ -1,6 +1,6 @@
// Code generated by version.sh (@generated) DO NOT EDIT.
package main
var githash = "7590355"
var buildstamp = "2021-12-10_13:21:22"
var commits = "252"
var version = "7590355-b252 - 2021-12-10_13:21:22"
var githash = "1520326"
var buildstamp = "2021-12-11_02:35:54"
var commits = "253"
var version = "1520326-b253 - 2021-12-11_02:35:54"