forked from 4MAW/FFS-NodeJS-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_random.js
276 lines (218 loc) · 7.08 KB
/
bot_random.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
// 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 );
} );
// 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 );
//setTimeout( start, 5000 );
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( 'Logging in...', 'LOGIN' );
username = process.argv[ 2 ] || "demo";
password = process.argv[ 3 ] || "demo";
me.name = username;
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 () {
me.team = teams[ Math.floor( Math.random() * teams.length ) ];
socket.emit( Constants.CHOOSE_TEAM_EVENT, me.team.id );
log.info( "Chosen team 0", "TEAM" );
} ); // 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 () {
log.error( "Error when logging in", "LOGIN" );
process.exit( -1 );
} );
socket.on( Constants.LOGIN_SUCCEED_EVENT, function ( data ) {
log.success( 'Logged in!', 'LOGIN' );
choose_team( data.id );
} );
socket.on( Constants.INVALID_TEAM_EVENT, function () {
log.error( "Error when choosing team", "TEAM" );
process.exit( -1 );
} );
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 counter = 0;
var ask_selection_question = function ( _c ) {
var skill_to_use;
var targets;
var r;
while ( skill_to_use === undefined ) {
r = Math.floor( Math.random() * me.team.characters[ _c ].class.skills.length );
skill_to_use = me.team.characters[ _c ].class.skills[ r ];
}
var team_source = he.team.characters;
if ( skill_to_use.defaultTeam === 'own' )
team_source = me.team.characters;
if ( skill_to_use.multiTarget === '2' ) {
targets = [];
while ( targets.length < 2 ) {
r = Math.floor( Math.random() * team_source.length );
if ( team_source[ r ].stats[ Constants.HEALTH_STAT_ID ] > 0 )
targets.push( team_source[ r ].id );
}
} else {
while ( targets === undefined ) {
r = Math.floor( Math.random() * team_source.length );
if ( team_source[ r ].stats[ Constants.HEALTH_STAT_ID ] > 0 )
targets = [ team_source[ r ].id ];
}
}
skill_to_use = skill_to_use.id;
selections[ _c ] = {
character: me.team.characters[ _c ].id,
skill: skill_to_use,
targets: targets
};
};
for ( var _c in me.team.characters )
ask_selection_question( _c );
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, 10 );
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 () {
pu.printBattleScenario( {
me: me,
he: he
} );
log.success( 'YOU WIN', 'GZ' );
process.exit( 0 );
} );
socket.on( Constants.LOSE_EVENT, function () {
pu.printBattleScenario( {
me: me,
he: he
} );
log.error( 'Y0U L0S3', 'N00B' );
process.exit( 0 );
} );