Skip to content

Commit 7444c95

Browse files
committed
Stats from savegame
not done
1 parent 0d0cf3b commit 7444c95

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

games/game.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ type Game struct {
2323
Environment []string `json:"environment"`
2424
Mods []string `json:"mods"`
2525
CustomParameters []string `json:"custom_parameters"`
26-
Stats map[string]int `json:"stats"`
26+
ConsoleStats map[string]int `json:"stats"`
2727
Playtime int64 `json:"playtime"`
2828
LastPlayed string `json:"last_played"`
2929
SaveGameCount int `json:"save_game_count"`
3030
Rating int `json:"rating"`
31-
LvlStats SaveStats
31+
Stats SaveStats
32+
StatsSum LevelStats
3233
}
3334

3435
// NewGame creates new instance of a game
@@ -42,7 +43,7 @@ func NewGame(name, sourceport, iwad string) Game {
4243
Environment: make([]string, 0),
4344
CustomParameters: make([]string, 0),
4445
Mods: make([]string, 0),
45-
Stats: make(map[string]int),
46+
ConsoleStats: make(map[string]int),
4647
}
4748

4849
// replace with given or first list entry
@@ -66,9 +67,19 @@ func NewGame(name, sourceport, iwad string) Game {
6667
return game
6768
}
6869

69-
func (g Game) GetStats() SaveStats {
70+
// ReadStats tries to read stats from the newest existing savegame
71+
func (g *Game) ReadStats() {
7072
lastSavePath, _ := g.lastSave()
71-
return getStatsFromSavegame(lastSavePath)
73+
g.Stats = getStatsFromSavegame(lastSavePath)
74+
g.StatsSum = LevelStats{}
75+
for _, s := range g.Stats.Levels {
76+
g.StatsSum.KillCount += s.KillCount
77+
g.StatsSum.TotalKills += s.TotalKills
78+
g.StatsSum.ItemCount += s.ItemCount
79+
g.StatsSum.TotalItems += s.TotalItems
80+
g.StatsSum.SecretCount += s.SecretCount
81+
g.StatsSum.TotalSecrets += s.TotalSecrets
82+
}
7283
}
7384

7485
// Run executes given configuration and launches the mod
@@ -133,6 +144,7 @@ func (g *Game) run(rcfg runconfig) (err error) {
133144

134145
// could take a while ...
135146
go processOutput(string(output), g)
147+
go g.ReadStats()
136148

137149
return
138150
}
@@ -346,12 +358,12 @@ func (g Game) cleansedName() string {
346358

347359
// processOutput processes the terminal output of the zdoom port
348360
func processOutput(output string, g *Game) {
349-
if g.Stats == nil {
350-
g.Stats = make(map[string]int)
361+
if g.ConsoleStats == nil {
362+
g.ConsoleStats = make(map[string]int)
351363
}
352364
for _, v := range strings.Split(output, "\n") {
353365
if stat, increment := parseStatline(v, g); stat != "" {
354-
g.Stats[stat] = g.Stats[stat] + increment
366+
g.ConsoleStats[stat] = g.ConsoleStats[stat] + increment
355367
}
356368
}
357369

games/games.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package games
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io/ioutil"
76
"sort"
87
"sync"
@@ -126,11 +125,8 @@ func loadGames() error {
126125
return err
127126
}
128127

129-
for i, v := range instance {
130-
//if lastSavePath, err := v.lastSave(); err == nil {
131-
instance[i].LvlStats = v.GetStats()
132-
fmt.Println(instance[i].LvlStats)
133-
//}
128+
for i, _ := range instance {
129+
go instance[i].ReadStats()
134130
}
135131

136132
return nil

games/levelStats.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ type LevelStats struct {
4040
}
4141

4242
func getStatsFromSavegame(path string) SaveStats {
43+
zeroLevel := LevelStats{}
44+
emtpyStats := SaveStats{
45+
Levels: []LevelStats{zeroLevel},
46+
}
47+
4348
sls, err := zdoomStatsFromJSON(path)
4449
if err == nil {
4550
return sls
@@ -50,7 +55,7 @@ func getStatsFromSavegame(path string) SaveStats {
5055
return sls
5156
}
5257

53-
return SaveStats{}
58+
return emtpyStats
5459
}
5560

5661
// ZDOOM
@@ -67,8 +72,9 @@ func zdoomStatsFromJSON(path string) (SaveStats, error) {
6772
},
6873
}
6974

70-
save.Stats.Savegame = path
71-
json.Unmarshal(jsonContent, &save)
75+
if err := json.Unmarshal(jsonContent, &save); err != nil {
76+
return SaveStats{}, err
77+
}
7278

7379
return save.Stats, nil
7480
}

tui/gamesTable.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ func populateGamesTable() {
7777
//var cell *tview.TableCell
7878
switch r {
7979
case 0:
80-
g = &games.Game{}
80+
//g = &games.Game{}
81+
return
8182
//cell = tview.NewTableCell("")
8283
default:
8384
g = &allGames[r-fixRows]

tui/stats.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,16 @@ func makeStatsTable(g *games.Game) *tview.Table {
2121
}
2222

2323
row := 0
24-
pts := float64(g.Playtime) / 1000 / 60
2524

26-
//saveStats := g.GetStats()
27-
stats.SetCell(row, 0, tview.NewTableCell("# Kills").SetTextColor(tview.Styles.SecondaryTextColor))
28-
if len(g.LvlStats.Levels) > 0 {
29-
stats.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%v", g.LvlStats.Levels[0].KillCount)).SetAlign(tview.AlignLeft))
30-
}
31-
32-
row++
25+
// generic stuff
3326
stats.SetCell(row, 0, tview.NewTableCell("# Savegames").SetTextColor(tview.Styles.SecondaryTextColor))
3427
stats.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%v", g.SaveCount())).SetAlign(tview.AlignLeft))
3528
row++
3629
stats.SetCell(row, 0, tview.NewTableCell("# Demos").SetTextColor(tview.Styles.SecondaryTextColor))
3730
stats.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%v", g.DemoCount())).SetAlign(tview.AlignLeft))
3831
row++
3932
stats.SetCell(row, 0, tview.NewTableCell("Playtime").SetTextColor(tview.Styles.SecondaryTextColor))
40-
stats.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%.2f min", pts)).SetAlign(tview.AlignLeft))
33+
stats.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%.2f min", float64(g.Playtime)/1000/60)).SetAlign(tview.AlignLeft))
4134
row++
4235
stats.SetCell(row, 0, tview.NewTableCell("Last Played").SetTextColor(tview.Styles.SecondaryTextColor))
4336
stats.SetCell(row, 1, tview.NewTableCell(fmt.Sprint(g.LastPlayed)).SetAlign(tview.AlignLeft))
@@ -46,7 +39,17 @@ func makeStatsTable(g *games.Game) *tview.Table {
4639
// stats.SetCell(row, 1, tview.NewTableCell(""))
4740
row++
4841

49-
for k, v := range g.Stats {
42+
// stats from savegames
43+
stats.SetCell(row, 0, tview.NewTableCell("# Kills").SetTextColor(tview.Styles.SecondaryTextColor))
44+
stats.SetCell(row+1, 0, tview.NewTableCell("# Secrets").SetTextColor(tview.Styles.SecondaryTextColor))
45+
stats.SetCell(row+2, 0, tview.NewTableCell("# Items").SetTextColor(tview.Styles.SecondaryTextColor))
46+
stats.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%v/%v", g.StatsSum.KillCount, g.StatsSum.TotalKills)).SetAlign(tview.AlignLeft))
47+
stats.SetCell(row+1, 1, tview.NewTableCell(fmt.Sprintf("%v/%v", g.StatsSum.SecretCount, g.StatsSum.TotalSecrets)).SetAlign(tview.AlignLeft))
48+
stats.SetCell(row+2, 1, tview.NewTableCell(fmt.Sprintf("%v/%v", g.StatsSum.ItemCount, g.StatsSum.TotalItems)).SetAlign(tview.AlignLeft))
49+
row += 4
50+
51+
// what the game printed into console
52+
for k, v := range g.ConsoleStats {
5053
stats.SetCell(row, 0, tview.NewTableCell(strings.Title("# "+k)).SetTextColor(tview.Styles.SecondaryTextColor))
5154
stats.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%v", v)).SetAlign(tview.AlignLeft))
5255
row++

0 commit comments

Comments
 (0)