Skip to content

Commit ab0dbad

Browse files
committed
Insert multiplayer matches into the database after match ends
1 parent 7a05876 commit ab0dbad

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

db/multiplayer_game_matches.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package db
2+
3+
import (
4+
"example.com/Quaver/Z/common"
5+
"example.com/Quaver/Z/objects"
6+
)
7+
8+
type MultiplayerMatch struct {
9+
Id int
10+
GameId int `db:"game_id"`
11+
TimePlayed int64 `db:"time_played"`
12+
MapMd5 string `db:"map_md5"`
13+
MapName string `db:"map"`
14+
HostId int `db:"host_id"`
15+
Ruleset objects.MultiplayerGameRuleset `db:"ruleset"`
16+
GameMode common.Mode `db:"game_mode"`
17+
GlobalModifiers common.Mods `db:"global_modifiers"`
18+
FreeMod objects.MultiplayerGameFreeMod `db:"free_mod_type"`
19+
HealthType objects.MultiplayerGameHealth `db:"health_type"`
20+
Lives int `db:"lives"`
21+
Aborted bool `db:"aborted"`
22+
}
23+
24+
// InsertIntoDatabase Inserts a multiplayer match into the database and returns the insert id of it.
25+
func (match *MultiplayerMatch) InsertIntoDatabase() error {
26+
query := "INSERT INTO multiplayer_game_matches" +
27+
"(game_id, time_played, map_md5, map, host_id, ruleset, game_mode, global_modifiers, free_mod_type, health_Type, lives, aborted) " +
28+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
29+
30+
result, err := SQL.Exec(query, match.GameId, match.TimePlayed, match.MapMd5, match.MapName, match.HostId, match.Ruleset, match.GameMode,
31+
match.GlobalModifiers, match.FreeMod, match.HealthType, match.Lives, match.Aborted)
32+
33+
if err != nil {
34+
match.Id = -1
35+
return err
36+
}
37+
38+
id, err := result.LastInsertId()
39+
40+
if err != nil {
41+
match.Id = -1
42+
return err
43+
}
44+
45+
match.Id = int(id)
46+
return nil
47+
}

multiplayer/game.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,17 @@ func (game *Game) EndGame() {
319319
return
320320
}
321321

322+
game.clearCountdown()
323+
game.clearReadyPlayers(false)
324+
game.updatePlayerWinCount()
325+
game.insertMatchIntoDatabase()
326+
game.rotateHost()
327+
322328
game.Data.InProgress = false
323329
game.playersInMatch = []int{}
324330
game.playersScreenLoaded = []int{}
325331
game.playersFinished = []int{}
326332
game.playersSkipped = []int{}
327-
game.clearCountdown()
328-
game.clearReadyPlayers(false)
329-
game.rotateHost()
330-
331-
game.updatePlayerWinCount()
332333
game.playerScores = map[int]*scoring.ScoreProcessor{}
333334

334335
game.sendPacketToPlayers(packets.NewServerGameEnded())
@@ -778,6 +779,31 @@ func (game *Game) updatePlayerWinCount() {
778779
}
779780
}
780781

782+
// Inserts the current match + scores into the database.
783+
func (game *Game) insertMatchIntoDatabase() {
784+
if len(game.playerScores) == 0 {
785+
return
786+
}
787+
788+
match := db.MultiplayerMatch{
789+
GameId: game.Data.Id,
790+
TimePlayed: time.Now().UnixMilli(),
791+
MapMd5: game.Data.MapMD5,
792+
MapName: game.Data.MapName,
793+
HostId: game.Data.HostId,
794+
Ruleset: game.Data.Ruleset,
795+
GameMode: game.Data.MapGameMode,
796+
GlobalModifiers: game.Data.GlobalModifiers,
797+
FreeMod: game.Data.FreeModType,
798+
}
799+
800+
err := match.InsertIntoDatabase()
801+
802+
if err != nil {
803+
log.Printf("Failed to insert match from game #%v into database - %v\n", game.Data.Id, err)
804+
}
805+
}
806+
781807
// Clears and stops the countdown timer.
782808
func (game *Game) clearCountdown() {
783809
if game.countdownTimer != nil {

0 commit comments

Comments
 (0)