-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
196 lines (164 loc) · 5.44 KB
/
handler.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import json
from playoff import Playoff, PlayoffException
import json
import time
import os
client_id = os.environ.get('PLAYOFF_CLIENT_ID')
client_secret = os.environ.get('PLAYOFF_CLIENT_SECRET')
tokenTableName = os.environ.get('DYNAMODB_TOKEN_TABLE')
class TokenStorer:
_token = ''
def set_token_dynamo(self, token):
import boto3
dynamo_db = boto3.resource('dynamodb')
client = dynamo_db.Table(tokenTableName)
response = client.put_item(
Item={
"token": tokenTableName,
"access_token": token['access_token'],
"token_type": token['token_type'],
"expires_at": str(token['expires_at'])
}
)
def get_token_dynamo(self):
import boto3
dynamo_db = boto3.resource('dynamodb')
client = dynamo_db.Table(tokenTableName)
response = client.get_item(
Key={"token": tokenTableName}
)
res = response["Item"]
d = {
"access_token": res['access_token'],
"token_type": res['token_type'],
"expires_at": res['expires_at']
}
return d
def invalid_response(message):
return {"statusCode": 400, "body": {"message":message}}
def playoff_player_not_found_error_response(message):
return {"statusCode": 404, "body": {"message":message}}
def playoff_error_response(message):
return {"statusCode": 500, "body": {"message":message}}
def play_action(event, context):
token_storer = TokenStorer()
pl = Playoff(
client_id=client_id,
client_secret=client_secret,
type="client",
allow_unsecure=True,
store=lambda token: token_storer.set_token_dynamo(token),
load=lambda: token_storer.get_token_dynamo(),
)
if "challengeid" not in event:
return invalid_response("no challenge id specified")
if "player" not in event:
return invalid_response("no player specified")
if "choices" not in event or not isinstance(event["choices"], list):
return invalid_response("no choices specified")
try:
choices = {}
for choice in event["choices"]:
variable = choice["q"]
value = choice["a"]
choices[variable] = value
r = pl.post(
route="/runtime/actions/"+event["challengeid"]+"/play",
query={"player_id":event["player"]},
body={
"variables": choices
}
)
result = pl.get(route="/admin/players/"+event["player"],)
# print(result)
status = {}
completed_challenges = []
points = []
for score in result["scores"]:
if "metric" in score and "value" in score:
metric = score["metric"]
if "type" in metric and "name" in metric:
if metric["type"] == 'point' and " totale" not in metric["name"]:
status[metric["name"]] = score["value"]
# e = r["events"]["local"][0]
#
# if e["changes"] and isinstance(e["changes"], list):
# for change in e["changes"]:
# if change["metric"]["type"] == "point":
# status[change["metric"]["name"]] = change["delta"]["new"]
# elif change["metric"]["type"] == "set":
# completed_challenges.append(change["delta"])
response = {
"statusCode": 200,
# "playoff_response": r,
"body": { "world": {
"status": status,
"points": {
"total": 126,
"available": 34,
"upgrade": {
"casa": 45,
"mobilita": 15,
"vita": 0,
"tempo": 60
}
}
},
"challenges": {
"available": 52,
"completed": completed_challenges
},
"progress": {
"ranking": 0.56,
"params": {
"sicurezza": 0.45,
"salute": 0.55,
"sostenibilita": 0.76,
"risparmio": 0.23
}
}
}
}
except PlayoffException as err:
if err.name == 'player_not_found':
return playoff_player_not_found_error_response(err.message)
else:
return playoff_error_response(err.message)
return response
# def play_action_without_stored_token_retrieval(event, context):
#
#
# pl = Playoff(
# client_id=client_id,
# client_secret=client_secret,
# type="client",
# allow_unsecure=True,
#
# )
#
# r = pl.post(
# route="/runtime/actions/sfida1/play",
# query={"player_id":"max"},
# body={
# "variables": {
# "question": 1,
# "answer": 2
# }
# }
# )
#
# response = {
# "statusCode": 200,
# "body": json.dumps(r)
# }
#
# return response
#
#
#
def get_user_status(event, context):
response = {
"statusCode": 200,
"body": dict()
}
return response