Skip to content

Commit 951cda8

Browse files
committed
Implement optional race timeout
1 parent 1664f9d commit 951cda8

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

events.cge

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ config {
1212
max_velocity: float,
1313
// The amount of checkpoints per game. default = 10
1414
checkpoint_count: int,
15-
// The time in milliseconds that a game is allowed to last. default = infinite
15+
// The time in seconds that a game is allowed to last. default = infinite
1616
timeout: int
1717
}
1818

@@ -71,8 +71,8 @@ event finished_players {
7171
players: list<finished_player>
7272
}
7373

74-
// The `game_over` event is sent when all players finished the game or the time runs out.
75-
event game_over {
74+
// The `race_over` event is sent when all players finished the game or the time runs out.
75+
event race_over {
7676
// The players that have finished the game before the time ran out.
7777
finished_players: list<finished_player>,
7878
// The IDs of the players that have not finished the game before the time ran out.

hoverrace/event_definitions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type GameConfig struct {
1313
MaxVelocity float64 `json:"max_velocity"`
1414
// The amount of checkpoints per game. default = 10
1515
CheckpointCount int `json:"checkpoint_count"`
16-
// The time in milliseconds that a game is allowed to last. default = infinite
16+
// The time in seconds that a game is allowed to last. default = infinite
1717
Timeout int `json:"timeout"`
1818
}
1919

@@ -93,10 +93,10 @@ type FinishedPlayersEventData struct {
9393
Players []FinishedPlayer `json:"players"`
9494
}
9595

96-
// The `game_over` event is sent when all players finished the game or the time runs out.
97-
const GameOverEvent cg.EventName = "game_over"
96+
// The `race_over` event is sent when all players finished the game or the time runs out.
97+
const RaceOverEvent cg.EventName = "race_over"
9898

99-
type GameOverEventData struct {
99+
type RaceOverEventData struct {
100100
// The players that have finished the game before the time ran out.
101101
FinishedPlayers []FinishedPlayer `json:"finished_players"`
102102
// The IDs of the players that have not finished the game before the time ran out.

hoverrace/game.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ func (g *Game) Run() {
5050
g.handleCommand(cmd.Origin, cmd.Cmd)
5151
}
5252
g.update(deltaTime)
53-
time.Sleep(targetFrameTime - time.Now().Sub(frameStart))
53+
time.Sleep(targetFrameTime - time.Since(frameStart))
54+
deltaTime = time.Since(frameStart)
5455

55-
deltaTime = time.Now().Sub(frameStart)
56-
// waited for countdown
56+
// correct time after waiting for countdown
5757
if deltaTime >= 5*time.Second {
5858
deltaTime = targetFrameTime
5959
}
@@ -204,6 +204,10 @@ func (g *Game) update(delta time.Duration) {
204204
Hovercrafts: g.hovercrafts,
205205
Time: time.Now().UnixMilli(),
206206
})
207+
208+
if g.running && g.config.Timeout > 0 && time.Since(g.startTime) > time.Duration(g.config.Timeout)*time.Second {
209+
g.finish()
210+
}
207211
}
208212

209213
func (g *Game) handleCommand(origin *cg.Player, cmd cg.Command) {
@@ -320,6 +324,17 @@ func (g *Game) positionHovercrafts() {
320324
}
321325

322326
func (g *Game) finish() {
327+
unfinishedPlayers := make([]string, 0)
328+
for _, p := range g.players {
329+
if !p.finished {
330+
unfinishedPlayers = append(unfinishedPlayers, p.id)
331+
}
332+
p.finished = true
333+
}
334+
g.cg.Send(RaceOverEvent, RaceOverEventData{
335+
FinishedPlayers: g.finishedPlayers,
336+
UnfinishedPlayers: unfinishedPlayers,
337+
})
323338
g.running = false
324339
g.checkpoints = g.checkpoints[:0]
325340
}

0 commit comments

Comments
 (0)