generated from oracle-devrel/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathlive_client_receiver.py
117 lines (93 loc) · 4.6 KB
/
live_client_receiver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'''
@author jasperan
This file uses RabbitMQ message queues to communicate Live Client API data from one endpoint to the other. You can virtually put your consumer
wherever you want, and let it process the incoming data from the producer.
Requires an AutoGluon pretrained model as --path, and an IP address (can be localhost) for testing both the receiver
and the producer in the same network.
'''
import re
import pika, sys, os
import datetime
from autogluon.tabular import TabularPredictor, TabularDataset
import argparse
import pandas as pd
from pika.credentials import PlainCredentials
import json
cli_parser = argparse.ArgumentParser()
cli_parser.add_argument('-p', '--path', type=str, help='Path to predictor.pkl', required=True)
cli_parser.add_argument('-i', '--ip', type=str, help='IP address to make requests to', required=True)
args = cli_parser.parse_args()
# We load the AutoGluon model.
save_path = args.path # specifies folder to store trained models
_PREDICTOR = TabularPredictor.load(save_path)
def main():
try:
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', heartbeat=600, blocked_connection_timeout=300))
channel = connection.channel()
# declare queue, in case the receiver is initialized before the producer.
channel.queue_declare(queue='live_client')
def callback(ch, method, properties, body):
print('{} | MQ Received packet'.format(datetime.datetime.now()))
process_and_predict(body.decode())
# consume queue
channel.basic_consume(queue='live_client', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
except pika.exceptions.StreamLostError:
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', heartbeat=600, blocked_connection_timeout=300))
def process_and_predict(input):
json_obj = json.loads(input)
team_color = str()
for x in json_obj['allPlayers']:
if x['team'] == 'ORDER':
team_color = 'blue'
else:
team_color = 'red'
print('Team {}: {}'.format(team_color, x['championName']))
# Timestamp given by the Live Client API is in thousands of a second from the starting point.
timestamp = int(json_obj['gameData']['gameTime'] * 1000)
data = [
json_obj['activePlayer']['championStats']['magicResist'],
json_obj['activePlayer']['championStats']['healthRegenRate'],
json_obj['activePlayer']['championStats']['spellVamp'],
timestamp,
json_obj['activePlayer']['championStats']['maxHealth'],
json_obj['activePlayer']['championStats']['moveSpeed'],
json_obj['activePlayer']['championStats']['attackDamage'],
json_obj['activePlayer']['championStats']['armorPenetrationPercent'],
json_obj['activePlayer']['championStats']['lifeSteal'],
json_obj['activePlayer']['championStats']['abilityPower'],
json_obj['activePlayer']['championStats']['resourceValue'],
json_obj['activePlayer']['championStats']['magicPenetrationFlat'],
json_obj['activePlayer']['championStats']['attackSpeed'],
json_obj['activePlayer']['championStats']['currentHealth'],
json_obj['activePlayer']['championStats']['armor'],
json_obj['activePlayer']['championStats']['magicPenetrationPercent'],
json_obj['activePlayer']['championStats']['resourceMax'],
json_obj['activePlayer']['championStats']['resourceRegenRate']
]
sample_df = pd.DataFrame([data], columns=['magicResist', 'healthRegenRate', 'spellVamp', 'timestamp', 'maxHealth',
'moveSpeed', 'attackDamage', 'armorPenetrationPercent', 'lifesteal', 'abilityPower', 'resourceValue', 'magicPenetrationFlat',
'attackSpeed', 'currentHealth', 'armor', 'magicPenetrationPercent', 'resourceMax', 'resourceRegenRate'])
prediction = _PREDICTOR.predict(sample_df)
pred_probs = _PREDICTOR.predict_proba(sample_df)
#print(type(prediction))
#print(type(pred_probs))
expected_result = prediction.get(0)
if expected_result == 0:
print('Expected LOSS, {}% probable'.format(pred_probs.iloc[0][0] * 100))
else:
print('Expected WIN, {}% probable'.format(pred_probs.iloc[0][1] * 100))
print('Win/loss probability: {}%/{}%'.format(
pred_probs.iloc[0][1] * 100,
pred_probs.iloc[0][0] * 100
))
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)