forked from steadicat/swarmation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayers.js
108 lines (89 loc) · 2.15 KB
/
players.js
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
var map = require('./map')
var MAX_IDLE = 120
var IDLE_AFTER_TURNS = 2
var PLAYERS = {}
var Player = function Player(client) {
this.id = client.id
this.client = client
this.idleTurns = 0
}
Player.get = function(client) {
if (!PLAYERS[client.id]) PLAYERS[client.id] = new Player(client)
return PLAYERS[client.id]
}
Player.byId = function(id) {
return PLAYERS[id]
}
Player.getList = function() {
var list = []
for (var id in PLAYERS) {
list.push(PLAYERS[id].getInfo())
}
return list
}
Player.getActive = function() {
var n = 0
for (var id in PLAYERS) {
if (!PLAYERS[id].idleTurns) n++
}
return n
}
Player.endTurn = function(socket) {
for (var id in PLAYERS) PLAYERS[id].endTurn()
}
Player.prototype = {
setInfo: function(info) {
var top = this.top
var left = this.left
for (var key in info) {
if (key == 'id') continue
if ((info[key] !== undefined) && (info[key] !== null)) this[key] = info[key]
}
map.move(left, top, this.left, this.top, this)
},
getInfo: function() {
return {
id: this.id,
left: this.left,
top: this.top,
name: this.name,
score: this.score,
total: this.total,
succeeded: this.succeeded
}
},
setActive: function() {
this.active = true
},
endTurn: function() {
if (this.active) {
this.idleTurns = 0
} else {
if (this.idleTurns == IDLE_AFTER_TURNS) {
this.client.emit('idle', { id: this.id })
this.client.broadcast.emit('idle', { id: this.id })
}
this.idleTurns++
if (this.idleTurns > MAX_IDLE) this.kick()
}
this.active = false
},
login: function(userId, token) {
this.userId = userId
this.token = token
},
disconnect: function(sockets) {
sockets.emit('disconnected', { id: this.id })
delete PLAYERS[this.id]
},
kick: function(socket) {
this.client.emit('kick', { reason: 'idle' })
this.client.broadcast.emit('disconnected', { id: this.id })
this.client.connection && this.client.connection.end()
delete PLAYERS[this.id]
}
}
var Players = {}
Players.PLAYERS = PLAYERS
Players.Player = Player
module.exports = Players