-
Notifications
You must be signed in to change notification settings - Fork 1
/
tk-score.pl
executable file
·1499 lines (1227 loc) · 43.8 KB
/
tk-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
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 -w
#
# tk-score - Managing and reporting the results and standings of a
# soccer league that plays once a week on tuesdays. This is reflected
# in the lack of scheduling options as currently avaible.
# Now using GIT!
use strict;
# Where you can find extra modules?
use lib "$ENV{HOME}/lib/perl";
use Season;
# Remove cwd, why?
no lib ".";
use IO::Handle;
#use Data::Dumper;
use Getopt::Long;
use List::Util 'shuffle';
use Pod::Usage;
use YAML qw(DumpFile LoadFile);
# Non-core Perl modules we require
my $count = 0;
foreach my $mod ("Tk",
"Tk::BrowseEntry", "Tk::DateEntry", "Tk::HList",
"Tk::ItemStyle", "Tk::DialogBox", "Tk::Month",
"Tk::FileSelect", "Tk::FileDialog",
"Date::Calc qw(Decode_Date_US Delta_Days Date_to_Days Add_Delta_Days)",
) {
eval "use $mod;1";
if ($@) {
warn " Missing: $mod\n";
$count++;
}
}
die "\nPlease install the above modules (from CPAN) to run this program.\n\n" if $count;
#---------------------------------------------------------------------
# Defaults and global variables.
#---------------------------------------------------------------------
my $VERSION = "v1.9 (2014-11-17)";
# Version of the Game file.
my $gf_version = "v2.0";
my $mail_from = "dsl\@stoffel.org";
my $mail_prog = "/usr/lib/sendmail";
my $default_background = "white";
my $default_background_odd = "white";
my $default_background_even = "lightgrey";
# Global font for all GUI elements
my $default_font_type = "Helvetica";
my $default_font_size = "10";
my $default_font = "$default_font_type $default_font_size";
my $game_file = "";
my $rpt_file = "new-season.rpt";
my $do_report = 0;
my $NeedSave = 0;
# Flush all output right away...
$|=1;
#---------------------------------------------------------------------
my @initial_team_names = qw(one two three four five six seven eight nine);
my @matches = ();
my @match_dates = ();
my @scores = ( '','F',0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
my %lining_team;
my %bye_team;
my $homeforfeit= 0;
my $homecoed = 'no';
my $homescore = " ";
my $match_datelist;
# Style Colors for DateList, made global so we can update them
# easily. Might not need to do this down the line though....
my $dls_red;
my $dls_green;
my $dls_done;
my $dls_blue;
my $notokcolor = 'darkgrey';
my $okcolor = 'lightgreen';
# Format is # -> Name
my @teams;
# Debugging and option parsing
my $DEBUG;
my $prog_help;
my $man;
# Used to configure DateEntry to ignore certain dates.
my @blockdates = ();
#---------------------------------------------------------------------
# These schedules should be stored in a YAML file somewhere else.
#---------------------------------------------------------------------
my $dolining = 0; # Do teams need to line during the season?
# Week 0 is scrimmage if used. But numbering starts from 1!!! Data
# Structure, which should be in an YAML file instead, though this way
# it works for DSL stuff nicely. Other leagues might have other
# needs.
my $first_match_scrimmage = 0;
# %sched_template = ( Number_Teams => {
# Week => [game,...,gameN,bye,lining],
# ....
# }
# );
my %sched_template =
# Only three matches per week here. generate_schedule() needs to
# handle this properly.
( 6 => {
0 => [ "3-2", "4-6", "1-5", "", "3" ],
1 => [ "2-1", "3-6", "4-5", "", "1" ],
2 => [ "3-4", "2-5", "6-1", "", "5" ],
3 => [ "6-4", "1-5", "2-3", "", "6" ],
4 => [ "4-1", "6-2", "5-3", "", "2" ],
5 => [ "5-6", "1-3", "4-2", "", "4" ],
6 => [ "5-4", "1-2", "6-3", "", "2" ],
7 => [ "5-2", "4-3", "1-6", "", "4" ],
8 => [ "5-1", "6-4", "3-2", "", "6" ],
9 => [ "2-6", "3-5", "1-4", "", "3" ],
10 => [ "3-1", "2-4", "6-5", "", "1" ],
11 => [ "Playoffs", "Playoffs", "Playoffs", "", "tbd" ],
12 => [ "Playoffs", "Playoffs", "Playoffs", "", "tbd" ],
},
8 => {
0 => [ "1-7", "2-3", "5-8", "4-6", "", "1" ],
1 => [ "1-2", "3-4", "5-6", "7-8", "", "1" ],
2 => [ "5-7", "6-8", "1-3", "2-4", "", "5" ],
3 => [ "4-8", "1-5", "2-6", "3-7", "", "5" ],
4 => [ "2-5", "3-8", "4-7", "1-6", "", "3" ],
5 => [ "3-6", "2-7", "1-8", "4-5", "", "3" ],
6 => [ "1-7", "4-6", "2-8", "3-5", "", "4" ],
7 => [ "4-1", "7-6", "8-5", "3-2", "", "4" ],
8 => [ "8-7", "6-5", "4-3", "2-1", "", "6" ],
9 => [ "4-2", "3-1", "8-6", "7-5", "", "2" ],
10 => [ "7-3", "6-2", "5-1", "8-4", "", "7" ],
11 => [ "6-1", "7-4", "8-3", "5-2", "", "7" ],
12 => [ "5-4", "8-1", "7-2", "6-3", "", "8" ],
13 => [ "5-3", "8-2", "6-4", "7-1", "", "8" ],
14 => [ "2-3", "5-8", "6-7", "1-4", "", "2" ],
15 => [ "Make-up", "Make-up", "Make-up", "Make-up", "", "tbd" ],
16 => [ "Playoffs", "Playoffs", "Playoffs", "Playoffs", "", "tbd" ],
17 => [ "Playoffs", "Playoffs", "Playoffs", "Playoffs", "", "tbd" ],
18 => [ "Playoffs", "Playoffs", "Playoffs", "Playoffs", "", "tbd" ],
},
9 => {
1 => [ "3-7", "5-9", "4-6", "2-8", "1", "9" ],
2 => [ "1-2", "4-9", "5-7", "6-8", "3", "9" ],
3 => [ "4-8", "1-3", "7-9", "5-6", "2", "8" ],
4 => [ "3-8", "6-7", "1-4", "2-9", "5", "8" ],
5 => [ "6-9", "7-8", "2-3", "1-5", "4", "6" ],
6 => [ "1-6", "2-4", "3-9", "5-8", "7", "6" ],
7 => [ "2-5", "1-7", "8-9", "3-4", "6", "5" ],
8 => [ "4-7", "3-5", "1-8", "2-6", "9", "5" ],
9 => [ "4-5", "3-6", "2-7", "1-9", "8", "4" ],
10 => [ "2-8", "4-6", "5-9", "3-7", "1", "4" ],
11 => [ "6-8", "5-7", "4-9", "1-2", "3", "7" ],
12 => [ "5-6", "7-9", "1-3", "4-8", "2", "7" ],
13 => [ "2-9", "1-4", "6-7", "3-8", "5", "1" ],
14 => [ "1-5", "2-3", "7-8", "6-9", "4", "1" ],
15 => [ "5-8", "3-9", "2-4", "1-6", "7", "3" ],
16 => [ "3-4", "8-9", "1-7", "2-5", "6", "3" ],
17 => [ "2-6", "1-8", "3-5", "4-7", "9", "2" ],
18 => [ "1-9", "2-7", "3-6", "4-5", "8", "2" ],
19 => [ "Make-up", "Make-up", "Make-up", "Make-up", "", "tbd" ],
20 => [ "Playoffs", "Playoffs", "Playoffs", "Playoffs", "", "tbd" ],
21 => [ "Playoffs", "Playoffs", "Playoffs", "Playoffs", "", "tbd" ],
22 => [ "Playoffs", "Playoffs", "Playoffs", "Playoffs", "", "tbd" ],
},
);
my $playoff_sched =
{ "Three Weeks, 8 Teams" =>
{ 1 => [ "1-8 (A)", "2-7 (B)", "3-6 (C)", "4-5 (D)" ],
2 => [ "W:A-W:D (E)", "W:B-W:C (F)", "L:D-L:A (G)", "L:C-L:B (H)" ],
3 => [ "W:E-W:F (Final)", "L:E-L:F", "W:G-W:H", "L:G-L:H" ],
},
"Two Weeks, 8 Teams" =>
{ 1 => [ "1-4 (A)", "2-3 (B)", "5-8 (C)", "6-7 (D)" ],
2 => [ "W:A-W:B (Final)", "L:A-L:B", "W:C-W:D", "L:C-L:D" ],
},
};
# Number of teams supported by schedules. This should really be
# broken out into their own module. Also, the 6 team schedule assumes
# three fields per/week, 8 and 9 team schedules assume 4 fields
# per/week. Need to update logic somehow to present this properly.
my @teamcnt = sort(qw(6 8 9));
my $max_numteams = $teamcnt[$#teamcnt];
my $numteams = $teamcnt[0];
my @playoff_rnds = qw(3 2 1 0);
my @games_per_week = qw(1 2 3 4 5 6);
# FIXME! This needs to be much more dynamic in how we figure it out...
my $matches_per_week = 4;
#my $num_playoffs = $playoff_rnds[0];
# Two rounds of games for each team playing every other team.
my $numweeks = ($numteams - 1) * 2;
my $week = 1;
my $curweek = 0;
my $curdate = "";
my $weekdate= "";
my @weeks;
# Per-team standings. Re-calculated depending on the week showing.
my $cnt = 1;
my %curmatch;
my %standings;
my %season;
#---------------------------------------------------------------------
# Sort the %standings array (see zero_standings for format) by RANK,
# W, L, T and maybe more...
#---------------------------------------------------------------------
sub cleanup_and_exit {
my $top = shift;
# We need to find and loop through all the open Season(s) we might
# have and make sure they are saved before we exit. FIXME!
if ($NeedSave) {
print "We gotta save first dude!\n\n";
my $text = "You have unsaved changes, do you want to Save and Exit, Exit without Save, or return to editing the Season?";
my $dialog = $top->DialogBox(-title => "Unsaved changes!",
-buttons => [ 'Save and Exit',
'Exit without Save',
'Cancel', ],
-default_button => 'Cancel');
$dialog->add('Label', -text => $text, -width => '30');
my $ok = 1;
while ($ok) {
my $answer = $dialog->Show( );
return if ($answer eq "Cancel");
&do_exit($dialog,$top) if ($answer eq "Exit without Save");
if ($answer eq "Save and Exit") {
$game_file = &save_season_file_as($top, $game_file,
\@teams,\@matches,\%standings,\%season);
# Only exit if we actually picked a filename to save to...
if ($game_file ne "") {
&do_exit($dialog,$top);
}
}
}
}
else {
&do_exit($top);
}
}
#---------------------------------------------------------------------
sub do_exit {
foreach my $top (@_) {
$top->destroy;
}
&Tk::exit;
}
#---------------------------------------------------------------------
# Crying out to be object oriented. Used by display widgets to enter
# weekly results.
sub init_matches_per_week {
# Matches per-week that are played.
my $num = shift @_;
my %t;
for (my $m=1; $m <= $num; $m++) {
$t{$m}->{HomeScore} = "";
$t{$m}->{HomeCoed} = 0;
$t{$m}->{HomePoints} = "";
$t{$m}->{AwayScore} = "";
$t{$m}->{AwayCoed} = 0;
$t{$m}->{AwayPoints} = "";
$t{$m}->{PointsLabels} = ();
$t{$m}->{Type} = "G";
}
return %t;
}
#---------------------------------------------------------------------
# Decodes a date and returns the number of days from Jan 1, 1970.
sub my_dtd {
my $d = shift @_;
return 0 if ($d eq "");
#print " my_dtd($d)\n";
my @a = Decode_Date_US($d);
return(Date_to_Days($a[0],$a[1],$a[2]));
}
#---------------------------------------------------------------------
# sort matches by week then time then field
sub byweektimefield {
$a->{Week} <=> $b->{Week} ||
$a->{Time} cmp $b->{Time} ||
$a->{Field} cmp $b->{Field};
}
#---------------------------------------------------------------------
# sort matches by date then time then field
sub bydatetimefield {
$a <=> $b ||
$a->{DTD} <=> $b->{DTD} ||
$a->{Time} cmp $b->{Time} ||
$a->{Field} cmp $b->{Field};
}
#---------------------------------------------------------------------
# If $new is blank, update all dates... I think.
sub updateweekdates {
my $week = shift;
my $old = shift;
my $new = shift;
print "updateweekdates($week, \"$old\",\"$new\")\n";
# check for bogus inputs
if ($new eq "" and $old eq "") {
return 0;
}
my %dates;
my $found=0;
foreach my $match (sort bydatetimefield @matches) {
if ($match->{"Date"} eq $old && $match->{"Week"} == $week) {
$match->{"Date"} = "$new";
# Make sure we update the optimzed sort
$match->{"DTD"} = &my_dtd($match->{'Date'});
$match->{"DateOrig"} = "$old";
print " match->{Date} = ", $match->{Date}, "\n";
$found++;
}
}
return $found;
}
#---------------------------------------------------------------------
# Input: Week Number
# Return: Date of the matches that week.
# Notes: All matches are on the same day.
sub week2date {
my $w = shift;
foreach my $match (sort bydatetimefield @matches) {
if ($match->{"Week"} == $w) {
my $d = $match->{Date};
#print "Week = $w, Date = $d\n";
return $d;
}
}
}
#---------------------------------------------------------------------
# Input: Date
# Return: Week number
# Notes: All matches are on the same date.
sub date2week {
my $d = shift;
foreach my $match (sort bydatetimefield @matches) {
if ($match->{"Date"} eq $d) {
my $w = $match->{Week};
#print "Week = $w, Date = $d\n";
return $w;
}
}
# Error condition...
return 0;
}
#---------------------------------------------------------------------
# Checks a global array of dates which are not allowed to be used.
# Might need to be changed into a callback which pulls out the dates
# to dis-allow instead, but that's easy to do...
sub dateentry_cfg {
my(%args) = @_;
if ($args{-date}) {
my($day,$month,$year) = @{ $args{-date} };
my $dw = $args{-datewidget};
my $grep = scalar(grep(m/$month\/$day\/$year/, @blockdates));
if ($grep != 0) {
if (defined $dw) {
$dw->configure(-state => 'disabled');
}
}
}
}
#---------------------------------------------------------------------
# Change the date of a match
sub match_reschedule {
my $top = shift;
my $old_date = shift;
my $week = &date2week($old_date);
print "match_reschedule($old_date)\n";
# Update the blockdates before we reschedule.
foreach my $i (&get_match_dates) {
push @blockdates, $i->[1];
}
my $new_date = "";
my $dialog = $top->DialogBox(-title => "Reschedule Match Date",
-buttons => [ 'Ok', 'Cancel' ],
-default_button => 'Ok');
$dialog->add('Label', -text => "Old Date: $old_date")->pack(-side => 'top');
$dialog->add('Label', -text => "New Date (MM/DD/YYYY)")->pack(-side => 'left');
$dialog->add('DateEntry', -textvariable => \$new_date,
-width => 12,
-configcmd => \&dateentry_cfg)->pack(-side => 'left');
my $ok = 1;
while ($ok) {
my $answer = $dialog->Show( );
if ($answer eq "Ok") {
print "New Date = $new_date\n";
if ($new_date =~ m/^\d\d\/\d\d\/\d\d\d\d$/) {
$ok--;
# Gotta be careful here... don't allow matches on an existing
# date! Or at least also use week number to keep them sane...
# BUT! We can allow them on Makeup days or Makeup/Playoff days.
my $num_matches = &updateweekdates($week,$old_date,$new_date);
&update_datelist($match_datelist);
}
else {
$top->messageBox(
-title => "Error! Bad Date format.",
-message => "Error! Bad Date format, please use MM/DD/YYYY.",
-type => 'Ok',
);
}
}
elsif ($answer eq "Cancel") {
print "No update made.\n";
$ok--;
}
}
}
#---------------------------------------------------------------------
sub error_msg {
my $msg = shift;
print "MSG: $msg\n";
}
#---------------------------------------------------------------------
# Validates the data entered when building a new season.
sub validate {
my $num = shift @_;
my $start = shift @_;
my $descrip = shift @_;
my $tref = shift @_;
my @t_entry = @$tref;
if ($start eq "") {
error_msg("Need a start date.");
return 0;
}
elsif ($descrip eq "") {
error_msg("Need a Season Description.");
return 0;
}
else {
my $cnt=0;
foreach my $i (@t_entry) {
$cnt++ if ($i->get ne "");
}
if ($cnt < $num) {
error_msg("You entered $cnt team names, you need at least $num.");
return 0;
}
}
return 1;
}
#---------------------------------------------------------------------
# Make sure all dates are mm/dd/yyyy, but we're not too hot on error
# checking here...
sub fix_week_date_fmt {
my $orig = shift;
my $new = $orig;
my ($y,$m,$d);
# Returns Y,M,D or empty list if error.
if (($y,$m,$d) = Decode_Date_US($orig)) {
$new = sprintf("%02s/%02s/%4s",$m,$d,$y);
}
return $new;
}
#---------------------------------------------------------------------
# Takes a date string and returns the date seven days on in MM/DD/YYYY
sub inc_by_week {
my $cur = shift;
my ($y,$m,$d) = Decode_Date_US($cur);
#print " inc_by_week($y/$m/$d) + 7d = ";
($y,$m,$d) = Add_Delta_Days($y,$m,$d,7);
#print " ($y/$m/$d)\n";
$cur = sprintf("%02s/%02s/%4s",$m,$d,$y);
return $cur;
}
#---------------------------------------------------------------------
sub check_holidays {
my $cur = shift;
my @hols = @_;
my $ishol = 0;
my ($cy,$cm,$cd) = Decode_Date_US($cur);
foreach my $h (@hols) {
my ($hy,$hm,$hd) = Decode_Date_US($h);
if (Delta_Days($cy,$cm,$cd,$hy,$hm,$hd) == 0) {
$ishol = 1;
}
}
#print "check_holidays($cur) = $ishol\n";
return $ishol;
}
#---------------------------------------------------------------------
# takes in a number of teams and a hash of team names and randomizes
# them. This looks involved, but it's because I pass in a hash of
# entry values, which I want to randomize...
sub randomize_teams {
my $ref = shift;
my @entry = @$ref;
my @h;
print "randomize_teams( ... )\n" if $DEBUG;
foreach my $e (@entry) {
my $g = $e->get;
push @h, $g unless $g eq "";
$e->delete(0,length($g));
}
for (my $i = 5; $i>0; $i--) {
@h = shuffle @h;
}
foreach my $e (@entry) {
$e->insert(0,shift @h || "");
}
}
#---------------------------------------------------------------------
# add_holiday
sub add_holiday {
my $win = shift;
my $holsref = shift;
my $holref = shift;
my $hlb = shift;
print "add_holiday()\n";
my %hols = %$holsref;
print " Holiday: $holref\n";
}
#---------------------------------------------------------------------
# del_holiday
sub del_holiday {
my $win = shift;
my $holsref = shift;
my $holref = shift;
my $hlb = shift;
print "del_holiday()\n";
}
#---------------------------------------------------------------------
sub teams_rename {
my $top = shift;
my $ref = shift;
my @ts = @$ref;
print "teams_rename()\n";
my $win = MainWindow->new();
$win->title("Rename Team");
$win->configure(-height => 400,
-width => 400,
-background => $default_background,
);
$win->optionAdd('*font*', $default_font);
my $setup_fr = $win->Frame(-borderwidth => 1, -relief => 'solid');
for(my $i = 0; $i <= $#ts; $i++) {
next if (!defined $ts[$i]);
$setup_fr->Entry(-textvariable => \$ts[$i], -width => '20')->
pack(-side => 'top');
}
$setup_fr->pack(-side => 'top', -fill => 'x');
my $but_fr = $win->Frame(-borderwidth => 1, -relief => 'solid');
my $done_but = $but_fr->Button(-text => "Done", -command => [ $win => 'destroy' ]);
my $cancel_but = $but_fr->Button(-text => "Cancel", -command => [ $win => 'destroy' ]);
# Spacer frames
$but_fr->Frame(-borderwidth => 0, -relief => 'flat')->pack(-side => 'left', -expand => 1);
$done_but->pack(-side => 'left', -fill => 'x');
$but_fr->Frame(-borderwidth => 0, -relief => 'flat')->pack(-side => 'left', -expand => 1);
$cancel_but->pack(-side => 'left', -fill => 'x');
$but_fr->Frame(-borderwidth => 0, -relief => 'flat')->pack(-side => 'left', -expand => 1);
$but_fr->pack(-side => 'top', -fill => 'x');
}
#---------------------------------------------------------------------
# Takes current date, returns date of NEXT match, or "" if none.
sub get_next_match_date {
my $cur_date = shift @_;
print "get_next_match_date($cur_date) = ";
my $found = 0;
foreach my $m (sort bydatetimefield @matches) {
# We will find four matches before we fall through...
if ($cur_date eq "$m->{Date}") {
$found++;
}
# We found a match, so this should be the first new date.
elsif ($found > 0) {
print "$m->{Date}\n";
return $m->{Date};
}
}
print "<empty>\n";
return "";
}
#---------------------------------------------------------------------
# Accessor for date info stored in each match. Returns an array of
# arrays with date(s) for all matches, sorted by date. Stupid format.
# FIXME
sub get_match_dates {
my %wks;
my $w;
my @t;
print " get_match_dates()\n";
foreach my $m (@matches) {
$w = $m->{'Week'};
# FIXME: piss-poor inefficient....
$wks{$w}{COMP} = &all_matches_complete($w);
$wks{$w}{T} = $m->{'Type'};
$wks{$w}{D} = $m->{'Date'};
# Optimize out Date::Calc Date_to_Days calls a bit.
$wks{$w}{DTD} = &my_dtd($m->{'Date'});
if (defined $m->{"DateOrig"}) {
$wks{$w}{O} = $m->{'DateOrig'};
} else {
$wks{$w}{O} = " ";
}
}
# Returning an array of crap... should just be dates, used to index
# into the @matches array.
foreach my $k (sort { $wks{$a}->{DTD} <=> $wks{$b}->{DTD} } keys %wks) {
push @t, [ $k, $wks{$k}{D}, $wks{$k}{O}, $wks{$k}{T},$wks{$k}{COMP} ];
}
return @t;
}
#---------------------------------------------------------------------
sub datelist_browse {
my $hl = shift;
my ($path) = (@_);
my $week = $hl->itemCget($path,0,-text);
my $date = $hl->itemCget($path,1,-text);
my $old = $hl->itemCget($path,2,-text);
#print "hl_browse($path) (week = $week, date = $date)\n";
&update_scores($date);
# Hack to try and update the matchlist when a set of matches is
# completed, or updated to NOT be complete any more.
#&update_datelist($match_datelist);
}
#---------------------------------------------------------------------
sub update_datelist {
my $hl = shift;
print "update_datelist()\n";
# Colors. Wish I could change colors of box borders...
$dls_red = $hl->ItemStyle('text', -foreground => '#800000', -background => $default_background);
$dls_blue = $hl->ItemStyle('text', -foreground => '#000080', -background => $default_background, -anchor=>'w');
$dls_green = $hl->ItemStyle('text', -foreground => 'green', -background => $default_background, -anchor=>'w');
$dls_done = $hl->ItemStyle('text', -background => 'lightgreen');
$hl->delete('all');
foreach my $key (&get_match_dates) {
#print " get_match_dates: ". join(", ",@$key) . "\n";
my $e = $hl->addchild("");
$hl->itemCreate($e, 0, -itemtype=>'text', -text => $key->[0], -style=>$dls_red);
$hl->itemCreate($e, 1, -itemtype=>'text', -text => $key->[1], -style=>$dls_blue);
$hl->itemCreate($e, 2, -itemtype=>'text', -text => $key->[2], -style=>$dls_blue);
$hl->itemCreate($e, 3, -itemtype=>'text', -text => $key->[3], -style=>$dls_blue);
# If the scoring is complete, color it done.
if ($key->[4]) {
$hl->itemConfigure($e, 0, -style => $dls_done);
$hl->itemConfigure($e, 1, -style => $dls_done);
$hl->itemConfigure($e, 2, -style => $dls_done);
$hl->itemConfigure($e, 3, -style => $dls_done);
}
}
}
#---------------------------------------------------------------------
sub init_datelist {
my $top = shift;
print "init_datelist()\n";
my @widths = ( 6, 16, 16, 6);
my $tw = 0;
foreach (@widths) {$tw += $_;}
my $hl = $top->Scrolled('HList', -scrollbars => 'ow',
-columns=> $#widths+1, -header => 1,
-height => 15,
-selectmode => 'single', -width => $tw,
)->pack(-fill => 'y');
$hl->configure(-browsecmd => [ \&datelist_browse, $hl ]);
$hl->header('create', 0, -itemtype => 'text', -text => "Week");
$hl->columnWidth(0, -char => $widths[0]);
$hl->header('create', 1, -itemtype => 'text', -text => "Date");
$hl->columnWidth(1, -char => $widths[1]);
$hl->header('create', 2, -itemtype => 'text', -text => "Old Date");
$hl->columnWidth(2, -char => $widths[2]);
$hl->header('create', 3, -itemtype => 'text', -text => "Type");
$hl->columnWidth(3, -char => $widths[3]);
&update_datelist($hl);
return $hl;
}
#---------------------------------------------------------------------
sub init_standings {
my $top = shift;
print "init_standings()\n";
&zero_standings(\%standings);
my $f = $top->Frame;
# Header
my $ff = $f->Frame(-pady => 10, -border => 1);
$ff->Label(-text => "#", -width => 2)->pack(-side => 'left');
$ff->Label(-text => "Team", -width => 20, -anchor => 'w')->pack(-side => 'left');
$ff->Label(-text => " W ", -width => 4)->pack(-side => 'left');
$ff->Label(-text => " T ", -width => 4)->pack(-side => 'left');
$ff->Label(-text => " L ", -width => 4)->pack(-side => 'left');
$ff->Label(-text => " F ", -width => 4)->pack(-side => 'left');
$ff->Label(-text => " C ", -width => 4)->pack(-side => 'left');
$ff->Label(-text => " GF", -width => 4)->pack(-side => 'left');
$ff->Label(-text => " GA", -width => 4)->pack(-side => 'left');
$ff->Label(-text => "PEN", -width => 4)->pack(-side => 'left');
$ff->Label(-text => "PTS", -width => 4)->pack(-side => 'left');
$ff->Label(-text => "Rank", -width => 4)->pack(-side => 'left');
$ff->pack(-side => 'top', -fill => 'x');
# We have to go by the MAXIMUM number of teams that could play.
foreach (my $x=1; $x <= $max_numteams; $x++) {
my $ff = $f->Frame()->pack(-side => 'top', -fill => 'x');
$ff->Label(-text => $x, -width => 2)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{TEAM}, -width => 20)->pack(-side => 'left');
$standings{$x}->{TEAM} = $teams[$x];
$ff->Label(-textvariable => \$standings{$x}->{W}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{T}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{L}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{F}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{C}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{GF}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{GA}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{PCNT}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{PTS}, -width => 4)->pack(-side => 'left');
$ff->Label(-textvariable => \$standings{$x}->{RANK}, -width => 4)->pack(-side => 'left');
$ff->pack(-side => 'top', -fill => 'x');
}
$f->pack(-side => 'top', -fill => 'x');
}
#---------------------------------------------------------------------
# Used to both initialize and reset standings when updated.
sub zero_standings {
if (@_) {
my $ref = shift;
for (my $t=1; $t <= $numteams; $t++) {
foreach my $k (qw( W L T F C PTS GF GA RANK)) {
$$ref{$t}->{$k} = 0;
}
}
}
else {
for (my $t=1; $t <= $numteams; $t++) {
foreach my $k (qw( W L T F C PTS GF GA RANK)) {
$standings{$t}->{$k} = 0;
}
}
}
}
#---------------------------------------------------------------------
sub update_standings {
my $date = shift;
my %tmp;
print "update_standings($date)\n";
# Zero out the standings first
&zero_standings(\%standings);
&zero_standings(\%tmp);
# Save the current week data back to @matches
# FIXME!
&save_curmatch($curdate);
# Now go through all matches and figure out standings.
foreach my $m (sort bydatetimefield @matches) {
my $matchdate = $m->{"Date"};
#print "m->{Type} = ", $m->{"Type"}, "\n";
if ($m->{"Type"} eq "G" && &my_dtd($matchdate) <= &my_dtd($curdate)) {
# Do we have full scores recorded for this match yet?
if ($m->{"Complete"}) {
my $h = $m->{"Home"};
my $a = $m->{"Away"};
#print " Match Complete: H: $h, A: $a\n";
if ($m->{HomeScore} eq "F" && $m->{AwayScore} eq "F") {
#print " Double Forfeit\n";
$tmp{$h}->{F}++;
$tmp{$a}->{F}++;
}
elsif ($m->{HomeScore} eq "F") {
#print " Home forfeit\n";
$tmp{$h}->{F}++;
$tmp{$a}->{W}++;
$tmp{$a}->{GF} += 5;
}
elsif ($m->{AwayScore} eq "F") {
#print " Away Forfeit\n";
$tmp{$a}->{F}++;
$tmp{$h}->{W}++;
$tmp{$h}->{GF} += 5;
}
else {
# reset scores and such...
#print " HS: $m->{HomeScore}, AS: $m->{AwayScore}\n";
$tmp{$h}->{GF} += $m->{HomeScore};
$tmp{$a}->{GA} += $m->{HomeScore};
$tmp{$h}->{GA} += $m->{AwayScore};
$tmp{$a}->{GF} += $m->{AwayScore};
if ($m->{HomeScore} < $m->{AwayScore}) {
$tmp{$h}->{L}++;
$tmp{$a}->{W}++;
}
elsif ($m->{HomeScore} == $m->{AwayScore}) {
$tmp{$h}->{T}++;
$tmp{$a}->{T}++;
}
elsif ($m->{HomeScore} > $m->{AwayScore}) {
$tmp{$h}->{W}++;
$tmp{$a}->{L}++;
}
}
$tmp{$h}->{C} += $m->{HomeCoed};
$tmp{$a}->{C} += $m->{AwayCoed};
#print " HomePoints = $m->{HomePoints}\n";
$tmp{$h}->{PTS} += $m->{HomePoints};
$tmp{$a}->{PTS} += $m->{AwayPoints};
}
}
}
# Now sort %tmp and update %standings
my $x = 1;
foreach my $idx (sort {
$tmp{$b}->{PTS} <=> $tmp{$a}->{PTS} ||
$tmp{$b}->{W} <=> $tmp{$a}->{W} ||
$tmp{$b}->{L} <=> $tmp{$a}->{L} ||
$tmp{$b}->{T} <=> $tmp{$a}->{T} ||
($tmp{$b}->{GF} - $tmp{$b}->{GA}) <=> ($tmp{$a}->{GF} - $tmp{$a}->{GA});
} keys %tmp) {
#print "$idx $teams{$idx} $tmp{$idx}->{PTS}\n";
foreach my $k (qw( W L T F C PTS GF GA RANK)) {
$standings{$x}->{$k} = $tmp{$idx}->{$k};
}
$standings{$x}->{TEAMNUM} = $idx;
$standings{$x}->{TEAM} = $teams[$idx];
$x++;
}
}
#---------------------------------------------------------------------
# Returns 1 if all matches in a week are complete, 0 otherwise.
sub all_matches_complete {
my $week = shift;
my $complete;
# This is inefficient, we should have a data structure tracking
# this info instead. Maybe when we go object oriented?
# How about a counter called 'completed' which would be quicker to check?
foreach my $m (sort bydatetimefield @matches) {
# We only care about $week, so skip any others, and return the
# results once we find the right week.
if ($m->{"Week"} == $week) {
$complete = 0;
for (my $i=1; $i <= $matches_per_week; $i++) {
$complete = $complete + $m->{"Complete"};
}
if ($complete == $matches_per_week) {
return 1;
}
return 0;
}
}