Skip to content

Commit 921487b

Browse files
committed
Adds prelim match stats tracking
1 parent 6cf7c45 commit 921487b

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

discord/discordGameState.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ type DiscordGameState struct {
5353
Running bool `json:"running"`
5454
Subscribed bool `json:"subscribed"`
5555

56+
MatchID int64 `json:"matchID"`
57+
MatchStartUnix int64 `json:"matchStartUnix"`
58+
Winners []game.GameWinner `json:"winners"`
59+
GameResult game.GameResult `json:"gameResult"`
60+
5661
UserData UserDataSet `json:"userData"`
5762
Tracking TrackingChannel `json:"tracking"`
5863

@@ -62,23 +67,21 @@ type DiscordGameState struct {
6267
}
6368

6469
func NewDiscordGameState(guildID string) *DiscordGameState {
65-
return &DiscordGameState{
66-
GuildID: guildID,
67-
ConnectCode: "",
68-
Linked: false,
69-
Running: false,
70-
Subscribed: false,
71-
UserData: UserDataSet{},
72-
Tracking: TrackingChannel{},
73-
GameStateMsg: MakeGameStateMessage(),
74-
AmongUsData: game.NewAmongUsData(),
75-
}
70+
dgs := DiscordGameState{GuildID: guildID}
71+
dgs.Reset()
72+
return &dgs
7673
}
7774

7875
func (dgs *DiscordGameState) Reset() {
76+
//Explicitly does not reset the GuildID!
7977
dgs.ConnectCode = ""
8078
dgs.Linked = false
8179
dgs.Running = false
80+
dgs.Subscribed = false
81+
dgs.MatchID = -1
82+
dgs.MatchStartUnix = -1
83+
dgs.Winners = []game.GameWinner{}
84+
dgs.GameResult = -1
8285
dgs.UserData = map[string]UserData{}
8386
dgs.Tracking = TrackingChannel{}
8487
dgs.GameStateMsg = MakeGameStateMessage()

discord/eventHandler.go

+16
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ func (bot *Bot) processTransition(phase game.Phase, dgsRequest GameStateRequest)
239239
lock.Release(ctx)
240240
return
241241
}
242+
//if we started a new game
243+
if oldPhase == game.LOBBY && phase == game.TASKS {
244+
matchID := bot.RedisInterface.GetAndIncrementMatchID()
245+
matchStart := time.Now().Unix()
246+
dgs.MatchStartUnix = matchStart
247+
dgs.MatchID = matchID
248+
log.Printf("New match has begun. ID %d and starttime %d\n", matchID, matchStart)
249+
//if we went to lobby from anywhere else but the menu, assume the game is over
250+
} else if phase == game.LOBBY && oldPhase != game.MENU {
251+
//TODO process the game's completion and send to Postgres
252+
//only process games that actually receive the end-game event from the capture! Might need to start a worker
253+
//to listen for this
254+
255+
dgs.MatchID = -1
256+
dgs.MatchStartUnix = -1
257+
}
242258

243259
bot.RedisInterface.SetDiscordGameState(dgs, lock)
244260
switch phase {

discord/redis.go

+12
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ func cacheHash(guildID string) string {
6767
return "automuteus:discord:" + guildID + ":cache"
6868
}
6969

70+
func matchIDKey() string {
71+
return "automuteus:match:counter"
72+
}
73+
74+
func (redisInterface *RedisInterface) GetAndIncrementMatchID() int64 {
75+
num, err := redisInterface.client.Incr(ctx, matchIDKey()).Result()
76+
if err != nil {
77+
log.Println(err)
78+
}
79+
return num
80+
}
81+
7082
func (redisInterface *RedisInterface) SetVersion(version string) {
7183
err := redisInterface.client.Set(ctx, versionKey(), version, 0).Err()
7284
if err != nil {

game/state.go

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ const (
3232
EXILED PlayerAction = iota
3333
)
3434

35+
type GameResult int
36+
37+
const (
38+
HumansByVote GameResult = iota
39+
HumansByTask
40+
ImpostorByVote
41+
ImpostorByKill
42+
ImpostorBySabotage
43+
ImpostorDisconnect
44+
HumansDisconnect
45+
)
46+
47+
type GameWinner struct {
48+
}
49+
3550
type PhaseNameString string
3651

3752
// PhaseNames for lowercase, possibly for translation if needed

0 commit comments

Comments
 (0)