forked from imrenagi/cloud-run-hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
170 lines (144 loc) · 3.33 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
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
)
type ArenaUpdate struct {
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
Arena struct {
Dimensions []int `json:"dims"`
State map[string]PlayerState `json:"state"`
} `json:"arena"`
}
type PlayerState struct {
URL string `json:"-"`
X int `json:"x"`
Y int `json:"y"`
Direction string `json:"direction"`
WasHit bool `json:"wasHit"`
Score int `json:"score"`
}
func (p PlayerState) GetDirection() Direction {
switch p.Direction {
case "N":
return North
case "W":
return West
case "E":
return East
default:
return South
}
}
type Mode string
func (m Mode) NeedLeaderboard() bool {
return m == ZombieMode || m == AggressiveMode
}
const (
NormalMode Mode = "normal"
BraveMode Mode = "brave"
// ZombieMode tries to attack player with lowest rank
ZombieMode Mode = "zombie"
// AggressiveMode tries to climbing up the leaderboard
AggressiveMode Mode = "aggressive"
)
type Game struct {
Arena Arena
PlayerStateByURL map[string]PlayerState
LeaderBoard LeaderBoard
Mode Mode
}
const (
defaultAttackRange int = 3
)
type GameOption func(*GameOptions)
type GameOptions struct {
Mode Mode
}
func WithGameMode(m Mode) GameOption {
return func(options *GameOptions) {
options.Mode = m
}
}
func NewGame(opts ...GameOption) Game {
o := &GameOptions{
Mode: NormalMode,
}
for _, opt := range opts {
opt(o)
}
return Game{
Mode: o.Mode,
}
}
func (g *Game) UpdateArena(ctx context.Context, a ArenaUpdate) {
ctx, span := tracer.Start(ctx, "Game.UpdateArena")
defer span.End()
width := a.Arena.Dimensions[0]
height := a.Arena.Dimensions[1]
arena := NewArena(width, height)
g.LeaderBoard = []PlayerState{}
for k, v := range a.Arena.State {
v.URL = k
arena.PutPlayer(v)
g.LeaderBoard = append(g.LeaderBoard, v)
}
g.Arena = arena
g.PlayerStateByURL = a.Arena.State
if g.Mode.NeedLeaderboard() {
g.UpdateLeaderBoard(ctx)
}
}
func (g Game) Player(url string) *Player {
pState := g.PlayerStateByURL[url]
player := NewPlayerWithUrl(url, pState)
player.Game = g
switch g.Mode {
case BraveMode:
player.Strategy = NewBraveStrategy()
default:
player.Strategy = NewNormalStrategy()
}
return player
}
func (g Game) Update(player *Player) {
updatedPlayer := g.PlayerStateByURL[player.Name]
player.X = updatedPlayer.X
player.Y = updatedPlayer.Y
player.Direction = updatedPlayer.Direction
player.WasHit = updatedPlayer.WasHit
player.Score = updatedPlayer.Score
player.Game = g
}
func (g *Game) UpdateLeaderBoard(ctx context.Context) {
ctx, span := tracer.Start(ctx, "Game.UpdateLeaderBoard")
defer span.End()
g.LeaderBoard.Sort()
}
func (g Game) GetPlayerStateByPosition(p Point) (PlayerState, bool) {
player := g.Arena.Grid[p.Y][p.X].Player
if player == nil {
return PlayerState{}, false
}
return *player, true
}
// GetPlayerByRank rank starts from 0 (highest rank)
func (g Game) GetPlayerByRank(rank int) *Player {
ps := g.LeaderBoard.GetPlayerByRank(rank)
if ps == nil {
return nil
}
return g.GetPlayerByPosition(Point{ps.X, ps.Y})
}
func (g Game) GetPlayerByPosition(p Point) *Player {
pState := g.Arena.Grid[p.Y][p.X].Player
if pState == nil {
return nil
}
player := NewPlayerWithUrl(pState.URL, *pState)
player.Game = g
return player
}