-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanagePlayers.js
59 lines (42 loc) · 1.2 KB
/
managePlayers.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
outlets = 1;
var instanceID;
var instances = {};
function addPlayer(deviceID) {
//outlet(0, deviceID);
for (var key in instances) {
if (instances.hasOwnProperty(key) && instances[key] === deviceID) {
post("DeviceID already exists in the list!");
return;
}
}
var freeIndex = getAvailableIndex();
if (freeIndex !== null) {
post("instance added", freeIndex);
instances[freeIndex] = deviceID;
} else {
post("All player slots are occupied!");
}
outlet(0, "setvalue", freeIndex, "addPlayer", deviceID);
}
function getAvailableIndex(){
// create a key value that doesnt already exist in the instances list
//instances can only have key values up to 8
for (var i = 1; i <= 8; i+=1) {
if (!instances.hasOwnProperty(i)) {
return i;
}
}
return null; // All slots are occupied.
}
function removePlayer(deviceID){
// remove item from list with value device ID
for (var key in instances) {
if (instances[key] == deviceID) {
outlet(0, "setvalue", parseInt(key), "removePlayer", deviceID);
delete instances[key];
return; // Early exit after finding and removing the deviceID.
}
}
post("DeviceID not found!"); // Only reached if deviceID was not in the instances.
}
//