forked from kennykytran/Web-Back-End-Project4
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredis_client.py
100 lines (73 loc) · 2.96 KB
/
redis_client.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
import collections
import dataclasses
import databases
import redis
import httpx
import json
import socket
import time
from time import sleep
from quart import Quart, request, abort, g
from quart_schema import QuartSchema, RequestSchemaValidationError, validate_request
app = Quart(__name__)
QuartSchema(app)
res = None
seconds = 0
while res is None:
try:
data = {'url':'http://127.0.0.1:5400/results'}
res = httpx.post("http://"+socket.getfqdn("127.0.0.1:5100/subscribe"),data=json.dumps(data), headers={'Content-Type': 'application/json'})
print(res)
except httpx.RequestError:
time.sleep(3.5)
print("Game service is not ready yet, give it a moment.")
time.sleep(2.0)
redis_client = redis.Redis(host='localhost', port=6379, db=0, charset='utf-8', decode_responses=True)
@dataclasses.dataclass
class LeaderboardInformation:
username: str
result: str
guesses: int
@app.route("/results", methods=["POST"])
@validate_request(LeaderboardInformation)
async def user_data(data):
results = dataclasses.asdict(data)
username = results['username']
num_guesses = results['guesses']
win_loss = results['result']
score, average_score, num_games = 0, 0, 1
if num_guesses > 6 or num_guesses < 1:
return {'Error':"Incorrect guesses! (out of bounds)"}
else:
if win_loss == 'win':
get_score = {1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 1}
score = get_score[num_guesses]
elif win_loss == 'loss':
score = 0
else:
return {'Error' : 'Incorrect result string'}, 404
if redis_client.hget('Scores', 'username') == username:
score = int(redis_client.hget("Scores", 'score')) + score
num_games = int(redis_client.hget("Scores", 'game_count')) + num_games
average_score = int(score/num_games)
redis_client.hset('Scores', 'username', username)
redis_client.hset('Scores', 'game_count', num_games)
redis_client.hset('Scores', 'result', win_loss)
redis_client.hset('Scores', 'score', score)
redis_client.hset('Scores', 'average_score', average_score)
value = redis_client.zadd("LeaderBoard", {username : average_score})
else:
redis_client.hset('Scores', 'username', username)
redis_client.hset('Scores', 'game_count', num_games)
redis_client.hset('Scores', 'result', win_loss)
redis_client.hset('Scores', 'score', score)
redis_client.hset('Scores', 'average_score', score)
value = redis_client.zadd("LeaderBoard" , {username : score})
return redis_client.hgetall('Scores'), 200
@app.route("/LeaderBoard/", methods=["GET"])
async def scores():
scorestop = redis_client.zrange("LeaderBoard", 0, 9, desc = True, withscores = True)
if scorestop != []:
return ('\n'.join(map(str, scorestop))), 200
else:
return {"Error": "No data"}, 404