-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpug.go
253 lines (207 loc) · 4.31 KB
/
pug.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"log"
"strings"
"strconv"
"math/rand"
"time"
)
const MAX_PLAYERS = 10
var validMaps, teamName []string
var pugManager []*PUG
var teamNameCT, teamNameT string
type PUG struct {
pugID int
mapName string
ircChannel string
players[] string
pugAdmin string
pugStarted, pugActive bool
}
func SetAllowedMaps(maps []string) {
validMaps = maps;
}
func GetValidMaps() string {
return strings.Join(validMaps, " ")
}
func IsValidMap(mapName string) bool {
validMaps := strings.Join(validMaps, " ")
if (strings.Contains(validMaps, mapName)) {
return true
} else {
return false
}
}
func GetPugCounter() (int) {
return len(pugManager)
}
func NewPug(pug *PUG) {
if (len(pugManager) == 0) {
pug.pugID = 0
} else {
pug.pugID = len(pugManager)
}
pugManager = append(pugManager, pug)
}
func GetPugByPlayer(player string) (*PUG, bool) {
for i := range pugManager {
if pugManager[i].GetPlayerByName(player) {
return pugManager[i], true
}
}
return nil, false
}
func GetPugByChannel(channel string) (*PUG, bool) {
for i := range pugManager {
if pugManager[i].GetIRCChannel() == channel {
return pugManager[i], true
}
}
return nil, false
}
func DeletePug(pugID int) {
for i := range pugManager {
if (pugID == pugManager[i].GetPugID()) {
pugManager = append(pugManager[:i], pugManager[i+1:]...)
return
}
}
}
func (p *PUG) GetPlayerByName(player string) (bool) {
if (!p.pugStarted) {
return false
}
for i := range p.players {
if (player == p.players[i]) {
return true
}
}
return false
}
func (p *PUG) PugStarted() bool {
return p.pugStarted
}
func (p *PUG) SetPugActive(active bool) {
p.pugActive = active
}
func (p *PUG) PugActive() bool {
return p.pugActive
}
func (p *PUG) GenerateRandomPassword(word string) string {
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Intn(999-100) + 100
return word + strconv.Itoa(i)
}
func (p *PUG) JoinPug(player string) bool {
if (!p.pugStarted || len(p.players) == MAX_PLAYERS) {
return false
}
if (len(p.players) == 0) {
p.pugAdmin = player;
}
players := strings.Join(p.players, " ")
if (strings.Contains(players, player)) {
return false
} else {
p.players = append(p.players, player)
return true
}
}
func (p *PUG) GetPlayerCount() int {
return len(p.players)
}
func (p *PUG) AssignNewAdmin() {
if (p.GetPlayerCount() == 1) {
p.pugAdmin = p.players[0]
return;
}
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Intn(p.GetPlayerCount()-0)
p.pugAdmin = p.players[i]
}
func (p *PUG) RandomisePlayerList() {
rand.Seed(time.Now().UnixNano())
for i := range p.players {
j := rand.Intn(i + 1)
p.players[i], p.players[j] = p.players[j], p.players[i]
}
}
func (p *PUG) UpdatePlayerNickname(oldNick string, newNick string) {
for i := range p.players {
if (oldNick == p.players[i]) {
p.players[i] = newNick
if (p.pugAdmin == oldNick) {
p.pugAdmin = newNick
}
break;
}
}
}
func (p *PUG) LeavePug(player string) bool {
if (!p.pugStarted) {
return false
}
for i := range p.players {
if (player == p.players[i]) {
p.players = append(p.players[:i], p.players[i+1:]...)
return true
}
}
return false
}
func (p *PUG) GetPugID() (int) {
return p.pugID
}
func (p *PUG) SetMap(mapName string) {
p.mapName = mapName
}
func (p *PUG) GetMap() string {
return p.mapName
}
func (p *PUG) GetAdmin() string {
return p.pugAdmin
}
func (p *PUG) SetIRCChannel(channel string) {
p.ircChannel = channel
}
func (p *PUG) GetIRCChannel() string {
return p.ircChannel
}
func (p *PUG) GetPlayers() []string {
return p.players
}
func (p *PUG) StartPug() {
if (p.pugStarted) {
return;
}
if (len(p.mapName) > 0) && IsValidMap(p.mapName) {
log.Printf("Pug map is %s", p.mapName)
} else {
p.mapName = "de_dust2"
}
p.pugStarted = true
}
func (p *PUG) EndPug() {
if (!p.pugStarted) {
return
}
p.pugStarted = false
p.pugActive = false
p.mapName = ""
p.players = nil
p.ircChannel = ""
}
func SetTeamName(names []string) {
teamName = names
teamNameCT, teamNameT = teamName[rand.Intn(len(teamName))], teamName[rand.Intn(len(teamName))]
for teamNameCT == teamNameT {
teamNameT = teamName[rand.Intn(len(teamName))]
}
log.Println("The set teams are: ", teamNameCT, " & ", teamNameT)
}
func GetTeamNameCT() string {
return teamNameCT
}
func GetTeamNameT() string {
return teamNameT
}