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
|
# convert this to a man page with:
# pod2man --stderr -s6 -c"Irssi Scripts" -r0.1 -u frotzglue.pl
# for HTML:
# pod2html frotzglue.pl
=pod
=head1 NAME
frotzglue.pl - run a z-code interactive fiction game within irssi, in a channel.
=head1 SYNOPSIS
/script load frotzglue.pl
=head1 DESCRIPTION
B<frotzglue.pl> is a Perl script for irssi(1) which runs a z-code game
using dfrotz(6), and allow users in an IRC channel to co-operatively
play the game.
You probably don't want to run this in your regular irssi session that
you use for normal chatting. It's basically a bot, intended to be run
with a dedicated irssi instance that uses its own separate user, or at
least a separate config dir (irssi's B<--home> option).
=head2 Gameplay
When a game is started, a list of players (IRC nicks) must be given. These
players will take turns sending a command to the game.
All output from the game is sent to the IRC channel where the game
was started. When the game is ready for input, there will be a prompt
with the current player's nick (whoever's turn it is) followed by a >
character.
Due to irssi's anti-flooding mechanism (and/or the IRC server's),
output will be slow, with pauses between lines. This means that some
games (ones that rely on timed prompts) might be unplayable.
=head2 Commands
Only public (channel) messages are accepted; there's no way to play or
control a game via private messages.
All commands are prefixed with B<.if>; any other channel text is ignored.
=head3 Admin Commands
These commands can be executed by anyone (TODO: for now; later there will be some
sort of access control).
=over 4
=item B<.if lsgames>
Show the list of supported games. These are hard-coded in the script.
TODO: store the game list as an irssi setting.
=item B<.if newgame> I<game> [I<player> I<...>]
Start a game. I<game> must be one of the supported games. Any number
of players may be given. If there are no players, a 1-player game is
started, with the only player being the user who started the game.
=item B<.if status>
Tell whether or not a game is running. If it is, show the game name, channel,
list of players, and whose turn it is.
=item B<.if endgame>
Ends any game in progress.
=back
=head3 Game Control Commands
These commands only work while a game is in progress, and can only be given
by players.
=over 4
=item B<.if skipturn>
Skip (pass) the current user's turn. Use this to avoid holding up gameplay
if someone is AFK or has gotten disconnected.
=item B<.if add> I<player> [I<player> I<...>]
Adds one or more players to the game.
=item B<.if rm> I<player> [I<player> I<...>]
Removes one or more players from the game. If there's only one player
on the list, attempts to remove him will be denied; the game must
always have at least one player.
=item B<.if ls>
List the players in the game, and tell whose turn it is. (Note: possibly this
shouldn't exist; .if status shows the same info...)
=back
=head3 Gameplay Commands
Any message beginning with B<.if> that isn't one of the recognized commands
above will be sent to the game as input. In a multi-player game, only the
player whose turn it is can send commands to the game.
Examples might be B<.if get sword> or B<.if inventory>.
To send a blank line to the game, say B<.if \n> or just B<.if> by itself.
To send one of the listed admin or control commands to the game instead of
acting on it, prefix it with B<-->. Example: B<.if -- ls> would send the
command "ls" to the game (where it would probably result in an error,
such as B<I don't know the word "ls">).
Currently, the B<save> and B<load> commands are disabled (for security
reasons). At some point, there might be a safe way to implement these.
=head1 EXAMPLES
[put an example here]
=head1 BUGS
All game text is assumed to end with a > prompt. If something the game
says doesn't end with >, there will be a 5-second timeout before the
text will appear... and the text will be printed again, next time a >
prompt is encountered. This is because of the author's inexperience
with the perl Expect module, and will likely be fixed soon.
Use of the dfrotz \ commands (list under CONFIGURATION in its man
page) is allowed, but probably shouldn't be. It's probably possible to
break the Expect stuff with some of the \ commands.
Only one game can be running at a time... not "one game per IRC
channel", just one, period. There should be a way to run separate
games in different channels.
There's a $VERBOSE variable for debugging, but it doesn't do much, and
the script has to be edited and reloaded to set/unset it.
=head1 AUTHOR
B. Watson <urchlay@slackware.uk>, AKA Urchlay on the Libera IRC network.
=cut
# Requirements:
# - irssi (of course).
# - dfrotz somewhere on $PATH.
# - Expect perl module.
# the game has a defined list of players, and on every move,
# the next player has control.
# commands are prefixed with .if:
# These commands work any time, and can be run by anyone (for now).
# .if lsgames
# - list all the games we know about.
# .if newgame <game> <player> <...>
# - start a new game.
# .if status
# - tell whether or not a game is running. if so, list filename, channels, and players.
# .if endgame
# - end any game in progress (and kill its dfrotz process).
# These commands only work while a game is in progress, and can only
# be issued by players:
# .if skipturn
# - pass the current player's turn. use if someone's AFK or disconnected.
# .if add <player>
# .if rm <player>
# .if ls
# - add/delete/list players.
# anything else beginning with .if is passed to the game as-is.
# if you *really* want to pass one of the above commands as game input,
# use e.g. ".if -- whatever".
use strict;
use vars qw($VERSION %IRSSI $VERBOSE $exp @players $cur_player %games $gameprefix $cur_game $gamechan);
use Irssi qw(signal_add);
$VERSION = '0.1';
%IRSSI = (
authors => 'B. Watson',
contact => 'urchlay@slackware.uk',
name => 'frotzglue',
description => 'Run a z-code interactive fiction game in a channel',
license => 'WTFPL',
);
$VERBOSE = 1; # set to 1 if needed.
use Expect;
$Expect::Log_Stdout = 0;
$gameprefix = "/usr/share/zcode/";
%games = (
zork1 => [ "Zork I", $gameprefix . "zork1.z3" ],
zork2 => [ "Zork II", $gameprefix . "zork2.z3" ],
zork3 => [ "Zork III", $gameprefix . "zork3.z3" ],
hhgg => [ "Hitchhiker's Guide", $gameprefix . "hhgg.z3" ],
);
sub player_prompt {
my ($server, $target) = @_;
return unless $exp;
$server->command("msg $target " . current_player() . ">");
}
sub end_game {
$exp->hard_close();
undef $exp;
undef $cur_game;
undef $gamechan;
undef @players;
undef $cur_player;
}
sub run_game {
my ($server, $target) = @_;
$exp->expect(10, '>');
my $xstat = $exp->exitstatus();
if(defined($xstat)) {
my $normal = $xstat ? "due to error" : "normally";
$server->command("msg $target *** game exited $normal with status $xstat.");
end_game();
return;
}
## my $err = $exp->error();
## if($err) {
## $server->command("msg $target *** got error $err, aborting game.");
## end_game();
## return;
## }
for(split /\n/, $exp->before()) {
s/\r//g;
$_ = " " unless length;
$server->command("msg $target $_");
}
}
sub cant_spawn {
my ($server, $target) = @_;
$server->command("msg $target *** game failed to execute!");
}
sub spawn_game {
return unless $cur_game;
my $gamepath = $games{$cur_game}[1];
return unless $gamepath;
$exp = Expect->new;
if(!$exp->spawn("dfrotz", "-q", "-m", "-f", "irc", $gamepath)) {
cant_spawn(@_);
end_game();
return;
}
run_game(@_);
player_prompt(@_);
}
sub next_player {
return unless @players;
$cur_player++;
$cur_player %= @players;
}
sub is_playing {
return 0 unless @players;
grep { $_ eq $_[0] } @players;
}
sub current_player {
return 0 unless @players;
return $players[$cur_player];
}
sub player_list {
return "" unless @players;
return join(", ", @players);
}
sub on_message {
my ($server, $msg, $nick, $address, $target) = @_;
my $mynick = $server->{nick};
$msg = '.if \n' if $msg =~ /^\.if\s*$/;
return unless $msg =~ s/^\.if\s+//;
return unless length $msg;
warn "nick='$nick', address='$address', target='$target', mynick='$mynick'" if $VERBOSE;
(my $cmd, my @args) = split " ", $msg;
$cmd = lc $cmd;
warn "cmd='$cmd'" if $VERBOSE;
# all responses start with this prefix.
my $resp = "msg $target $nick";
if($cmd eq 'newgame') {
if(!@args) {
$server->command("$resp: newgame requires at least a game name.");
return;
}
my $game = shift @args;
if(!@args) {
#$server->command("$resp: newgame requires at least one player.");
#return;
push @args, $nick;
}
if($exp) {
$server->command("$resp: a game of $cur_game is already in progress in $gamechan; use 'endgame' to end it.");
} else {
if(!$games{$game}) {
$server->command("$resp: no such game as $game; try '.if lsgames'.");
return;
}
$server->command("$resp: starting a " . @args . "-player game of $game in $target.");
$cur_game = $game;
@players = @args;
$cur_player = 0;
$gamechan = $target;
spawn_game($server, $target);
}
return;
} elsif($cmd eq 'help') {
$server->command("$resp: Usage: .if [newgame <players>|status|endgame|lsgames|skipturn|add|ls|rm|<game command>]");
return;
} elsif($cmd eq 'status') {
if($exp) {
$server->command("$resp: game '$cur_game' is running on $gamechan, with players " . player_list() . "; " . current_player() . "'s turn.");
} else {
$server->command("$resp: game is NOT running.");
}
return;
} elsif($cmd eq 'endgame') {
if($exp) {
$server->command("$resp: ending game.");
end_game();
} else {
$server->command("$resp: can't end; game is NOT running.");
}
return;
} elsif($cmd eq 'lsgames') {
$server->command("$resp: I know how to play these games:");
for(sort keys %games) {
$server->command("$resp: $_ ($games{$_}[0])");
}
return;
}
if(!$exp) {
$server->command("$resp: '$cmd' not valid unless game is running.");
return;
}
if($target ne $gamechan) {
$server->command("$resp: wrong channel; game is running in $gamechan.");
return;
}
if(!is_playing($nick)) {
$server->command("$resp: $nick: you are not a player in the game.");
return;
}
if($cmd eq 'skipturn') {
$server->command("$resp: skipping " . current_player() . "'s turn.");
next_player();
player_prompt();
return;
} elsif($cmd eq 'add') {
for(@args) {
if(is_playing($_)) {
$server->command("$resp: can't add $_: already playing.");
} else {
push @players, $_;
$server->command("$resp: added player: $_");
}
}
return;
} elsif($cmd eq 'rm') {
for(@args) {
if(is_playing($_)) {
if(@players == 1) {
$server->command("$resp: cannot remove last player from game.");
return;
}
my $p = $_;
@players = grep { $_ ne $p } @players;
$cur_player %= @players;
$server->command("$resp: removed $_.");
} else {
$server->command("$resp: can't remove $_ (not playing).");
}
}
return;
} elsif($cmd eq 'ls') {
$server->command("$resp: " . player_list());
return;
} elsif($cmd eq 'save' || $cmd eq 'load') {
$server->command("$resp: save and load commands are disabled.");
return;
} else {
if(current_player() eq $nick) {
$msg =~ s/^\s*--\s*//;
$server->command("$resp: sending '$msg' to game.");
$msg = "" if $msg eq '\n';
$exp->send($msg . "\n");
run_game($server, $target);
next_player();
player_prompt($server, $target);
} else {
$server->command("$resp: it's not your turn; it's " . current_player() . "'s.");
return;
}
}
}
signal_add('message public', 'on_message');
|