From 6671b847b432aca57cf29979dbce45a69d932322 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Tue, 31 Mar 2020 20:38:41 -0300 Subject: Initial commit --- lib/Matrix/Bot/Plugin/Dominating.pm6 | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 lib/Matrix/Bot/Plugin/Dominating.pm6 (limited to 'lib/Matrix/Bot/Plugin') 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, :matiaslina, :YareYare, :nstefani, :fedediaz, :d365noscope; + +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.map( + -> $room-id { Matrix::Bot::Event.new(:$room-id, :$message) } + ) +} + +method current-game { + $.game-id // %.config +} + +multi method handle-room-text($e where *.message.starts-with("!dominating") --> Str) { + if $e.message ~~ m/"!dominating change-game " $ = [ \d+ ]/ { + say "Changing game id from {$.current-game} to {$}"; + my $pre-game-id = $!game-id; + $!game-id = $; + return "Changing game id from {$pre-game-id} to {$}"; + } +} + +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.Int; + + my %players = $data.values.map({ . => . }).List; + my $turn = %players{$data.head.}; + + 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; +} -- cgit v1.2.3-70-g09d2