-
Notifications
You must be signed in to change notification settings - Fork 17
/
Game.php
1560 lines (1328 loc) · 53.3 KB
/
Game.php
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace pr2\multi;
class Game extends Room
{
const LEVEL_COMPASS = 3236908; // for top hat
const LEVEL_BUTO = 1738847; // for jigg hat
const LEVEL_DELIVERANCE = 1896157; // for slender set
const LEVEL_SEA = 2255404; // for sea set
const LEVEL_DEEPER = 6493337; // for jellyfish hat
const LEVEL_HAUNTED = 1782114; // for epic jack-o'-lantern head
const LEVEL_CHEESE = 6207945; // for cheese hat
const LEVEL_BLOBFISH = 5985129; // for blobfish head
const LEVEL_TURKEY = 2585329; // for epic turkey set
const MODE_RACE = 'race';
const MODE_DEATHMATCH = 'deathmatch';
const MODE_EGG = 'egg';
const MODE_OBJECTIVE = 'objective';
const MODE_HAT = 'hat';
const PLAYER_SIR = 5321458; // sir sirlington
const PLAYER_CLINT = 5451130; // clint the cowboy
private $finish_array = array();
private $course_id;
private $from_room;
private $start_time;
private $begun = false;
private $loose_hat_array = array();
private $next_hat_id = 0;
private $prize;
private $prize_cancelled = false;
private $prizer_prize = false;
private $campaign;
private $mode = self::MODE_RACE;
private $hatCountdownEnd = -1;
private $hasHats = -1;
private $ending_egg = false;
private $finish_count = 0;
private $finish_positions = array();
private $cowboy_chance = '';
private $cowboy_mode = false;
private $tournament = false;
private $valid_hats = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
protected $room_name = 'game_room';
protected $temp_id = 0;
public function __construct($course_id, $from_room)
{
$this->course_id = $course_id;
$this->from_room = $from_room;
$this->tournament = PR2SocketServer::$tournament;
$this->start_time = microtime(true);
}
public function addPlayer($player)
{
if (count($this->finish_array) < 4) {
Room::addPlayer($player);
$player->socket->write('tournamentMode`' . (int) PR2SocketServer::$tournament);
$player->socket->write('startGame`'.$this->course_id);
$player->temp_id = $this->temp_id;
$player->pos_x = 0;
$player->pos_y = 0;
$player->average_vel_x = 0;
$player->average_vel_y = 0;
$player->lives = 3;
$this->temp_id++;
$race_stats = new RaceStats($player);
array_push($this->finish_array, $race_stats);
$player->race_stats = $race_stats;
}
}
public function removePlayer($player)
{
Room::removePlayer($player);
$this->finishDrawing($player);
$player->race_stats->still_here = false;
if (!isset($player->race_stats->finish_time)) {
$this->setFinishTime($player, 'forfeit');
} else {
$this->broadcastFinishTimes();
}
$player->race_stats = null;
$player->temp_id = null;
unset($player->temp_id);
if (count($this->player_array) <= 0) {
$this->remove();
}
}
public function init()
{
$this->recordPlays();
$this->determinePrize();
//send character info
foreach ($this->player_array as $player) {
$player->race_stats->finished_race = false;
$player->socket->write($player->getLocalInfo());
$this->sendToRoom($player->getRemoteInfo(), $player->user_id);
}
//super booster
if (!$this->tournament) {
foreach ($this->player_array as $player) {
if ($player->super_booster) {
$player->super_booster = false;
$this->sendToRoom('superBooster`' . $player->temp_id, -1);
}
}
}
//happy hour
if (HappyHour::isActive()) {
$this->sendToAll('happyHour`');
}
//tournament
if ($this->tournament) {
announce_tournament($this);
}
}
private function recordPlays()
{
global $play_count_array;
$player_count = count($this->player_array);
if (isset($play_count_array[$this->course_id])) {
$play_count_array[$this->course_id] += $player_count;
} else {
$play_count_array[$this->course_id] = $player_count;
}
}
// check if a player is present
private function isPlayerHere($player_id)
{
$ret = false;
foreach ($this->player_array as $player) {
if ($player->user_id == $player_id) {
$ret = true;
}
}
return $ret;
}
private function determinePrize()
{
$player_count = count($this->player_array);
global $campaign_array;
if (isset($campaign_array[$this->course_id])) {
$this->campaign = $campaign_array[$this->course_id];
}
// campaign prizes
if (isset($this->campaign)) {
$campaign_prize = Prizes::find($this->campaign->prize_type, $this->campaign->prize_id);
if ($player_count >= 4 || (isset($campaign_prize) && $campaign_prize->isUniversal())) {
$this->prize = $campaign_prize;
}
}
// The Golden Compass by -Shadowfax-; Awards: Top Hat
if ($this->course_id == self::LEVEL_COMPASS) {
$this->prize = Prizes::$TOP_HAT;
}
// -Deliverance- by changelings; Awards: Slender Set
if ($this->course_id == self::LEVEL_DELIVERANCE) {
$slender_prizes = array(Prizes::$SLENDER_HEAD, Prizes::$SLENDER_BODY, Prizes::$SLENDER_FEET);
$this->prize = $slender_prizes[array_rand($slender_prizes)];
}
// ~Under the sea~ by Rammjet; Awards: Sea Set
if ($this->course_id == self::LEVEL_SEA) {
$sea_prizes = array(Prizes::$SEA_HEAD, Prizes::$SEA_BODY, Prizes::$SEA_FEET);
$this->prize = $sea_prizes[array_rand($sea_prizes)];
}
// Deeper by Sothal; Awards: Jellyfish Hat
if ($this->course_id == self::LEVEL_DEEPER) {
$this->prize = Prizes::$JELLYFISH_HAT;
}
// Haunted House 2 by DareDevil1510; Awards: Epic Jack-o'-Lantern Head
if ($this->course_id == self::LEVEL_HAUNTED) {
$this->prize = Prizes::$EPIC_JACKOLANTERN_HEAD;
}
// Underwater World by Odin0030; Awards: Blobfish Head
if ($this->course_id == self::LEVEL_BLOBFISH) {
$this->prize = Prizes::$BLOBFISH_HEAD;
}
// Happy Thanksgiving!!! by Rage Runner; Awards: Epic Turkey Set
if ($this->course_id == self::LEVEL_TURKEY) {
$turkey_prizes = [
Prizes::$EPIC_TURKEY_HEAD,
Prizes::$EPIC_TURKEY_BODY,
Prizes::$EPIC_TURKEY_FEET
];
$this->prize = $turkey_prizes[array_rand($turkey_prizes)];
}
// Sir Sirlington; Awards: Epic Sir Set + Epic Top Hat
if ($this->isPlayerHere(self::PLAYER_SIR)) {
$sir_prizes = [
Prizes::$EPIC_TOP_HAT,
Prizes::$EPIC_SIR_HEAD,
Prizes::$EPIC_SIR_BODY,
Prizes::$EPIC_SIR_FEET
];
$this->prize = $sir_prizes[array_rand($sir_prizes)];
}
// Clint the Cowboy; Awards: Epic Cowboy Hat
if ($this->isPlayerHere(self::PLAYER_CLINT)) {
$this->prize = Prizes::$EPIC_COWBOY_HAT;
}
// random part/upgrade prizes
if (!isset($this->prize) && $player_count >= 1) {
if (rand($player_count*2, 20) >= 19) {
$prize_array = array(
Prizes::$TACO_HEAD,
Prizes::$TACO_BODY,
Prizes::$TACO_FEET,
Prizes::$INVISIBLE_HEAD,
Prizes::$INVISIBLE_BODY,
Prizes::$INVISIBLE_FEET,
Prizes::$GINGERBREAD_HEAD,
Prizes::$GINGERBREAD_BODY,
Prizes::$GINGERBREAD_FEET,
Prizes::$STICK_HEAD,
Prizes::$STICK_BODY,
Prizes::$STICK_FEET,
Prizes::$SIR_HEAD,
Prizes::$SIR_BODY,
Prizes::$SIR_FEET,
Prizes::$BASKETBALL_HEAD,
Prizes::$ARMOR_HEAD,
Prizes::$EPIC_CLASSIC_HEAD,
Prizes::$EPIC_CLASSIC_BODY,
Prizes::$EPIC_CLASSIC_FEET,
Prizes::$EPIC_TIRED_HEAD,
Prizes::$EPIC_DRESS_BODY,
Prizes::$EPIC_SANDAL_FEET,
Prizes::$EPIC_FLOWER_HEAD,
Prizes::$EPIC_STRAP_BODY,
Prizes::$EPIC_HEEL_FEET
);
$this->prize = $prize_array[rand(0, count($prize_array)-1)];
}
}
// random hat prizes
if (!isset($this->prize) && $player_count >= 2) {
if (rand(0, 40) == 40) {
$this->prize = Prizes::$EXP_HAT;
}
if (rand(0, 45) == 45) {
$this->prize = Prizes::$SANTA_HAT;
}
if (rand(0, 50) == 50) {
$this->prize = Prizes::$PARTY_HAT;
}
if (rand(0, 40) == 40 && HappyHour::isActive()) {
$this->prize = Prizes::$JUMP_START_HAT;
}
}
// don't set prizes on servers with tournament mode enabled
if (PR2SocketServer::$no_prizes) {
$this->prize = null;
}
// tell the world
if (isset($this->prize)) {
$this->sendToAll('setPrize`'.$this->prize->toStr());
}
}
public function prizerSetPrize($user_id, $type, $id)
{
$this->prizer_prize = true;
if (PR2SocketServer::$prizer_id !== 0 && $user_id === PR2SocketServer::$prizer_id) {
$prize = Prizes::find($type, $id);
if (isset($prize)) {
$this->prize = $prize;
$this->prize_cancelled = false;
$this->sendToAll('setPrize`'.$this->prize->toStr());
}
}
}
public function cancelPrize($player)
{
if ($this->prize_cancelled === true || !isset($this->prize)) {
return; // no point in continuing...
}
$clint_cond = $player->user_id === self::PLAYER_CLINT && $this->prize == Prizes::$EPIC_COWBOY_HAT;
$sir_cond = $player->user_id === self::PLAYER_SIR && (
$this->prize == Prizes::$EPIC_TOP_HAT ||
$this->prize == Prizes::$EPIC_SIR_HEAD ||
$this->prize == Prizes::$EPIC_SIR_BODY ||
$this->prize == Prizes::$EPIC_SIR_FEET
);
$prizer_cond = PR2SocketServer::$prizer_id === $player->user_id && $this->prizer_prize;
if ($player->group === 3 || $sir_cond || $clint_cond || $prizer_cond) {
$this->prize = null;
$this->prize_cancelled = true;
$this->prizer_prize = false;
$this->sendToAll("cancelPrize`$player->name");
}
}
public function finishDrawing($player, $data = null)
{
if ($player->race_stats->drawing === true) {
$arr = explode('`', $data);
$player->race_stats->drawing = false;
if (isset($data)) {
$rs = $player->race_stats;
$rs->level_hash = $arr[0];
$rs->mode = $arr[1];
$rs->finish_positions = $arr[2];
$rs->finish_count = $arr[3];
$rs->cowboy_chance = $arr[4];
$rs->bad_hats = $arr[5];
}
$this->sendToAll('finishDrawing`'.$player->temp_id);
}
$this->maybeBeginRace();
}
private function maybeBeginRace()
{
$begin_race = true;
foreach ($this->player_array as $player) {
if ($player->race_stats->drawing === true) {
$begin_race = false;
break;
}
}
if ($begin_race && !$this->begun) {
$this->beginRace();
}
}
private function beginRace()
{
if (!$this->begun) {
$this->begun = true;
$this->mode = $this->democratize('mode');
$this->hash = $this->democratize('level_hash');
$this->finish_positions = $this->democratize('finish_positions');
$this->finish_count = $this->democratize('finish_count');
$this->cowboy_chance = $this->democratize('cowboy_chance');
$bad_hats = $this->democratize('bad_hats');
// turn finish positions into an array
if ($this->finish_positions != 'all') {
$this->finish_positions = json_decode($this->finish_positions);
}
// remove arti on hat attack and arti level
if ($this->mode === self::MODE_HAT || $this->course_id == Artifact::$level_id) {
array_splice($this->valid_hats, 12, 1);
}
// remove other invalid hats from valid hats array
$this->valid_hats = array_values(array_diff($this->valid_hats, explode(',', $bad_hats)));
// handle hat attack mode
if ($this->mode === self::MODE_HAT) {
// don't start a hat attack level if there's only one player in the game
if (count($this->player_array) <= 1) {
$hat_err = 'Error: You can\'t play a hat attack level by yourself. :(';
}
// sanity: are there any valid hats? (this should never trigger)
if (count($this->valid_hats) === 0) {
$hat_err = 'Error: No valid hats.';
}
// throw error if triggered
if (!empty($hat_err)) {
foreach ($this->player_array as $player) {
$this->quitRace($player);
$player->socket->write("forceQuit`");
$player->socket->write("message`$hat_err");
}
return;
}
}
// boot people with the wrong level hash
foreach ($this->player_array as $player) {
if ($this->hash !== $player->race_stats->level_hash) {
$this->quitRace($player);
}
}
// jigg hat
if ($this->course_id == self::LEVEL_BUTO) {
$hat = $this->makeHat($this->next_hat_id++, Hats::JIGG, 0xFFFFFF, -1);
$this->loose_hat_array[$hat->id] = $hat;
$x = 13450;
$y = -6200;
$rot = 0;
$this->sendToAll("addEffect`Hat`$x`$y`$rot`$hat->num`$hat->color`$hat->color2`$hat->id", -1);
}
// cheese hat
if ($this->course_id == self::LEVEL_CHEESE) {
$hat = $this->makeHat($this->next_hat_id++, Hats::CHEESE, 0xFFD860, 0x000000);
$this->loose_hat_array[$hat->id] = $hat;
$x = 13878;
$y = 7214;
$rot = 0;
$this->sendToAll("addEffect`Hat`$x`$y`$rot`$hat->num`$hat->color`$hat->color2`$hat->id", -1);
}
// place artifact hat
if ($this->course_id == Artifact::$level_id) {
$hat = $this->makeHat($this->next_hat_id++, Hats::ARTIFACT, 0xFFFFFF, -1);
$this->loose_hat_array[$hat->id] = $hat;
$x = Artifact::$x;
$y = Artifact::$y;
$rot = Artifact::$rot;
$this->sendToAll("addEffect`Hat`$x`$y`$rot`$hat->num`$hat->color`$hat->color2`$hat->id", -1);
}
// eggs
if ($this->mode == self::MODE_EGG) {
$this->sendToAll('setEggSeed`'.rand(0, 99999));
$this->sendToAll('addEggs`10');
}
// sfchm
if ($this->cowboy_chance === '') {
$this->cowboy_chance = 5;
}
if ($this->tournament && $this->cowboy_chance != 100) {
$this->cowboy_chance = 0;
}
if (!isset($this->campaign) && rand(1, 100) <= $this->cowboy_chance) {
$this->cowboy_mode = true;
$this->sendToAll('cowboyMode`');
}
// hats
$this->initHats();
// start
$this->start_time = microtime(true);
$this->sendToAll('ping`' . time());
$this->sendToAll('beginRace`');
// if anyone forfeited, show it properly
$this->broadcastFinishTimes();
}
}
private function initHats()
{
foreach ($this->player_array as $player) {
$player->worn_hat_array = array();
$hat_id = $player->hat;
$hat_color = $player->hat_color;
$hat_color2 = $player->getSecondColor('hat', $hat_id);
// change hat to tournament mode hat when enabled
if ($this->tournament) {
$hat_id = PR2SocketServer::$tournament_hat;
if ($this->mode === self::MODE_HAT && $hat_id == 1) {
$hat_id = 2; // change nothing to exp hat when tournament mode is enabled on hat attack
}
}
// remove artifact hat a player is wearing on artifact level
if ($this->course_id == Artifact::$level_id && $hat_id == 14) {
$hat_id = 1;
}
// remove the hat if player is wearing one that's invalid (this should never trigger)
if ($hat_id > 1 && !in_array($hat_id, $this->valid_hats)) {
$hat_id = 1;
}
// cowboy mode
if ($this->cowboy_mode) {
$hat_id = 5;
}
// change the hat to something random during hat attack if they aren't wearing a valid hat
if ($this->mode === self::MODE_HAT && !in_array($hat_id, $this->valid_hats)) {
$hat_id = $this->valid_hats[rand(0, count($this->valid_hats) - 1)];
$msg = 'Howdy! Here\'s a random hat to use just for this level. Thank me later!!';
$player->socket->write("chat`Fred the G. Cactus`3`$msg");
}
// init the hat and add to this player's worn hat array
if ($hat_id != 1) {
$hat = $this->makeHat($this->next_hat_id++, $hat_id, $hat_color, $hat_color2);
$player->worn_hat_array[0] = $hat;
}
// send this player's hat array to all players
$this->sendToAll($this->getHatStr($player));
}
}
private function makeHat($id, $num, $color, $color2)
{
$hat = new \stdClass();
$hat->id = $id;
$hat->num = $num;
$hat->color = $color;
$hat->color2 = $color2;
return $hat;
}
private function democratize($var)
{
$winner = '';
$candidates = array();
foreach ($this->player_array as $player) {
$candidate = $player->race_stats->{$var};
if ($candidate != '') {
if (!isset($candidates[$candidate])) {
$candidates[$candidate] = 0;
}
$candidates[ $candidate ]++;
}
}
arsort($candidates);
reset($candidates);
$winner = key($candidates);
return $winner;
}
public function remoteFinishRace($player, $data)
{
if ($this->isStillPlaying($player->temp_id)) {
if ($this->mode == self::MODE_RACE) {
list($finish_id, $x, $y) = explode('`', $data);
$this->verifyFinishPosition($x, $y, $finish_id);
} elseif ($this->mode == self::MODE_HAT) {
$msg = 'Psst... finish blocks don\'t do anything in hat attack mode!';
$player->socket->write("chat`Fred the G. Cactus`3`$msg");
return;
}
$this->finishRace($player);
}
}
private function timeFormat($time, $digits = 2)
{
if ($time < 0) {
$time = 0;
}
return round($time, $digits);
}
public function finishRace($player)
{
if ($player->race_stats->finished_race === false
&& !isset($player->race_stats->finish_time)
&& $player->race_stats->drawing === false
&& $this->begun === true
) {
// get/format/validate/set finish time
$finish_microtime = microtime(true);
$full_time = $finish_microtime - $this->start_time;
$finish_time = $this->timeFormat($full_time);
$broadcast_time = $this->timeFormat($full_time, 3);
$finish_time = $finish_time > 31536000 ? 0 : $finish_time; // if the race time > 1 year, set it to 0
$this->setFinishTime($player, $finish_time);
// exp time modifier (propotional before 2 mins)
$time_mod = $finish_time / 120;
$time_mod = $time_mod > 1 ? 1 : $time_mod;
// checks for a "true" finish for awarding prizes
$true_fin = strpos($player->race_stats->finish_time, 'forfeit') !== 0;
// check if all objectives reached
$completed_perc = 0;
if ($this->mode == self::MODE_OBJECTIVE && $this->finish_count > 0) {
$objective_count = count($player->race_stats->objectives_reached);
$objective_count = $objective_count > $this->finish_count ? $this->finish_count : $objective_count;
$completed_perc = $objective_count / $this->finish_count;
$true_fin = $completed_perc == 1; // falsify true finish if all objs not reached
}
// get finish placement
$place = array_search($player->race_stats, $this->finish_array);
// announce on tournament server
if ($true_fin && $place == 0 && count($this->finish_array) > 1 && $finish_time > 10 && $this->tournament) {
$this->broadcastResults($player, $broadcast_time);
}
// init winning prize
$prize = null;
// set for winner or universal
if (isset($this->prize) && ($place == 0 || $this->prize->isUniversal())) {
$prize = $this->prize;
}
// set for buto
$isButo = $this->course_id == self::LEVEL_BUTO;
if ($isButo && $player->wearingHat(Hats::JIGG) && !$player->hasPart('hat', Hats::JIGG)) {
$prize = Prizes::$JIGG_HAT;
}
// set for cheese
$isCheese = $this->course_id == self::LEVEL_CHEESE;
if ($isCheese && $player->wearingHat(Hats::CHEESE) && !$player->hasPart('hat', Hats::CHEESE)) {
$prize = Prizes::$CHEESE_HAT;
}
// award prize to player
if (isset($prize) && $true_fin) {
$autoset = $prize->getType() == 'hat';
$result = $player->gainPart($prize->getType(), $prize->getId(), $autoset);
if ($result == true) {
// prize popup
$player->write('winPrize`' . $prize->toStr());
// determine if a special account
$spec_acc = null;
$spec_ids = [PR2SocketServer::$prizer_id, self::PLAYER_CLINT, self::PLAYER_SIR];
$player_names = '';
for ($i = 0; $i < count($this->finish_array); $i++) {
$p = $this->finish_array[$i];
$player_names .= ($i > 0 ? ' / ' : '') . "$p->name";
if (in_array($p->user_id, $spec_ids)) {
// assign the spec_acc var if we've hit one of the following conditions
$prizer_cond = $p->user_id === PR2SocketServer::$prizer_id && $this->prizer_prize === true;
$clint_cond = $p->user_id == self::PLAYER_CLINT && $this->prize == Prizes::$EPIC_COWBOY_HAT;
$sir_cond = $p->user_id === self::PLAYER_SIR && (
$this->prize == Prizes::$EPIC_TOP_HAT ||
$this->prize == Prizes::$EPIC_SIR_HEAD ||
$this->prize == Prizes::$EPIC_SIR_BODY ||
$this->prize == Prizes::$EPIC_SIR_FEET
);
if ($prizer_cond || $clint_cond || $sir_cond) {
$spec_acc = $p;
}
}
}
// record the prize in the prize log
if (!empty($spec_acc)) {
$msg = "$spec_acc->name awarded a prize to $player->name from $spec_acc->ip "
."{user_id: $spec_acc->user_id, "
."winner_id: $player->user_id, "
."level_id: $this->course_id, "
.'part_type: ' . $prize->getType() . ', '
.'part_id: ' . $prize->getId() . ', '
.'prize_name: ' . $prize->getFullName() . ', '
.'is_prizer: ' . ($prizer_cond ? 'yes' : 'no') . ', '
."who_was_there: $player_names}";
db_op('prize_action_insert', [$spec_acc->user_id, $msg, 'award', $prizer_cond, $spec_acc->ip]);
}
}
}
// exp gain
$tot_exp_gain = 0;
// welcome back bonus
$welcome_back_bonus = 0;
if ($player->exp_today == 0 && $player->rank >= 5) {
$welcome_back_bonus = 1000;
} // level bonus
else {
$level_bonus = $this->applyExpCurve($player, 25 * $time_mod);
// sanity check, think it works fine here
$level_bonus = $level_bonus >= 5 && $finish_time <= 3 ? 0 : $level_bonus;
// update level bonus proportionally according to objs reached in obj mode
if ($this->mode == self::MODE_OBJECTIVE && $this->finish_count > 0) {
$level_bonus *= $completed_perc;
}
// round level bonus for int exp
$level_bonus = round($level_bonus);
// make level bonus 0 on tournament
$level_bonus = PR2SocketServer::$no_prizes ? 0 : $level_bonus;
// write award back to player and add to total exp gain
if ($this->mode == self::MODE_DEATHMATCH) {
$player->write('award`Survival Bonus`+ '.$level_bonus);
} elseif ($this->mode == self::MODE_OBJECTIVE && $completed_perc < 1) {
$player->write('award`Level Attempted`+ '.$level_bonus);
} else {
$player->write('award`Level Completed`+ '.$level_bonus);
}
$tot_exp_gain += $level_bonus;
}
// opponent bonus
for ($i = $place + 1; $i < count($this->finish_array); $i++) {
$race_stats = $this->finish_array[$i];
if ($race_stats->rank < 100 && PR2SocketServer::$no_prizes === false) {
$exp_gain = ($race_stats->rank+5) * $time_mod;
$exp_gain = ceil($this->applyExpCurve($player, $exp_gain));
} else {
$exp_gain = 0;
}
$tot_exp_gain += $exp_gain;
$player->write('award`Defeated '.$race_stats->name.'`+ '.$exp_gain);
}
// handle hats
$hat_bonus = 0;
$wearing_kong = false;
foreach ($player->worn_hat_array as $hat) {
if ($hat->num == 2) {
$hat_bonus += 1;
} elseif ($hat->num == 3) {
$wearing_kong = true;
}
}
// remove hats after finishing in hat attack mode
if ($this->mode === self::MODE_HAT) {
$this->loseAllHats($player);
}
// handle gp gain
if ($place == 0 && count($this->finish_array) > 1 && $finish_time > 10) {
$this->giveGp($player, $wearing_kong);
}
// apply exp bonuses to total exp multiplier and multiply total exp gain by anything less than 12
$exp_multiplier = 1;
$exp_multiplier += $hat_bonus > 4 ? 4 : $hat_bonus;
$exp_multiplier *= HappyHour::isActive() ? 2 : 1;
$exp_multiplier += isset($this->campaign) && $this->from_room === 'campaign' ? 2 : 0;
$tot_exp_gain = round($tot_exp_gain * ($exp_multiplier > 12 ? 12 : $exp_multiplier));
// exp award notification
if ($exp_multiplier > 1) {
$bonuses = new \stdClass();
$bonuses->hat = $hat_bonus > 0;
$bonuses->campaign = isset($this->campaign) && $this->from_room === 'campaign';
$bonuses->hh = HappyHour::isActive();
// determine number of awards
$awards_arr = array();
$num = 0;
foreach ($bonuses as $key => $var) {
// not awarding?
if ($var !== true) {
continue;
}
// format, push to array, increment loop count
$key = $key === 'hh' ? ($bonuses->campaign ? strtoupper($key) : 'Happy Hour') : ucfirst($key);
array_push($awards_arr, $key);
$num++;
}
// build string
$awards_str = $num > 2 || $bonuses->hh ? join('/', $awards_arr) : join(' & ', $awards_arr);
$awards_str .= $num > 1 ? ' Bonuses' : ' Bonus';
// return to player
$player->write("award`$awards_str`exp X $exp_multiplier");
}
// apply welcome back bonus after all multipliers
if ($welcome_back_bonus > 0) {
$tot_exp_gain += $welcome_back_bonus;
$player->write('award`Welcome Back Bonus`+ 1,000');
}
// apply artifact bonus after all multipliers
if ($this->course_id == Artifact::$level_id && $player->wearingHat(Hats::ARTIFACT) && $true_fin) {
$result = save_finder($player);
if ($result) {
$max_artifact_bonus = 50000;
$artifact_bonus = round($max_artifact_bonus * ($player->rank + $player->rt_available) / 60);
if ($artifact_bonus > $max_artifact_bonus) {
$artifact_bonus = $max_artifact_bonus;
}
$user_id = (int) $player->user_id;
if (Artifact::$first_finder === $user_id && Artifact::$bubbles_winner !== $user_id) {
$artifact_bonus += 10000;
}
if ($artifact_bonus > 0) {
$tot_exp_gain += $artifact_bonus;
$player->write('award`Artifact Found!`+ ' . number_format($artifact_bonus));
}
}
}
// reset unrealistic EXP gain
$tot_exp_gain = $tot_exp_gain > 100000 ? 0 : $tot_exp_gain;
// disconnect anyone trying to earn exp too quick
if ($player->last_exp_time >= (time() - 2)) {
$player->socket->write("message`Botting is a no-no. :(");
$player->remove();
}
// log/increment exp and maybe save
$player->last_exp_time = time();
$player->incExp($tot_exp_gain);
$player->maybeSave();
} else {
$this->setFinishTime($player, 'forfeit');
}
// they finished
$player->race_stats->finished_race = true;
// everyone finishes at the same time in egg mode
$this->maybeEndEgg();
}
private function broadcastResults($player, $finish_time)
{
global $chat_room_array;
$finish_int = (int) floor($finish_time);
$minutes = (int) floor($finish_int / 60);
$seconds = str_pad(floor($finish_time - ($minutes * 60)), 2, 0, STR_PAD_LEFT);
$milliseconds = round(($finish_time - ($minutes * 60) - $seconds), 3);
$milliseconds = str_pad(substr($milliseconds, 2), 3, 0);
$str = $minutes . ':' . $seconds . '.' . $milliseconds;
if (isset($chat_room_array['main'])) {
$main = $chat_room_array['main'];
$message = '';
$names = array();
foreach ($this->finish_array as $rs) {
$names[] = userify($this->idToPlayer($rs->temp_id), $rs->name, $rs->group, $rs->mod_power);
}
$vs_names = join(' vs ', $names);
$html_name = userify($player, $player->name, $player->group, mod_power($player));
$message = "$vs_names: // $html_name wins with a time of $str!";
$main->sendChat("systemChat`$message", -1);
}
}
private function maybeEndDeathmatch()
{
if ($this->mode == self::MODE_DEATHMATCH) {
$unfinished = 0;
foreach ($this->finish_array as $race_stats) {
if (!isset($race_stats->finish_time)) {
$unfinished++;
}
}
if ($unfinished === 1) {
foreach ($this->player_array as $player) {
if (!$player->race_stats->finished_race && !isset($player->auto_win_deathmatch)) {
$last_player = $player;
}
}
if (isset($last_player)) {
$last_player->auto_win_deathmatch = true;
$this->start_time -= 1;
$this->finishRace($last_player);
unset($last_player->auto_win_deathmatch);
}
}
}
}
private function maybeEndEgg()
{
if ($this->mode === self::MODE_EGG && !$this->ending_egg) {
$someone_finished = false;
$everyone_quit = true;
foreach ($this->player_array as $player) {
if ($player->race_stats->finished_race) {
$someone_finished = true;
break;
}
}
foreach ($this->player_array as $player) {
if (!$player->race_stats->quit_race) {
$everyone_quit = false;
break;
}
}
if ($someone_finished || $everyone_quit) {
$this->ending_egg = true;
foreach ($this->player_array as $player) {
if (!$player->race_stats->finished_race) {
$this->finishRace($player);
}
}
}
}
}
public function quitRace($player)
{
$this->finishDrawing($player);
if ($player->race_stats->finished_race == false) {
$player->race_stats->quit_race = true;
if ($this->mode === self::MODE_HAT) {
$this->loseAllHats($player);
}
if ($this->mode == self::MODE_DEATHMATCH && $this->begun) {
$this->finishRace($player);
} elseif ($this->mode == self::MODE_OBJECTIVE && $this->begun) {
$this->finishRace($player);
} elseif ($this->mode === self::MODE_EGG) {
$this->maybeEndEgg();
} elseif ($this->mode === self::MODE_RACE || $this->mode === self::MODE_HAT) {
$player->race_stats->finished_race = true;
$this->setFinishTime($player, 'forfeit');
}
}
}
protected function sortFinishArrayRace($a, $b)
{
$a_time = $a->finish_time;
$b_time = $b->finish_time;
if (!isset($a_time)) {
$a_time = 9999998;
}
if (!isset($b_time)) {
$b_time = 9999998;
}
if ($a_time == 'forfeit') {
$a_time = 9999999;
}
if ($b_time == 'forfeit') {
$b_time = 9999999;
}
if ($a_time == $b_time) {
return 0;
} elseif ($a_time < $b_time) {
return -1;
} else {
return 1;
}
}
protected function sortFinishArrayDeathmatch($a, $b)
{
$a_time = $a->finish_time;
$b_time = $b->finish_time;