contrib/nitrpg: add a listener tool to update the game from GithubEvents
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 3 Feb 2015 21:37:21 +0000 (22:37 +0100)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 4 Feb 2015 18:14:57 +0000 (19:14 +0100)
This listener must be running to keep the game up to date.

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

contrib/nitrpg/src/listener.nit [new file with mode: 0644]

diff --git a/contrib/nitrpg/src/listener.nit b/contrib/nitrpg/src/listener.nit
new file mode 100644 (file)
index 0000000..a77ec9f
--- /dev/null
@@ -0,0 +1,58 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This tool is runned to listen to `Github::Event` and update the game.
+module listener
+
+import statistics
+import reactors
+import github::hooks
+
+# `HookListener` that redirects events to a `Game` instance.
+class RpgHookListener
+   super HookListener
+
+       # Registered reactors list.
+       var reactors = new Array[GameReactor]
+
+       # Dispatch event to registered `reactors`.
+       redef fun apply_event(event) do
+               var game = new Game(api, event.repo)
+               # TODO handle verbosity with opts
+               game.verbose_lvl = 1
+               for reactor in reactors do reactor.react_event(game, event)
+       end
+end
+
+if args.length != 2 then
+       print "Error: missing argument"
+       print ""
+       print "Usage:"
+       print "listener <host> <port>"
+       exit 1
+end
+
+var host = args[0]
+var port = args[1].to_i
+
+var api = new GithubAPI(get_github_oauth)
+
+var listener = new RpgHookListener(api, host, port)
+listener.reactors.add new StatisticsReactor
+listener.reactors.add new PlayerReactor
+
+print "Listening events on {host}:{port}"
+listener.listen