-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplayer.go
51 lines (41 loc) · 887 Bytes
/
player.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
package main
import (
"fmt"
"math/rand"
"golang.org/x/net/websocket"
)
// Player holds the state of a player.
type Player struct {
ID string
Nickname string
Ready bool
Conn *Connection
}
var players []*Player
var nameBases = []string{"user", "player", "person", "rip", "oc", "car", "mouse", "frog", "piano"}
func nicknameExists(nick string) bool {
for _, player := range players {
if player.Nickname == nick {
return true
}
}
return false
}
func genNickname() string {
var nick string
loop := true
for loop {
nick = fmt.Sprintf("%s%d", nameBases[rand.Intn(len(nameBases))], rand.Intn(1000))
loop = nicknameExists(nick)
}
return nick
}
func makePlayer(ws *websocket.Conn) *Player {
player := &Player{
ID: UniqIDf(),
Nickname: genNickname(),
Conn: makeConnection(ws),
}
players = append(players, player)
return player
}