-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
468 lines (426 loc) · 12.9 KB
/
engine.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
var GameState = require('./common/GameState.js');
var Player = require('./models/player.js');
require('./common/utils.js');
//the game engine
var game_engine = {
game_state : new GameState(),
start : function(){
// for(var i=0; i<this.game_state.punts.length; i++){
// this.game_state.punts[i].ware = this.game_state.wares[i+1];
// }
if(this.game_state.started){
return;
}
for(var i=0; i<this.game_state.players.length; i++){
this.game_state.players[i].init();
}
this.game_state.init();
this.game_state.phase = 0;
this.game_state.started = true;
this.init_shares();
this.start_auction();
},
start_auction:function(){
this.game_state.auction_players = [];
this.game_state.auction_price = 0;
for(var i=0; i<this.game_state.players.length; i++){
this.game_state.auction_players.push(this.game_state.players[i].id);
}
for(var i=0; i<this.game_state.punts.length; i++){
this.game_state.punts[i].init();
// this.game_state.punts[i].ware = this.game_state.wares[i+1];
}
for(var i=0; i<this.game_state.spaces.length; i++){
this.game_state.spaces[i].owner = null;
}
},
add_player : function(client) {
if(this.game_state.started){
return -1;
}
var player_id = this.game_state.players.length;
// console.log("clientId:"+client.id);
this.game_state.players.push(new Player(player_id, client.id, "test"));
// console.log("clientId2:"+this.game_state.players[0].clientId);
// this.num_players += 1;
return player_id;
},
remove_player : function(client) {
console.log("removing " + client);
var selected;
for(var i=0; i< this.game_state.players.length; i++){
var player = this.game_state.players[i];
if(player.clientId == client.id){
selected = player;
}
}
this.game_state.players.remove(selected);
if(this.game_state.players.length == 0){
this.game_state.started = false;
}
},
next_player : function() {
if (this.game_state.players.length == 0) {
return 0;
}
if(this.game_state.phase == 0){
this.game_state.current_player_id = this.get_next_auction_player();
// console.log("current_player_id:"+ this.game_state.current_player_id);
}else{
this.game_state.current_player_id = (this.game_state.current_player_id + 1) % this.game_state.players.length;
this.game_state.acted_players += 1;
}
},
get_next_auction_player:function(){
var current_index = this.game_state.auction_players.indexOf(this.game_state.current_player_id);
var next_player_id = null;
var removeArray = [];
// console.log("this.game_state.current_player_id:"+this.game_state.current_player_id);
// console.log("current_index:"+current_index);
for (var i = 0; i < this.game_state.auction_players.length; i++) {
var player = this.game_state.players[this.game_state.auction_players[i]];
var share_count = player.shares["nutme"] + player.shares["ginseng"] + player.shares["silk"] + player.shares["jade"];
// || player.connect_statue == false
if ( (player.money + share_count*12) <= this.game_state.auction_price || !player.auction_state) {
player.auction_state = false;
removeArray.push(player.id);
}else if(!next_player_id && i > current_index){
next_player_id = player.id;
}
}
for(var i=0; i<removeArray.length; i++){
var playerid = removeArray[i];
var index = this.game_state.auction_players.indexOf(playerid);
this.game_state.auction_players.splice(index, 1);
}
// console.log(this.game_state.auction_players);
if(!next_player_id){
next_player_id = this.game_state.auction_players[0];
}
return next_player_id;
},
get_gamestate : function() {
return this.game_state;
},
get_punt_count:function(state){
var count = 0;
for (var i = 0; i < this.game_state.punts.length; i++) {
var punt = this.game_state.punts[i];
if(punt.state == state){
count++;
}
}
return count;
},
place_fail_punt:function(){
var count = 0;
for (var i = 0; i < this.game_state.punts.length; i++) {
var punt = this.game_state.punts[i];
console.log("punt state:"+punt.state);
if(punt.state == 1){
punt.state = 3;
punt.order = count;
count++;
}
}
},
init_shares:function(){
for(var k=0; k<2; k++){
for(var i = 0; i < this.game_state.players.length; i++){
var j = Math.floor(Math.random()*this.game_state.shares_array.length); // random num
var player = this.game_state.players[i];
var share = this.game_state.shares_array[j];
this.choose_share(player, share, false);
}
}
},
handle_action : function(action) {
//perform action
if(action.type === "start") {
this.start();
return true;
}else if(action.type === "dice") {
this.roll_dice();
return true;
}else if(action.type === "ready"){
this.start_new_auction_phase();
}else if(action.type === "init_punts"){
return this.init_punts(action);
}else if (action.type === "place") {
return this.place_dude(action);
} else if (action.type === "choose_ware") {
return this.choose_ware(action);
} else if (action.type === "auction") {
return this.auction(action);
} else if (action.type === "auction_drop") {
return this.auction_drop();
} else if (action.type === "choose_share") {
return this.choose_share_by_action(action);
}
return false;
},
init_punts:function(action){
var array = action.position;
var wares = action.wares;
var result = true;
if(array[0] + array[1] + array[2] == 9){
// console.log(array[0] + array[1] + array[2]);
for(var i in array){
if(array[i]>5 || array[i]<0){
result = false;
}
}
if(result){
this.game_state.punts[0].position = array[0];
this.game_state.punts[1].position = array[1];
this.game_state.punts[2].position = array[2];
this.advance_phase();
}
}else{
result = false;
}
this.game_state.punts[0].ware = this.game_state.wares[wares[0]];
this.game_state.punts[1].ware = this.game_state.wares[wares[1]];
this.game_state.punts[2].ware = this.game_state.wares[wares[2]];
return result;
},
start_new_auction_phase:function(action){
this.game_state.acted_players += 1;
this.advance_phase();
},
auction : function(action) {
add_price = 1;
if (add_price < 1) {
return false;
}
var add_price = action.add_price;
this.game_state.auction_price += parseInt(add_price); // add_price是在当前竞价上增加的值
this.game_state.last_captain = this.game_state.current_player_id;
// console.log(this.game_state.last_captain);
this.next_player();
if(this.auction_result()){
this.advance_phase();
}
return true;
},
auction_drop : function() {
var player = this.game_state.players[this.game_state.current_player_id];
player.auction_state = false; // 将当前player的竞选状态置为false,使之不能参加下次竞选
// index = this.game_state.auction_players.indexOf(player.id);
// this.game_state.auction_players.splice(index, 1);
this.next_player();
if(this.auction_result()){
this.advance_phase();
}
return true;
},
auction_result : function() {
// console.log("last_captain:"+this.game_state.last_captain);
if (this.game_state.auction_players.length <= 1) {
this.game_state.players[this.game_state.last_captain].roleId = 1;
this.game_state.players[this.game_state.last_captain].money -= this.game_state.auction_price;
this.game_state.captain_id = this.game_state.last_captain;
return true;
}else {
return false;
}
},
choose_share_by_action:function(action){
var player = this.game_state.players[action.player_id];
if(player.roleId != 1){
return false;
}
var share = action.share;
this.advance_phase();
return this.choose_share(player, share);
},
choose_share:function(player, share, buy){
if(this.game_state.shares[share] > 0){
player.shares[share] += 1;
if(buy){
player.money -= (this.game_state.share_prices[share] == 0 ? 5 : this.game_state.share_prices[share]);
}
this.game_state.shares[share] -= 1;
this.game_state.shares_array.remove(share);
return true;
}
return false;
},
roll_dice : function() {
for (var i = 0; i < this.game_state.punts.length; i++) {
var punt = this.game_state.punts[i];
punt.position += (1 + Math.floor(Math.random() * 6));
if(punt.position > 13 && punt.state == 1){
punt.order = this.get_punt_count(2);
punt.state = 2;
}
}
},
place_dude : function(action) {
//assert it is the current player
console.log("place");
if(!this.is_correct_player(action)) {
console.log("not is_correct_player");
return false;
}
//assert it is the correct turn
var space = this.game_state.spaces[action.space_id];
var player = this.game_state.players[action.player_id];
//can't place on owned square
if(space.owner != null) {
console.log("has owner");
return false;
}
//check that you can afford it
if(space.payment > player.money) {
console.log("invalid money");
return false;
}
space.owner = action.player_id;
player.money -= space.payment;
this.next_player();
this.advance_phase();
return true;
},
choose_ware : function(action) {
var ships = this.game_state.punts;
ships[0].ware = action.first_ware;
ships[1].ware = action.second_ware;
ships[2].ware = action.third_ware;
return true;
},
is_correct_player : function(action) {
return action.player_id === this.game_state.current_player_id
},
init : function() {
this.game_state = new GameState();
return this;
},
advance_phase : function() {
var end_of_placement = function(engine) {
engine.game_state.acted_players = 0;
engine.game_state.current_player_id = engine.game_state.captain_id;
engine.roll_dice();
}
switch(this.game_state.phase) {
case -1:{ //waiting user_action phase
if(this.game_state.acted_players == this.game_state.players.length) {
this.game_state.phase = 0;
this.game_state.acted_players = 0;
this.start_auction();
}
break;
}
case 3: {
//check phase
if(this.game_state.acted_players == this.game_state.players.length) {
end_of_placement(this);
this.game_state.phase += 1;
}
break;
}
case 4: {
//check phase
if(this.game_state.acted_players == this.game_state.players.length) {
end_of_placement(this);
this.game_state.phase += 1;
}
break;
}
case 5: {
//check phase
if(this.game_state.acted_players == this.game_state.players.length) {
end_of_placement(this);
this.place_fail_punt();
this.compute_round_score();
this.compute_share_price();
if(this.end_conditions_met()) {
this.game_state.phase = 7; //game over
this.game_state.started = false;
this.find_out_winner();
} else {
this.game_state.phase = -1;
//this.start_auction();
}
}
break;
}
default:{
this.game_state.phase += 1;
}
}
},
end_conditions_met : function() {
for(var share_name in this.game_state.share_prices) {
if (this.game_state.share_prices[share_name] >= 30) {
return true;
}
}
return false;
},
find_out_winner:function(){
var players = this.game_state.players;
var max = 0;
var winner;
for(var i=0; i<players.length; i++){
if(players[i].total_score(this.game_state) > max){
max = players[i].total_score(this.game_state);
winner = players[i];
}
}
this.game_state.winner = winner;
},
compute_round_score : function() {
//tally up the score
var ships_crossed = 0;
var ships = this.game_state.punts;
var players = this.game_state.players;
for(var i = 0; i < ships.length; i++) {
var ship = ships[i];
if(ship.position > 13) {
console.log("crossed:" + ship.ware.name)
ships_crossed += 1;
var spaces = this.spaces_with_ware(ship.ware.name, true);
console.log("spaces with " + ship.ware.name + ":" + spaces.length);
var loot = spaces.length === 0 ? 0 : ship.ware.earn / spaces.length;
for(var j = 0; j < spaces.length; j++) {
var space = spaces[j];
players[space.owner].money += loot;
}
}
}
//now assign the rest of spaces
var spaces = this.game_state.spaces;
for(var i = 13; i < spaces.length; i++) {
var space = spaces[i];
if(space.owner !== null) {
players[space.owner].money += space.earn;
}
}
},
compute_share_price : function() {
var ships = this.game_state.punts;
var players = this.game_state.players;
for(var i = 0; i < ships.length; i++) {
var ship = ships[i];
if(ship.position > 13) {
this.game_state.share_prices[ship.ware.name] += (this.game_state.share_prices[ship.ware.name] < 10 ? 5 : 10);
}
}
},
spaces_with_ware : function(ware, skip_empty) {
var spaces = this.game_state.spaces;
var ret = [];
for(var i = 0; i < spaces.length; i++) {
var space = spaces[i];
if(skip_empty && space.owner === null) {
continue;
}
if(space.ware === ware) {
ret.push(space);
}
}
return ret;
}
}
module.exports = game_engine;