forked from spaceapegames/FoosLadder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
104 lines (91 loc) · 2.23 KB
/
utils.js
File metadata and controls
104 lines (91 loc) · 2.23 KB
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
var https = require('https');
exports.getFacebookData = function (accessToken, callback)
{
var options = {
host: "graph.facebook.com",
port: 443,
path: "/me?access_token="+accessToken
};
//console.log(options.host, options.port, options.path);
https.get(options, function(res)
{
var bodystring = '';
res.on('data', function (data) {
bodystring += data;
});
res.on('end', function () {
var obj;
try
{
obj = {status:"ok", body:JSON.parse(bodystring)};
}
catch (e) {
obj = {status:"error", body:e}
console.log("getFacebookData ERROR: ", e);
}
callback(obj);
});
}).on('error', function(e)
{
console.log("getFacebookData: ERROR", e);
callback({status:"error", body:e});
});
}
exports.getLeftPlayersOfObject = function(body)
{
var players = [];
addToListIfExists(players, body.leftPlayer1);
addToListIfExists(players, body.leftPlayer2);
return players;
}
exports.getRightPlayersOfObject = function(body)
{
var players = [];
addToListIfExists(players, body.rightPlayer1);
addToListIfExists(players, body.rightPlayer2);
return players;
}
exports.isDuoMatch = function(matchData)
{
return matchData.leftPlayers.length > 1 || matchData.rightPlayers.length > 1;
}
exports.getPlayerIds = function(matchData)
{
return matchData.rightPlayers.concat(matchData.leftPlayers);
}
exports.getWinnerIds = function(matchData)
{
if(matchData.leftScore > matchData.rightScore) return matchData.leftPlayers;
else if(matchData.leftScore < matchData.rightScore) return matchData.rightPlayers;
else return [];
}
exports.getLoserIds = function(matchData)
{
if(matchData.leftScore < matchData.rightScore) return matchData.leftPlayers;
else if(matchData.leftScore > matchData.rightScore) return matchData.rightPlayers;
else return matchData.rightPlayers.concat(matchData.leftPlayers);
}
function addToListIfExists(list, value)
{
if(value)
{
list.push(value);
}
}
exports.readPropertyChainStr = function(obj, dotString)
{
var properties = dotString.split(".");
return readPropertyChain(obj, properties);
}
function readPropertyChain(obj, properties)
{
for( var X in properties)
{
obj = obj[properties[X]];
if(obj == null)
{
return "";
}
}
return obj;
}