aboutsummaryrefslogtreecommitdiff
path: root/lib/Matrix
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@gmail.com>2020-03-31 20:38:41 -0300
committerMatias Linares <matiaslina@gmail.com>2020-03-31 20:38:41 -0300
commit6671b847b432aca57cf29979dbce45a69d932322 (patch)
treee7107de45b35e7a4dc707d4e76e8f24213486394 /lib/Matrix
downloaddeprecated-bots-master.tar.gz
Initial commitHEADmaster
Diffstat (limited to 'lib/Matrix')
-rw-r--r--lib/Matrix/Bot/DeprecatedOrg.pm656
-rw-r--r--lib/Matrix/Bot/Plugin/Dominating.pm696
2 files changed, 152 insertions, 0 deletions
diff --git a/lib/Matrix/Bot/DeprecatedOrg.pm6 b/lib/Matrix/Bot/DeprecatedOrg.pm6
new file mode 100644
index 0000000..d7daea8
--- /dev/null
+++ b/lib/Matrix/Bot/DeprecatedOrg.pm6
@@ -0,0 +1,56 @@
+use v6.c;
+unit class Matrix::Bot::DeprecatedOrg:ver<0.0.1>:auth<cpan:MATIASL>;
+
+use Config;
+use Matrix::Bot;
+use Matrix::Bot::Plugin::Dominating;
+
+
+sub MAIN($config-file where *.IO.f) is export {
+ my $config = Config.new().read($config-file);
+
+ my %matrix-config = $config<matrix>;
+ dd %matrix-config;
+ dd $config<dominating>;
+
+ my $bot = Matrix::Bot.new(
+ home-server => %matrix-config<server>,
+ username => %matrix-config<username>,
+ password => %matrix-config<password>,
+ plugins => [
+ Matrix::Bot::Plugin::Dominating.new(:config($config<dominating>))
+ ],
+ );
+
+ $bot.run;
+}
+
+=begin pod
+
+=head1 NAME
+
+Matrix::Bot::DeprecatedOrg - blah blah blah
+
+=head1 SYNOPSIS
+
+=begin code :lang<perl6>
+
+use Matrix::Bot::DeprecatedOrg;
+
+=end code
+
+=head1 DESCRIPTION
+
+Matrix::Bot::DeprecatedOrg is ...
+
+=head1 AUTHOR
+
+Matias Linares <matiaslina@gmail.com>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2020 Matias Linares
+
+This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
+
+=end pod
diff --git a/lib/Matrix/Bot/Plugin/Dominating.pm6 b/lib/Matrix/Bot/Plugin/Dominating.pm6
new file mode 100644
index 0000000..2d1cb85
--- /dev/null
+++ b/lib/Matrix/Bot/Plugin/Dominating.pm6
@@ -0,0 +1,96 @@
+use Config;
+use HTTP::UserAgent;
+use JSON::Fast;
+use Matrix::Bot::Plugin;
+use Matrix::Bot::Event;
+
+unit class Matrix::Bot::Plugin::Dominating is Matrix::Bot::Plugin;
+
+
+has HTTP::UserAgent $!ua;
+has Int $!last-updated;
+has $!current-player = "d365noscope";
+has $.game-id is rw;
+has %.config;
+
+has %!matrix-players =:baco<Baco>, :matiaslina<Matias>, :YareYare<huaira>, :nstefani<nico_st>, :fedediaz<fede_diaz>, :d365noscope<Manu>;
+
+submethod BUILD(:%!config, :$!game-id) {
+ $!ua = HTTP::UserAgent.new();
+ $!last-updated = DateTime.now.posix;
+}
+
+method current-matrix-player {
+ %!matrix-players{$!current-player} // "$!current-player";
+}
+
+method generate-events(Str $message) {
+ %.config<rooms>.map(
+ -> $room-id { Matrix::Bot::Event.new(:$room-id, :$message) }
+ )
+}
+
+method current-game {
+ $.game-id // %.config<starting_game>
+}
+
+multi method handle-room-text($e where *.message.starts-with("!dominating") --> Str) {
+ if $e.message ~~ m/"!dominating change-game " $<game-id> = [ \d+ ]/ {
+ say "Changing game id from {$.current-game} to {$<game-id>}";
+ my $pre-game-id = $!game-id;
+ $!game-id = $<game-id>;
+ return "Changing game id from {$pre-game-id} to {$<game-id>}";
+ }
+}
+
+multi method event-handler(--> Supply) {
+ my $handler = supply {
+ my $last-updated = now.Int;
+ loop {
+ CATCH {
+ when X::AdHoc {
+ .message.note;
+ sleep 60;
+ next;
+ }
+ default { .^name.say; .resume }
+ }
+ my %form = last_update => ~$last-updated;
+ my $response = $!ua.post(
+ "https://dominating12.com/game/{$.current-game}/play/update-state",
+ %form
+ );
+
+ if $response.is-success {
+ my $data = from-json($response.content);
+
+ $last-updated = $data<now>.Int;
+
+ my %players = $data<players>.values.map({ .<user_id> => .<username> }).List;
+ my $turn = %players{$data<turns>.head.<user_id>};
+
+ if $turn.defined && $turn eq "" {
+ say "[{DateTime.now()}] {$.current-matrix-player} won!";
+ emit($_) for self.generate-events("{$.current-matrix-player} gano!");
+ exit 0;
+ }
+
+ if $turn.defined && $turn ne $!current-player {
+ say "[{DateTime.now()}] Changing turn from $!current-player to $turn";
+ $!current-player = $turn;
+ if $!current-player ne "" {
+ emit($_) for self.generate-events("Le toca a $.current-matrix-player !");
+ }
+ } else {
+ say "[{DateTime.now()}] Still turn of $!current-player";
+ }
+
+ sleep 60 * 5;
+ } else {
+ note "Error response: {$response.status-line}";
+ }
+ }
+ }
+
+ return $handler;
+}