Skip to content

Commit

Permalink
Merge pull request #176 from hasegaw/feature/statink_submit_event
Browse files Browse the repository at this point in the history
Feature/statink submit event
  • Loading branch information
hasegaw committed Apr 14, 2016
2 parents 3478b2b + 390da72 commit a2db51c
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 8 deletions.
3 changes: 3 additions & 0 deletions ikalog/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ def create_context(self):
'engine': {
'frame': None,
'service': {
'call_plugins': self.call_plugins,
'call_plugins_later': self.call_plugins,
# For backward compatibility
'callPlugins': self.call_plugins,
}
},
Expand Down
12 changes: 12 additions & 0 deletions ikalog/outputs/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def on_inkopolis_lottery_done(self, context):
)
self.write_debug_log(sys._getframe().f_code.co_name, context, text=s)

# output: stat.ink
def on_output_statink_submission_done(self, context, params={}):
s = "url = %s" % params.get('url', None)
self.write_debug_log(sys._getframe().f_code.co_name, context, text=s)

def on_output_statink_submission_error(self, context, params={}):
s = "url = %s" % params.get('url', None)
self.write_debug_log(sys._getframe().f_code.co_name, context)

def on_output_statink_submission_dryrun(self, context, params={}):
self.write_debug_log(sys._getframe().f_code.co_name, context)

# UI support

def on_option_tab_create(self, notebook):
Expand Down
62 changes: 54 additions & 8 deletions ikalog/outputs/statink.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
# IkaLog Output Plugin for Stat.ink


def call_plugins_mock(event_name, params=None, debug=False):
pass


class StatInk(object):

def apply_ui(self):
Expand Down Expand Up @@ -532,7 +536,9 @@ def write_payload_to_file(self, payload, basename=None):
IkaUtils.dprint('%s: Failed to write msgpack file' % self)
IkaUtils.dprint(traceback.format_exc())

def _post_payload_worker(self, payload):
def _post_payload_worker(self, context, payload):
# This function runs on worker thread.

if self.dry_run == 'server':
payload['test'] = 'dry_run'

Expand All @@ -552,6 +558,8 @@ def _post_payload_worker(self, payload):
timeout=120.0, # Timeout (in sec)
)

# Post the payload

try:
req = pool.urlopen('POST', self.url_statink_v1_battle,
headers=http_headers,
Expand All @@ -561,10 +569,22 @@ def _post_payload_worker(self, payload):
# Handle incorrect certificate error.
IkaUtils.dprint('%s: SSLError, value: %s' % (self, e.value))

statink_reponse = json.loads(req.data.decode('utf-8'))
error = 'error' in statink_reponse
if error:
IkaUtils.dprint('%s: API Error occured')
# Error detection

error = False
try:
statink_response = json.loads(req.data.decode('utf-8'))
error = 'error' in statink_response
if error:
IkaUtils.dprint('%s: API Error occured')
except:
error = True
IkaUtils.dprint('%s: Stat.ink return non-JSON response')
statink_response = {
'error': 'Not a JSON response',
}

# Debug messages

if self.show_response_enabled or error:
IkaUtils.dprint('%s: == Response begin ==' % self)
Expand All @@ -579,7 +599,33 @@ def _post_payload_worker(self, payload):
)
)

def post_payload(self, payload, api_key=None):
# Trigger a event.

try:
call_plugins_func = \
context['engine']['service']['call_plugins_later']
except:
call_plugins_func = call_plugins_mock

if error:
call_plugins_func(
'on_output_statink_submission_error',
params=statink_response
)

elif statink_response.get('id', 0) == 0:
call_plugins_func(
'on_output_statink_submission_dryrun',
params=statink_response
)

else:
call_plugins_func(
'on_output_statink_submission_done',
params=statink_response
)

def post_payload(self, context, payload, api_key=None):
if self.dry_run == True:
IkaUtils.dprint(
'%s: Dry-run mode, skipping POST to stat.ink.' % self)
Expand All @@ -598,7 +644,7 @@ def post_payload(self, payload, api_key=None):
payload['apikey'] = api_key

thread = threading.Thread(
target=self._post_payload_worker, args=(payload,))
target=self._post_payload_worker, args=(context, payload))
thread.start()

def print_payload(self, payload):
Expand Down Expand Up @@ -676,7 +722,7 @@ def on_game_session_end(self, context):
if self.debug_writePayloadToFile:
self.write_payload_to_file(payload)

self.post_payload(payload)
self.post_payload(context, payload)

def on_game_killed(self, context):
self._add_event(context, {'type': 'killed'})
Expand Down
19 changes: 19 additions & 0 deletions ikalog/outputs/websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ def on_game_session_end(self, context):
'event': 'on_game_session_end',
})

# stat.ink
def on_output_statink_submission_done(self, context, params):
self._send_message({
'event': 'on_output_statink_submission_done',
'response': params,
})

def on_output_statink_submission_dryrun(self, context, params):
self._send_message({
'event': 'on_output_statink_submission_dryrun',
'response': params,
})

def on_output_statink_submission_error(self, context, params):
self._send_message({
'event': 'on_output_statink_submission_error',
'response': params,
})

def worker_func(self, websocket_server):
print(websocket_server)
self.application = tornado.web.Application([
Expand Down
47 changes: 47 additions & 0 deletions tools/websocket_client.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!-- very simple WS sample -->

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WebSocket Sample</title>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(function(){
// WebSocket作成
var ws = new WebSocket("ws://localhost:9090/ws");

// WebSocket open時のイベントハンドラ登録
ws.onopen = function(){
$("#log").prepend("&lt;onopen&gt; " + "<br/>");
}

// WebSocket message受信時のイベントハンドラ登録
ws.onmessage = function(message){
$("#log").prepend(message.data + "<br/>");
}

// WebSocket error時のイベントハンドラ登録
ws.onerror = function(){
$("#log").prepend("&lt;onerror&gt; " + "<br/>");
}

// WebSocket close時のイベントハンドラ登録
ws.onclose = function(){
$("#log").prepend("&lt;onclose&gt; " + "<br/>");
}

// Windowが閉じられた(例:ブラウザを閉じた)時のイベントを設定
$(window).unload(function() {
ws.onclose(); // WebSocket close
})

// キー入力時のイベントを設定
// $("#message").keyup(function(e){
// ws.send($("#message").val()); // WebSocketを使いサーバにメッセージを送信
// });

})
</script>
</head>
<body><input type="text" id="message" /><div id="log" /></body>
</html>

0 comments on commit a2db51c

Please sign in to comment.