-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.pl
executable file
·946 lines (835 loc) · 26.1 KB
/
score.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
#!/usr/bin/env perl
#===============================================================================
#
# FILE: score.pl
#
# USAGE: ./score.pl -f players.txt
#
# DESCRIPTION: Tournament management script for Android: Netrunner
#
# REQUIREMENTS: Enter the players into a text file, one player per line.
# AUTHOR: Adam Fairbrother (Hegz), [email protected]
# VERSION: 1.0
#===============================================================================
use strict;
use warnings;
use utf8;
no warnings qw(experimental);
use 5.14.0;
use YAML::XS qw(DumpFile LoadFile);
use Term::UI;
use Term::ReadLine;
use Perl6::Form;
use List::MoreUtils qw(firstidx);
use Scalar::Util qw(looks_like_number);
my $Players_File = $ARGV[0];
$Players_File =~ s/(.*)\..{0,3}/$1/;
my $data_file = $Players_File . ".yml";
my $score_round;
my $player_data;
my $total_rounds;
unless ( -e $data_file ) {
Load_Player_data();
}
else {
$player_data = LoadFile($data_file);
$score_round = $player_data->{META}->{score_round};
$total_rounds = $player_data->{META}->{total_rounds};
for (keys $player_data) {
unless ( defined $player_data->{$_}->{status}) {
$player_data->{$_}->{status} = 'Active';
}
}
}
my $term = Term::ReadLine->new('brand');
delete $player_data->{BYE};
delete $player_data->{META};
# Main Menu
my @menu = (
'Add Score Data',
'Advance Round',
'Show Matchups',
'View standings',
'Administrator Override',
'Save & Quit'
);
my $main_menu;
do {
$main_menu = $term->get_reply(
prompt => "Menu selection",
choices => \@menu,
default => $menu[-1],
print_me => "\nCurrent Round:"
. ( $score_round + 1 )
. " of $total_rounds\n",
);
for ($main_menu) {
no warnings qw(experimental);
Score_Data() when $_ eq $menu[0];
Select_round() when $_ eq $menu[1];
Show_Matchups() when $_ eq $menu[2];
View_Standings() when $_ eq $menu[3];
Override() when $_ eq $menu[4];
}
} while ( $main_menu ne $menu[-1] );
Save_File();
exit 0;
# End of main
sub Save_File {
# Save data to file
$player_data->{'META'} = {
score_round => $score_round,
total_rounds => $total_rounds,
};
DumpFile( $Players_File . ".yml", $player_data );
delete $player_data->{BYE};
delete $player_data->{META};
}
sub Select_round {
# Advance the round and set pairings.
# Check to see that all current round data has been scored.
for (keys $player_data) {
unless (defined $player_data->{$_}->{prestige}[$score_round]){
print "\nERROR: Missing Score data for round " . ($score_round + 1) ." Unable to advance\n\n";
return 0;
}
}
#Check to see if is already the last round
if (($score_round + 1) eq $total_rounds) {
print "\nERROR: Already at the last round.\n\n";
return 0;
}
$score_round++;
Make_Pairing();
return 0;
}
sub sum {
# Total the values in an array
my @array = @_;
my $total = 0;
for (@array) {
$total += $_ if defined $_;
}
return $total;
}
sub Make_Pairing {
# Make all the pairings for the current round.
# Sort players based on Prestige.
my @Players = sort {
sum( @{ $player_data->{$b}->{prestige} } )
<=> sum( @{ $player_data->{$a}->{prestige} } )
} keys $player_data;
for my $i ( 0 .. $#Players ) {
# Match the top player with the next valid player in the list.
my $player = $Players[$i];
if ($player_data->{$player}->{status} eq 'Disabled') {
$player_data->{$player}->{opponents}[$score_round] = 'N/A';
}
unless ( defined $player_data->{$player}->{opponents}[$score_round] || $player_data->{$player}->{status} eq 'Disabled') {
# Filter out players that have a match, and disabled players
my $opponent = $i; # Index for the player in the @Players array
my $nomatch = 0; # valid matching
my $BYE; # Player will be paired with BYE
do {
# Check next opponent in list to see if they are a valid pair
$opponent++; # set the array index for the proposed opponent
$nomatch = 0;
if ( defined $Players[$opponent] && $player_data->{$Players[$opponent]}->{status} ne 'Disabled') {
# Ensure the next player in the list is defined, Active
if ( defined $player_data->{$Players[$opponent]}->{opponents}[$score_round] ) {
# Check to ensure opponent doesn't have existing match data;
$nomatch = 1;
}
unless ($nomatch) {
for ( @{ $player_data->{$player}->{opponents} } ) {
# Check the players previous opponents.
if ( $_ eq $Players[$opponent] ) {
# This matching took place in a previous round.
$nomatch = 1;
}
}
}
}
else {
# End of list of players and no valid match match found
$nomatch = 0;
$BYE = 1;
}
} while ($nomatch); # Check next opponent if match failed.
if ($BYE) {
# Set the matching and prestige for the BYE
push @{ $player_data->{$player}->{opponents} }, 'BYE';
$player_data->{$player}->{prestige}[$score_round] = 4;
}
else {
# Match player with the selected opponent, match opponent with player
push @{ $player_data->{$player}->{opponents} },
$Players[$opponent];
push @{ $player_data->{ $Players[$opponent] }->{opponents} },
$player;
}
}
} # Proceed to the next player
Save_File();
return 0;
}
sub Show_Matchups {
my %matchups;
for ( keys $player_data ) {
unless ($player_data->{$_}->{status} eq 'Disabled') {
$matchups{$_} = $player_data->{$_}->{opponents}[$score_round];
}
}
my $spacer = 0;
for ( keys $player_data ) {
$spacer = length if length > $spacer;
}
my $format = "{>>>} {<{" . $spacer . "}<} {><<} {<{" . $spacer . "}<}";
print "Round " . ($score_round + 1) . " Pairings\n\n";
print form $format, 'Table', 'Player', ' ', 'Player';
my $index = 1;
my $BYE;
for ( sort keys %matchups ) {
if ( defined $matchups{$_} ) {
if ($matchups{$_} eq 'BYE') {
$BYE = $_;
next;
}
my $table = $index++ . '.';
print form $format, $table, $_, '<->', $matchups{$_};
delete $matchups{ $matchups{$_} };
}
}
if (defined $BYE) {
print form $format, ' ', $BYE, '<->', 'BYE';
}
print "\n";
return 0;
}
sub View_Standings {
# Print the current standings for the tournament
# Sort the players on Prestige, then Strength of Schedule
my @temp =
map { [ sum( @{ $player_data->{$_}->{prestige} } ), $_ ] }
keys $player_data;
for (@temp) {
my $Sos = 0;
my $name = $_->[1];
for ( @{ $player_data->{$name}->{opponents} } ) {
$Sos += sum( @{ $player_data->{$_}->{prestige} } )
unless $_ eq 'BYE' || $_ eq 'N/A';
}
push $_, $Sos;
}
my @Sorted_Players =
map { $_->[1] } sort { $b->[0] <=> $a->[0] || $b->[2] <=> $a->[2] } @temp;
# Determine the required field width for names
my $spacer = 0;
for ( keys $player_data ) {
$spacer = length if length > $spacer;
}
# Build form format line and Header
my $format = " {>>>} {<{" . ($spacer) . "}<} {<<<<<<} {<} ";
my @header = ('Rank', 'Player name', 'Prestige', 'SoS');
for (1 .. ($score_round + 1)) {
$format = $format . " {<{". ($spacer + 4) . "}<}";
push @header, " Round " . $_;
}
# Print Header
print form $format,@header;
# Populate & print form data line
my $Rank = 0;
for my $player (@Sorted_Players) {
my @Print_Line;
# Add Rank, Mark disabled players rank as '!DEL-'
if ($player_data->{$player}->{status} eq 'Disabled') {
push @Print_Line, '!DEL-';
}
else {
push @Print_Line, ++$Rank;
}
# Add the Player Name
push @Print_Line, $player;
# Add the total Prestige
my $prestige = sum( @{ $player_data->{$player}->{prestige} } );
push @Print_Line, $prestige;
# Add the Strength of Schedule
my $Sos;
for ( @{ $player_data->{$player}->{opponents} } ) {
$Sos += sum( @{ $player_data->{$_}->{prestige} } )
unless $_ eq 'BYE' || $_ eq 'N/A';
}
$Sos = 0 unless defined $Sos;
push @Print_Line, $Sos;
# Add Round Results and Opponents
for my $opp ( @{ $player_data->{$player}->{opponents} } ) {
my $round = firstidx { $_ eq "$opp" } @{ $player_data->{$player}->{opponents} };
my $round_prestige = ' ';
if ( defined $player_data->{$player}->{prestige}[$round] ) {
$round_prestige = $player_data->{$player}->{prestige}[$round];
}
push @Print_Line, "[$round_prestige] $opp";
}
print form $format, @Print_Line;
}
print "\n";
return 0;
}
sub Load_Player_data {
# Load in the initial list of players and generate initial pairings
# Load Players
open( my $fh_players, "<", $ARGV[0] ) or die "$!";
chomp( my @players = <$fh_players> );
close $fh_players;
# Add Bye if needed
if ( scalar @players % 2 == 1 ) {
push @players, "BYE";
}
# Determine number of rounds
my $rounds;
for ( scalar @players ) {
no warnings qw(experimental);
$rounds = 6 when $_ >= 33;
$rounds = 5 when $_ >= 17;
$rounds = 4 when $_ >= 9;
$rounds = 3 when $_ >= 5;
$rounds = 2 when $_ >= 2;
}
# Generate Pairings at random
do {
my $player1 = splice @players, int( rand( scalar @players ) ), 1;
my $player2 = splice @players, int( rand( scalar @players ) ), 1;
$player_data->{$player1} = {
opponents => [$player2],
prestige => [ ( undef, ) x $rounds ],
status => 'Active',
};
$player_data->{$player2} = {
opponents => [$player1],
prestige => [ ( undef, ) x $rounds ],
status => 'Active',
};
if ( $player2 eq 'BYE' ) {
$player_data->{$player1}->{prestige}[0] = 4;
}
elsif ( $player1 eq 'BYE' ) {
$player_data->{$player2}->{prestige}[0] = 4;
}
} while ( scalar @players > 0 );
$score_round = 0;
$total_rounds = $rounds;
return 0;
}
sub Score_Data {
# Filter out players with score data for this round
my @sorted_players;
for ( sort keys %$player_data ) {
unless ( defined $player_data->{$_}->{prestige}[$score_round] || $player_data->{$_}->{status} eq 'Disabled') {
push @sorted_players, $_;
}
}
if ( scalar @sorted_players == 0 ) {
print "No More players to score for round "
. ( $score_round + 1 ) . ".\n";
return;
}
push @sorted_players, 'Return';
# prompt for player
my $Player = $term->get_reply(
prompt => 'Score which player?',
choices => \@sorted_players,
default => $sorted_players[-1],
);
if ($Player eq $sorted_players[-1]) {
return 0;
}
my $Player2 = $player_data->{$Player}->{opponents}[$score_round];
print "\nScoring match vs $Player2\n";
# valid scoring options as of Aug, 2014
my @score_options = (
'Won both games',
'Won and Lost',
'Won Game and Timeout',
'Lost Game and Timeout',
'Timeout on first game',
'Lost both games',
'Return'
);
# Prompt for Score
my $score = $term->get_reply(
prompt => 'Match Result',
choices => \@score_options,
default => $score_options[-1],
);
if ($score eq $score_options[-1]) {
return 0;
}
my $Player1_Timeout_Mod = 0;
my $Player2_Timeout_Mod = 0;
if ($score =~ m/Timeout/) {
# if there was a timeout, check for Lead Tie or Deficit
my @timeout = (
'Agenda Point Lead',
'Agenda Point Tie',
'Agenda Point Deficit',
'Return'
);
my $timeout_result = $term->get_reply(
prompt => 'Timout Result',
choices => \@timeout,
default => $timeout[-1],
);
if ($timeout_result eq $timeout[-1]) {
return 0;
}
for ($timeout_result) {
no warnings qw(experimental);
when ($timeout_result eq $timeout[0]) {
$Player1_Timeout_Mod = 1;
}
when ($timeout_result eq $timeout[1]) {
$Player1_Timeout_Mod = 1;
$Player2_Timeout_Mod = 1;
}
when ($timeout_result eq $timeout[2]) {
$Player2_Timeout_Mod = 1;
}
}
}
for ($score) {
no warnings qw(experimental);
when ( $score eq $score_options[0] ) {
# Won both Games
$player_data->{$Player}->{prestige}[$score_round] = 4;
$player_data->{$Player2}->{prestige}[$score_round] = 0;
}
when ( $score eq $score_options[1] ) {
# Split games
$player_data->{$Player}->{prestige}[$score_round] = 2;
$player_data->{$Player2}->{prestige}[$score_round] = 2;
}
when ( $score eq $score_options[2] ) {
# Won Game 1 and timeout
$player_data->{$Player}->{prestige}[$score_round] = 2 + $Player1_Timeout_Mod;
$player_data->{$Player2}->{prestige}[$score_round] = 0 + $Player2_Timeout_Mod;
}
when ( $score eq $score_options[3] ) {
# Lost Game 1 and timeout
$player_data->{$Player}->{prestige}[$score_round] = 0 + $Player1_Timeout_Mod;
$player_data->{$Player2}->{prestige}[$score_round] = 2 + $Player2_Timeout_Mod;
}
when ( $score eq $score_options[4] ) {
# Timeout first game
$player_data->{$Player}->{prestige}[$score_round] = 0 + $Player1_Timeout_Mod;
$player_data->{$Player2}->{prestige}[$score_round] = 0 + $Player2_Timeout_Mod;
}
when ( $score eq $score_options[5] ) {
# Lost both Games
$player_data->{$Player}->{prestige}[$score_round] = 0;
$player_data->{$Player2}->{prestige}[$score_round] = 4;
}
}
Save_File();
return 0;
}
sub Override {
# Note: This whole password thing is a joke. Anything other then a blank string will let you in.
my $term = Term::ReadLine->new('passwd');
my $passwd = $term->readline("Please enter the Administrator Password:");
if ($passwd eq '') {
print "\nPassword incorrect\n --== ACCESS DENIED ==--\n";
return 0;
}
print "\n --== ACCESS GRANTED ==--\n";
print "\n !!Please be carefull with the options in this menu!!\n";
my @menu = (
'Disable Player',
'Enable player',
'Add Player',
'Score Adjustment',
'Opponent Adjustment',
'Re Create Pairings',
'Override Rounds',
'Return',
);
my $override_menu;
do {
$override_menu = $term->get_reply(
prompt => "Admin Function",
choices => \@menu,
default => $menu[-1],
print_me => "\nADMINISTRATOR OVERRIDE ENABLED",
);
for ($override_menu) {
no warnings qw(experimental);
Admin_Disable() when $_ eq $menu[0];
Admin_Enable() when $_ eq $menu[1];
Admin_Add() when $_ eq $menu[2];
Admin_Score() when $_ eq $menu[3];
Admin_Pairing() when $_ eq $menu[4];
Admin_Matchups() when $_ eq $menu[5];
Admin_Round() when $_ eq $menu[6];
}
} while ( $override_menu ne $menu[-1] );
return 0;
}
sub Admin_Matchups {
for ( keys $player_data ) {
$player_data->{$_}->{opponents}[$score_round] = undef;
}
Make_Pairing();
}
sub Admin_Round {
my $term = Term::ReadLine->new('round');
print "\nCurrent Total rounds Required: " . $total_rounds . "\n\n";
my $new_total_rounds = $term->readline("Set the new total to (Enter to Cancel):");
if ($new_total_rounds eq '') {
print "\nTotal Rounds Unchanged\n";
}
elsif ( (looks_like_number($new_total_rounds)) && ($new_total_rounds gt 0) ) {
$total_rounds = $new_total_rounds;
}
else {
print "\nError: Please enter a NUMBER Greater then 0.\n";
}
print "\nCurrent Round: " . $score_round + 1 . "\n\n";
my $new_round = $term->readline("Set the curent round to (Enter to Cancel):");
if ($new_round eq '') {
print "\nCurrent Round Unchanged\n";
}
elsif ( (looks_like_number($new_round)) && ($new_round lt $total_rounds) && ($total_rounds gt 0)) {
$score_round = $new_round - 1;
}
else {
print "\nError: Please enter a NUMBER Greater then 0 and less then the total number of rounds.\n";
}
print "\nPlease be sure to Re Create the Pairings if the current round changed.\n";
return 0;
}
sub Admin_Score {
my $spacer = 0;
for ( keys $player_data ) {
$spacer = length if length > $spacer;
}
my $format = "{<{" . ($spacer) . "}<}";
my @header = ("Player name");
for (1 .. $total_rounds) {
$format = $format . " {>>>>>>>}";
push @header, " Round " . $_;
}
my $title = form $format, @header;
chomp $title;
my @menu;
my @players;
for my $p (sort keys $player_data) {
my @data = ($p);
for (0 .. ($total_rounds) -1 ) {
if (defined $player_data->{$p}->{prestige}[$_]){
push @data, $player_data->{$p}->{prestige}[$_];
}
else {
push @data, 'N/A';
}
}
push @players, $p;
push @menu, (form $format, @data);
}
chomp @menu;
push @menu, "Cancel Edit";
# Prompt for player
my $player = $term->get_reply(
prompt => 'Player to edit',
choices => \@menu,
default => $menu[-1],
print_me => "\n $title",
);
if ($player eq $menu[-1]){
return 0;
}
my $select = $players[ firstidx { $_ eq $player } @menu];
$format = "{<<<<<<<<<<} {>>>>>>>}";
@menu = ();
my @rounds;
for (0 .. ($total_rounds) -1 ) {
my @data;
push @rounds, $_;
push @data, "Round " . ($_ + 1);
if (defined $player_data->{$select}->{prestige}[$_]){
push @data, $player_data->{$select}->{prestige}[$_];
}
else {
push @data, 'N/A';
}
push @menu, (form $format, @data);
}
push @menu, "Cancel Edit";
chomp @menu;
# Prompt for round
my $round = $term->get_reply(
prompt => 'Round to edit',
choices => \@menu,
default => $menu[-1],
print_me => "\n Round Prestige",
);
if ($round eq $menu[-1]){
return 0;
}
my $r = $rounds[ firstidx { $_ eq $round } @menu];
my $term = Term::ReadLine->new('prestige');
my $new_prestige;
do {
$new_prestige = $term->readline("Set prestige to (Enter to Cancel):");
if ($new_prestige eq '') {
print "\nPrestige for $select for Round " . $r +1 . " Unchanged.\n";
}
elsif ( looks_like_number($new_prestige) ) {
$player_data->{$select}->{prestige}[$r] = $new_prestige;
}
else {
print "\nError: Please enter a NUMBER.\n";
}
} until ( looks_like_number($new_prestige) || $new_prestige eq '' );
my $opponent = $player_data->{$select}->{opponents}[$r];
do {
print "Set prestige for " . $select . "s Round " . $r + 1 . " opponenet: $opponent\n";
$new_prestige = $term->readline("Set prestige to (Enter to Cancel):");
if ($new_prestige eq '') {
print "\nPrestige for $opponent for Round " . $r +1 . " Unchanged.\n";
}
elsif ( looks_like_number($new_prestige) ) {
$player_data->{$opponent}->{prestige}[$r] = $new_prestige;
}
else {
print "\nError: Please enter a NUMBER.\n";
}
} until ( looks_like_number($new_prestige) || $new_prestige eq '' );
return 0;
}
sub Admin_Pairing {
my $spacer = 0;
for ( keys $player_data ) {
$spacer = length if length > $spacer;
}
my @rounds;
my $format = "{<{" . ($spacer) . "}<}";
my @header = ("Player name");
for (1 .. $total_rounds) {
$format = $format . " {<{" . ($spacer) . "}<}";
push @header, " Round " . $_;
push @rounds, "Round " . $_;
}
print "\n ";
print form $format, @header;
my @pairing_data;
for my $p (sort keys $player_data) {
my @data = ($p);
for (0 .. ($total_rounds) -1 ) {
if (defined $player_data->{$p}->{opponents}[$_]){
push @data, $player_data->{$p}->{opponents}[$_];
}
else {
push @data, 'N/A';
}
}
push @pairing_data, (form $format, @data);
}
print " @pairing_data";
push @rounds, "Cancel Edit";
# Prompt for player
my $round = $term->get_reply(
prompt => 'Choose round',
choices => \@rounds,
default => $rounds[-1],
print_me => "\nEdit pairing data for which round?",
);
if ($round eq $rounds[-1]){
return 0;
}
my @Match_Rounds = (0 .. $total_rounds);
my $selected_round = $Match_Rounds[ firstidx { $_ eq $round } @rounds];
$format = "{<{" . ($spacer) . "}<} {<{" . ($spacer) . "}<}";
my $title = form $format, 'Player', 'Opponent';
chomp $title;
my @round_match;
do {
my @players;
for my $p (sort keys $player_data) {
my @data = ($p);
if (defined $player_data->{$p}->{opponents}[$selected_round]){
push @data, $player_data->{$p}->{opponents}[$selected_round];
push @players, $p;
}
else {
push @data, 'N/A';
}
push @round_match, (form $format, @data);
}
chomp @round_match;
push @round_match, "Clear All Pairings", "Return";
my $round = $term->get_reply(
prompt => 'Choose pairing',
choices => \@round_match,
default => $round_match[-1],
print_me => " " . $title,
);
my $selected_player = $players[ firstidx { $_ eq $round } @rounds];
if ($round eq $round_match[-2]){
for (keys $player_data) {
$player_data->{$_}->{opponents}[$selected_round] = undef;
}
}
elsif ($round ne $round_match[-1]) {
my @menu;
for (sort keys $player_data) {
push @menu, $_ unless $_ eq $selected_player;
}
push @menu, "Cancel";
my $opponent = $term->get_reply(
prompt => "Select the opponent for $selected_player",
choices => \@menu,
default => $menu[-1],
print_me => "\nOpponent Selection",
);
if ($opponent eq $menu[-1]){
next;
}
else {
$player_data->{$selected_player}->{opponents}[$selected_round] = $opponent;
$player_data->{$selected_player}->{prestige}[$selected_round] = undef;
$player_data->{$opponent}->{opponents}[$selected_round] = $selected_player;
$player_data->{$opponent}->{prestige}[$selected_round] = undef;
}
}
} until ($round eq $round_match[-1]);
}
sub Admin_Add {
my $term = Term::ReadLine->new('new');
my $new_player = $term->readline("New Player Name (Enter to Cancel):");
if ($new_player eq '') {
return 0;
}
# TODO Players are mis assigned when disabled players exist.
if ((scalar (keys $player_data)) % 2 == 1) {
for (keys $player_data) {
if ( $player_data->{$_}->{opponents}[$score_round] eq 'BYE'){
$player_data->{$_}->{opponents}[$score_round] = $new_player;
$player_data->{$_}->{prestige}[$score_round] = undef;
$player_data->{$new_player} = {
opponents => [('N/A', ) x $score_round],
prestige => [ ( undef, ) x $total_rounds ],
status => 'Active',
};
for ( 0 .. ($score_round - 1 )) {
$player_data->{$new_player}->{prestige}[$_] = 0;
}
$player_data->{$new_player}->{opponents}[$score_round] = $_;
$player_data->{$new_player}->{prestige}[$score_round] = undef;
last;
}
}
}
else {
$player_data->{$new_player} = {
opponents => [('N/A', ) x $score_round],
prestige => [ ( undef, ) x $total_rounds ],
status => 'Active',
};
for ( 0 .. ($score_round - 1 )) {
$player_data->{$new_player}->{prestige}[$_] = 0;
}
$player_data->{$new_player}->{opponents}[$score_round] = 'BYE';
$player_data->{$new_player}->{prestige}[$score_round] = 4;
}
}
sub Admin_Disable {
# Filter out players who are disabled
my @sorted_players;
for ( sort keys %$player_data ) {
unless ($player_data->{$_}->{status} eq 'Disabled') {
push @sorted_players, $_;
}
}
if ( scalar @sorted_players == 0 ) {
print "ERROR: No Active players. What kind of tournament are you running?\n";
return;
}
push @sorted_players, 'Return';
# prompt for player
my $Player = $term->get_reply(
prompt => 'Disable which player?',
choices => \@sorted_players,
default => $sorted_players[-1],
);
if ($Player eq $sorted_players[-1]){
return 0;
}
print $Player . " has been DEREZED.\n";
print $Player . "'s previous opponent Will be matched with BYE, or player who was previosuly matched with BYE.\n";
$player_data->{$Player}->{status} = 'Disabled';
my $Opponent = $player_data->{$Player}->{opponents}[$score_round];
if ( (scalar @sorted_players) % 2 == 1 ) {
# Odd Number of players remain in the tournament, Match Previous opponent with BYE
$player_data->{$Opponent}->{opponents}[$score_round] = 'BYE';
$player_data->{$Opponent}->{prestige}[$score_round] = '4';
}
else {
# Even Number or players. Find the previous BYE matching and replace
for (keys $player_data) {
if ($player_data->{$_}->{opponents}[$score_round] eq 'BYE') {
$player_data->{$_}->{opponents}[$score_round] = $Opponent;
$player_data->{$_}->{prestige}[$score_round] = undef;
$player_data->{$Opponent}->{opponents}[$score_round] = $_;
$player_data->{$Opponent}->{prestige}[$score_round] = undef;
last;
}
}
}
# Remove the disabled players Current Opponent, and set prestige to 0
$player_data->{$Player}->{opponents}[$score_round] = 'N/A';
$player_data->{$Player}->{prestige}[$score_round] = 0;
return 0;
}
sub Admin_Enable {
# Filter out players who are not disabled
my @sorted_players;
for ( sort keys %$player_data ) {
if ($player_data->{$_}->{status} eq 'Disabled') {
push @sorted_players, $_;
}
}
if ( scalar @sorted_players == 0 ) {
print "ERROR: No Players are disabled at this time.\n";
return;
}
push @sorted_players, 'Return';
# prompt for player
my $Player = $term->get_reply(
prompt => 'Enable which player?',
choices => \@sorted_players,
default => $sorted_players[-1],
);
if ($Player eq $sorted_players[-1]) {
return 0;
}
print $Player . " has been REZED.\n";
print $Player . "Will be matched with the BYE, or player who previosuly had the BYE.\n";
$player_data->{$Player}->{status} = 'Active';
if ( ((scalar @sorted_players) + 1) % 2 == 1 ) {
my $Opponent;
for (keys $player_data) {
if ($player_data->{$_}->{opponents}[$score_round] eq 'BYE') {
$Opponent = $_;
last;
}
}
$player_data->{$Opponent}->{opponents}[$score_round] = $Player;
$player_data->{$Opponent}->{prestige}[$score_round] = undef;
$player_data->{$Player}->{opponents}[$score_round] = $Opponent;
$player_data->{$Player}->{prestige}[$score_round] = undef;
}
else {
$player_data->{$Player}->{opponents}[$score_round] = 'BYE';
$player_data->{$Player}->{prestige}[$score_round] = 4;
}
return 0;
}