-
Notifications
You must be signed in to change notification settings - Fork 20
/
Spinach.pm
2294 lines (1821 loc) · 83.9 KB
/
Spinach.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
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
# File: Spinach.pm
#
# Purpose: Trivial game engine with a twist. Game is played in rounds. Each
# round players choose a category of questions. Then a random question from
# that category is shown. All players then privately submit a "lie" to the
# bot. Then all "lies" are revealed along with the true answer. Players
# gain points every time another player picks their lie. Very fun!
# SPDX-FileCopyrightText: 2018-2024 Pragmatic Software <[email protected]>
# SPDX-License-Identifier: MIT
package PBot::Plugin::Spinach;
use parent 'PBot::Plugin::Base';
use PBot::Imports;
use PBot::Core::Storage::HashObject;
use PBot::Plugin::Spinach::Stats;
use PBot::Plugin::Spinach::Rank;
use JSON;
use Lingua::EN::Fractions qw/fraction2words/;
use Lingua::EN::Numbers qw/num2en num2en_ordinal/;
use Lingua::EN::Numbers::Years qw/year2en/;
use Lingua::Stem qw/stem/;
use Lingua::EN::ABC qw/b2a/;
use Time::Duration qw/concise duration/;
use Text::Unidecode;
use Encode;
use Text::Levenshtein::XS 'distance';
use Data::Dumper;
$Data::Dumper::Sortkeys = sub {
my ($h) = @_; my @a = sort grep { not /^(?:seen_questions|alternativeSpellings)$/ } keys %$h; \@a;
};
$Data::Dumper::Useqq = 1;
sub initialize($self, %conf) {
$self->{pbot}->{commands}->add(
name => 'spinach',
help => 'Trivia game based on Fibbage',
subref => sub { $self->cmd_spinach(@_) },
);
$self->{pbot}->{event_dispatcher}->register_handler('irc.part', sub { $self->on_departure(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.quit', sub { $self->on_departure(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.kick', sub { $self->on_kick(@_) });
$self->{channel} = $self->{pbot}->{registry}->get_value('spinach', 'channel') // '##spinach';
my $default_file = $self->{pbot}->{registry}->get_value('spinach', 'file') // 'trivia.json';
$self->{questions_filename} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . "/spinach/$default_file";
$self->{stopwords_filename} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spinach/stopwords';
$self->{metadata_filename} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spinach/metadata';
$self->{stats_filename} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spinach/stats.sqlite';
$self->{metadata} = PBot::Core::Storage::HashObject->new(
pbot => $self->{pbot},
name => 'Spinach Metadata',
filename => $self->{metadata_filename}
);
$self->{metadata}->load;
$self->set_metadata_defaults;
$self->{stats} = PBot::Plugin::Spinach::Stats->new(
pbot => $self->{pbot},
filename => $self->{stats_filename}
);
$self->{rankcmd} = PBot::Plugin::Spinach::Rank->new(
pbot => $self->{pbot},
filename => $self->{stats_filename},
channel => $self->{channel}
);
$self->create_states;
$self->load_questions;
$self->load_stopwords;
# seconds between tocks
$self->{tock_duration} = 30;
# total tocks for choosecategory/picktruth
$self->{choosecategory_max_tocks} = 4;
$self->{picktruth_max_tocks} = 4;
}
sub unload($self) {
$self->{pbot}->{commands}->remove('spinach');
$self->{pbot}->{event_queue}->dequeue_event('spinach loop');
$self->{stats}->end if $self->{stats_running};
$self->{pbot}->{event_dispatcher}->remove_handler('irc.part');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.quit');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.kick');
}
sub on_kick($self, $event_type, $event) {
my ($nick, $user, $host) = ($event->nick, $event->user, $event->host);
my $channel = $event->{args}[0];
return 0 if lc $channel ne $self->{channel};
$self->player_left($nick, $user, $host);
return 0;
}
sub on_departure($self, $event_type, $event) {
my ($nick, $user, $host) = ($event->nick, $event->user, $event->host);
my ($channel, $type) = (lc $event->to, uc $event->type);
return 0 if $type ne 'QUIT' and $channel ne $self->{channel};
$self->player_left($nick, $user, $host);
return 0;
}
sub load_questions($self, $filename = undef) {
if (not defined $filename) {
$filename = exists $self->{loaded_filename} ? $self->{loaded_filename} : $self->{questions_filename};
} else {
$filename = $self->{pbot}->{registry}->get_value('general', 'data_dir') . "/spinach/$filename";
}
$self->{pbot}->{logger}->log("Spinach: Loading questions from $filename...\n");
my $contents = do {
open my $fh, '<', $filename or do {
$self->{pbot}->{logger}->log("Spinach: Failed to open $filename: $!\n");
return "Failed to load $filename";
};
local $/;
my $text = <$fh>;
close $fh;
$text;
};
$self->{loaded_filename} = $filename;
$self->{questions} = decode_json $contents;
$self->{categories} = ();
my $questions;
foreach my $key (keys %{$self->{questions}}) {
foreach my $question (@{$self->{questions}->{$key}}) {
$question->{category} = uc $question->{category};
$self->{categories}{$question->{category}}{$question->{id}} = $question;
$question->{seen_timestamp} //= 0;
$question->{value} //= 0;
$questions++;
}
}
my $categories;
foreach my $category (sort { keys %{$self->{categories}{$b}} <=> keys %{$self->{categories}{$a}} } keys %{$self->{categories}}) {
# my $count = keys %{$self->{categories}{$category}};
# $self->{pbot}->{logger}->log("Category [$category]: $count\n");
$categories++;
}
$self->{pbot}->{logger}->log("Spinach: Loaded $questions questions in $categories categories.\n");
return "Loaded $questions questions in $categories categories.";
}
sub save_questions($self) {
my $json = JSON->new;
my $json_text = $json->pretty->canonical->utf8->encode($self->{questions});
my $filename = exists $self->{loaded_filename} ? $self->{loaded_filename} : $self->{questions_filename};
open my $fh, '>', $filename or do {
$self->{pbot}->{logger}->log("Failed to open Spinach file $filename: $!\n");
return;
};
print $fh "$json_text\n";
close $fh;
}
sub load_stopwords($self) {
open my $fh, '<', $self->{stopwords_filename} or do {
$self->{pbot}->{logger}->log("Spinach: Failed to open $self->{stopwords_filename}: $!\n");
return;
};
foreach my $word (<$fh>) {
chomp $word;
$self->{stopwords}{$word} = 1;
}
close $fh;
}
sub set_metadata_defaults($self) {
my $defaults = {
category_choices => 7,
category_autopick => 0,
min_players => 2,
stats => 1,
seen_expiry => 432000,
min_difficulty => 0,
max_difficulty => 25000,
max_missed_inputs => 3,
debug_state => 0,
rounds => 3,
questions => 3,
bonus_rounds => 1,
};
foreach my $key (keys %$defaults) {
if (not $self->{metadata}->exists('settings', $key)) {
$self->{metadata}->set('settings', $key, $defaults->{$key}, 1);
}
}
}
my %color = (
white => "\x0300",
black => "\x0301",
blue => "\x0302",
green => "\x0303",
red => "\x0304",
maroon => "\x0305",
purple => "\x0306",
orange => "\x0307",
yellow => "\x0308",
lightgreen => "\x0309",
teal => "\x0310",
cyan => "\x0311",
lightblue => "\x0312",
magneta => "\x0313",
gray => "\x0314",
lightgray => "\x0315",
bold => "\x02",
italics => "\x1D",
underline => "\x1F",
reverse => "\x16",
reset => "\x0F",
);
sub cmd_spinach($self, $context) {
my $arguments = $context->{arguments};
$arguments =~ s/^\s+|\s+$//g;
my $usage = "Usage: spinach join|exit|ready|unready|choose|lie|reroll|skip|keep|score|show|rank|categories|filter|set|unset|load|state|edit|kick|abort; for more information about a command: spinach help <command>";
my $command;
($command, $arguments) = split / /, $arguments, 2;
$command = defined $command ? lc $command : '';
my ($channel, $result);
given ($command) {
when ('help') {
given ($arguments) {
when ('help') { return "Seriously?"; }
when ('join') { return "Use `join` to start/join a game. A on-going game can be joined at any time."; }
when ('ready') { return "Use `ready` to ready-up for a game."; }
when ('unready') { return "Use `unready` to no longer be ready for a game."; }
when ('exit') { return "Use `exit` to leave a game."; }
when ('skip') { return "Use `skip` to skip a question and return to the \"choose category\" stage. A majority of the players must agree to skip."; }
when ('keep') { return "Use `keep` to vote to prevent the current question from being rerolled or skipped."; }
when ('abort') { return "Use `abort` to immediately end a game."; }
when ('load') { return "Use `load` to load a trivia database."; }
when ('edit') { return "Use `edit` to view and edit question metadata."; }
when ('state') { return "Use `state` to view and manipulate the game state machine."; }
when ('reroll') { return "Use `reroll` to get a different question from the same category."; }
when ('kick') { return "Use `kick` to forcefully remove a player."; }
when ('players') { return "Use `players` to list players and their ready-state or scores."; }
when ('score') { return "Use `score` to display player scores."; }
when ('choose') { return "Use `choose` to choose category, submit lie, or select truth."; }
when ('lie') { return "Use `lie` (or `choose`) to submit a lie."; }
when ('truth') { return "Use `truth` (or `choose`) to select a truth."; }
when ('show') { return "Show the current question again."; }
when ('categories') { return "Use `categories` to list available categories."; }
when ('filter') { return "Use `filter` to set category include/exclude filters."; }
when ('set') { return "Use `set` to set game metadata values (e.g. rounds, questions per rounds, minimum players, etc; see `spinach set settings` for a list of values)."; }
when ('unset') { return "Use `unset` to delete game metadata values."; }
when ('rank') { return "Use `rank` to show ranking of player stats."; }
default {
if (length $arguments) {
return "Spinach has no such command '$arguments'.";
} else {
return "Usage: spinach help <command>";
}
}
}
}
when ('edit') {
my $admin = $self->{pbot}->{users}->loggedin_admin($self->{channel}, $context->{hostmask});
if (not $admin) {
return "$context->{nick}: Only admins may edit questions.";
}
my ($id, $key, $value) = split /\s+/, $arguments, 3;
if (not defined $id) {
return "Usage: spinach edit <question id> [key [value]]";
}
$id =~ s/,//g;
my $question;
foreach my $q (@{$self->{questions}->{questions}}) {
if ($q->{id} == $id) {
$question = $q;
last;
}
}
if (not defined $question) {
return "$context->{nick}: No such question.";
}
if (not defined $key) {
my $dump = Dumper $question;
$dump =~ s/\$VAR\d+ = \{\s*//;
$dump =~ s/ \};\s*$//;
return "$context->{nick}: Question $id: $dump";
}
if (not defined $value) {
my $v = $question->{$key} // 'unset';
return "$context->{nick}: Question $id: $key => $v";
}
if ($key !~ m/^(?:question|answer|category)$/i) {
return "$context->{nick}: You may not edit that key.";
}
$question->{$key} = $value;
$self->save_questions;
return "$context->{nick}: Question $id: $key set to $value";
}
when ('load') {
my $u = $self->{pbot}->{users}->loggedin($self->{channel}, $context->{hostmask});
if (not $u or not $self->{pbot}->{capabilities}->userhas($u, 'botowner')) {
return "$context->{nick}: Only botowners may reload the questions.";
}
$arguments = undef if not length $arguments;
return $self->load_questions($arguments);
}
when ('join') {
if ($self->{current_state} eq 'nogame') {
$self->start_game;
}
return $self->player_join($context->{nick}, $context->{user}, $context->{host});
}
when ('ready') {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
foreach my $player (@{$self->{state_data}->{players}}) {
if ($player->{id} == $id) {
if ($self->{current_state} ne 'getplayers') {
return "/msg $context->{nick} This is not the time to use `ready`.";
}
if ($player->{ready} == 0) {
$player->{ready} = 1;
$player->{score} = 0;
return "/msg $self->{channel} $context->{nick} is ready!";
} else {
return "/msg $context->{nick} You are already ready.";
}
}
}
return "$context->{nick}: You haven't joined this game yet. Use `j` to play now!";
}
when ('unready') {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
foreach my $player (@{$self->{state_data}->{players}}) {
if ($player->{id} == $id) {
if ($self->{current_state} ne 'getplayers') {
return "/msg $context->{nick} This is not the time to use `unready`.";
}
if ($player->{ready} != 0) {
$player->{ready} = 0;
return "/msg $self->{channel} $context->{nick} is no longer ready!";
} else {
return "/msg $context->{nick} You are already not ready.";
}
}
}
return "$context->{nick}: You haven't joined this game yet. Use `j` to play now!";
}
when ('exit') {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
my $removed = 0;
for (my $i = 0; $i < @{$self->{state_data}->{players}}; $i++) {
if ($self->{state_data}->{players}->[$i]->{id} == $id) {
splice @{$self->{state_data}->{players}}, $i--, 1;
$removed = 1;
}
}
if ($removed) {
if ($self->{state_data}->{current_player} >= @{$self->{state_data}->{players}}) {
$self->{state_data}->{current_player} = @{$self->{state_data}->{players}} - 1;
}
if (not @{$self->{state_data}->{players}}) {
$self->{current_state} = 'nogame';
$self->{pbot}->{event_queue}->update_repeating('spinach loop', 0);
return "/msg $self->{channel} $context->{nick} has left the game! All players have left. The game has been stopped.";
} else {
return "/msg $self->{channel} $context->{nick} has left the game!";
}
} else {
return "$context->{nick}: But you are not even playing the game.";
}
}
when ('abort') {
if (not $self->{pbot}->{users}->loggedin_admin($self->{channel}, $context->{hostmask})) {
return "$context->{nick}: Only admins may abort the game.";
}
$self->{current_state} = 'gameover';
return "/msg $self->{channel} $context->{nick}: The game has been aborted.";
}
when ($_ eq 'score' or $_ eq 'players') {
if ($self->{current_state} eq 'getplayers') {
my @names;
foreach my $player (@{$self->{state_data}->{players}}) {
if (not $player->{ready}) {
push @names, "$player->{name} $color{red}(not ready)$color{reset}";
} else {
push @names, $player->{name};
}
}
my $players = join ', ', @names;
$players = 'none' if not @names;
return "Current players: $players";
}
# score
if (not @{$self->{state_data}->{players}}) {
return "There is nobody playing right now.";
}
my $text = '';
my $comma = '';
foreach my $player (sort { $b->{score} <=> $a->{score} } @{$self->{state_data}->{players}}) {
$text .= "$comma$player->{name}: " . $self->commify($player->{score});
$comma = '; ';
}
return $text;
}
when ('kick') {
if (not $self->{pbot}->{users}->loggedin_admin($self->{channel}, $context->{hostmask})) {
return "$context->{nick}: Only admins may kick people from the game.";
}
if (not length $arguments) { return "Usage: spinach kick <nick>"; }
my $removed = 0;
for (my $i = 0; $i < @{$self->{state_data}->{players}}; $i++) {
if (lc $self->{state_data}->{players}->[$i]->{name} eq $arguments) {
splice @{$self->{state_data}->{players}}, $i--, 1;
$removed = 1;
}
}
if ($removed) {
if ($self->{state_data}->{current_player} >= @{$self->{state_data}->{players}}) {
$self->{state_data}->{current_player} = @{$self->{state_data}->{players}} - 1;
}
return "/msg $self->{channel} $context->{nick}: $arguments has been kicked from the game.";
} else {
return "$context->{nick}: $arguments isn't even in the game.";
}
}
when ('n') {
return $self->normalize_text($arguments);
}
when ('v') {
my ($truth, $lie) = split /;/, $arguments;
if (!defined $truth || !defined $lie) {
return "Usage: spinach v <truth>;<lie>";
}
return $self->validate_lie($self->normalize_text($truth), $self->normalize_text($lie));
}
when ('reroll') {
if ($self->{current_state} eq 'getlies') {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
my $player;
my $rerolled = 0;
my $keep;
foreach my $i (@{$self->{state_data}->{players}}) {
if ($i->{id} == $id) {
$i->{reroll} = 1;
delete $i->{keep};
$rerolled++;
$player = $i;
} elsif ($i->{reroll}) {
$rerolled++;
} elsif ($i->{keep}) {
$keep++;
}
}
if (not $player) {
return "$context->{nick}: You are not playing in this game. Use `j` to start playing now!";
}
my $needed = int(@{$self->{state_data}->{players}} / 2) + 1;
$needed -= $rerolled;
$needed += $keep;
my $votes_needed;
if ($needed == 1) { $votes_needed = "$needed more vote to reroll!"; }
elsif ($needed > 1) { $votes_needed = "$needed more votes to reroll!"; }
else { $votes_needed = "Rerolling..."; }
return "/msg $self->{channel} $color{red}$context->{nick} has voted to reroll for another question from the same category! $color{reset}$votes_needed";
} else {
return "$context->{nick}: This command can be used only during the \"submit lies\" stage.";
}
}
when ('skip') {
if ($self->{current_state} eq 'getlies') {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
my $player;
my $skipped = 0;
my $keep = 0;
foreach my $i (@{$self->{state_data}->{players}}) {
if ($i->{id} == $id) {
$i->{skip} = 1;
delete $i->{keep};
$skipped++;
$player = $i;
} elsif ($i->{skip}) {
$skipped++;
} elsif ($i->{keep}) {
$keep++;
}
}
if (not $player) { return "$context->{nick}: You are not playing in this game. Use `j` to start playing now!"; }
my $needed = int(@{$self->{state_data}->{players}} / 2) + 1;
$needed -= $skipped;
$needed += $keep;
my $votes_needed;
if ($needed == 1) { $votes_needed = "$needed more vote to skip!"; }
elsif ($needed > 1) { $votes_needed = "$needed more votes to skip!"; }
else { $votes_needed = "Skipping..."; }
return "/msg $self->{channel} $color{red}$context->{nick} has voted to skip this category! $color{reset}$votes_needed";
} else {
return "$context->{nick}: This command can be used only during the \"submit lies\" stage.";
}
}
when ('keep') {
if ($self->{current_state} eq 'getlies') {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
my $player;
foreach my $i (@{$self->{state_data}->{players}}) {
if ($i->{id} == $id) {
$i->{keep} = 1;
delete $i->{skip};
delete $i->{reroll};
$player = $i;
last;
}
}
if (not $player) {
return "$context->{nick}: You are not playing in this game. Use `j` to start playing now!";
}
return "/msg $self->{channel} $color{green}$context->{nick} has voted to keep playing the current question!";
} else {
return "$context->{nick}: This command can be used only during the \"submit lies\" stage.";
}
}
when ($_ eq 'lie' or $_ eq 'truth' or $_ eq 'choose') {
$arguments //= '';
$arguments = lc $arguments;
if ($self->{current_state} eq 'choosecategory') {
if (not length $arguments) { return "Usage: spinach choose <integer>"; }
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
if (not @{$self->{state_data}->{players}} or $id != $self->{state_data}->{players}->[$self->{state_data}->{current_player}]->{id}) {
return "$context->{nick}: It is not your turn to choose a category.";
}
if ($arguments !~ /^[0-9]+$/) {
return "$context->{nick}: Please choose a category number. $self->{state_data}->{categories_text}";
}
$arguments--;
if ($arguments < 0 or $arguments >= @{$self->{state_data}->{category_options}}) {
return "$context->{nick}: Choice out of range. Please choose a valid category. $self->{state_data}->{categories_text}";
}
if ($arguments == @{$self->{state_data}->{category_options}} - 2) {
$arguments = (@{$self->{state_data}->{category_options}} - 2) * rand;
$self->{state_data}->{current_category} = $self->{state_data}->{category_options}->[$arguments];
return "/msg $self->{channel} $context->{nick} has chosen RANDOM CATEGORY! Randomly choosing category: $self->{state_data}->{current_category}!";
} elsif ($arguments == @{$self->{state_data}->{category_options}} - 1) {
$self->{state_data}->{reroll_category} = 1;
return "/msg $self->{channel} $context->{nick} has chosen REROLL CATEGORIES! Rerolling categories...";
} else {
$self->{state_data}->{current_category} = $self->{state_data}->{category_options}->[$arguments];
return "/msg $self->{channel} $context->{nick} has chosen $self->{state_data}->{current_category}!";
}
}
if ($self->{current_state} eq 'getlies') {
if (not length $arguments) { return 'Usage: spinach lie <text>'; }
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
my $player;
foreach my $i (@{$self->{state_data}->{players}}) {
if ($i->{id} == $id) {
$player = $i;
last;
}
}
if (not $player) {
return "$context->{nick}: You are not playing in this game. Use `j` to start playing now!";
}
$arguments = $self->normalize_text($arguments);
my @truth_count = split /\s/, $self->{state_data}->{current_question}->{answer};
my @lie_count = split /\s/, $arguments;
my $validate = $self->validate_lie($self->{state_data}->{current_question}->{answer}, $arguments);
# check alternate answers if lie is not already too similar to default answer
if ($validate == 1) {
# check alternative answers
foreach my $alt (@{$self->{state_data}->{current_question}->{alternativeSpellings}}) {
$validate = self->validate_lie($alt, $arguments);
# end loop if too similar to an alternative
last if $validate != 1;
}
}
if ($validate != 1) {
if ($validate == 0) {
$self->send_message($self->{channel}, "$color{yellow}$context->{nick} has found the truth!$color{reset}");
return "$context->{nick}: You have found the truth! Submit a different lie.";
} elsif ($validate == -1) {
$self->send_message($self->{channel}, "$color{cyan}$context->{nick} has found part of the truth!$color{reset}");
} else {
$self->send_message($self->{channel}, "$color{cyan}$context->{nick} has misspelled the truth!$color{reset}");
}
return "$context->{nick}: Your lie is too similar to the truth! Submit a different lie.";
}
if (++$player->{lie_count} > 2) {
return "/msg $context->{nick} You cannot change your lie again this round.";
}
my $changed = exists $player->{lie};
$player->{lie} = $arguments;
if ($changed) { return "/msg $self->{channel} $context->{nick} has changed their lie!"; }
else { return "/msg $self->{channel} $context->{nick} has submitted a lie!"; }
}
if ($self->{current_state} eq 'findtruth') {
if (not length $arguments) { return 'Usage: spinach truth <integer>'; }
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($context->{nick}, $context->{user}, $context->{host});
my $player;
foreach my $i (@{$self->{state_data}->{players}}) {
if ($i->{id} == $id) {
$player = $i;
last;
}
}
if (not $player) {
return "$context->{nick}: You are not playing in this game. Use `j` to start playing now!";
}
if ($arguments !~ /^[0-9]+$/) {
return "$context->{nick}: Please select a truth number. $self->{state_data}->{current_choices_text}";
}
$arguments--;
if ($arguments < 0 or $arguments >= @{$self->{state_data}->{current_choices}}) {
return "$context->{nick}: Selection out of range. Please select a valid truth. $self->{state_data}->{current_choices_text}";
}
my $changed = exists $player->{truth};
$player->{truth} = uc $self->{state_data}->{current_choices}->[$arguments];
if ($player->{truth} eq $player->{lie}) {
delete $player->{truth};
return "$context->{nick}: You cannot select your own lie!";
}
if ($changed) { return "/msg $self->{channel} $context->{nick} has selected a different truth!"; }
else { return "/msg $self->{channel} $context->{nick} has selected a truth!"; }
}
return "$context->{nick}: It is not time to use this command.";
}
when ('show') {
if ($self->{current_state} =~ /(?:getlies|findtruth|showlies)/) {
$self->showquestion_helper($self->{state_data}, 1);
return '';
}
return "$context->{nick}: There is nothing to show right now.";
}
when ('categories') {
if (not length $arguments) { return "Usage: spinach categories <regex>"; }
my $result = eval {
use re::engine::RE2 -strict => 1;
my @categories = grep { /$arguments/i } keys %{$self->{categories}};
if (not @categories) { return "No categories found."; }
my $text = "";
my $comma = "";
foreach my $cat (sort @categories) {
$text .= "$comma$cat: " . keys %{$self->{categories}{$cat}};
$comma = ",\n";
}
return $text;
};
return "$arguments: $@" if $@;
return $result;
}
when ('filter') {
my ($cmd, $args) = split / /, $arguments, 2;
$cmd = lc $cmd;
if (not length $cmd) { return "Usage: spinach filter include <regex> | exclude <regex> | show | clear"; }
given ($cmd) {
when ($_ eq 'include' or $_ eq 'exclude') {
if (not length $args) { return "Usage: spinach filter $_ <regex>"; }
eval { "" =~ /$args/ };
return "Bad filter $args: $@" if $@;
my @categories = grep { /$args/i } keys %{$self->{categories}};
if (not @categories) { return "Bad filter: No categories match. Try again."; }
$self->{metadata}->set('filter', "category_" . $_ . "_filter", $args);
return "Spinach $_ filter set.";
}
when ('clear') {
$self->{metadata}->remove('filter');
return "Spinach filter cleared.";
}
when ('show') {
if (not $self->{metadata}->exists('filter', 'category_include_filter') and not $self->{metadata}->exists('filter', 'category_exclude_filter')) {
return "There is no Spinach filter set.";
}
my $text = "Spinach ";
my $comma = "";
if ($self->{metadata}->exists('filter', 'category_include_filter')) {
$text .= "include filter set to: " . $self->{metadata}->get_data('filter', 'category_include_filter');
$comma = "; ";
}
if ($self->{metadata}->exists('filter', 'category_exclude_filter')) {
$text .= $comma . "exclude filter set to: " . $self->{metadata}->get_data('filter', 'category_exclude_filter');
}
return $text;
}
default { return "Unknown filter command '$cmd'."; }
}
}
when ('state') {
my ($command, $args) = split /\s+/, $arguments;
if ($command eq 'show') {
return "Previous state: $self->{previous_state}; current state: $self->{current_state}; previous result: $self->{state_data}->{previous_result}";
}
if ($command eq 'set') {
if (not length $args) { return "Usage: spinach state set <new state>"; }
my $u = $self->{pbot}->{users}->loggedin($self->{channel}, $context->{hostmask});
if (not $self->{pbot}->{capabilities}->userhas($u, 'admin')) { return "$context->{nick}: Only admins may set game state."; }
$self->{previous_state} = $self->{current_state};
$self->{current_state} = $args;
return "State set to $args";
}
if ($command eq 'result') {
if (not length $args) { return "Usage: spinach state result <current state result>"; }
my $admin = $self->{pbot}->{users}->loggedin_admin($self->{channel}, $context->{hostmask});
if (not $admin) { return "$context->{nick}: Only admins may set game state."; }
$self->{state_data}->{previous_result} = $self->{state_data}->{result};
$self->{state_data}->{result} = $args;
return "State result set to $args";
}
return "Usage: spinach state show | set <new state> | result <current state result>";
}
when ('set') {
my ($index, $key, $value) = split /\s+/, $arguments;
if (not defined $index) { return "Usage: spinach set <metadata> [key [value]]"; }
if (lc $index eq 'settings' and $key and lc $key eq 'stats' and defined $value and $self->{current_state} ne 'nogame') {
return "Spinach stats setting cannot be modified while a game is in progress.";
}
my $admin = $self->{pbot}->{users}->loggedin_admin($self->{channel}, $context->{hostmask});
if (defined $value and not $admin) { return "$context->{nick}: Only admins may set game settings."; }
return $self->{metadata}->set($index, $key, $value);
}
when ('unset') {
my ($index, $key) = split /\s+/, $arguments;
if (not defined $index or not defined $key) { return "Usage: spinach unset <metadata> <key>"; }
if (lc $index eq 'settings' and lc $key eq 'stats' and $self->{current_state} ne 'nogame') {
return "Spinach stats setting cannot be modified while a game is in progress.";
}
my $admin = $self->{pbot}->{users}->loggedin_admin($self->{channel}, $context->{hostmask});
if (not $admin) { return "$context->{nick}: Only admins may set game settings."; }
return $self->{metadata}->unset($index, $key);
}
when ('rank') {
return $self->{rankcmd}->rank($arguments);
}
default { return $usage; }
}
return $result;
}
sub start_game($self) {
$self->{state_data} = {
players => [],
bonus_round => 0,
};
$self->{current_state} = 'getplayers';
$self->{pbot}->{event_queue}->enqueue_event(
sub {
$self->run_one_state;
}, 1, 'spinach loop', 1
);
}
sub player_join($self, $nick, $user, $host) {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($nick, $user, $host);
foreach my $player (@{$self->{state_data}->{players}}) {
if ($player->{id} == $id) {
return "$nick: You have already joined this game.";
}
}
my $player = {
id => $id,
name => $nick,
score => 0,
ready => $self->{current_state} eq 'getplayers' ? 0 : 1,
missedinputs => 0
};
push @{$self->{state_data}->{players}}, $player;
return "/msg $self->{channel} $nick has joined the game!";
}
sub player_left($self, $nick, $user, $host) {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($nick, $user, $host);
for (my $i = 0; $i < @{$self->{state_data}->{players}}; $i++) {
if ($self->{state_data}->{players}->[$i]->{id} == $id) {
splice @{$self->{state_data}->{players}}, $i--, 1;
if ($self->{state_data}->{current_player} >= @{$self->{state_data}->{players}}) {
$self->{state_data}->{current_player} = @{$self->{state_data}->{players}} - 1;
}
$self->send_message($self->{channel}, "$nick has left the game!");
last;
}
}
}
sub send_message($self, $to, $text, $delay = 0) {
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
my $message = {
nick => $botnick,
user => 'spinach',
host => 'localhost',
hostmask => "$botnick!spinach\@localhost",
keyword => 'spinach',
command => 'spinach',
checkflood => 1,
message => $text
};
$self->{pbot}->{interpreter}->add_message_to_output_queue($to, $message, $delay);
}
sub add_new_suggestions($self, $state) {
my $question = undef;
my $modified = 0;
foreach my $player (@{$state->{players}}) {
if ($player->{deceived}) {
$self->{pbot}->{logger}->log("Adding new suggestion for $state->{current_question}->{id}: $state->{current_question}->{question}: $player->{deceived}\n");
if (not grep { lc $_ eq lc $player->{deceived} } @{$state->{current_question}->{suggestions}}) {
if (not defined $question) {
foreach my $q (@{$self->{questions}->{questions}}) {
if ($q->{id} == $state->{current_question}->{id}) {
$question = $q;
last;