-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnothanks.pl
1518 lines (1338 loc) · 45.2 KB
/
nothanks.pl
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
#!/usr/bin/perl
##
# File : nothanks.pl
# Date : 19/Jun/2022
# Author : spjspj
# Purpose : Implement No Thanks!
# Purpose : Requires having an Apache service setup (see conf file)
##
use strict;
use LWP::Simple;
use Socket;
use File::Copy;
use List::Util qw(shuffle);
use Time::HiRes;
use URI::Escape;
use POSIX qw(strftime);
$| = 1;
my %revealed_cards_imgs;
my $BCK = "back";
my $NUM_COUNTERS_AT_START_OF_GAME = 7;
my $NUM_CARDS_IN_FULL_DECK = 25;
my $NUM_CARDS_TO_REMOVE = 8;
my $GAME_WON = 0;
my $reason_for_game_end = "";
my $CURRENT_LOGIN_NAME = "";
my $SHOW_CARDS_MODE = 0;
my $IN_DEBUG_MODE = 0;
my $BAD_GUYS = -1;
my $GOOD_GUYS = 1;
my $PATH = "d:\\perl_programs\\nothanks";
my $nothanks = "<img id=\"xxx\" width=\"80\" height=\"131\" src=\"big_c.png\"><\/img>";
my %rand_colors;
my $DEBUG = "";
my @player_names;
my @NEEDS_REFRESH;
my @NEEDS_ALERT;
my @deck;
my $cards_left_in_deck;
my $current_flipped_card;
my $current_number_of_counters;
my %player_counters;
my %player_cards;
my $taken_cards = "";
my %already_shuffled;
my $needs_shuffle_but_before_next_card_picked = 0;
my %BANNED_NAMES;
my $RINGINGROOM_URL_LINK;
my %NOT_HIDDEN_INFO;
my %PLAYER_IS_BOT;
my $whos_turn;
my $pot_whos_turn;
my $num_players_in_game = -1;
my $NUM_EXPOSED_CARDS = 0;
my $num_cards_per_player = $NUM_COUNTERS_AT_START_OF_GAME;
my @player_ips;
my $num_players_in_lobby = 0;
sub add_to_debug
{
$DEBUG .= "$_[0]<br>\n";
print "$_[0]\n";
}
sub game_won
{
my $win_con = $_ [0];
if ($GAME_WON == 0)
{
force_needs_refresh();
$GAME_WON = $_ [0];
$reason_for_game_end = $_ [1];
add_to_debug ("GAME WON: $reason_for_game_end");
add_to_debug (join ("<br>", @deck));
}
}
sub get_game_won
{
if ($GAME_WON == 0)
{
return "";
}
my $t = "Won..";
return $t;
}
sub do_shuffle
{
@deck = shuffle (@deck);
}
my $DO_DEBUG = 0;
sub get_debug
{
if ($DO_DEBUG)
{
return ("xxx $DEBUG yyy");
}
return "";
}
sub write_to_socket
{
my $sock_ref = $_ [0];
my $msg_body = $_ [1];
my $form = $_ [2];
my $redirect = $_ [3];
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime (time);
my $yyyymmddhhmmss = sprintf "%.4d%.2d%.2d-%.2d%.2d%.2d", $year+1900, $mon+1, $mday, $hour, $min, $sec;
$msg_body = '<html><head><META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"><br></head><body>' . $form . $msg_body . get_debug() . "</body></html>";
$msg_body =~ s/\n\n/\n/mig;
$msg_body =~ s/\n\n/\n/mig;
$msg_body =~ s/\n\n/\n/mig;
$msg_body .= chr (13) . chr (10);
#$msg_body =~ s/<img.*?src="(.*?)".*?>(.*?)<\/img>/$1 - $2/img;
$msg_body =~ s/href="/href="\/nothanks\//img;
$msg_body =~ s/\/\//\//img;
$msg_body =~ s/nothanks.nothanks/nothanks/img;
$msg_body =~ s/nothanks.nothanks/nothanks/img;
$msg_body =~ s/nothanks.nothanks/nothanks/img;
$msg_body =~ s/nothanks.nothanks/nothanks/img;
my $header;
if ($redirect =~ m/^redirect/i)
{
$header = "HTTP/1.1 302 Moved\nLocation: \/nothanks\/\nLast-Modified: $yyyymmddhhmmss\nConnection: close\nContent-Type: text/html; charset=UTF-8\nContent-Length: " . length ($msg_body) . "\n\n";
}
elsif ($redirect =~ m/^noredirect/i)
{
if ($CURRENT_LOGIN_NAME ne "")
{
$header = "HTTP/1.1 200 OK\nLast-Modified: $yyyymmddhhmmss\nConnection: close\nContent-Type: text/html\nSet-Cookie: newloginname=$CURRENT_LOGIN_NAME\nContent-Length: " . length ($msg_body) . "\n\n";
}
else
{
$header = "HTTP/1.1 200 OK\nLast-Modified: $yyyymmddhhmmss\nConnection: close\nContent-Type: text/html\nContent-Length: " . length ($msg_body) . "\n\n";
}
}
$msg_body = $header . $msg_body;
syswrite ($sock_ref, $msg_body);
}
sub old_bin_write_to_socket
{
my $sock_ref = $_ [0];
my $img = $_ [1];
my $buffer;
my $size = 0;
if (-f $img)
{
$size = -s $img;
}
my $msg_body = "HTTP/2.0 200 OK\nDate: Mon, 20 May 2019 13:20:41 GMT\nConnection: close\nContent-type: image/jpeg\nContent-length: $size\n\n";
syswrite ($sock_ref, $msg_body);
open IMAGE, $img;
binmode IMAGE;
my $buffer;
while (read (IMAGE, $buffer, 16384))
{
syswrite ($sock_ref, $buffer);
}
}
my %ips_slowed_down_for;
my %images;
sub newer_bin_write_to_socket
{
my $sock_ref = $_ [0];
my $img = $_ [1];
my $ip = $_ [4];
my $buffer;
my $size = 0;
if (-f $img)
{
$size = -s $img;
}
my $img_type = $img;
$img_type =~ s/.*\.//;
my $msg_body = "HTTP/2 304 Not Modified\nContent-type: image/$img_type\nConnection: close\nContent-length: $size\nExpires: Sat, 16 Aug 2025 06:46:59 GMT\nLast-modified: Fri, 14 Aug 2020 06:48:59 GMT\nCache-control: public, max-age=31536000\n\n";
my $buffer;
my $len;
if (!defined ($images {$img}))
{
$msg_body = "HTTP/2 200 OK\nContent-type: image/$img_type\nConnection: close\nContent-length: $size\nExpires: Sat, 16 Aug 2025 06:46:59 GMT\nLast-modified: Fri, 14 Aug 2020 06:48:59 GMT\nCache-control: public, max-age=31536000\n\n";
open IMAGE, $img;
binmode IMAGE;
while ($len = read (IMAGE, $buffer, 10000))
{
$images {$img} .= $buffer;
}
$images {$img} .= $buffer;
close IMAGE;
}
$ips_slowed_down_for {$ip} ++;
if ($ips_slowed_down_for {$ip} < 10)
{
$msg_body = "HTTP/2 200 OK\nContent-type: image/$img_type\nConnection: close\nContent-length: $size\nExpires: Sat, 16 Aug 2025 06:46:59 GMT\nLast-modified: Fri, 14 Aug 2020 06:48:59 GMT\nCache-control: public, max-age=31536000\n\n";
}
else
{
#$msg_body = "HTTP/2 304 Not Modified\nContent-type: image/$img_type\nConnection: close\nContent-length: $size\nExpires: Sat, 16 Aug 2025 06:46:59 GMT\nLast-modified: Fri, 14 Aug 2020 06:48:59 GMT\nCache-control: public, max-age=31536000\n\n";
$msg_body = "HTTP/2 200 OK\nContent-type: image/$img_type\nConnection: close\nContent-length: $size\nExpires: Sat, 16 Aug 2025 06:46:59 GMT\nLast-modified: Fri, 14 Aug 2020 06:48:59 GMT\nCache-control: public, max-age=31536000\n\n";
}
syswrite ($sock_ref, $msg_body);
if (defined ($images {$img}))
{
syswrite ($sock_ref, $images {$img});
}
}
sub experimental_bin_write_to_socket
{
my $sock_ref = $_ [0];
my $img = $_ [1];
my $ip = $_ [4];
my $buffer;
my $size = 0;
if (-f $img)
{
$size = -s $img;
}
my $img_type = $img;
$img_type =~ s/.*\.//;
my $msg_body = "HTTP/2 200 OK\nContent-type: image/$img_type\nContent-length: $size\nPragma: no-cache\n\n";
my $buffer;
my $len;
if (!defined ($images {$img}))
{
#$msg_body = "HTTP/2 200 OK\nContent-type: image/$img_type\nPragma: no-cache\n\n";
open IMAGE, $img;
binmode IMAGE;
while ($len = read (IMAGE, $buffer, 10000))
{
$images {$img} .= $buffer;
}
$images {$img} .= $buffer;
close IMAGE;
}
$ips_slowed_down_for {$ip} ++;
if ($ips_slowed_down_for {$ip} < 10)
{
#$msg_body = "HTTP/2 200 OK\nContent-type: image/$img_type\nPragma: no-cache\n\n";
}
else
{
#$msg_body = "HTTP/2 200 OK\nContent-type: image/$img_type\nPragma: no-cache\n\n";
}
syswrite ($sock_ref, $msg_body);
if (defined ($images {$img}))
{
syswrite ($sock_ref, $images {$img});
#syswrite ($sock_ref, $images {$img});
}
}
sub bin_write_to_socket
{
my $sock_ref = $_ [0];
my $img = $_ [1];
my $buffer;
my $size = 0;
if (-f $img)
{
$size = -s $img;
}
my $img_type = $img;
$img_type =~ s/.*\.//;
my $msg_body = "HTTP/2 304 Not Modified\ncontent-type: image/$img_type\nConnection: close\ncontent-length: $size\nexpires: Sat, 16 Aug 2025 06:46:59 GMT\nlast-modified: Fri, 14 Aug 2020 06:48:59 GMT\ncache-control: public, max-age=31536000\n\n";
my $buffer;
my $len;
if (!defined ($images {$img}))
{
$msg_body = "HTTP/2 200 OK\ncontent-type: image/$img_type\nConnection: close\ncontent-length: $size\nexpires: Sat, 16 Aug 2025 06:46:59 GMT\nlast-modified: Fri, 14 Aug 2020 06:48:59 GMT\ncache-control: public, max-age=31536000\n\n";
open IMAGE, $img;
binmode IMAGE;
while ($len = read (IMAGE, $buffer, 10000))
{
$images {$img} .= $buffer;
}
$images {$img} .= $buffer;
close IMAGE;
}
$msg_body = "HTTP/2 200 OK\ncontent-type: image/$img_type\nConnection: close\ncontent-length: $size\nexpires: Sat, 16 Aug 2025 06:46:59 GMT\nlast-modified: Fri, 14 Aug 2020 06:48:59 GMT\ncache-control: public, max-age=31536000\n\n";
syswrite ($sock_ref, $msg_body);
if (defined ($images {$img}))
{
syswrite ($sock_ref, $images {$img});
}
}
sub read_from_socket
{
my $sock_ref = $_ [0];
my $ch = "";
my $prev_ch = "";
my $header = "";
my $rin = "";
my $rout;
my $min;
my $max;
my $msg_type;
my $msg_body;
my $msg_len;
vec ($rin, fileno ($sock_ref), 1) = 1;
# Read the message header
while ((!(ord ($ch) == 13 and ord ($prev_ch) == 10)))
{
if (select ($rout=$rin, undef, undef, 200) == 1)
{
$prev_ch = $ch;
# There is at least one byte ready to be read..
if (sysread ($sock_ref, $ch, 1) < 1)
{
return "resend";
}
$header .= $ch;
my $h = $header;
$h =~ s/(.)/",$1-" . ord ($1) . ";"/emg;
}
}
return $header;
}
sub get_player_IP
{
my $id = $_ [0];
if ($id < $num_players_in_game)
{
return $player_ips [$id];
}
return -1;
}
sub get_player_id
{
my $IP = $_ [0];
my $ip_find;
my $i = 0;
my $x = get_player_id_from_name ($CURRENT_LOGIN_NAME, "aaa");
if ($x != -1)
{
return $x;
}
while ($i < $num_players_in_lobby)
{
if ($player_ips [$i] eq $IP)
{
return $i;
}
$i ++;
}
return -1;
}
sub flip_top_card
{
$current_flipped_card = "GAME_OVER";
if ($cards_left_in_deck > 0)
{
$current_flipped_card = $deck [0];
$current_number_of_counters = 0;
}
else
{
return 0;
}
my $i = 1;
my $index = 0;
my @new_deck;
while ($i < $cards_left_in_deck)
{
$new_deck [$index] = $deck [$i];
$i++;
$index++;
}
@deck = @new_deck;
return 1;
}
sub setup_deck
{
# Cards in deck currently..
my $num_cards_to_ban = int (rand ($NUM_CARDS_TO_REMOVE + 3));
my $total_number_cards_in_deck = $NUM_CARDS_IN_FULL_DECK - $num_cards_to_ban;
my %banned_cards;
my $i;
for ($i = 0; $i < $num_cards_to_ban; $i++)
{
my $banned_card = int (rand ($NUM_CARDS_IN_FULL_DECK - 3)) + 3;
if (!defined ($banned_cards {$banned_card}))
{
$banned_cards {$banned_card} = 1;
}
else
{
$i --;
}
}
my $index = 0;
my $str = "";
my @new_deck;
for ($i = 3; $i <= $NUM_CARDS_IN_FULL_DECK; $i++)
{
if (!defined ($banned_cards {$i}))
{
$new_deck [$index] = $i;
$index++;
$str .= "$i,";
}
}
@deck = @new_deck;
$cards_left_in_deck = $total_number_cards_in_deck;
do_shuffle ();
flip_top_card ();
add_to_debug ("Setup deck with: $str");
add_to_debug ("Ignoring these cards: " . join (",", sort (keys %banned_cards)));
add_to_debug (join (",", @deck));
}
sub get_player_name
{
my $ID = $_ [0];
return $player_names [$ID];
}
sub set_whos_turn
{
$whos_turn = $_ [0];
}
sub get_player_id_from_name
{
my $this_name = $_ [0];
my $degg = $_ [1];
my $id = 0;
for ($id = 0; $id < scalar @player_names; $id++)
{
if ($this_name eq $player_names [$id])
{
return $id;
}
}
return -1;
}
sub is_bot
{
my $name = $_ [0];
my $id = $_ [1];
my $from = $_ [2];
if ($name eq "")
{
$name = $player_names [$id];
}
if (defined ($PLAYER_IS_BOT {$name}) && ($PLAYER_IS_BOT {$name} == 1))
{
return 1;
}
return 0;
}
sub noone_else_wants
{
my $check_card = $_ [0];
my $ignore_id = $_ [1];
my $one_up = "(^|,)" . ($check_card + 1) . "(\$|,)";
my $one_down = "(^|,)" . ($check_card - 1) . "(\$|,)";
my $i;
for ($i = 0; $i < $num_players_in_game; $i++)
{
if ($i != $ignore_id)
{
my $cards = $player_cards {$i};
# One up, one down
print ("Farming - checking $cards vs $one_up and $one_down\n");
if ($cards =~ m/$one_up/ || $cards =~ m/$one_down/)
{
return 0;
}
}
}
return 1;
}
sub others_on_zero
{
my $this_bot = $_ [0];
my $i = 0;
for ($i = 0; $i < $num_players_in_game; $i++)
{
if ($this_bot != $i && $player_counters {$i} == 0)
{
return 1;
}
}
return 0;
}
sub handle_bot_next_go
{
#$taken_cards .= "Bot of $whos_turn is being handled for $current_flipped_card." . ($current_number_of_counters + 1) . "\n";
if ($current_flipped_card eq "GAME_OVER" || $current_flipped_card eq "")
{
return;
}
my $bots_counters = $player_counters {$whos_turn};
if ($bots_counters > 0)
{
my $cards = $player_cards {$whos_turn};
my $current_score = get_score_from_cards ($cards, $whos_turn) + 1;
$cards .= "$current_flipped_card,";
my $poss_score = get_score_from_cards ($cards, $whos_turn) - $current_number_of_counters;
if (others_on_zero ($whos_turn))
{
pass_card_w_id ("passcard.$current_flipped_card." . ($current_number_of_counters + 1), $whos_turn);
return;
}
if ($current_score >= $poss_score)
{
# Can do counter farming?
if ($current_flipped_card - $current_number_of_counters > 2 * $num_players_in_game)
{
if (noone_else_wants ($current_flipped_card, $whos_turn))
{
#$taken_cards .= "Bot " . get_player_name ($whos_turn) . " is counterfarming.. for $current_flipped_card ($current_score vs $poss_score)<br>";
pass_card_w_id ("passcard.$current_flipped_card." . ($current_number_of_counters + 1), $whos_turn);
return;
}
}
#$taken_cards .= " Bot " . get_player_name ($whos_turn) . " is taking $current_flipped_card ($current_score vs $poss_score)<br>";
take_card_with_id ("takecard.$current_flipped_card.$current_number_of_counters", $whos_turn);
handle_bot_next_go ();
return;
}
my $rand_counters = int (rand (10)) + 5;
my $rand_chance = int (rand ($current_number_of_counters)) + $current_number_of_counters / 2;
if ($current_number_of_counters > $rand_chance && $current_number_of_counters > $rand_counters)
{
take_card_with_id ("takecard.$current_flipped_card.$current_number_of_counters", $whos_turn);
handle_bot_next_go ();
return;
}
#$taken_cards .= "Bot " . get_player_name ($whos_turn) . " $whos_turn is passing.. for $current_flipped_card ($current_score vs $poss_score)<br>";
pass_card_w_id ("passcard.$current_flipped_card." . ($current_number_of_counters + 1), $whos_turn);
return;
}
take_card_with_id ("takecard.$current_flipped_card.$current_number_of_counters", $whos_turn);
handle_bot_next_go ();
}
sub set_next_turn
{
$whos_turn++;
if ($whos_turn >= $num_players_in_game)
{
$whos_turn = 0;
}
my $is_bot = is_bot ("", $whos_turn);
if ($is_bot)
{
#$taken_cards .= " $whos_turn is a bot!!<br>";
handle_bot_next_go ();
}
}
sub get_player_name_from_IP
{
my $IP = $_ [0];
my $id = get_player_id ($IP);
return $player_names [$id];
}
sub add_new_user
{
my $in = $_ [0];
my $IP = $_ [1];
my $is_bot = $_ [2];
my $this_name = "";
if ($in =~ m/name=([\w][\w][\w][\w_]+)_*$/)
{
$this_name = $1;
$this_name =~ s/\W/_/g;
$this_name =~ s/_*$//g;
$this_name =~ s/HTTP.*//g;
$IP .= "_$this_name";
if ($this_name !~ m/..../)
{
return "";
}
}
else
{
return "";
}
my $ip_find;
foreach $ip_find (@player_ips)
{
if ($ip_find eq $IP)
{
return "";
}
}
my $name_find;
print ("IN ADDING $this_name!!!\n");
foreach $name_find (@player_names)
{
if ($name_find eq $this_name)
{
return "";
}
}
print (" OK IN ADDING $this_name!!!\n");
{
add_to_debug ("ADDING NEW_USER ($this_name)..\n");
$player_names [$num_players_in_lobby] = $this_name;
$player_ips [$num_players_in_lobby] = $IP;
add_to_debug ("ADDING NEW_USER Player IPS:" . join ("<br>", @player_ips));
add_to_debug ("ADDING NEW_USER Player Names:" . join ("<br>", @player_names));
$NEEDS_REFRESH [$num_players_in_lobby] = 1;
$NEEDS_ALERT [$num_players_in_lobby] = 0;
$num_players_in_lobby++;
add_to_debug ("ADDING NEW_USER ($this_name)..\n");
my $col = sprintf ("#%lX%1X%1X", int (rand (200) + 55), int (rand (200) + 55), int (rand (200) + 55));
$rand_colors {$this_name} = $col;
add_to_debug ("RAND COLOR - $this_name = $rand_colors{$this_name} ($col)\n");
$PLAYER_IS_BOT {$this_name} = 0;
if ($is_bot)
{
$PLAYER_IS_BOT {$this_name} = 1;
}
return "Welcome $this_name";
}
}
sub boot_person
{
my $person_to_boot = $_ [0];
my $person_to_boot_id = get_player_id_from_name ($person_to_boot, "bbbb");
if ($person_to_boot_id == -1)
{
return;
}
if (game_started () == 1)
{
return;
}
my @new_player_ips;
my $len = scalar @player_ips;
my @new_player_names;
my $i = 0;
my $new_i = 0;
add_to_debug ("BOOT_PERSON $i, $len boot_person $person_to_boot ");
while ($i < $len)
{
add_to_debug ("BOOT_PERSON in $i, $len boot_person $person_to_boot ");
if ($i == $person_to_boot_id)
{
add_to_debug ("Booting this person BOOT_PERSON found in $i, $len boot_person $person_to_boot add $player_names[$i] to banned..");
$BANNED_NAMES {$player_names [$i]} = 1;
$i++;
$num_players_in_lobby--;
next;
}
add_to_debug ("Not booting this person BOOT_PERSON found in $new_i, $i, $len boot_person $person_to_boot ");
$new_player_names [$new_i] = $player_names [$i];
$new_player_ips [$new_i] = $player_ips [$i];
$i++;
$new_i++;
}
if ($num_players_in_lobby < 0)
{
$num_players_in_lobby = 0;
}
add_to_debug ("Before BOOT_PERSON ($person_to_boot) had names of: " . join (",", sort (@player_names)));
add_to_debug ("Before BOOT_PERSON ($person_to_boot) had ips of: " . join (",", sort (@player_ips)));
@player_names = @new_player_names;
@player_ips = @new_player_ips;
add_to_debug ("After BOOT_PERSON ($person_to_boot) had names of: " . join (",", sort (@player_names)));
add_to_debug ("After BOOT_PERSON ($person_to_boot) had ips of: " . join (",", sort (@player_ips)));
return "";
}
sub reduce_cards_by_1
{
$num_cards_per_player --;
if ($num_cards_per_player < 2)
{
game_won ($BAD_GUYS, "Too many rounds");
}
}
sub force_needs_refresh
{
my $i = 0;
add_to_debug (" IN FORCING REFRESH\n");
for ($i = 0; $i < $num_players_in_lobby; $i++)
{
$NEEDS_REFRESH [$i] = 1;
add_to_debug (" FORCING REFRESH FOR $i - " . get_player_name ($i));
}
add_to_debug (" DONE FORCING REFRESH");
}
sub force_needs_refresh_trigger
{
my $i = 0;
my $reason = $_ [0];
for ($i = 0; $i < $num_players_in_lobby; $i++)
{
$NEEDS_REFRESH [$i] = 1;
}
}
sub get_needs_refresh
{
my $i = 0;
my $IP = $_ [0];
my $id = get_player_id ($IP);
if ($NEEDS_REFRESH [$id])
{
$NEEDS_REFRESH [$id] = 0;
return 1;
}
return 0;
}
sub pass_card_w_id
{
my $in = $_ [0];
my $id = $_ [1];
my $n = get_player_name ($id);
if ($id != $whos_turn)
{
return ("");
}
if ($in =~ m/pass.*card\.(\d+)\.(\d+)/)
{
my $card_taken = $1;
my $num_tokens = $2;
$player_counters {$id} --;
$current_number_of_counters++;
set_next_turn ();
}
force_needs_refresh ();
}
sub pass_card
{
my $in = $_ [0];
my $IP = $_ [1];
my $id = get_player_id ($IP);
return pass_card_w_id ($in, $id);
}
sub take_card_with_id
{
my $in = $_ [0];
my $id = $_ [1];
my $n = get_player_name ($id);
if ($id != $whos_turn)
{
return ("");
}
if ($in =~ m/take.*card\.(\d+)\.(\d+)/)
{
my $card_taken = $1;
my $num_tokens = $2;
my $name_of_card_picked = $deck [$card_taken];
#$taken_cards .= get_player_name ($id) . " took $card_taken (and had $player_counters{$id} counters and will gain $num_tokens)<br>";
$taken_cards .= get_player_name ($id) . " took $card_taken <br>"; #(and had $player_counters{$id} counters and will gain $num_tokens)<br>";
print ("TOOK name_of_card_picked=$name_of_card_picked by $id!!!\n");
add_to_debug ("KNOWS $name_of_card_picked\n");
$needs_shuffle_but_before_next_card_picked = 0;
$player_cards {$id} .= "$card_taken,";
$player_counters {$id} += $num_tokens;
flip_top_card ();
}
force_needs_refresh ();
}
sub take_card
{
my $in = $_ [0];
my $IP = $_ [1];
my $id = get_player_id ($IP);
return take_card_with_id ($in, $id);
}
sub new_game
{
if ($num_players_in_game != -1)
{
return debug_game ();
}
if ($num_players_in_lobby == 0)
{
return debug_game ();
}
$num_players_in_game = $num_players_in_lobby;
set_whos_turn (int (rand ($num_players_in_game)));
$GAME_WON = 0;
$NUM_EXPOSED_CARDS = 0;
# Setup the deck..
setup_deck ();
$player_counters {0} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {1} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {2} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {3} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {4} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {5} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {6} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {7} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {8} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {9} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_cards {0} = "";
$player_cards {1} = "";
$player_cards {2} = "";
$player_cards {3} = "";
$player_cards {4} = "";
$player_cards {5} = "";
$player_cards {6} = "";
$player_cards {7} = "";
$player_cards {8} = "";
$player_cards {9} = "";
$num_cards_per_player = $NUM_COUNTERS_AT_START_OF_GAME;
force_needs_refresh();
my %new_already_shuffled;
%already_shuffled = %new_already_shuffled;
$needs_shuffle_but_before_next_card_picked = 0;
$taken_cards = "";
my $is_bot = is_bot ("", $whos_turn);
if ($is_bot)
{
handle_bot_next_go ();
}
return;
}
sub reset_game
{
$num_players_in_game = -1;
$GAME_WON = 0;
$NUM_EXPOSED_CARDS = 0;
$reason_for_game_end = "";
my @new_deck;
@deck = @new_deck;
my $out = "Game reset <a href=\"\/\">Lobby or Game window<\/a>";
force_needs_refresh();
my %new_already_shuffled;
%already_shuffled = %new_already_shuffled;
my %new_NOT_HIDDEN_INFO;
%NOT_HIDDEN_INFO = %new_NOT_HIDDEN_INFO;
$needs_shuffle_but_before_next_card_picked = 0;
$player_counters {0} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {1} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {2} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {3} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {4} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {5} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {6} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {7} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {8} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_counters {9} = $NUM_COUNTERS_AT_START_OF_GAME;
$player_cards {0} = "";
$player_cards {1} = "";
$player_cards {2} = "";
$player_cards {3} = "";
$player_cards {4} = "";
$player_cards {5} = "";
$player_cards {6} = "";
$player_cards {7} = "";
$player_cards {8} = "";
$player_cards {9} = "";
$taken_cards = "";
return $out;
}
sub in_game
{
my $id = get_player_id_from_name ($CURRENT_LOGIN_NAME, "ccc");
if ($id >= 0 && $id < $num_players_in_game)
{
return 1;
}
return 0;
}
sub get_score_from_cards
{
my $cards = $_ [0];
my $id = $_ [1];
my %cards;
while ($cards =~ s/^(\d+),//)
{
my $c = $1;
$cards {$c} = "<img width=\"30\" height=\"43\" src=\"card$1.png\"><\/img>";
}
my $k;
my $score = 0;
my $total_cards = 0;
for $k (sort {$a <=> $b} (keys %cards))
{
$score += $k;
$total_cards ++;
if (exists ($cards {$k - 1}))
{
$cards {$k} = "<img width=\"12\" height=\"43\" src=\"half_card.png\"><\/img>";
$score -= $k;
}
if (exists ($cards {$k + 1}))
{
$cards {$k} = "<img width=\"30\" height=\"43\" src=\"card$1_lead.png\"><\/img>";
}
}
return $score;
}
sub get_images_from_cards
{
my $cards = $_ [0];
my $id = $_ [1];