Skip to content

Commit

Permalink
added timer support, fixed voldyman#1
Browse files Browse the repository at this point in the history
  • Loading branch information
voldyman committed May 18, 2020
1 parent 5563f08 commit f931fc2
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 30 deletions.
30 changes: 23 additions & 7 deletions actionrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"sync"
"time"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -145,20 +146,17 @@ func (a *ActionRouter) RoomByName(roomName string, action RoomAction) bool {
func startRoomRouter(r *Room) RoomActionReceiver {
actionChan := make(chan RoomAction)
go func() {
for {
select {
case action := <-actionChan:
action(r)
}
for action := range actionChan {
action(r)
}
}()
return actionChan
}

func (a *ActionRouter) CheckIfPlayerExists(playerID string, res func(players, rooms int, playerID string, isInRoom bool, gs *gameState)) {
func (a *ActionRouter) CheckIfPlayerExists(playerID string, res func(players, rooms int, playerID string, isInRoom bool, gs gameState)) {
rr := a.PlayerRoomReceiver(playerID)
if rr == nil {
res(a.Players(), a.Rooms(), playerID, false, nil)
res(a.Players(), a.Rooms(), playerID, false, gameState{})
return
}

Expand All @@ -167,6 +165,23 @@ func (a *ActionRouter) CheckIfPlayerExists(playerID string, res func(players, ro
}
}

func (a *ActionRouter) TimedPlayerRoomAction(playerID string, action RoomAction) {
go func() {
ticker := time.NewTicker(1 * time.Second)
for {
<-ticker.C
rr := a.PlayerRoomReceiver(playerID)
if rr == nil {
ticker.Stop()
log.WithField("PlayerID", playerID).Warn("could not setup timed action, player not in any room")
return
}

rr <- action
}
}()
}

func (a *ActionRouter) LeaveRoom(playerID string, action RoomAction) bool {
if rr := a.PlayerRoomReceiver(playerID); rr != nil {
rr <- action
Expand All @@ -179,6 +194,7 @@ func (a *ActionRouter) LeaveRoom(playerID string, action RoomAction) bool {

// todo(voldy): delay this for later?
if len(r.Players) == 0 {
close(a.nameRooms[r.Name])
delete(a.nameRooms, r.Name)
}
}
Expand Down
2 changes: 1 addition & 1 deletion game.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func NewGame(bt BoardType) *Game {
Turn: turn,
Over: false,
Winner: nil,
Timer: 5,
Timer: 5 * 60,
Board: generateBoard(bt, turn),
Log: []GameLog{},
Clue: nil,
Expand Down
2 changes: 1 addition & 1 deletion pkged.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions public/js/codenames.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ function updateLog(log) {
")";
} else if (logEntry.event === "endTurn") {
logSpan.innerText = logEntry.team + " team ended their turn";
} else if (logEntry.event === "timeout") {
logSpan.innerText = logEntry.team + " did not answer on time and ended their turn";
}
logDiv.prepend(logSpan);
});
Expand Down
54 changes: 44 additions & 10 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ func (r *Room) switchTurns() {
"ToTeam": otherTeam(r.Game.Turn),
}).Info("Switching teams")

r.Game.Timer = r.Game.TimerAmount
r.Game.Turn = otherTeam(r.Game.Turn)
r.Game.turnsTaken = 0
r.Game.Clue = nil
Expand Down Expand Up @@ -425,14 +426,23 @@ func (r *Room) ChangeCards(playerID, pack string) {
r.Game.WordPool = wordpoolSize(r.boardType)
}

func (r *Room) GameState() *gameState {
return &gameState{
func (r *Room) GameState() gameState {
game := Game{}
if r.Game != nil {
game = *r.Game
}
players := map[string]Player{}
for p := range r.Players {
players[p] = *r.Players[p]
}

return gameState{
Room: r.Name,
Game: r.Game,
Game: game,
Difficulty: r.Difficulty,
Consensus: r.Consesus,
Mode: r.Mode,
Players: r.Players,
Players: players,
}
}

Expand All @@ -443,6 +453,30 @@ func (r *Room) ChangeTimer(playerID string, value float64) {
}

r.Game.TimerAmount = value * 60
r.Game.Timer = r.Game.TimerAmount
}

func (r *Room) TimerTick() bool {
if r.Mode != "timed" {
return false
}
if r.Game.Timer > 0 {
r.Game.Timer--
}
if r.Game.Timer == 0 {
r.Game.Log = append(r.Game.Log, GameLog{
Event: "timeout",
Team: r.Game.Turn,
EndedTurn: true,
})
r.switchTurns()
return true
}

if r.Game.Over {
return true
}
return false
}

// PlayerLogged ... Get a player and automatically log if not ok
Expand Down Expand Up @@ -473,10 +507,10 @@ func buildSet(elem ...string) map[string]struct{} {
}

type gameState struct {
Room string `json:"room"`
Players map[string]*Player `json:"players"`
Game *Game `json:"game"`
Difficulty string `json:"difficulty"`
Mode string `json:"mode"`
Consensus string `json:"consensus"`
Room string `json:"room"`
Players map[string]Player `json:"players"`
Game Game `json:"game,omitempty"`
Difficulty string `json:"difficulty"`
Mode string `json:"mode"`
Consensus string `json:"consensus"`
}
1 change: 0 additions & 1 deletion room_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"log"
"testing"
)

Expand Down
30 changes: 20 additions & 10 deletions socket_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func socketServer(a *ActionRouter) *socketio.Server {

s.Emit("reset")

a.CheckIfPlayerExists(playerID, func(players, rooms int, playerID string, isInRoom bool, gs *gameState) {
a.CheckIfPlayerExists(playerID, func(players, rooms int, playerID string, isInRoom bool, gs gameState) {
s.Emit("serverStats", struct {
Players int `json:"players"`
Rooms int `json:"rooms"`
SessionID string `json:"sessionId"`
IsExistingPlayer bool `json:"isExistingPlayer"`
GameState *gameState `json:"gameState"`
Players int `json:"players"`
Rooms int `json:"rooms"`
SessionID string `json:"sessionId"`
IsExistingPlayer bool `json:"isExistingPlayer"`
GameState gameState `json:"gameState,omitempty"`
}{
Players: players,
Rooms: rooms,
Expand Down Expand Up @@ -182,13 +182,12 @@ func socketServer(a *ActionRouter) *socketio.Server {
"PlayerID": ctx.PlayerID,
}).Info("received leaveRoomRequest")

a.RoomForPlayer(ctx.PlayerID, func(r *Room) {
r.Leave(ctx.PlayerID)

a.LeaveRoom(ctx.PlayerID, func(r *Room) {
s.Leave(r.Name)

server.BroadcastToRoom("/", r.Name, "gameState", r.GameState())
})

s.Emit("reset")
s.Emit("leaveResponse", leaveRoomResponse{
Success: true,
Expand Down Expand Up @@ -333,6 +332,9 @@ func socketServer(a *ActionRouter) *socketio.Server {
type switchModeRequest struct {
Mode string `json:"mode"`
}
type timerUpdateMessage struct {
Timer float64 `json:"timer"`
}
server.OnEvent("/", "switchMode", func(s socketio.Conn, req switchModeRequest) {
ctx, ok := s.Context().(connContext)
if !ok {
Expand All @@ -348,9 +350,17 @@ func socketServer(a *ActionRouter) *socketio.Server {

ok = a.RoomForPlayer(ctx.PlayerID, func(r *Room) {
r.SwitchMode(ctx.PlayerID, req.Mode)

server.BroadcastToRoom("/", r.Name, "gameState", r.GameState())
})

a.TimedPlayerRoomAction(ctx.PlayerID, func(r *Room) {
if turnOver := r.TimerTick(); turnOver {
server.BroadcastToRoom("/", r.Name, "gameState", r.GameState())
}
server.BroadcastToRoom("/", r.Name, "timerUpdate", timerUpdateMessage{
Timer: r.Game.Timer,
})
})
if !ok {
s.Emit("reset")
}
Expand Down

0 comments on commit f931fc2

Please sign in to comment.