-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbeopardy
executable file
·1421 lines (1234 loc) · 33.5 KB
/
beopardy
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
# vim:set ts=4 sw=4 cin sm:
# This code is under the BSD Licence
# (c) by Stefan `Sec` Zehl <[email protected]>
use constant beopardy =>
q$Id: beopardy,v 1.16 2006/12/23 16:09:21 sec Exp sec $;
use strict;
use Tk;
use Tk::X11Font;
use Tk::Dialog;
use Tk::JPEG;
use Tk::PNG;
use Socket;
use FileHandle;
use Getopt::Std;
use IO::Handle;
use Encode;
binmode(STDOUT, ":utf8");
# Global options. Set via getopt.
my $debug=0;
my $override=0; # Ignore windowmanager?
my $force=0; # move focus for kbd-mode?
my $tty=0; # use tty/serial input?
my $splash=0; # Display a splashscreen?
my $soundd=0; # Use Soundd-output?
my $mood=0; # Publish mood?
my $socket=0; # tty emulated by tcp socket?
my $geometry=0; # How big should I be?
my $readfile; # Should I read from a file?
my $questfile="Jeopardy"; # Where are the questions?
my $savefile=0; # Should I save to a file?
my $global_question_inhibit=0;
my $global_is_dbl=0;
my $global_dbl_maxamt=0;
my $global_dbl_amt=0;
my $global_dbl_ply=0;
# L10n
my %ln = (
"Richtig" => "right",
"Falsch" => "wrong",
"Oops" => "oops",
"ist etwas unruhig" => "seems a bit nervous",
"Knopf loesen" => "release your button",
"nochmal" => "try again",
"Zuhören..." => "listen...",
);
sub _ {
my $key=shift;
return $key; # Deutsch-Mode.
if (defined ($ln{$key})){
return $ln{$key};
}else{
return $key;
};
};
my %opt;
getopts('ydoftsg:r:hwbmi:', \%opt);
# Possible screen sizes
my %screen=( 320 => 200,
640 => 480,
800 => 600,
1024 => 786);
my @beopardy = split(/ /,beopardy);
if (defined $opt{h}){
print <<EOF;
This is Beopardy $beopardy[2] $beopardy[3].
beopardy -options Gamefile Player1 Player2 ...
-h This help.
-d Debug mode. Print lots'o stuff.
-o Override. Ignore Windowmanager. (fullscreen)
-f Force keyboard focus. (fullscreen)
-t Tty input. Use when Buzzers are connected.
-s Socket. Emulate Tty input via tcp socket (port 3333)
-b Beopardy splashscreen. Default for serial input.
-r <file> Read saved game from File.
-i <file> Read questions from File.
-w Save game progress for reading with -r.
-y Contact soundd at localhost 32001
-m Moodlamp support. Works via soundd (requires -y)
-g <size> geometry. Select window size.
0: fullscreen (default)
EOF
my $x=1;
foreach (sort {$a <=> $b } keys %screen){
printf "\t\t%1d: %4dx%4d\n",$x++,$_,$screen{$_};
}
print "";
exit(42);
}
$debug=1 if (defined $opt{d});
$override=1 if (defined $opt{o});
$force=1 if (defined $opt{f});
$tty=1 if (defined $opt{t});
$socket=1 if (defined $opt{s});
$savefile=1 if (defined $opt{w});
$splash=1 if (defined $opt{b});
$soundd=1 if (defined $opt{y});
$mood=1 if (defined $opt{m});
if($mood && !$soundd){
die "Mood support requires soundd!";
};
if($opt{r}){ # Read savefile
if ( -f $opt{r} ){
$readfile=$opt{r};
};
};
if($opt{i}){ # Change questfile
if ( -f $opt{i} ){
$questfile=$opt{i};
}elsif( -f "Jeopardy.$opt{i}"){
$questfile="Jeopardy.$opt{i}";
}else{
die "Can't find game: $opt{i}";
}
}
if ($socket){$tty=1}; # socket emulates tty.
if ($tty){$splash=1}; # tty requires splash.
my $tl = MainWindow -> new -> toplevel;
$tl->appname(beopardy);
$tl->bind('<M>',sub{ snd_play("start"); });
my ($width,$height)=($tl->screenwidth,$tl->screenheight); # Size of game field.
if (defined $opt{g}){
if ($opt{g} =~ /^(\d+)[x*](\d+)$/){
($width,$height)=($1,$2);
} elsif ( defined $screen{$opt{g}}){
($width,$height)=($opt{g},$screen{$opt{g}});
}else{
my @x = (sort {$a<=>$b} keys %screen);
if ((--$opt{g}<=$#x) && ($opt{g}>=0 )){
$width=$x[$opt{g}];
$height=$screen{$width};
};
};
}
my $q=5; # Wieviele Fragen/Kategorie?
my $qwidth=35; # Width of a question.
my $catwidth =10; # Width of categories.
my $namewidth=10; # Width of player names
if ($tty){
print "Opening tty\n";
if($socket){
my $port = 3333;
my $proto = getprotobyname('tcp');
socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
|| die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!" ;
listen(Server,SOMAXCONN) || die "listen: $!";
print "Now connect to port $port...\n";
my $paddr = accept(Client,Server);
my($iport,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);
print "connection from $name [", inet_ntoa($iaddr), "] at port $iport\n";
}else{
# Be sure to set device to -crtscts 19200
open(Client,"+>/dev/ttyUSB0") || die "open: $!";
# use IO::Stty;
# IO::Stty::stty(\*Client,"-crtscts");
# IO::Stty::stty(\*Client,"19200");
# IO::Stty::stty(\*Client,"-echo");
# IO::Stty::stty(\*Client,"-icrnl");
};
print "done.\n";
autoflush Client;
};
my $snd_save; # resume sound..
if($soundd){
my $port = 32001;
my $proto = getprotobyname('tcp');
socket(Soundd, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
my $iaddr = inet_aton("localhost");
my $paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
connect(Soundd, $paddr) || die "connect: $!";
autoflush Soundd;
};
&snd_stop(0); # Just to make sure, and to install snd_eat.
&mood("boot");
print "Reading questions....\n";
my %jdata;
open (J,"<:encoding(UTF-8)",$questfile) || die;
my ($nam,$c);
while (<J>){
chomp;
next if ((!defined $nam) && (!/^>/));
next if /^\s*(#|$)/;
next if /^------*$/;
if (/^>(.*)/){
if(defined $nam){
printf "%-20s:%2d\n",$nam,$c if ($debug);
};
$nam=$1;
$c=0;
next;
}
$_.=" ";
if (!s/(?<!\\)\\n/\n/g){
s/(.{10,$qwidth})\s+/$1\n/mg;
};
s/\\\\/\\/mg;
$jdata{$nam}[++$c]=$_;
};
close(J);
printf "%-20s:%2d\n",$nam,$c if ($debug);
print "Totalling ",(scalar keys %jdata)," categories.\n";
sub logname{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year+=1900;$mon++;
return (sprintf "%04d%02d%02d.%02d%02d",$year,$mon,$mday,$hour,$min);
}
sub read_game{
my @Cat; # Namen der Kategorien
my $q=shift; # Wieviele Fragen/Kategorie?
my $gamefile=shift(@ARGV)||"Test.jg";
$gamefile .= ".jg" if ( -f $gamefile.".jg" );
$gamefile =~ /^([^.]+)(.jg)?/;
my $title=$1; # Titel des Spielfelds.
print "\nReading game '$title' ...\n";
open(G,"<:encoding(UTF-8)","$gamefile") || die;
while (<G>){
chomp;
next if (/^\s*(#|$)/);
s/^>//;
push @Cat,$_;
};
close(G);
my $p=0;
for (@Cat){
printf "%-20s:%2d\n",$_,$#{$jdata{$_}} if ($debug);
if ($#{$jdata{$_}} < $q){
print "ERROR: not enough questions in \"$_\"\n";
$p++;
};
if ($#{$jdata{$_}} > $q){
print "WARN : too many questions in \"$_\"\n";
# $p++;
};
};
if ($p){
print "Hit enter to continue...\n";
$p=<STDIN>;
};
if($savefile){
my($ts)=logname();
open(SAV,">$gamefile.$$.$ts");
select(SAV);$|=1;
select(STDOUT);
}else{
open(SAV,">-");
};
return $title,@Cat;
};
# Titel und Fragen
my ($title,@Cat)=&read_game($q);
my @players;
if ($#ARGV>0){
@players=@ARGV;
}else{
@players=qw(Foo Bar Baz);
};
my @colors=qw(darkgrey darkred darkgreen darkblue darkcyan yellow);
@colors=qw(darkgrey darkred darkgreen darkcyan darkmagenta yellow);
unshift @players,"Nobody";
my @points;
# Here starts the Tk part...
my $qfont=$tl->X11Font('-*-arial-medium-r-*-*-60-*-*-*-*-*-iso8859-*');
print "Question-Font:\n$qfont\n" if ($debug);
my $tfont=$tl->X11Font('-*-verdana-medium-r-*-*-50-*-*-*-*-*-iso8859-*');
print "Title-Font:\n$tfont\n" if($debug);
my $sfont=$tl->X11Font('-*-arial-medium-r-*-*-25-*-*-*-*-*-iso8859-*');
print "Small-Font:\n$sfont\n" if($debug);
my $fixedfont=$tl->X11Font('-*-courier 10 pitch-medium-r-*-*-60-*-*-*-*-*-iso8859-*');
print "Fixed-Font:\n$fixedfont\n" if($debug);
my $sfixedfont=$tl->X11Font('-*-courier 10 pitch-medium-r-*-*-30-*-*-*-*-*-iso8859-*');
print "Small-Fixed-Font:\n$sfixedfont\n" if($debug);
my $utf8font=$tl->X11Font('-*-arial-medium-r-*-*-30-*-*-*-*-*-iso8859-*');
print "UTF8-Font:\n$utf8font\n" if($debug);
unless ($qfont and $tfont and $sfont ){
die "One or more fonts failed.\n";
};
$tl->configure(-height => $height, -width => $width);
$tl->resizable(0,0);
$tl->packPropagate(0); # Keep the size.
#$tl->overrideredirect(1) if ($override);
$tl->focus if ($override);
#$tl->fullscreen(1) if ($override);
$tl->attributes(-topmost => 'yes') if ($override);
$tl->attributes(-fullscreen => 'yes') if ($override);
#$tl->highlightthickness(0) if ($override);
#$tl->grabGlobal() if ($override);
# Title of Gamefield
my $tlabel = $tl -> Label (
-text => $title,
-relief => 'ridge',
-font => $tfont,
) -> pack(-fill => 'x');
$tl->eventAdd('<<quit>>'=>'<Button-3>');
$tl->eventAdd('<<quit>>'=>'<Q>');
$tl->bind('<<quit>>',sub{&snd_stop();print "Done:\n",map {sprintf "%10s:%5d\n",$players[$_],$points[$_]} sort {$points[$b]<=>$points[$a]} (1..$#points);exit});
#$tl->bind('<Button-2>',sub{$tl->focus;$tl->focusForce;$buttons[1][1]->focus};);
$tl->bind('<Z>',sub{&snd_zap()});
# Game-Buttons.
my $bframe=$tl->Frame->pack(-fill=>'both',-expand=>1);
my @button; # The TkButtons
my @pts; # Who got points from this question?
my @log; # Pointslog
my @frame; # The TkFrames, one per category.
my @mframe; # A frame per button to avoid stupid resizing.
for my $cat (0..$#Cat){
$frame[$cat]=$bframe->Frame(
-width => 1,
-height => 1,
)->pack(
-side =>'left',
-fill =>'both',
-expand => 1,
);
$frame[$cat]->packPropagate(0);
$button[$cat][0] = $frame[$cat]->Label(
-width => $catwidth,
-text => $Cat[$cat],
-font => $sfont,
)->pack(-fill => 'both');
for my $q (1..$q) {
$mframe[$cat][$q] = $frame[$cat]->Frame(
# height => 1
)-> pack( -fill => 'both', -expand => 1);
$mframe[$cat][$q]->packPropagate(0);
$button[$cat][$q] = $mframe[$cat][$q]->Button(
-text => "${q}00",
-command => [\&selectQuest,$tl,$cat,$q],
-font => $tfont,
-highlightcolor => "red",
)->pack( -fill => 'both', -expand => 1);
$button[$cat][$q]->bind('<h>',[\&moveCrsr,$cat-1,$q ]);
$button[$cat][$q]->bind('<j>',[\&moveCrsr,$cat ,$q+1]);
$button[$cat][$q]->bind('<k>',[\&moveCrsr,$cat ,$q-1]);
$button[$cat][$q]->bind('<l>',[\&moveCrsr,$cat+1,$q ]);
$button[$cat][$q]->bind('<E>',[\&edit_pts,$cat ,$q ]);
$button[$cat][$q]->bind('<F>',[\&finish]);
$button[$cat][$q]->bind('<R>',[\&randomply]);
$button[$cat][$q]->bind('<Y>',[\&ser_reset]);
$button[$cat][$q]->bind('<r>',[\&random_ray]);
# $button[$cat][$q]->bind('<FocusIn>',sub{
# $button[$cat][$q]->configure(-border=>10)});
# $button[$cat][$q]->bind('<FocusOut>',sub{
# $button[$cat][$q]->configure(-border=>2)});
# Fuer mh, der die VI-Keys nicht mag...
$button[$cat][$q]->bind('<Left>' ,[\&moveCrsr,$cat-1,$q ]);
$button[$cat][$q]->bind('<Down>' ,[\&moveCrsr,$cat ,$q+1]);
$button[$cat][$q]->bind('<Up>' ,[\&moveCrsr,$cat ,$q-1]);
$button[$cat][$q]->bind('<Right>',[\&moveCrsr,$cat+1,$q ]);
};
};
$button[0][1]->focus;
#$button[0][1]->focusForce if ($force);
@points=(0)x(4);
if($#players>3){
@points=(0)x($#players+1);
};
sub scoreboard(){ # Scoreboard.
my $sframe=$tl->Frame->pack(-side=>'top',-fill=>'x');
my @pborder;
my @pframes;
my @pnames;
my @pscores;
for (1..$#players){
if ($_ == $#players){
$pborder[$_]=$sframe;
}else{
$pborder[$_]=$sframe->Frame->pack(
-side =>'left',
-fill =>'x',
-expand =>1,
);
};
$pframes[$_]=$pborder[$_]->Label( -relief=>'ridge')->pack( -side=>'left');
$pnames[$_] =$pframes[$_]->Label(
-width =>$namewidth,
-font => $sfont,
-anchor =>'w',
-textvar => \$players[$_],
-background => $colors[$_],
-foreground => "white",
)->pack;
$pscores[$_]=$pframes[$_]->Label(
-textvar => \$points[$_],
-font => $sfont,
-anchor =>'e',
)->pack(-fill=>'x');
};
print SAV "[ply] ",join("/",@players),"\n";
return $sframe;
};
#print "Board done.\n";
if(defined $readfile){
my($c,$f,$sgn,$ply);
print "Reading Savegame...\n";
open(RSAV,$readfile) || die "Savegame error: $!";
while(<RSAV>){
if (/^\[ply\]/){
(undef,$ply)=split;
@players=split(/\//,$ply);
# unshift @players,"Nobody";
next;
};
if(/^\[DBL\] (-?\d+)/){
$global_dbl_amt=$1;
$global_is_dbl=1;
};
next if(! /^\[sav\]/);
(undef,$c,$f,$sgn,$ply)=split;
print "$c - $f - $sgn - $ply\n";
updPlayfield($c,$f,$ply,$sgn);
};
close(RSAV);
};
my $sframe=scoreboard();
print "\nGame start.\n\n";
my @elist;
my $dlgbtn;
my $splash_frame;
$tl->waitVisibility; # To fix stacking order
if($splash){ # Splash-Screen
my $dlg=$tl->Toplevel;
my $i;
my $rframe;
# $dlg->configure(-height => $height, -width => $width);
# $dlg->resizable(0,0);
# $dlg->packPropagate(0); # Keep the size.
$dlg->overrideredirect(1);
my $q=$dlg->Label(qw/-relief raised -width 80/)->pack;
$dlg->bind('<M>',sub{
snd_play("start");
});
$i= $q->Pixmap("beopardy",-file => "img/beopardy.xpm");
if(defined($i)){
$q->Label(-image=>"beopardy")->pack(-side=>"top");
};
$rframe=$q->Frame->pack(-fill=>'both',-expand=>1);
my $f = $rframe;
$splash_frame=$f;
my $row=0;
foreach (1..$#players) {
# my $e = $f->Entry(qw/-relief sunken -width 40/);
my $e = $f->Entry(-relief => "sunken",
-font => $sfont,
-width => 30);
$e->insert(0,$players[$_]);
if($tty){
$e->bind('<Return>',[\&ser_reset,\&ser_PlyEn]);
}else{
$e->bind('<Return>',[\&ply_focus,$_+1]);
};
$e->bind('<FocusOut>',[\&set_ply,$_]);
my $l = $f->Label(-text => "Player $_",
-width => $namewidth,
-background => $colors[$_],
-foreground => "white",
-font => $sfont,
-anchor => 'e', -justify => 'right');
Tk::grid( $l, -row => $row, -column => 0, -sticky => 'e');
Tk::grid( $e, -row => $row++, -column => 1,-sticky => 'ew');
$f->gridRowconfigure(1,-weight => 1);
$elist[$_]=$e;
}
$dlgbtn=$q->Button(
-text => "Start",
-font => $qfont,
# -width => 30,
-height => 2,
-command => sub {$splash=0;&snd_stop;&ser_reset if($tty);$button[0][1]->focus; $button[0][1]->focusForce if ($force);&mood("start");$dlg->destroy;
print SAV "[ply] ",join("/",@players),"\n";
},
)->pack(
-fill =>'x',
-expand =>1,
);
if($tty){
&ser_PlyEn;
}else{
&ply_focus(".",1);
};
$dlg->idletasks;
$dlg->geometry(sprintf "+%d+%d",($dlg->screenwidth-$dlg->width)/2,($dlg->screenheight-$dlg->height)/2);
$dlg->raise;
$dlg->grab;
$dlg->focusForce;
$tl->lower($dlg);
};
sub set_ply {
my ($crap,$key)=@_;
print "set_ply $key\n" if($debug);
$players[$key]=$crap->get;
};
sub ser_PlyEn {
if($tty){
$tl->fileevent(\*Client,'readable',[\&ser_SelectPly,@_]);
$dlgbtn->focus;
$dlgbtn->focusForce;
}else{
die "ser_en: $_[0]\n";
};
&mood("setup 0");
};
sub ply_focus {
my ($crap, $key)=@_;
&mood("setup $key");
if ($key>$#players){
if($tty && ($key==($#players+1))){
my $f=$splash_frame;
my $row=$key-1;
$players[$key]="New Player $key";
$points[$key]=0;
my $e = $f->Entry(-relief => "sunken",
-font => $sfont,
-width => 30);
$e->insert(0,$players[$key]);
$e->bind('<Return>',[\&ser_reset,\&ser_PlyEn]);
$e->bind('<FocusOut>',[\&set_ply,$key]);
my $l = $f->Label(-text => "Player $key",
-width => $namewidth,
-font => $sfont,
-background => $colors[$key],
-foreground => "white",
-anchor => 'e', -justify => 'right');
Tk::grid( $l, -row => $row, -column => 0, -sticky => 'e');
Tk::grid( $e, -row => $row++, -column => 1,-sticky => 'ew');
$f->gridRowconfigure(1,-weight => 1);
$elist[$key]=$e;
$elist[$key]->focus;
$elist[$key]->selectionRange(0,'end');
$sframe->destroy;
$sframe=scoreboard();
$sframe->idletasks;
return;
};
$dlgbtn->focus;
# $dlgbtn->focusForce if($force);
}else{
$elist[$key]->focus;
$elist[$key]->selectionRange(0,'end');
};
};
sub ser_SelectPly {
my ($crap)=@_;
my $key=<Client>;
print "SER<: <$key>\n" if($debug);
$key=~s/\r?\n$//;
$key=$key+0;
if($key == 0){
&ser_reset;
}else{
&ply_focus(".",$key);
&ser_dis;
};
};
# Make the 'resetting' window...
my $reset=$tl->Toplevel;
$reset->overrideredirect(1);
$reset->resizable(0,0);
$reset->geometry("-0+0");
$reset->withdraw;
$reset->Label(-text=>"resetting",-background=>"green")->pack;
$tl->after(100,\&ser_reset) if($tty);
MainLoop;
# We selected a question...
my %imgcache;
sub selectQuest{
my ($otl,$c,$f)=@_;
print "global_question_inhibit= $global_question_inhibit\n";
return if $global_question_inhibit;
print "Q$f / \"$Cat[$c]\":\n$jdata{$Cat[$c]}[$f]\n";
if( $jdata{$Cat[$c]}[$f] =~ s/\s*\[dbl\]\s*// && $global_is_dbl == 0){
$global_is_dbl=1;
&enterdbl;
return;
};
if($global_is_dbl == 1){
$_[3]->destroy;
};
my $tl = $otl->Toplevel;
$tl->fileevent(\*Client,'readable',"");
$tl->configure(-height => $height, -width => $width);
if(!$override){ # XXX my windowmanager is broken %)
$tl->geometry("+".($otl->rootx-28)."+".($otl->rooty-9));
};
$tl->resizable(0,0);
$tl->packPropagate(0); # Keep the size.
$tl->overrideredirect(1) if($override);
$tl->grab;
my $tlabel = $tl->Label(
-text => $Cat[$c],
-relief => 'ridge',
-font => $tfont,
) -> pack(-fill => 'x');
# XXX Pixmap-Hack. *funfunfun*
my ($typ,$fnam);
$typ="";
print $jdata{$Cat[$c]}[$f]," <-\n" if($debug);
if ($jdata{$Cat[$c]}[$f] =~ /^\[(img|snd):(.*?)\]\s*/){
# print "JA\n";
$typ=$1;$fnam=$2;
print "This is of $typ - \'$2\'\n";
if($typ eq "img"){
if(!defined($imgcache{$fnam})){
if(-f "img/".$fnam){
$imgcache{$fnam} = $tl->Photo($fnam,-file => "img/".$fnam);
}elsif(-f "img/".$fnam.".xpm"){
$imgcache{$fnam} = $tl->Pixmap($fnam,-file => "img/".$fnam.".xpm");
}else{
print "img/".$fnam." not found\n";
};
};
};
}
if ($typ ne "snd"){
&snd_play("think",1);
};
$snd_save=undef;
my $question;
if (defined($fnam) && defined($imgcache{$fnam})){
$question = $tl->Label(
-image => "$fnam",
);
}else{
my $txt=$jdata{$Cat[$c]}[$f];
my $magicfont=$qfont;
if ($txt =~ s/^\[fixed\]//){
$magicfont=$fixedfont;
};
if ($txt =~ s/^\[sfixed\]//){
$magicfont=$sfixedfont;
};
if ($txt =~ s/^\[utf8\]//){
# $txt=Encode::decode_utf8($txt);
};
if(defined($fnam)&&($typ eq "snd")){
if(!$soundd){$txt.="\n[ERR:Nosound]"};
&snd_play($fnam);
$snd_save=$fnam;
};
if($txt=~s/^\[snd:.*?\]\s*//){
if(!$txt){
$txt=_("Zuhören...");
};
};
if($txt=~s/^\[img:.*?\]\s*//){
if(!$txt){
$txt=_("[ERR:Noimg]");
};
};
$question = $tl->Label(
-text => $txt,
-font => $magicfont,
-justify => "left",
);
};
$question->pack(
-fill =>'both',
-expand =>1
);
$tl->focusForce if ($force);
$tl->bind('<S>',sub{&snd_stop()});
$tl->bind('<R>',sub{&snd_resume()});
$tl->bind('<Key>',[\&answerQuest,$c,$f,Ev('A')]);
$tl->waitVisibility; # To fix stacking order
&mood("question");
if($global_is_dbl){
$tl->afterIdle(sub {&ser_answerQuest($tl,$c,$f) if ($tty);});
}else{
$tl->afterIdle(sub {&ser_en($tl,$c,$f) if ($tty);}); # fix Bug#2
};
};
my @cur_ser_answer=undef;
sub ser_answerQuest {
my ($crap,$c,$f)=@_;
my $key;
if($global_is_dbl==1){
$key=$global_dbl_ply;
}else{
$key=<Client>;
};
&mood("player $key");
print "SER<: <$key>\n" if($debug);
&snd_stop if($snd_save);
$key=~s/\r?\n$//;
my $ich=$crap->Toplevel;
$ich->overrideredirect(1) if($override);
$ich->resizable(0,0);
$ich->geometry("-0-0");
my $ftl = $ich->Frame( -relief => 'ridge', -bd => 4)->pack;
$cur_ser_answer[0]=$ftl->Label(
-text => $players[$key],
-font => $qfont,
)->pack(
-fill =>'x',
-expand => 1,
);
my $bframe=$ftl->Frame->pack(-fill=>'both',-expand=>1);
my $br=$bframe->Button(
-text => _('Richtig'),
-command => sub{
&snd_stop;
&answerQuest($crap,$c,$f,$key)
},
)->pack(-side =>'left');
my $bf=$bframe->Button(
-text => _('Falsch'),
-command => sub{
$ich->destroy;
&answerQuest($crap,$c,$f,-$key);
&snd_resume;
$crap->focusForce if ($force);
&ser_en($crap,$c,$f)},
)->pack(-side =>'left');
my $bo=$bframe->Button(
-text => _('Oops'),
-command => sub{$ich->destroy;
&ser_reset;
# &snd_stop;
&mood("question");
&snd_resume;
$crap->focusForce if ($force);
&ser_en($crap,$c,$f)},
)->pack;
# $ich->bind('<q>',sub{$ich->destroy; &ser_reset; &ser_en($crap,$c,$f)});
# $ich->bind('<o>',sub{$ich->destroy; &ser_reset; &ser_en($crap,$c,$f)});
# $ich->bind('<r>',[\&answerQuest,$crap,$c,$f,$key]);
# $ich->bind('<f>',sub{$ich->destroy;&answerQuest($crap,$c,$f,-$key);&ser_en($crap,$c,$f)});
$br->bind('<l>',sub{$bf->focus});
$bf->bind('<l>',sub{$bo->focus});
$bo->bind('<l>',sub{$br->focus});
$br->bind('<h>',sub{$bo->focus});
$bf->bind('<h>',sub{$br->focus});
$bo->bind('<h>',sub{$bf->focus});
# mh ist auch hier faul :)
$br->bind('<Right>',sub{$bf->focus});
$bf->bind('<Right>',sub{$bo->focus});
$bo->bind('<Right>',sub{$br->focus});
$br->bind('<Left>' ,sub{$bo->focus});
$bf->bind('<Left>' ,sub{$br->focus});
$bo->bind('<Left>' ,sub{$bf->focus});
$br->focusForce if($force);
# print "ser_answer done\n";
};
sub ser_en{
$tl->fileevent(\*Client,'readable',[\&ser_answerQuest,@_]);
};
sub ser_dis{
$tl->fileevent(\*Client,'readable',[\&ser_noinp]);
};
sub mood{
return if (!$soundd);
return if (!$mood);
my($sound)=(shift);
print Soundd "M $sound\r\n";
Soundd->flush();
};
sub snd_play{
return if (!$soundd);
my($sound,$what)=(shift,shift);
print Soundd "P $sound\r\n";
Soundd->flush();
if($what){
$tl->fileevent(\*Soundd,'readable',[\&snd_color]); # DWIM!
}else{
$tl->fileevent(\*Soundd,'readable',[\&snd_eat]);
};
};
sub snd_stop{
return if (!$soundd);
print Soundd "Stop\r\n";
Soundd->flush();
$tl->fileevent(\*Soundd,'readable',[\&snd_eat]);
};
sub snd_zap{
return if (!$soundd);
print Soundd "Z\r\n";
Soundd->flush();
$tl->fileevent(\*Soundd,'readable',[\&snd_eat]);
};
sub snd_resume{
return if (!$soundd);
if($snd_save){
print Soundd "P $snd_save\r\n";
Soundd->flush();
};
$tl->fileevent(\*Soundd,'readable',[\&snd_eat]);
};
# User answered the Question.
sub answerQuest{
my ($crap,$c,$f,$key)=@_;
print "answered: $c $f $key\n";
my $sgn=1;
if(lc($key) =~ "q"){
&snd_stop(0);
&mood("board");
$crap->destroy;
&ser_dis if ($tty); # "input nach 'q' auf frage" fix.
$button[$c][$f]->focusForce if ($force);
$global_dbl_ply=0; # Nobody has the tag...
return;
};
&snd_stop() if ($key eq "0"); # XXX
my $pos;
$key=-$pos if (($pos=index('0!"#$%&/()',$key))>0);
$key=-3 if($key eq '§');
$key="0" if($key eq "`"); # Be nice on ami-kbd
$key="0" if($key eq "^"); # Be nice on german-kbd
print "->$key\n" if ($debug);
if($key=~/^-?\d$/){
if ($key<0) {
$sgn=-$sgn;
$key=-$key;
&mood("wrong");
}else{
&mood("right");
};
if ($key <= $#players){
&updPlayfield($c,$f,$key,$sgn);
$crap->destroy if ($sgn>0);
&ser_reset if ($tty);
$button[$c][$f]->focusForce if ($force && ($sgn>0));
# &snd_stop(0);
return;
};
};
print "I don't like this key ($key)...\n" if($debug);
};
sub updPlayfield {
my ($c,$f,$key,$sgn)=@_;
print $sgn<0?"bad":"good"," for ",$players[$key],"\n";
print SAV "[DBL] $global_dbl_amt\n" if ($global_is_dbl);
print SAV "[sav] $c $f $sgn $key\n";
push @{$log[$c][$f]},[$sgn,$key,$global_is_dbl?$global_dbl_amt:0];
if($global_is_dbl){
$points[$key]+=$sgn*$global_dbl_amt;
$global_is_dbl=0;
}else{
$points[$key]+=$sgn*$f*100;
};
if($sgn>0){
$global_dbl_ply=$key;
}; # cache player who has the tag....
updBox($button[$c][$f],$c,$f);
print "Pts:",(map {sprintf "%s:%d/",$players[$_],$points[$_]} (1..$#points)),"\n";
}
sub moveCrsr{
my ($widget,$c,$f)=@_;
$c=0 if($c>$#Cat);
$c=$#Cat if($c<0);
$f=1 if($f>$q);
$f=$q if($f<1);
$button[$c][$f]->focus;
return;
};
sub ser_reset{
print "Resetting...\n" if ($debug);
my $ok;
my $quux= $tl->focusSave();
$reset->deiconify;
$reset->raise;
$reset->grab;
do {
&ser_dis;
print Client "R\r\n";
Client->flush();
print "SER>: R\n" if($debug);
$ok=<Client>;
print "SER<: <$ok>\n" if($debug);
$ok=~s/\r?\n//;
if ($ok ne "A"){
if ($ok =~ /(\d)/) {
&ser_fatal($players[$1].", "._("Knopf loesen").".");
}else{
&ser_fatal("ser_reset got $ok");