-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess.pm
108 lines (89 loc) · 2.03 KB
/
guess.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
#!/usr/bin/perl
package BobboBot::guess;
use warnings;
use strict;
use POSIX;
use List::MoreUtils qw(none); # array searching
use BobboBot::math;
use BobboBot::users;
use BotData;
my $number = pickNumber();
my $guesses = 0;
my @players;
my $file = 'data/guess.json';
my $data = BotData->new($file, \&addPlayer);
sub addPlayer
{
return {play => 0, win => 0, lose => 0, guess => 0};
}
sub pickNumber
{
return floor(rand(100)) + 1;
}
sub think
{
$number = pickNumber();
$data->incStat('thought');
$data->save();
undef(@players);
$guesses = 0;
return {type => 'ACTION', text => 'thinks of another number betwen 1 and 100.'};
}
sub run
{
my @args = @{$_[0]->{arg}};
if (defined $args[0] && ($args[0] eq "stat" || $args[0] eq "stats"))
{
my $player = $args[1] || undef;
return $data->printStats($player);
}
if (index($_[0]->{where}, '#') == -1)
{
return ""; # do nothing if not in public
}
my ($player) = split('!', $_[0]->{who});
my $guess = shift(@args);
if (!defined $guess || !isNumber($guess))
{
return 'Not a number';
}
if (floor($guess) != $guess)
{
return 'I only think of integers';
}
$guesses++;
$data->incStat('guess', $player);
push(@players, $player) if (none {$_ eq $player} @players);
if ($guess < $number)
{
return 'Too low!';
}
elsif ($guess > $number)
{
return 'Too high!';
}
else
{
foreach my $p (@players)
{
$data->incStat('play', $p);
next if ($p eq $player);
$data->incStat('lose', $p);
}
$data->incStat('win', $player);
return ['Well done, you guessed it! It took ' . $guesses . ' guesses to get it!', think()];
}
}
sub help
{
return ['!guess guess - Guess my number of 1 to 100!'.
'!guess stat [player] - Retrieve game stats.'];
}
sub auth
{
return accessLevel('normal');
}
BobboBot::module::addCommand('guess', 'run', \&BobboBot::guess::run);
BobboBot::module::addCommand('guess', 'help', \&BobboBot::guess::help);
BobboBot::module::addCommand('guess', 'auth', \&BobboBot::guess::auth);
1;