-
Notifications
You must be signed in to change notification settings - Fork 0
/
round_workflow.py
265 lines (221 loc) · 9.81 KB
/
round_workflow.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import asyncio, logging, time, sys, temporalio, os
from modules.payloads.rounds import Round, WorkflowRound, RoundCreated, RoundStatus
from modules.workflows.players import player_create, player_auth, player_round
from modules.workflows.rounds import start, round_customers, round_sent_user, round_search, round_confirmations, round_report, round_level, round_handshake
from pydantic.typing import Union
from modules.levels import Levels
from modules.workflows.round import RoundWorkflow
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
level_info = Levels(0)
print(level_info.customers().get("users"))
print(level_info.customers().get("count"))
print(level_info.customers().get("count") * level_info.customers().get("users"))
# test data to kick off this player and round
username:str = "test_user"
level:int = 0
#
# Setup the player
#
print("setup player")
auth_token:str = asyncio.run(player_create.setup_player(
username=username))
print(f"Received auth_token: {auth_token}")
# # @todo we need the player to have the current round workflow id stored, so that we can get to it easily
#
# Start the player's round
#
print(f"check auth for {username}:{auth_token}")
auth_valid:bool = asyncio.run(player_auth.check(
username=username,
auth_token=auth_token))
#this will help us control the flow below, for mocking out steps
round_status = None
if True == auth_valid:
# at this point, the user will send their information about
# their server and the level they want
round:Round = Round(
path="http://127.0.0.1:8000",
level=level
)
# check if the user has an active round, by querying the player workflow
# if they do have a round, we will get everything in the Round payload
# Round:
# path
# level
# workflow_id
# round_id
print("Check whether the player has a round already")
try:
round_status:Union[bool, WorkflowRound] = asyncio.run(
player_round.get_current_round(username)
)
except temporalio.client.WorkflowQueryFailedError as err:
sys.exit()
if False != round_status:
print("Player has a current round, should only happen when we have an active round, because we will update on round start and round end")
#@todo in the API, return the data about the current round
print(f"Let's get the round the player asked for from {round}")
# This will get the round and update the player with the round info
round_data:RoundCreated = asyncio.run(start.start(
username = username,
level = level,
round = round
))
print(round_data)
logging.info(round_data)
if None != round_data.status_code:
status_code = round_data.status_code
body = {
"message": round_data.message
}
print(f"the handshake failed with {status_code} :: {body}")
sys.exit() # stop processing
round_status = RoundStatus(
status = round_data.status,
path = round_data.path,
level = round_data.level,
round = round_data.round
)
if type(round_data.round) == bool:
print("Got a failure when creating the round, username didn't get returned properly")
sys.exit()
round_current_level = asyncio.run(
round_level.get_level(
username=username,
level=level
)
)
logging.info(f"Round current level {round_current_level}")
print(f"Round Status: {round_status}")
# this is to kill execution if something went wrong in this testing script
if None == round_status:
raise Exception("Something happened in the setup steps before we got customers")
#
# Gameplay has started at this point, we will get the customers for our testing
# the user will receive a customer/user paid over their ngrok URL
#
print("get_customers()")
try:
customers = asyncio.run(round_customers.get_all_customers(
username=username,
level=level)
)
except temporalio.client.WorkflowQueryFailedError as err:
print(err)
sys.exit()
print("got the customers")
print(f"{customers}")
# loop through the customers we have from the workflow
# this simulates waht the player will have to do in their code, one pass through these steps per user received
for customerid, users in customers['users'].items():
logging.info(f"Found customer: {customerid}")
# @todo build in cancellations to this test
confirmations = {}
for userid, user in users.items():
logging.info(f"Starting Background check for user: {userid}")
logging.info(f"Send sent_user signal")
try:
asyncio.run(round_sent_user.sent_user(
username=username,
level=level,
customerid=customerid,
userid=userid
))
except temporalio.client.WorkflowQueryFailedError as err:
sys.exit()
#
# For each user, we have to run the checks
# Each search is a signal to start, then a query to verify
# Notifications blocks the other queries and should be done first
# if the user has not rejected, then we can go to the next search
# for level 0, there are no rejections
#
search_name = "notification"
logging.info(f"Starting {userid}'s {search_name} search")
# there will be one endpoint for starting the search
try:
asyncio.run(round_search.start(
username=username,
level=level,
search_name=search_name,
customerid=customerid,
userid=userid,
current_time=time.time()
))
except temporalio.client.WorkflowQueryFailedError as err:
sys.exit()
# players will implement this loop in their code, we will not
# implement this loop in the api endpoint
search_status = {"status": "pending"}
while search_status["status"] != "complete":
# the other endpoint will be to get the status
try:
search_status = asyncio.run(round_search.status(
username=username,
level=level,
search_name=search_name,
customerid=customerid,
userid=userid,
current_time=time.time()
))
except temporalio.client.WorkflowQueryFailedError as err:
sys.exit()
print(f"{search_name} status is {search_status}")
confirmations["notification"] = search_status["confirmation"]
#@todo add checks that the 'notification' is completed by the player before they try to do these next checks
print(f"Time to do the other searches!")
for search_name in [ "ssn", "credit", "social"]:
logging.info(f"Starting {userid}'s {search_name} search")
# there will be one endpoint for starting the search
try:
asyncio.run(round_search.start(
username=username,
level=level,
search_name=search_name,
customerid=customerid,
userid=userid,
current_time=time.time()
))
except temporalio.client.WorkflowQueryFailedError as err:
sys.exit()
# players will implement this loop in their code, we will not
# implement this loop in the api endpoint
search_status = {"status": "pending"}
while search_status["status"] == "pending":
# the other endpoint will be to get the status
try:
search_status = asyncio.run(round_search.status(
username=username,
level=level,
search_name=search_name,
customerid=customerid,
userid=userid,
current_time=time.time()
))
except temporalio.client.WorkflowQueryFailedError as err:
sys.exit()
print(f"{search_name} status is {search_status}")
confirmations[search_name] = search_status["confirmation"]
print(f"Confirmations: {confirmations}")
asyncio.run(round_confirmations.send_confirmations(
username=username,
level=level,
customerid=customerid,
userid=userid,
confirmations=confirmations
))
round_current_level = asyncio.run(round_level.get_level(username=username, level=level))
logging.info(f"Round current level {round_current_level}")
print("get_reports()")
player_round_report = None
player_round_report = asyncio.run(
round_report.get_report(
username=username,
level=level,
path=round.path,
RoundWorkflow=RoundWorkflow
)
)
message = f"Player Round Report: {player_round_report}"
logging.info(message)