-
Notifications
You must be signed in to change notification settings - Fork 0
/
epi_games.pl
executable file
·442 lines (419 loc) · 16.5 KB
/
epi_games.pl
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
#!/bin/perl
use strict;
use warnings;
use Config::Simple;
use Data::Dumper;
use DateTime;
use DateTime::Duration;
use DateTime::Format::MySQL;
use DateTime::Format::DateParse;
use DBI;
use List::Util qw(shuffle);
use Log::Log4perl;
use POE;
use POE::Component::IRC::State;
use POE::Component::IRC::Plugin::BotCommand;
use POE::Component::IRC::Plugin::Connector;
use Switch;
use lib "/opt/evepriceinfo";
use Token qw(token_add token_take);
use EPIUser qw(is_subscriber is_authorized is_owner);
use EPIBlackJack qw(newshoe deal show_game givecard valuehand eval_game get_curr_game);
use constant {
true => 1,
false => 0,
};
my $cfg = new Config::Simple('/opt/evepriceinfo/epi.conf');
my $DBName = $cfg->param("DBName");
my $DBUser = $cfg->param("DBUser");
my $DBPassword = $cfg->param("DBPassword");
my $dbh = DBI->connect("DBI:mysql:database=$DBName;host=localhost",
"$DBUser", "$DBPassword",
{'RaiseError' => 1});
$dbh->{mysql_auto_reconnect} = 1;
my $sth = $dbh->prepare('SELECT * FROM epi_configuration');
$sth->execute;
my $ref = $sth->fetchall_hashref('setting');
my $twitch_user = $ref->{'twitch_user'}->{'value'};
my $twitch_pwd = $ref->{'twitch_pwd'}->{'value'};
my $twitch_svr = $ref->{'twitch_svr'}->{'value'};
my $twitch_port = $ref->{'twitch_port'}->{'value'};
my $debug = $ref->{'debug'}->{'value'};
my $install_dir = $ref->{'install_dir'}->{'value'};
my @channels = ($ref->{'channel'}->{'value'});
my $log_conf = $install_dir.$ref->{'log_conf'}->{'value'};
$sth->finish;
Log::Log4perl::init_and_watch($log_conf,60);
my $logger = Log::Log4perl->get_logger;
my @cmds = ();
my %help = ();
push(@cmds,'_start');
$sth = $dbh->prepare('SELECT * FROM epi_commands WHERE CmdModule like ?');
$sth->execute('games');
$ref = $sth->fetchall_hashref('CmdKey');
foreach ( keys %$ref ) {
push(@cmds,"irc_botcmd_".$ref->{$_}->{'Command'});
$help{$ref->{$_}->{'Command'}}=$ref->{$_}->{'HelpInfo'};
}
$sth->finish;
my $irc = POE::Component::IRC::State->spawn(
Nick => $twitch_user,
Server => $twitch_svr,
Port => $twitch_port,
Username => $twitch_user,
Password => $twitch_pwd,
Debug => $debug,
WhoJoiners => 0,
) or die "Error: $!";
POE::Session->create(
package_states => [
main => [ @cmds ],
],
);
$poe_kernel->run();
sub _start {
my ($kernel, $heap) = @_[KERNEL ,HEAP];
$heap->{connector} = POE::Component::IRC::Plugin::Connector->new();
$irc->plugin_add('Connector' => $heap->{connector} );
$irc->plugin_add('BotCommand', POE::Component::IRC::Plugin::BotCommand->new(
Addressed => 0,
Prefix => '!',
Method => 'privmsg',
Ignore_unknown => 1,
Commands => { %help },
Help_sub => \&help,
));
$irc->yield(register => qw(all));
$irc->yield(connect => { } );
return;
}
sub irc_botcmd_slot {
my $nick = (split /!/, $_[ARG0])[0];
my ($where, $arg) = @_[ARG1, ARG2];
if (!$arg) {
$irc->yield(privmsg => $where, "/me - TwitchSlots, use 1 to 3 tokens. Payout Table: http://tinyurl.com/twitchslots");
return;
}
if ($arg =~ /help/ || $arg =~ /\?/) {
$irc->yield(privmsg => $where, "/me - TwitchSlots, use 1 to 3 tokens. Payout Table: http://tinyurl.com/twitchslots");
return;
}
if ($arg !~ /^\d+$/) {
$irc->yield(privmsg => $where, "/me - $nick, The number of tokens must be a number!");
return;
}
$irc->yield(privmsg => $where, "/me - Must specify how many tokens to use (1 - 3).") if !$arg;
return if !$arg;
if ($arg < 1 || $arg > 3) {
$irc->yield(privmsg => $where, "/me - Must use 1 to 3 tokens for the slot machine.");
return;
}
my $max = $dbh->selectrow_array("SELECT Tokens FROM followers WHERE TwitchID = \"$nick\"");
if ($arg > $max) {
$irc->yield(privmsg => $where, "/me - $nick, you do not have $arg tokens!");
return;
}
my $lastslot = $dbh->selectrow_array("SELECT SlotTime FROM SlotTime WHERE TwitchID = \"$nick\"");
my $duration;
if ($lastslot) {
my $dt1 = DateTime::Format::MySQL->parse_datetime($lastslot);
my $dt2 = DateTime->now(time_zone=>'local');
my $days = ($dt2 - $dt1)->days;
my $hours = ($dt2 - $dt1)->hours;
my $mins = ($dt2 - $dt1)->minutes;
my $secs = ($dt2 - $dt1)->seconds;
$duration = ($days * 86400) + ($hours * 3600) + ($mins * 60) + $secs;
} else {
$duration = 301;
}
my $sublevel = is_subscriber($nick);
my $threshold = 300;
switch ($sublevel) {
case [1..100] {$threshold=60}
}
if (!timelimit($where,$nick,$threshold,$duration)) {
return;
}
my $sth = $dbh->prepare('INSERT INTO SlotTime (TwitchID,SlotTime) VALUES (?,NULL) ON DUPLICATE KEY UPDATE SlotTime = NULL');
$sth->execute($nick);
$sth->finish;
token_take("evepriceinfo",$arg,$nick);
$sth = $dbh->prepare('UPDATE TokenStats SET SlotTokensIn = SlotTokensIn + ? WHERE StatDate = current_date');
$sth->execute($arg);
$sth->finish;
my @wheel1 = ([';P',':)','R)',':(',';P',';)','<3',':(','B)',';)','R)',':P',':O','O_o','R)',':D',':O',':z','<3',':(','B)',';)'],
[ 1, 4, 6, 7, 9, 16, 20, 21, 25, 28, 30, 31, 33, 39, 41, 42, 44, 52, 56, 57, 61, 64],
[ 3, 5, 6, 8, 15, 19, 20, 24, 27, 29, 30, 32, 38, 40, 41, 43, 51, 55, 56, 60, 63, 64],
[ 2, 0, 4, 0, 2, 0, 5, 0, 3, 0, 4, 0, 1, 0, 4, 0, 1, 0, 5, 0, 3, 0]);
my @wheel2 = ([';P',':)','R)',':(',';P',';)','<3',':(','B)',';)','R)',':P',':O','O_o','R)',':D',':O',':z','<3',':(','B)',';)'],
[ 1, 3, 5, 6, 8, 15, 19, 20, 24, 27, 29, 30, 32, 37, 39, 40, 42, 48, 52, 53, 57, 64],
[ 2, 4, 5, 7, 14, 18, 19, 23, 26, 28, 29, 31, 36, 38, 39, 41, 47, 51, 52, 56, 63, 64],
[ 2, 0, 4, 0, 2, 0, 5, 0, 3, 0, 4, 0, 1, 0, 4, 0, 1, 0, 5, 0, 3, 0]);
my @wheel3 = ([';P',':)','R)',':(',';P',';)','<3',':(','B)',';)','R)',':P',':O','O_o','R)',':D',':O',':z','<3',':(','B)',';)'],
[ 1, 4, 5, 6, 8, 14, 17, 19, 22, 27, 29, 31, 33, 39, 41, 42, 44, 52, 57, 58, 63, 64],
[ 3, 4, 5, 7, 13, 16, 18, 21, 26, 28, 30, 32, 38, 40, 41, 43, 51, 56, 57, 62, 63, 64],
[ 2, 0, 4, 0, 2, 0, 5, 0, 3, 0, 4, 0, 1, 0, 4, 0, 1, 0, 5, 0, 3, 0]);
my @payout = ([ 500,200, 75, 40,20,10, 5,2],
[1000,400,150, 80,40,20,10,4],
[6000,600,225,120,60,30,15,6]);
my $msg = "";
my @line = ();
my %wheel = ();
$wheel{1} = int(rand(64)) + 1;
for (my $i = 0;$i < 22;$i++) {
if ($wheel{1} >= $wheel1[1][$i] && $wheel{1} <= $wheel1[2][$i] ) {
$msg = $msg.$wheel1[0][$i]." ";
$line[0] = $wheel1[3][$i];
last;
}
}
$wheel{2} = int(rand(64)) + 1;
for (my $i = 0;$i < 22;$i++) {
if ($wheel{2} >= $wheel2[1][$i] && $wheel{2} <= $wheel2[2][$i] ) {
$msg = $msg.$wheel2[0][$i]." ";
$line[1] = $wheel2[3][$i];
last;
}
}
$wheel{3} = int(rand(64)) + 1;
for (my $i = 0;$i < 22;$i++) {
if ($wheel{3} >= $wheel3[1][$i] && $wheel{3} <= $wheel3[2][$i] ) {
$msg = $msg.$wheel3[0][$i];
$line[2] = $wheel3[3][$i];
last;
}
}
$logger->debug("Wheel1:$wheel{1} | Wheel2:$wheel{2} | Wheel3:$wheel{3}");
$logger->debug("Wheel1:$line[0] | Wheel2:$line[1] | Wheel3:$line[2]");
$logger->debug("Wheel1:$msg");
# Check for a winner.
my $win = false;
my $match = false;
my @symbol = (false,false,false);
my $winvalue= 0;
for (my $i = 0;$i < 3;$i++) {
$winvalue = $winvalue + $line[$i];
if ($line[$i] > 0) {
$symbol[$i] = true if ($line[$i] < 4);
if ($i == 2) {
if ($line[$i-2] == $line[$i] && $line[$i-1] == $line[$i]) {
$match = true;
} else {
$match = false;
}
} elsif ($i > 0 && $line[$i-1] == $line[$i]) {
$match = true;
} else {
$match = false;
}
$win = true;
} else {
$win = false;
$match = false;
last;
}
}
# Pay the winner
my $prize = 0;
if ($win) {
if ($match && $winvalue == 15) {
$prize = $payout[$arg-1][0];
} elsif ($match && ($winvalue == 12)) {
$prize = $payout[$arg-1][1];
} elsif (!$match && !($symbol[0]||$symbol[1]||$symbol[2])) {
$prize = $payout[$arg-1][2];
} elsif ($match && ($winvalue == 9)) {
$prize = $payout[$arg-1][3];
} elsif ($match && ($winvalue == 6)) {
$prize = $payout[$arg-1][4];
} elsif ($match && ($winvalue == 3)) {
$prize = $payout[$arg-1][5];
} elsif (!$match && ($symbol[0]&&$symbol[1]&&$symbol[2])) {
$prize = $payout[$arg-1][6];
} else {
$prize = $payout[$arg-1][7];
}
$msg = $msg." Winner! $nick has won $prize tokens!";
token_add("evepriceinfo",$prize,$nick);
$sth = $dbh->prepare('UPDATE TokenStats SET SlotTokensOut = SlotTokensOut + ? WHERE StatDate = current_date');
$sth->execute($prize);
$sth->finish;
}
$irc->yield(privmsg => $where, "$msg");
return;
}
sub irc_botcmd_deal {
my $nick = (split /!/, $_[ARG0])[0];
my ($where, $arg) = @_[ARG1, ARG2];
return if &tw_stream_online($where);
if (!$arg) {
$irc->yield(privmsg => $where, "/me - BlackJack, use 1 to 25 tokens.");
return;
}
if ($arg =~ /help/) {
$irc->yield(privmsg => $where, "/me - BlackJack, use 1 to 25 tokens.");
return;
}
if ($arg !~ /^\d+$/) {
$irc->yield(privmsg => $where, "/me - $nick, The number of tokens must be a number!");
return;
}
if ($arg < 1 || $arg > 25) {
$irc->yield(privmsg => $where, "/me - Must use 1 to 25 tokens for BlackJack.");
return;
}
my $max = $dbh->selectrow_array("SELECT Tokens FROM followers WHERE TwitchID = \"$nick\"");
if ($arg > $max) {
$irc->yield(privmsg => $where, "/me - $nick, you do not have $arg tokens!");
return;
}
my $lastbj = $dbh->selectrow_array("SELECT TTL FROM BJGame WHERE TwitchID = \"$nick\"");
my $duration;
if ($lastbj) {
my $dt1 = DateTime::Format::MySQL->parse_datetime($lastbj);
my $dt2 = DateTime->now(time_zone=>'local');
my $days = ($dt2 - $dt1)->days;
my $hours = ($dt2 - $dt1)->hours;
my $mins = ($dt2 - $dt1)->minutes;
my $secs = ($dt2 - $dt1)->seconds;
$duration = ($days * 86400) + ($hours * 3600) + ($mins * 60) + $secs;
} else {
newshoe($nick,$arg);
$irc->yield(privmsg => $where, "/me - $nick, starting with a new shoe!");
$duration = 3601;
}
my $sublevel = $dbh->selectrow_array("SELECT SubLevel FROM Rushlock_TwitchSubs WHERE TwitchName = \"$nick\"");
my $threshold = 300;
switch ($sublevel) {
case [1..100] {$threshold=60}
}
if (!timelimit($where,$nick,$threshold,$duration)) {
return;
}
token_take("evepriceinfo",$arg,$nick);
$sth = $dbh->prepare('UPDATE TokenStats SET BJTokensIn = BJTokensIn + ? WHERE StatDate = current_date');
$sth->execute($arg);
$sth->finish;
my $shoe;
my @curr_game = get_curr_game($nick);
# @curr_game structure
# [0] BJKey
# [1] TwitchID
# [2] Shoe
# [3] Hand
# [4] Dealer
# [5] Bet
# [6] TTL
my $CardsInShoe = length($curr_game[2])/2;
if ($CardsInShoe < 104) {
newshoe($nick,$arg);
$irc->yield(privmsg => $where, "/me - $nick, shuffling a new shoe!");
@curr_game = get_curr_game($nick);
}
if ($curr_game[3]) {
# There is a current valid game.
$irc->yield(privmsg => $where, "/me - $nick, you have a current game running.");
} else {
# Deal a new hand.
$irc->yield(privmsg => $where, "/me - New hand starting for $nick.");
deal($nick,$curr_game[2],$arg);
@curr_game = get_curr_game($nick);
if (valuehand($curr_game[3]) == 21 || valuehand($curr_game[4]) == 21) {
$irc->yield(privmsg => $where, "/me - ".eval_game($nick));
return;
}
}
$irc->yield(privmsg => $where, "/me - ".show_game($nick,0));
return;
}
sub irc_botcmd_hit {
my $nick = (split /!/, $_[ARG0])[0];
my $where = $_[ARG1];
return if &tw_stream_online($where);
my @curr_game = get_curr_game($nick);
if (!$curr_game[3]) {
$irc->yield(privmsg => $where, "/me - $nick, you do not have a current game!");
return;
}
$curr_game[3] = givecard($nick,$curr_game[2],$curr_game[3]);
my $value = valuehand($curr_game[3]);
if ($value > 21) {
$irc->yield(privmsg => $where, "/me - ".eval_game($nick));
} elsif ($value == 21) {
$irc->yield(privmsg => $where, "/me - ".eval_game($nick));
}else {
$irc->yield(privmsg => $where, "/me - ".show_game($nick,0));
}
return;
}
sub irc_botcmd_stand {
my $nick = (split /!/, $_[ARG0])[0];
my $where = $_[ARG1];
return if &tw_stream_online($where);
my @curr_game = get_curr_game($nick);
if (!$curr_game[3]) {
$irc->yield(privmsg => $where, "/me - $nick, you do not have a current game!");
return;
}
$irc->yield(privmsg => $where, "/me - ".eval_game($nick));
return;
}
sub irc_botcmd_doubledown {
my $nick = (split /!/, $_[ARG0])[0];
my $where = $_[ARG1];
return if &tw_stream_online($where);
my @curr_game = get_curr_game($nick);
if (!$curr_game[3]) {
$irc->yield(privmsg => $where, "/me - $nick, you do not have a current game!");
return;
}
my $max = $dbh->selectrow_array("SELECT Tokens FROM followers WHERE TwitchID = \"$nick\"");
if ($curr_game[5] > $max) {
$irc->yield(privmsg => $where, "/me - $nick, you do not have $curr_game[5] tokens to double down with!");
return;
}
if (length($curr_game[3]) == 4) {
token_take("evepriceinfo",$curr_game[5],$nick);
$sth = $dbh->prepare('UPDATE TokenStats SET BJTokensIn = BJTokensIn + ? WHERE StatDate = current_date');
$sth->execute($curr_game[5]);
$sth->finish;
$curr_game[5] = $curr_game[5] + $curr_game[5];
my $sth = $dbh->prepare('UPDATE BJGame SET Bet = ?, TTL = NULL WHERE TwitchID = ?');
$sth->execute($curr_game[5],$nick);
$sth->finish;
$curr_game[3] = givecard($nick,$curr_game[2],$curr_game[3]);
$irc->yield(privmsg => $where, "/me - ".eval_game($nick));
} else {
$irc->yield(privmsg => $where, "/me - You can only double down on your first two cards.")
}
return;
}
sub timelimit {
my $where = $_[0];
my $nick = $_[1];
my $threshold = $_[2];
my $duration = $_[3];
return true if is_owner($nick);
if ($duration < $threshold) {
if ($threshold == 60) {
$irc->yield(privmsg => $where, "/me - $nick, you can only play once every ".($threshold/60)." minute.");
return false;
} else {
$irc->yield(privmsg => $where, "/me - $nick, you can only play once every ".($threshold/60)." minutes. To unlock playing more often and other perks, consider subscribing here on Twitch, or supporting the channel on patreon.com/rushlock");
return false;
}
}
return true;
}
sub tw_stream_online {
my $channelname = $_[0];
$channelname =~ s/^#//g;
my $sth = $dbh->prepare('SELECT * from channel_status WHERE Channel like ?');
$sth->execute($channelname);
my @ref = $sth->fetchrow_array();
return true if ($ref[2] =~ m/Online/);
return false;
}
sub help {
return;
}