-
Notifications
You must be signed in to change notification settings - Fork 1
/
room.go
195 lines (167 loc) · 3.83 KB
/
room.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"encoding/json"
"fmt"
"net"
"strconv"
"time"
)
const (
STEP_WAIT_MS = 75
)
type Room struct {
Connections []net.Conn
Name string
}
type Game struct {
Players []NetworkPlayer
Step int
}
type NetworkPlayer struct {
Connection net.Conn
Alive bool
Moves []Move
MoveChannel chan Move
Name string
Score int
}
func HandleRoom(room Room) {
game := NewGame(room.Connections)
for {
game.Play()
time.Sleep(time.Second * 1)
game.Reset()
}
}
func (g *Game) Reset() {
for i := range g.Players {
g.Players[i].Alive = true
g.Players[i].Moves = []Move{GetStartingSpot(i)}
}
}
func NewGame(connections []net.Conn) Game {
g := Game{}
g.Players = make([]NetworkPlayer, len(connections))
for i := range g.Players {
g.Players[i].Connection = connections[i]
g.Players[i].Alive = true
g.Players[i].MoveChannel = make(chan Move, 100)
g.Players[i].Moves = []Move{GetStartingSpot(i)}
g.Players[i].Name = "Player " + strconv.Itoa(i)
g.Players[i].Score = 0
go MoveListener(g.Players[i].Connection, g.Players[i].MoveChannel)
}
return g
}
func MoveListener(conn net.Conn, moveChannel chan Move) {
conn.SetDeadline(time.Time{})
dec := json.NewDecoder(conn)
for {
var m Move
err := dec.Decode(&m)
if err != nil {
panic(err)
}
moveChannel <- m
}
}
func (g *Game) GetState(step int) (s State) {
s.Step = step
players := make([]PlayerState, len(g.Players))
for i := range g.Players {
players[i].Alive = g.Players[i].Alive
players[i].Name = g.Players[i].Name
players[i].Score = g.Players[i].Score
//return last move, for dead this may be previous
if players[i].Alive {
players[i].Move = g.Players[i].Moves[step]
} else {
players[i].Move = g.Players[i].Moves[len(g.Players[i].Moves)-1]
}
}
s.Players = players
return s
}
func (g *Game) Winner() int {
alivePlayers := 0
lastAlive := -2
for i, player := range g.Players {
if player.Alive {
alivePlayers++
lastAlive = i
}
}
if alivePlayers >= 2 {
return -1
}
return lastAlive
}
func (g *Game) Play() {
g.Step = 1
for g.Winner() == -1 { // GameOver
prevState := g.GetState(g.Step - 1)
if SERVER_DEBUG {
fmt.Println("Prev State", prevState)
}
for i, player := range g.Players {
prevState.PlayerIndex = i
out := StateToJSON(prevState)
player.Connection.Write(out)
}
// wait for new moves
time.Sleep(STEP_WAIT_MS * time.Millisecond)
// guess next move
for i := range g.Players {
if g.Players[i].Alive {
g.Players[i].Moves = append(g.Players[i].Moves, UpdateMove(g.Players[i].Moves[g.Step-1].D, g.Players[i].Moves[g.Step-1]))
} else {
continue
}
exit := false
for {
select {
case move := <-g.Players[i].MoveChannel:
// Only take direction, incase of fowl play, CACAWWWW
move = UpdateMove(move.D, g.Players[i].Moves[g.Step-1])
g.Players[i].Moves[g.Step] = move
default:
exit = true
}
if exit {
break
}
}
}
g.ValidateLastStep()
g.Step++
}
winner := g.Winner()
if winner != -2 {
g.Players[winner].Score++
}
// Game over, send last state, with updated score
prevState := g.GetState(g.Step - 1)
for i, player := range g.Players {
prevState.PlayerIndex = i
out := StateToJSON(prevState)
player.Connection.Write(out)
}
}
func (g *Game) ValidateLastStep() {
for i, player := range g.Players {
if !player.Alive {
continue
}
lastMove := player.Moves[g.Step]
if lastMove.X <= 0 || lastMove.Y <= 0 || lastMove.X >= ARENA_WIDTH-1 || lastMove.Y >= ARENA_HEIGHT-1 {
g.Players[i].Alive = false
}
for otherPlayerIndex, otherPlayer := range g.Players {
for otherMoveIndex, otherMove := range otherPlayer.Moves {
if lastMove.X == otherMove.X && lastMove.Y == otherMove.Y && !(otherMoveIndex == g.Step && otherPlayerIndex == i) {
g.Players[i].Alive = false
}
}
}
}
}