From badebceeeaeffa645173a500c2f10eb2ba30b3f8 Mon Sep 17 00:00:00 2001 From: Dan Langford <721364+danlangford@users.noreply.github.com> Date: Tue, 29 Dec 2020 12:09:23 -0700 Subject: [PATCH] initial ideas to discuss game event notifications --- src/engine/BMInterface.php | 11 +++++++ src/engine/BMInterfaceGameAction.php | 48 +++++++++++++++++++++++++--- src/notify/sns.php | 36 +++++++++++++++++++++ test/src/notify/sns.test.php | 33 +++++++++++++++++++ 4 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 src/notify/sns.php create mode 100644 test/src/notify/sns.test.php diff --git a/src/engine/BMInterface.php b/src/engine/BMInterface.php index b571f6ea9..001d77fe1 100644 --- a/src/engine/BMInterface.php +++ b/src/engine/BMInterface.php @@ -45,6 +45,13 @@ class BMInterface { */ protected static $conn = NULL; + /** + * Communication to notification service + * + * @var SnsClient + */ + protected static $snsClient = NULL; + /** * Owning BMInterface, allows back navigation * @@ -73,10 +80,13 @@ public function __construct($isTest = FALSE) { if ($isTest) { require_once __DIR__.'/../../test/src/database/mysql.test.inc.php'; + require_once __DIR__.'/../../test/src/notify/sns.test.php'; } else { require_once __DIR__.'/../database/mysql.inc.php'; + require_once __DIR__.'/../notify/sns.php'; } self::$conn = conn(); + self::$snsClient = snsClient(); } /** @@ -98,6 +108,7 @@ public function cast($className) { $result->message = $this->message; $result->timestamp = $this->timestamp; $result::$conn = self::$conn; + $result::$snsClient = self::$snsClient; return $result; } diff --git a/src/engine/BMInterfaceGameAction.php b/src/engine/BMInterfaceGameAction.php index 757957d57..2f848fcc4 100644 --- a/src/engine/BMInterfaceGameAction.php +++ b/src/engine/BMInterfaceGameAction.php @@ -80,10 +80,11 @@ public function load_game_action_log(BMGame $game, $logEntryLimit) { } /** - * Save action log entries generated during the action in progress + * Save and publish action log entries generated during the action in progress * - * Any game action entries which were generated should both be loaded into - * the message so the calling player can see them, and saved into the database + * Any game action entries which were generated should be loaded into + * the message so the calling player can see them, saved into the database, + * and published to notification service * * @param BMGame $game * @return void @@ -92,6 +93,8 @@ public function save_action_log($game) { if (count($game->actionLog) > 0) { $this->load_message_from_game_actions($game); $this->log_game_actions($game); + $this->notify_game_actions($game); + $game->empty_action_log(); } } @@ -924,7 +927,6 @@ protected function log_game_actions(BMGame $game) { $this->$typefunc($log_id, $gameAction->params); } } - $game->empty_action_log(); } /** @@ -956,4 +958,42 @@ protected function load_message_from_game_actions(BMGame $game) { $this->set_message($message); } } + + /** + * Publish new game action entries to notification service (SNS) + * + * @param BMGame $game + * @return void + */ + protected function notify_game_actions(BMGame $game) { + + // function load_message_from_game_actions() already prepared a friendly message + // TODO: consider if constructing a more programmatic (json) message would be appropriate + + $message = $this->message; + + // TODO consider best way to get topic. possibly in sns.php + // maybe creating own wrapper object to wrap up SnSClient & topicArn + $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; + + try { + $result = self::$snsClient->publish([ + 'Message' => $message, + 'TopicArn' => $topic, + 'MessageAttributes' => [ + 'gameId' => [ + 'DataType' => 'String', + 'StringValue' => $game->gameId + ] + ] + ]); + // TODO: analyze result + } catch (AwsException $e) { + // output error message if fails + // TODO: what is the buttonweavers approach to error logging? + // TODO: consider swallowing all errors an treating notifications as optional + // notifications should probably not impede any other functionality + error_log($e->getMessage()); + } + } } diff --git a/src/notify/sns.php b/src/notify/sns.php new file mode 100644 index 000000000..40374e68a --- /dev/null +++ b/src/notify/sns.php @@ -0,0 +1,36 @@ + 'buttonweavers', + 'region' => 'us-east-1', + 'version' => 'latest' + ]); + + return $SnSclient; +} diff --git a/test/src/notify/sns.test.php b/test/src/notify/sns.test.php new file mode 100644 index 000000000..7c80b7ff0 --- /dev/null +++ b/test/src/notify/sns.test.php @@ -0,0 +1,33 @@ + 'buttonweavers', + 'region' => 'us-east-1', + 'version' => '2010-03-31' + ]); + + return $SnSclient; +}