-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.go
157 lines (143 loc) · 3.99 KB
/
game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"log"
)
// these constants represent different game status messages
const waitPaired = "Waiting to get paired"
const gameBegins = "Game begins!"
const over = "Game Over"
const resetWaitPaired = "Our friend has been disconnected... Waiting to get paired again"
//playerInfo is the struct inside gameState
type playerInfo struct {
Status bool `json:"status"`
ID int `json:"id"`
}
//results is the struct inside gameState
type results struct {
PlayerOne string `json:"playerOne"`
PlayerTwo string `json:"playerTwo"`
}
// gameState is the struct which represents the gameState between two players
type gameState struct {
//renaming json values here to confirm the standard (lowercase var names)
StatusMessage string `json:"statusMessage"`
Messages []string `json:"messages"`
Started bool `json:"started"`
Over bool `json:"over"`
PlayerOne playerInfo `json:"playerOne"`
PlayerTwo playerInfo `json:"playerTwo"`
Results results `json:"results"`
Reset bool `json:"reset"`
//These are not exported to JSON
numberOfPlayers int
playerOneChoice string
playerTwoChoice string
}
// newGameState is the constructor for the gameState struct and creates the initial gameState Struct (empty board)
func newGameState() gameState {
pi := playerInfo{
Status: false,
}
gs := gameState{
StatusMessage: waitPaired,
Messages: make([]string, 0),
Started: false,
PlayerOne: pi,
PlayerTwo: pi,
//These are not exported to JSON
numberOfPlayers: 0,
}
return gs
}
// addPlayer informs the gameState about the new player and alters the statusMessage
func (gs *gameState) addPlayer() {
gs.numberOfPlayers++
switch gs.numberOfPlayers {
case 1:
gs.StatusMessage = waitPaired
case 2:
gs.StatusMessage = gameBegins
gs.Started = true
}
}
// recordMove takes in the message and records it
// set id to player
// define type and run funcs accordingly
// record to messages if message
// record move if otherwise
func (gs *gameState) recordMove(msg *incomingMessage) {
switch msg.Type {
case "message":
gs.Reset = false
gs.recordMessage(msg.Data)
case "submission":
gs.Reset = false
gs.recordSubmission(msg)
case "command":
gs.Reset = true
gs.restartGame()
}
gs.checkWinner()
}
//TODO: Refactor
//recordSubmission takes the response and assigns it to the player
func (gs *gameState) recordSubmission(msg *incomingMessage) {
if gs.PlayerOne.ID == 0 {
gs.PlayerOne.ID = msg.ID
gs.playerOneChoice = msg.Data
gs.PlayerOne.Status = true
} else if gs.PlayerOne.ID == msg.ID {
gs.playerOneChoice = msg.Data
gs.PlayerOne.Status = true
} else if gs.PlayerTwo.ID == 0 {
gs.PlayerTwo.ID = msg.ID
gs.playerTwoChoice = msg.Data
gs.PlayerTwo.Status = true
} else if gs.PlayerTwo.ID == msg.ID {
gs.playerTwoChoice = msg.Data
gs.PlayerTwo.Status = true
}
}
func (gs *gameState) checkWinner() {
if gs.PlayerOne.Status && gs.PlayerTwo.Status {
gs.Results = results{
gs.playerOneChoice,
gs.playerTwoChoice,
}
gs.Over = true
gs.StatusMessage = over
}
}
func (gs *gameState) recordMessage(text string) {
gs.Messages = append(gs.Messages, text)
}
// restartGame sets the gameState to a state so that a new game between the same
// players can begin
func (gs *gameState) restartGame() {
pi := playerInfo{
Status: false,
}
gs.StatusMessage = gameBegins
gs.Over = false
gs.PlayerOne = pi
gs.PlayerTwo = pi
gs.Results = results{}
gs.playerOneChoice = ""
gs.playerTwoChoice = ""
}
// resetGame is needed, when one player drops out. It sets the gameState to a state so that
// the player who is left can wait for a new opponent.
func (gs *gameState) resetGame() {
gs.restartGame()
gs.Started = false
gs.StatusMessage = resetWaitPaired
}
// gameStateToJSON marshals the gameState struct to JSON represented by a slice of bytes
func (gs *gameState) gameStateToJSON() []byte {
json, err := json.Marshal(gs)
if err != nil {
log.Fatal("Error in marshalling json:", err)
}
return json
}