Send game state to both players from the perspective of player 1 (black)

This commit is contained in:
Trevor Slocum 2023-11-15 14:41:56 -08:00
parent ce3a930372
commit c3db42daee
3 changed files with 44 additions and 9 deletions

View file

@ -6,6 +6,8 @@ Replace spaces with underscores when sending a password to the server.
When connected via TCP, commands and events are separated by newlines.
Players always perceive games from the perspective of player number 1 (black).
## User commands
### Format

View file

@ -84,12 +84,42 @@ func (g *serverGame) sendBoard(client *serverClient) {
if client.playerNumber == 2 {
ev.GameState.Game = ev.GameState.Copy()
ev.GameState.PlayerNumber = 1
ev.GameState.Player1, ev.GameState.Player2 = ev.GameState.Player2, ev.GameState.Player1
ev.GameState.Player1.Number = 1
ev.GameState.Player2.Number = 2
switch ev.GameState.Turn {
case 1:
ev.GameState.Turn = 2
case 2:
ev.GameState.Turn = 1
}
switch ev.GameState.DoublePlayer {
case 1:
ev.GameState.DoublePlayer = 2
case 2:
ev.GameState.DoublePlayer = 1
}
switch ev.GameState.Winner {
case 1:
ev.GameState.Winner = 2
case 2:
ev.GameState.Winner = 1
}
if ev.GameState.Roll1 == 0 || ev.GameState.Roll2 == 0 {
ev.GameState.Roll1, ev.GameState.Roll2 = ev.GameState.Roll2, ev.GameState.Roll1
}
// Flip board.
for space := 1; space <= 24; space++ {
ev.Board[space] = g.Game.Board[bgammon.FlipSpace(space, client.playerNumber)]
ev.Board[space] = g.Game.Board[bgammon.FlipSpace(space, client.playerNumber)] * -1
}
ev.Board[bgammon.SpaceHomePlayer], ev.Board[bgammon.SpaceHomeOpponent] = ev.Board[bgammon.SpaceHomeOpponent], ev.Board[bgammon.SpaceHomePlayer]
ev.Board[bgammon.SpaceBarPlayer], ev.Board[bgammon.SpaceBarOpponent] = ev.Board[bgammon.SpaceBarOpponent], ev.Board[bgammon.SpaceBarPlayer]
ev.Board[bgammon.SpaceHomePlayer], ev.Board[bgammon.SpaceHomeOpponent] = ev.Board[bgammon.SpaceHomeOpponent]*-1, ev.Board[bgammon.SpaceHomePlayer]*-1
ev.Board[bgammon.SpaceBarPlayer], ev.Board[bgammon.SpaceBarOpponent] = ev.Board[bgammon.SpaceBarOpponent]*-1, ev.Board[bgammon.SpaceBarPlayer]*-1
ev.Moves = bgammon.FlipMoves(g.Game.Moves, client.playerNumber)

View file

@ -772,11 +772,6 @@ COMMANDS:
continue
}
ev := &bgammon.EventRolled{
Roll1: clientGame.Roll1,
Roll2: clientGame.Roll2,
}
ev.Player = string(cmd.client.name)
if clientGame.Turn == 0 && clientGame.Roll1 != 0 && clientGame.Roll2 != 0 {
if clientGame.Roll1 > clientGame.Roll2 {
clientGame.Turn = 1
@ -788,6 +783,14 @@ COMMANDS:
}
}
clientGame.eachClient(func(client *serverClient) {
ev := &bgammon.EventRolled{
Roll1: clientGame.Roll1,
Roll2: clientGame.Roll2,
}
ev.Player = string(cmd.client.name)
if client.playerNumber == 2 {
ev.Roll1, ev.Roll2 = ev.Roll2, ev.Roll1
}
client.sendEvent(ev)
if clientGame.Turn != 0 || !client.json {
clientGame.sendBoard(client)
@ -1052,7 +1055,7 @@ COMMANDS:
ev2 := &bgammon.EventJoined{
GameID: newGame.id,
PlayerNumber: 2,
PlayerNumber: 1,
}
ev2.Player = newGame.Player2.Name