-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
213 lines (177 loc) · 4.1 KB
/
model.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
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"time"
"sync"
)
const cardDir = "res/cards"
const blackPath = "res/monke.jpg"
type Team struct {
id int
points int
players []*Player
}
func (t *Team) getID() int {
return t.id
}
func (t *Team) addPlayer(p *Player){
p.team = t
t.players = append(t.players, p)
}
type Card struct {
id int
}
func (c Card) getPath() string {
return cardDir + "/" + fmt.Sprint(c.id) + ".png"
}
type Player struct {
id int
name string
team *Team
connected bool
}
type ModelImpl struct {
sync.Mutex
nextID int
currentPlayer int
currentCard Card
teams []*Team
Players map[int]*Player
cards []Card
extractableCards []int
}
func (this *ModelImpl) getNumCards() int {
return len(this.cards)
}
func (this *ModelImpl) getBlackPath() string {
return blackPath
}
func (this *ModelImpl) init(numTeams int){
rand.Seed(time.Now().UTC().UnixNano())
//initialize teams
this.teams = make([]*Team, numTeams)
for i := range this.teams {
this.teams[i] = &Team{id: i, points: 0}
this.teams[i].players = make([]*Player,0)
}
//load number of cards from cards dir
cards, _ := ioutil.ReadDir(cardDir)
//initialize extractableCards
this.extractableCards = make([]int, len(cards))
for i := range this.extractableCards {
this.extractableCards[i] = i
}
//initialize cards
this.cards = make([]Card, len(cards))
for i := range this.cards {
this.cards[i] = Card{i}
}
//initilize players map
this.Players = make(map[int]*Player)
}
func (this *ModelImpl) getTeamWithLessPlayers() *Team {
min := len(this.teams[0].players)
team := this.teams[0]
for _, t := range this.teams {
if len(t.players) < min {
team = t
}
}
return team
}
func (this *ModelImpl) setDisconnected(id int){
this.Players[id].connected = false;
}
func (this *ModelImpl) setConnected(id int){
this.Players[id].connected = true;
}
func (this *ModelImpl) existsName(name string) bool {
fmt.Println("Checking ", name)
for _, p := range this.Players {
if p.name == name {
return true
}
}
return false
}
func (this *ModelImpl) getPlayerID(name string) (int, error) {
for id, p := range this.Players {
if p.name == name {
return id, nil
}
}
return -1, nil
}
//returns true if name is available
func (this *ModelImpl) checkName(name string) bool {
for _, p := range this.Players {
if p.name == name {
return false
}
}
return true
}
func (this *ModelImpl) getPlayerList() playersList {
var result playersList
for _, p := range this.Players {
entry := playerEntry{
Name : p.name,
Team : p.team.id,
IsTurn : p.id == this.currentPlayer,
}
result.addPlayer(entry)
}
return result
}
func (this *ModelImpl) addPlayer(name string) (*Player, error) {
if !(this.checkName(name)){
return nil, errors.New("Name already taken")
}
id := this.nextID
this.nextID++
team := this.getTeamWithLessPlayers()
p := &Player{name: name,
team: team,
id: len(this.Players),
connected: true,
}
team.addPlayer(p)
if _, ok := this.Players[id]; ok {
return nil, errors.New("Player already in game")
}
this.Players[id] = p
return p, nil
}
func remove(s []int, i int) []int {
s[len(s)-1], s[i] = s[i], s[len(s)-1]
return s[:len(s)-1]
}
func (this *ModelImpl) extractCard() Card {
//get random number
n := rand.Intn(len(this.extractableCards))
index := this.extractableCards[n]
//remove element n from extractableCards
this.extractableCards = remove(this.extractableCards, n)
this.currentCard = this.cards[index]
return this.currentCard
}
func (this *ModelImpl) nextTurn() {
this.currentPlayer = (this.currentPlayer + 1) % len(this.Players)
}
func (this* ModelImpl) currentTeam() int {
return this.Players[this.currentPlayer].team.id
}
func (this* ModelImpl) getTeam(playerID int) *Team {
return this.Players[playerID].team
}
func (this *ModelImpl) isWatching(playerID int) bool{
//a player is watching if he is playing or the current player is not from his team
notFromTeam := this.getTeam(playerID).id != this.currentTeam()
return this.isPlaying(playerID) || notFromTeam
}
func (this *ModelImpl) isPlaying(playerID int) bool{
return playerID == this.currentPlayer
}