-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiHandler.js
More file actions
288 lines (236 loc) · 7.85 KB
/
Copy pathapiHandler.js
File metadata and controls
288 lines (236 loc) · 7.85 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// var host = "unit-test.newgrounds.io";
// var version = "1.0.0";
var app_id = "45678:1beQXL4p";
var encrypt_key = "Z9GpzFnORCh+HC5X0rC8VQ==";
var ngio = new Newgrounds.io.core(app_id, encrypt_key);
ngio.debug = false;
// ngio.callComponent("Gateway.getDatetime", {}, function(result) {
// if (result.success) {
// console.log('The current date/time on the Newgrounds.io server is '+result.datetime);
// } else {
// console.log('ERROR!', result.error.message);
// }
// });
function onLoggedIn() {
console.log("Welcome " + ngio.user.name + "!");
loadMedalsAndScoreboards();
menuController.onLoggedIn(ngio.user.name);
// console.log(ngio.session_id);
}
function onLoginFailed() {
console.log("There was a problem logging in: " . ngio.login_error.message );
}
function onLoginCancelled() {
console.log("The user cancelled the login.");
}
/*
* Before we do anything, we need to get a valid Passport session. If the player
* has previously logged in and selected 'remember me', we may have a valid session
* already saved locally.
*/
function initSession() {
ngio.getValidSession(function() {
if (ngio.user) {
/*
* If we have a saved session, and it has not expired,
* we will also have a user object we can access.
* We can go ahead and run our onLoggedIn handler here.
*/
onLoggedIn();
} else {
/*
* If we didn't have a saved session, or it has expired
* we should have been given a new one at this point.
* This is where you would draw a 'sign in' button and
* have it execute the following requestLogin function.
*/
menuController.onLoggedOut();
}
});
}
/*
* Call this when the user clicks a 'sign in' button from your game. It MUST be called from
* a mouse-click event or pop-up blockers will prevent the Newgrounds Passport page from loading.
*/
function requestLogin(callback) {
ngio.requestLogin(function(){onLoggedIn();if(callback)callback();}, onLoginFailed, onLoginCancelled);
/* you should also draw a 'cancel login' buton here */
}
/*
* Call this when the user clicks a 'cancel login' button from your game.
*/
function cancelLogin() {
/*
* This cancels the login request made in the previous function.
* This will also trigger your onLoginCancelled callback.
*/
ngio.cancelLoginRequest();
}
/*
* If your user is logged in, you should also draw a 'sign out' button for them
* and have it call this.
*/
function logOut() {
ngio.logOut(function() {
/*
* Because we have to log the player out on the server, you will want
* to handle any post-logout stuff in this function, wich fires after
* the server has responded.
*/
console.log("logged out");
initSession();
});
}
/* vars to record any medals and scoreboards that get loaded */
var medals, scoreboards;
/* handle loaded medals */
function onMedalsLoaded(result) {
if (result.success)
{
medals = result.medals;
if(ngio.debug)
for (var i = 0; i < medals.length; i++)
{
medals[i].unlocked=false;
console.log(medals[i]);
}
}
}
/* handle loaded scores */
function onScoreboardsLoaded(result) {
if (result.success) scoreboards = result.scoreboards;
}
/* load our medals and scoreboards from the server */
function loadMedalsAndScoreboards()
{
ngio.queueComponent("Medal.getList", {}, onMedalsLoaded);
ngio.queueComponent("ScoreBoard.getBoards", {}, onScoreboardsLoaded);
ngio.executeQueue();
}
/* You could use this function to draw the medal notification on-screen */
function onMedalUnlocked(medal) {
console.log('MEDAL GET:', medal.name);
menuController.displayMedalUnlock(medal);
}
function reconnect(callback, shouldNotify)
{
ngio.getValidSession(function() {
if (ngio.user) {
/*
* If we have a saved session, and it has not expired,
* we will also have a user object we can access.
* We can go ahead and run our onLoggedIn handler here.
*/
onLoggedIn();
callback();
} else {
/*
* If we didn't have a saved session, or it has expired
* we should have been given a new one at this point.
* This is where you would draw a 'sign in' button and
* have it execute the following requestLogin function.
*/
menuController.onLoggedOut();
// requestLogin();
if(shouldNotify)
menuController.onSessionExpired(callback);
}
});
}
function checkConnection(callback, shouldNotify)
{
if(!callback)callback=function(){};
console.log("checking session");
if(!ngio||!ngio.session_id)
{
reconnect(callback, shouldNotify);
return;
}
ngio.callComponent("App.checkSession", {id:ngio.session_id}, function(result){
if(result.success)
{
if(result.session.expired)
{
console.log("session expired");
reconnect(callback, shouldNotify);
}else
{
console.log("session active");
callback();
}
}
else
{
logOut();
reconnect(callback, shouldNotify);
}
});
}
function pingServerToPreventExpiration()
{
// ngio.callComponent("Gateway.ping", {}, function(result)
// {
// if(ngio.debug)
// console.log(result);
// });
// if(ngio.debug)
// console.log("ping");
checkConnection();
}
function unlockMedal(medal_name)
{
checkConnection(function()
{
unlockMedal2(medal_name);
},true);
}
function unlockMedal2(medal_name) {
/* If there is no user attached to our ngio object, it means the user isn't logged in and we can't unlock anything */
if (!ngio.user) return;
var medal;
for (var i = 0; i < medals.length; i++) {
medal = medals[i];
/* look for a matching medal name */
if (medal.name == medal_name) {
/* we can skip unlocking a medal that's already been earned */
if (!medal.unlocked) {
/* unlock the medal from the server */
ngio.callComponent('Medal.unlock', {id:medal.id}, function(result) {
if (result.success){
onMedalUnlocked(result.medal);
medal.unlocked=true;
}
else console.log("failed to unlock medal");
});
}
else
{
console.log("medal already unlocked");
// onMedalUnlocked(medal);
}
return;
}
}
}
function postScore(board_name, score_value, callback)
{
checkConnection(function()
{
postScore2(board_name, score_value);
if(callback)
callback();
},true);
}
function postScore2(board_name, score_value) {
/* If there is no user attached to our ngio object, it means the user isn't logged in and we can't post anything */
if (!ngio.user) return;
var score;
for (var i = 0; i < scoreboards.length; i++) {
scoreboard = scoreboards[i];
if(scoreboard.name == board_name)
{
ngio.callComponent('ScoreBoard.postScore', {id:scoreboard.id, value:score_value});
console.log(score_value +" posted to " + board_name);
}
}
}