aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/game
diff options
context:
space:
mode:
Diffstat (limited to 'backend/game')
-rw-r--r--backend/game/http.go5
-rw-r--r--backend/game/hub.go10
-rw-r--r--backend/game/models.go6
3 files changed, 19 insertions, 2 deletions
diff --git a/backend/game/http.go b/backend/game/http.go
index f9036c5..865c724 100644
--- a/backend/game/http.go
+++ b/backend/game/http.go
@@ -56,5 +56,10 @@ func (h *sockHandler) HandleSockGolfWatch(c echo.Context) error {
if hub == nil {
return echo.NewHTTPError(http.StatusNotFound, "Game not found")
}
+
+ if hub.game.gameType != gameType1v1 {
+ return echo.NewHTTPError(http.StatusBadRequest, "Only 1v1 game is supported")
+ }
+
return serveWatcherWs(hub, c.Response(), c.Request())
}
diff --git a/backend/game/hub.go b/backend/game/hub.go
index 239f5da..70bf71f 100644
--- a/backend/game/hub.go
+++ b/backend/game/hub.go
@@ -81,7 +81,7 @@ func (hub *gameHub) run() {
entriedPlayerCount++
}
}
- if entriedPlayerCount == 2 {
+ if entriedPlayerCount == hub.game.playerCount {
err := hub.q.UpdateGameState(hub.ctx, db.UpdateGameStateParams{
GameID: int32(hub.game.gameID),
State: string(gameStateWaitingStart),
@@ -101,7 +101,7 @@ func (hub *gameHub) run() {
readyPlayerCount++
}
}
- if readyPlayerCount == 2 {
+ if readyPlayerCount == hub.game.playerCount {
startAt := time.Now().Add(11 * time.Second).UTC()
for player := range hub.players {
player.s2cMessages <- &playerMessageS2CStart{
@@ -300,6 +300,11 @@ func (hubs *GameHubs) RestoreFromDB(ctx context.Context) error {
description: *row.Description,
}
}
+ // TODO: N+1
+ playerRows, err := hubs.q.ListGamePlayers(ctx, int32(row.GameID))
+ if err != nil {
+ return err
+ }
hubs.hubs[int(row.GameID)] = newGameHub(ctx, &game{
gameID: int(row.GameID),
durationSeconds: int(row.DurationSeconds),
@@ -307,6 +312,7 @@ func (hubs *GameHubs) RestoreFromDB(ctx context.Context) error {
displayName: row.DisplayName,
startedAt: startedAt,
problem: problem_,
+ playerCount: len(playerRows),
}, hubs.q)
}
return nil
diff --git a/backend/game/models.go b/backend/game/models.go
index 6c299d6..4080482 100644
--- a/backend/game/models.go
+++ b/backend/game/models.go
@@ -6,9 +6,13 @@ import (
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/api"
)
+type gameType = api.GameGameType
type gameState = api.GameState
const (
+ gameType1v1 = api.N1V1
+ gameTypeMultiplayer = api.Multiplayer
+
gameStateClosed gameState = api.Closed
gameStateWaitingEntries gameState = api.WaitingEntries
gameStateWaitingStart gameState = api.WaitingStart
@@ -20,11 +24,13 @@ const (
type game struct {
gameID int
+ gameType gameType
state gameState
displayName string
durationSeconds int
startedAt *time.Time
problem *problem
+ playerCount int
}
type problem struct {