forked from 4MAW/FFS-NodeJS-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
356 lines (278 loc) · 8.99 KB
/
app.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Dependencies.
var request = require( 'request' ),
config = require( './config.js' ),
Q = require( 'q' ),
log = require( './vendor/log.js' ),
api = require( './vendor/api.js' ),
pu = require( './vendor/printUtils.js' ),
Constants = require( './vendor/constants.js' ),
io = require( 'socket.io-client' ),
readline = require( 'readline' );
// Exit handlers.
process.on( 'SIGINT', function () {
process.exit( -1 );
} );
/*
process.on( 'exit', function ()
{
socket.disconnect();
} );
*/
// Defers.
var defers = {
name: Q.defer(), // Resolved when user has chosen his name.
prematch: Q.defer(), // Resolved when user has chosen his team and has to wait for the other player.
otherPlayerPrematchReady: Q.defer(), // Resolved when the other user has chosen his team.
matchReady: Q.defer(), // Resolved when match is ready.
startMatch: Q.defer() // Resolved when match has started.
};
// Functions.
var critical_error = function ( error, area ) {
log.error( error, area );
process.exit( -1 );
};
// Settings.
var baseURL = config.baseURL + ':' + config.port;
var me = {
id: null,
team: null,
name: null
};
var he = {
id: null,
team: null,
name: null
};
var currently_in_decision_phase = false;
// Socket.
var socket = io.connect( baseURL );
socket.on( 'disconnect', function ( data ) {
critical_error( data, 'DISCONNECT' );
} );
var login = function () {
var defer_username = Q.defer();
var defer_password = Q.defer();
var username = null;
var password = null;
log.input( 'You have to login to play', 'LOGIN' );
var rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} );
rl.question( "Write your username: ".yellow, function ( answer ) {
username = answer;
me.name = username;
rl.close();
defer_username.resolve();
} );
defer_username.promise.then( function () {
var rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} );
rl.question( "Write your password: ".yellow, function ( answer ) {
password = answer;
rl.close();
defer_password.resolve();
} );
} );
defer_password.promise.then( function () {
socket.emit( Constants.LOGIN_EVENT, {
username: username,
password: password
} );
} );
};
var choose_team = function ( id ) {
// @TODO Query teams for current user.
// @TODO Request the list of teams only once.
request( baseURL + '/player/' + id + '/teams', function ( err, res ) {
if ( err ) critical_error( err );
var teams = JSON.parse( res.body );
api.loadTeams( teams ).then( function () {
log.input( 'Choose a team from the list:', 'TEAM' );
console.log( '-----------------------------------' );
pu.printTeamList( teams );
console.log( '-----------------------------------' );
var rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} );
rl.question( 'Team number: '.yellow, function ( answer ) {
me.team = teams[ answer ];
rl.close();
socket.emit( Constants.CHOOSE_TEAM_EVENT, me.team.id );
} );
} ); // All characters ready.
} ); // Teams request.
};
socket.on( Constants.WELCOME_EVENT, function () {
// I'm connected to server so I have to login.
login();
} );
socket.on( Constants.LOGIN_FAILED_EVENT, function () {
// I my login failed, login again.
login();
} );
socket.on( Constants.LOGIN_SUCCEED_EVENT, function ( data ) {
choose_team( data.id );
} );
socket.on( Constants.INVALID_TEAM_EVENT, function () {
choose_team();
} );
socket.on( Constants.VALID_TEAM_EVENT, function () {
log.info( 'Server is looking for a proper rival...', 'WAIT' );
} );
socket.on( Constants.MATCH_FOUND_EVENT, function () {
log.info( 'A rival has been found!', 'WAIT' );
} );
socket.on( Constants.SEND_RIVAL_INFO_EVENT, function ( environment ) {
var rival = environment.rival;
he.name = rival.name;
he.team = rival.team;
me.team = environment.team;
var assign_class = function ( _c ) {
return function ( _class ) {
me.team.characters[ _c ].class = _class;
};
};
var promises = [];
for ( var _c in me.team.characters ) {
var defer = Q.defer();
defer.promise.then( assign_class( _c ) );
promises.push( defer.promise );
api.loadClass( me.team.characters[ _c ].class ).then( defer.resolve );
}
Q.all( promises ).then( function () {
// Client should wait for complete initialization.
} );
} );
var decision_phase = function () {
currently_in_decision_phase = true;
pu.printBattleScenario( {
me: me,
he: he
} );
var selections = [];
var selection_promises = [];
var questions_to_ask = [];
var ask_selection_question = function ( _c, def ) {
return function () {
var c = me.team.characters[ _c ];
if ( c.alive ) {
log.info( 'What should ' + c.name + ' do?', 'CHARACTER' );
log.input( 'Choose a skill from the list:', 'SKILL' );
pu.printSkills( c );
var rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} );
rl.question( 'Skill to do: '.yellow, function ( answer ) {
rl.close();
selections[ _c ] = {
character: me.team.characters[ _c ].id,
skill: me.team.characters[ _c ].class.skills[ answer ].id,
targets: []
};
rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} );
var _i;
if ( me.team.characters[ _c ].class.skills[ answer ].multiTarget !== "2" ) {
log.input( 'Choose a target from the list:', 'SKILL' );
for ( _i in he.team.characters )
console.log( '\tCharacter ' + _i + ': ' + he.team.characters[ _i ].name );
rl.question( 'Target: '.yellow, function ( answer ) {
selections[ _c ].targets.push( he.team.characters[ answer ].id );
rl.close();
def.resolve();
} );
} else {
log.input( 'Choose a target from the list:', 'SKILL' );
for ( _i in he.team.characters )
console.log( '\tCharacter ' + _i + ': ' + he.team.characters[ _i ].name );
rl.question( 'Target: '.yellow, function ( answer ) {
selections[ _c ].targets.push( he.team.characters[ answer ].id );
rl.close();
rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} );
log.input( 'Choose a target from the list:', 'SKILL' );
for ( _i in he.team.characters )
console.log( '\tCharacter ' + _i + ': ' + he.team.characters[ _i ].name );
rl.question( 'Target: '.yellow, function ( answer ) {
selections[ _c ].targets.push( he.team.characters[ answer ].id );
rl.close();
def.resolve();
} );
} );
}
} );
}
};
};
var last_defer = Q.defer();
var last_promise = last_defer.promise;
for ( var _c in me.team.characters ) {
var c = me.team.characters[ _c ];
if ( c.alive ) {
var def = Q.defer();
last_promise.then( ask_selection_question( _c, def ) );
last_promise = def.promise;
}
}
last_defer.resolve();
last_promise.then( function () {
if ( currently_in_decision_phase ) {
currently_in_decision_phase = false;
log.info( 'Wait for the other player...', 'WAIT' );
socket.emit( Constants.DECISION_MADE_EVENT, selections );
} else {
log.warn( 'A timeout happened!' );
}
} );
};
socket.on( Constants.DECISIONS_PHASE_END_EVENT, function () {
currently_in_decision_phase = false;
log.info( 'Decisions phase ended!' );
} );
socket.on( Constants.ROUND_RESULTS_EVENT, function ( decisions ) {
for ( var i in decisions ) {
var d = decisions[ i ];
var changes = d.changes;
for ( var c in changes ) {
var change = changes[ c ];
var player_affected = he;
for ( var j in me.team.characters )
if ( me.team.characters[ j ].id === change.character.id ) player_affected = me;
var player_user = ( player_affected === he ) ? me : he;
for ( j in player_affected.team.characters ) {
if ( player_affected.team.characters[ j ].id === change.character.id ) {
if ( change.item.key === "stat" ) {
player_affected.team.characters[ j ].stats[ change.item.value ] += parseInt( change.change );
log.status( player_user.name + ' ordered ' + d.skill.caller.name + ' to use ' + d.skill.name + ' against ' + player_affected.team.characters[ j ].name + ', dealing ' + change.change + ' damage points', 'BATTLE' );
} else if ( change.item.key === "status" ) {
if ( change.change === "+" )
log.status( player_user.name + ' ordered ' + d.skill.caller.name + ' to use ' + d.skill.name + ' against ' + player_affected.team.characters[ j ].name + ', ' + change.item.value + 'ing him', 'BATTLE' );
else
log.status( player_user.name + ' ordered ' + d.skill.caller.name + ' to use ' + d.skill.name + ' against ' + player_affected.team.characters[ j ].name + ', healing his ' + change.item.value + 'ing', 'BATTLE' );
}
break;
}
}
}
}
} );
socket.on( Constants.DECISIONS_PHASE_START_EVENT, function () {
decision_phase();
} );
socket.on( Constants.WIN_EVENT, function () {
log.success( 'YOU WIN', 'GZ' );
process.exit( 0 );
} );
socket.on( Constants.LOSE_EVENT, function () {
log.error( 'Y0U L0S3', 'N00B' );
process.exit( 0 );
} );