/
services.go
187 lines (160 loc) · 4.58 KB
/
services.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
package main
import (
"errors"
"log"
"sync"
"github.com/coopernurse/gorp"
"github.com/nu7hatch/gouuid"
)
type GameService interface {
NewGame(gameType string, db *gorp.DbMap) (*Game, *Player, error)
ConnectToGame(db *gorp.DbMap, gameId string, playerObj interface{}) (*Game, *Player, error)
GetGame(db *gorp.DbMap, gameId string, playerId int) (*Game, *Player, error)
HostJoin(gameId string) chan Message
PlayerJoin(gameId string, playerId int) chan Message
PlayerLeave(gameId string, playerId int)
Broadcast(gameId string, msg Message)
SendHost(gameId string, msg Message)
GetConnectedPlayers(gameId string) []int
}
type Channels struct {
players map[int]chan Message
host chan Message
}
// TODO: this all needs to be in a different package
type GameServiceImpl struct {
sync.RWMutex
ChannelMap map[string]*Channels
}
func (gs *GameServiceImpl) HostJoin(gameId string) chan Message {
gs.Lock()
defer gs.Unlock()
log.Printf("HERE")
// host is usually first to join a game so most of the time this will be called
if gs.ChannelMap[gameId] == nil {
log.Printf("channel map created for game %v", gameId)
gs.ChannelMap[gameId] = &Channels{players: map[int]chan Message{}}
}
if gs.ChannelMap[gameId].host == nil {
log.Printf("Host connecting for first time")
gs.ChannelMap[gameId].host = make(chan Message)
}
return gs.ChannelMap[gameId].host
}
func (gs *GameServiceImpl) PlayerJoin(gameId string, playerId int) chan Message {
gs.Lock()
defer gs.Unlock()
// if the server restarts and a player rejoins before the host, this will be called
if gs.ChannelMap[gameId] == nil {
log.Printf("First player to connect to game is player %v", playerId)
gs.ChannelMap[gameId] = &Channels{players: map[int]chan Message{}}
}
gs.ChannelMap[gameId].players[playerId] = make(chan Message)
return gs.ChannelMap[gameId].players[playerId]
}
func (gs *GameServiceImpl) PlayerLeave(gameId string, playerId int) {
gs.Lock()
defer gs.Unlock()
close(gs.ChannelMap[gameId].players[playerId])
delete(gs.ChannelMap[gameId].players, playerId)
}
func (gs *GameServiceImpl) Broadcast(gameId string, msg Message) {
gs.RLock()
defer gs.RUnlock()
for _, p := range gs.ChannelMap[gameId].players {
p <- msg
}
}
func (gs *GameServiceImpl) SendHost(gameId string, msg Message) {
gs.RLock()
defer gs.RUnlock()
if gs.ChannelMap[gameId].host == nil {
log.Printf("HOST NOT CONNECTED?!")
}
gs.ChannelMap[gameId].host <- msg
}
func (gs *GameServiceImpl) GetConnectedPlayers(gameId string) []int {
gs.RLock()
defer gs.RUnlock()
players := []int{}
for pid, _ := range gs.ChannelMap[gameId].players {
players = append(players, pid)
}
return players
}
func (gs *GameServiceImpl) NewGame(gameType string, db *gorp.DbMap) (*Game, *Player, error) {
u, err := uuid.NewV4()
if err != nil {
return nil, nil, err
}
game := &Game{Id: u.String(), State: "lobby", Type: gameType}
err = db.Insert(game)
if err != nil {
return nil, nil, err
}
player := &Player{
Game: game.Id,
Role: Host,
}
err = db.Insert(player)
if err != nil {
return nil, nil, err
}
return game, player, nil
}
func (gs *GameServiceImpl) ConnectToGame(db *gorp.DbMap, gameId string, playerObj interface{}) (*Game, *Player, error) {
obj, err := db.Get(Game{}, gameId)
if err != nil {
return nil, nil, err
}
if obj == nil {
return nil, nil, errors.New("Player not saved to session")
}
game := obj.(*Game)
var player *Player
if playerObj == nil { // no, it's a new player
player = &Player{
Game: game.Id,
}
// save to db so we can find them if they disconnect
err = db.Insert(player)
if err != nil {
return nil, nil, err
}
} else { // player is rejoining
playerObj, err := db.Get(Player{}, playerObj)
if err != nil {
return nil, nil, err
}
player = playerObj.(*Player)
// TODO: this would screw with any games they are currently already in?
if player.Game != game.Id {
player.Game = game.Id
count, err := db.Update(player)
if count == 0 {
return nil, nil, errors.New("Player update effected 0 rows")
}
if err != nil {
return nil, nil, err
}
log.Printf("Joining player id is: %#v", player.Id)
} else {
log.Printf("Returning player id is: %#v", player.Id)
}
}
return game, player, nil
}
func (gs *GameServiceImpl) GetGame(db *gorp.DbMap, gameId string, playerId int) (*Game, *Player, error) {
obj, err := db.Get(Player{}, playerId)
if err != nil {
return nil, nil, err
}
player := obj.(*Player)
// get the game from the db to load the state, other info
g, err := db.Get(Game{}, gameId)
if err != nil {
return nil, nil, err
}
game := g.(*Game)
return game, player, nil
}