aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/game/hub.go27
-rw-r--r--backend/game/message.go23
2 files changed, 44 insertions, 6 deletions
diff --git a/backend/game/hub.go b/backend/game/hub.go
index 170f142..c61f2bb 100644
--- a/backend/game/hub.go
+++ b/backend/game/hub.go
@@ -68,7 +68,7 @@ func (hub *gameHub) run() {
hub.closeWatcherClient(watcher)
}
case message := <-hub.playerC2SMessages:
- switch message.message.(type) {
+ switch msg := message.message.(type) {
case *playerMessageC2SEntry:
log.Printf("entry: %v", message.message)
// TODO: assert state is waiting_entries
@@ -142,11 +142,34 @@ func (hub *gameHub) run() {
}
hub.game.state = gameStateStarting
}
+ case *playerMessageC2SCode:
+ // TODO: assert game state is gaming
+ log.Printf("code: %v", message.message)
+ code := msg.Data.Code
+ score := len(code)
+ message.client.s2cMessages <- &playerMessageS2CExecResult{
+ Type: playerMessageTypeS2CExecResult,
+ Data: playerMessageS2CExecResultPayload{
+ Score: &score,
+ Status: api.Success,
+ },
+ }
default:
log.Fatalf("unexpected message type: %T", message.message)
}
case <-ticker.C:
- if hub.game.state == gameStateGaming {
+ if hub.game.state == gameStateStarting {
+ if time.Now().After(*hub.game.startedAt) {
+ err := hub.q.UpdateGameState(hub.ctx, db.UpdateGameStateParams{
+ GameID: int32(hub.game.gameID),
+ State: string(gameStateGaming),
+ })
+ if err != nil {
+ log.Fatalf("failed to set game state: %v", err)
+ }
+ hub.game.state = gameStateGaming
+ }
+ } else if hub.game.state == gameStateGaming {
if time.Now().After(hub.game.startedAt.Add(time.Duration(hub.game.durationSeconds) * time.Second)) {
err := hub.q.UpdateGameState(hub.ctx, db.UpdateGameStateParams{
GameID: int32(hub.game.gameID),
diff --git a/backend/game/message.go b/backend/game/message.go
index 23774ce..9116bde 100644
--- a/backend/game/message.go
+++ b/backend/game/message.go
@@ -8,10 +8,12 @@ import (
)
const (
- playerMessageTypeS2CPrepare = "player:s2c:prepare"
- playerMessageTypeS2CStart = "player:s2c:start"
- playerMessageTypeC2SEntry = "player:c2s:entry"
- playerMessageTypeC2SReady = "player:c2s:ready"
+ playerMessageTypeS2CPrepare = "player:s2c:prepare"
+ playerMessageTypeS2CStart = "player:s2c:start"
+ playerMessageTypeS2CExecResult = "player:s2c:execreslut"
+ playerMessageTypeC2SEntry = "player:c2s:entry"
+ playerMessageTypeC2SReady = "player:c2s:ready"
+ playerMessageTypeC2SCode = "player:c2s:code"
)
type playerMessageC2SWithClient struct {
@@ -26,10 +28,14 @@ type playerMessageS2CPrepare = api.GamePlayerMessageS2CPrepare
type playerMessageS2CPreparePayload = api.GamePlayerMessageS2CPreparePayload
type playerMessageS2CStart = api.GamePlayerMessageS2CStart
type playerMessageS2CStartPayload = api.GamePlayerMessageS2CStartPayload
+type playerMessageS2CExecResult = api.GamePlayerMessageS2CExecResult
+type playerMessageS2CExecResultPayload = api.GamePlayerMessageS2CExecResultPayload
type playerMessageC2S = interface{}
type playerMessageC2SEntry = api.GamePlayerMessageC2SEntry
type playerMessageC2SReady = api.GamePlayerMessageC2SReady
+type playerMessageC2SCode = api.GamePlayerMessageC2SCode
+type playerMessageC2SCodePayload = api.GamePlayerMessageC2SCodePayload
func asPlayerMessageC2S(raw map[string]json.RawMessage) (playerMessageC2S, error) {
var typ string
@@ -46,6 +52,15 @@ func asPlayerMessageC2S(raw map[string]json.RawMessage) (playerMessageC2S, error
return &playerMessageC2SReady{
Type: playerMessageTypeC2SReady,
}, nil
+ case playerMessageTypeC2SCode:
+ var payload playerMessageC2SCodePayload
+ if err := json.Unmarshal(raw["data"], &payload); err != nil {
+ return nil, err
+ }
+ return &playerMessageC2SCode{
+ Type: playerMessageTypeC2SCode,
+ Data: payload,
+ }, nil
default:
return nil, fmt.Errorf("unknown message type: %s", typ)
}