/
Bpardy.pm
196 lines (171 loc) · 3.35 KB
/
Bpardy.pm
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
package Bpardy;
use warnings;
use strict;
use autodie;
use feature qw(switch);
our $game;
our $questions;
sub cat2id;
sub id2cat;
sub ply2id;
sub id2ply;
###
### small Helper functions
###
sub dbg {print "dummy-dbg: @_\n"};
sub setdebug{
no warnings 'redefine';
*Bpardy::dbg=shift;
};
sub err { # TODO: Need to think about error handling
dbg (@_);
die @_;
};
###
### main game logic
###
sub setup {
$game={
players => 3,
names => [qw(Foo Bar Baz)],
scores => [0,0,0],
};
};
sub boardsetup {
my ($quest,@cats)=@_;
$game->{cats}=\@cats;
my @gui;
for my $cat (0..$#cats+1){
for my $pts (0..$quest-1){
$gui[$cat][$pts]= {
state=>"free",
score=>($pts+1)*100
};
};
};
$game->{board}=\@gui;
return 1;
};
sub load { # Old-style load -- a bit Hacky.
my $gamefile=shift;
my $q=5; # Wieviele Fragen/Kategorie?
my %jdata;
my $qwidth=35; # Width of a question.
open (J,"<Jeopardy") || die;
my ($nam,$c);
while (<J>){
chomp;
next if ((!defined $nam) && (!/^>/));
next if /^\s*(#|$)/;
if (/^>(.*)/){
$nam=$1;
$c=0;
next;
}
$_.=" ";
if (!s/(?<!\\)\\n/\n/g){
s/(.{10,$qwidth})\s+/$1\n/mg;
};
s/\\\\/\\/mg;
$jdata{$nam}[++$c]=$_;
};
<J>;
close(J);
my @Cat; # Namen der Kategorien
$gamefile .= ".jg" if ( -f $gamefile.".jg" );
$gamefile =~ /^([^.]+)(.jg)?/;
my $title=$1; # Titel des Spielfelds.
&dbg ("Reading game '$title' ...");
open(G,"<$gamefile") || die;
while (<G>){
chomp;
next if (/^\s*(#|$)/);
push @Cat,$_;
};
<G>;
close(G);
boardsetup($q,@Cat);
my $p=0;
my $qid=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++;
};
for my $theq (0..$q-1){
$qid++;
$game->{board}[cat2id $_][$theq]{qid}=$qid;
if($jdata{$_}[$theq+1] =~ m!\[img:(.*)\]!){
$questions->{$qid}= {
type => "image",
url => $1.".png",
};
}else{
$questions->{$qid}= {
type => "text",
text => $jdata{$_}[$theq+1],
};
};
};
};
return "$title (".join("/",@Cat).")";
};
sub ask {
my $id=shift;
# TODO: dies the q exist at all?
return $questions->{$id} ;
};
sub answer {
my $obj = shift;
if (!defined $questions->{$obj->{id}}){
err ("Answer with nonexistant id");
};
# TODO: more syntax checks
$game->{board}{$obj->{id}}{state}="taken";
given ($obj->{type}){
when ("correct") {
$game->{board}{$obj->{id}}{state}="taken";
$game->{board}{$obj->{id}}{color}=$obj->{player};
$game->{board}{$obj->{id}}{names}[0]=$obj->{player}; # XXX: push
$game->{score}[$obj->{player}]+=$game->{board}{$obj->{id}}{score};
};
when ("wrong") {
$game->{board}{$obj->{id}}{state}="taken";
$game->{board}{$obj->{id}}{color}=$obj->{player};
$game->{board}{$obj->{id}}{names}[0]="-".$obj->{player}; # XXX: push
$game->{score}[$obj->{player}]-=$game->{board}{$obj->{id}}{score};
};
default {
err("unknown answer type");
}
};
};
###
### id translat0rs
###
sub cat2id {
my $id=0;
for my $cat (@{$game->{cats}}){
return $id if $cat eq $_[0];
$id++;
};
};
sub id2cat {
return $game->{cats}[$_[0]-1];
};
sub ply2id {
my $id=0;
for my $ply (@{$game->{names}}){
return $id if $ply eq $_[0];
$id++;
};
};
sub id2ply {
return $game->{names}[$_[0]-1];
};
1;