diff --git a/bot.go b/bot.go index c738d6f..64b613c 100644 --- a/bot.go +++ b/bot.go @@ -104,6 +104,7 @@ func (b *Bot) BotHandlers() { b.bot.Handle("/value", botGraphValue) b.bot.Handle("/value_delta", botGraphValueDelta) + b.bot.Handle("/deliveries", botGraphDeliveries) b.bot.Handle("/money", botGraphMoney) b.bot.Handle("/income", botGraphIncome) b.bot.Handle("/loan", botGraphLoan) @@ -149,6 +150,7 @@ func botHelp(m *tb.Message) { msg = fmt.Sprintf("%s/offline - set player offline\r\n", msg) msg = fmt.Sprintf("%s/value - value graph\r\n", msg) msg = fmt.Sprintf("%s/value_delta - delta value graph\r\n", msg) + msg = fmt.Sprintf("%s/deliveries - deliveries graph\r\n", msg) msg = fmt.Sprintf("%s/money - money graph\r\n", msg) msg = fmt.Sprintf("%s/income - income graph\r\n", msg) msg = fmt.Sprintf("%s/loan - loan graph\r\n", msg) diff --git a/bot_graph.go b/bot_graph.go index b88d0ea..4c93d85 100644 --- a/bot_graph.go +++ b/bot_graph.go @@ -215,6 +215,97 @@ func botGraphValueDelta(m *tb.Message) { return } +func botGraphDeliveries(m *tb.Message) { + var ( + maxVal float64 + unitFactor float64 + unitName string + ) + for _, dStats := range cfg.StatsMonthly { + for _, stat := range dStats { + valueFloat := float64(stat.DeliveredCargoLastQuarter) + if math.Abs(valueFloat) > maxVal { + maxVal = math.Abs(valueFloat) + } + } + } + if maxVal > 1000000000 { + unitFactor = 1000000000 + unitName = "billion" + } else if maxVal > 1000000 { + unitFactor = 1000000 + unitName = "million" + } else { + unitFactor = 1 + unitName = "" + } + + var vals map[int]plotter.XYs + vals = make(map[int]plotter.XYs) + for ccID, dStats := range cfg.StatsMonthly { + vals[ccID] = make(plotter.XYs, 0) + for dStr, stat := range dStats { + d, err := time.Parse("20060102", dStr) + logErrorDebug(err, "botGraphDeliveries : time.Parse") + if err != nil { + bot.SendChat(m.Chat.ID, fmt.Sprintf("time.Parse : %s", err)) + return + } + dateFloat := float64(d.Year()) + float64(d.Month()-1)/12 + valueFloat := float64(stat.DeliveredCargoLastQuarter) + pt := plotter.XY{ + X: dateFloat, + Y: valueFloat / unitFactor, + } + vals[ccID] = append(vals[ccID], pt) + } + sort.Slice(vals[ccID], func(i, j int) bool { return vals[ccID][i].X < vals[ccID][j].X }) + } + + p := plot.New() + p.Title.Text = "Company Deliveries" + p.X.Label.Text = "Year" + if unitName != "" { + p.Y.Label.Text = fmt.Sprintf("Deliveries (%s)", unitName) + } else { + p.Y.Label.Text = "Deliveries" + } + + i := 0 + for ccID, xys := range vals { + cc := cfg.Clients[ccID] + + l, s, err := plotter.NewLinePoints(xys) + logErrorDebug(err, "botGraphDeliveries : plotter.NewLinePoints") + if err != nil { + bot.SendChat(m.Chat.ID, fmt.Sprintf("plotter.NewLinePoints : %s", err)) + return + } + + l.Color = cfg.GetClientColor(ccID) + l.Dashes = plotutil.Dashes(2) + s.Color = cfg.GetClientColor(ccID) + s.Shape = plotutil.Shape(0) + + p.Add(l) + p.Add(s) + p.Legend.Add(cc.Username, l, s) + + i++ + } + + err := p.Save(12*vg.Inch, 8*vg.Inch, "/app/data/points.png") + logErrorDebug(err, "botGraphDeliveries : plot.Save") + if err != nil { + bot.SendChat(m.Chat.ID, fmt.Sprintf("plot.Save : %s", err)) + return + } + + bot.SendChatImage(m.Chat.ID, "/app/data/points.png") + + return +} + func botGraphMoney(m *tb.Message) { var ( maxVal float64 diff --git a/version.go b/version.go index 125ed79..b1efcf3 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,6 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "bd6a542" -var buildstamp = "2021-12-11_03:50:14" -var commits = "256" -var version = "bd6a542-b256 - 2021-12-11_03:50:14" +var githash = "f21a8e5" +var buildstamp = "2021-12-12_06:30:54" +var commits = "257" +var version = "f21a8e5-b257 - 2021-12-12_06:30:54"