-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
132 lines (112 loc) · 4.44 KB
/
server.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
import asyncio
from coordinator import Game, NotAllowedException
from http.server import BaseHTTPRequestHandler, HTTPServer, HTTPStatus
import aiohttp
io = aiohttp.ClientSession()
import json
import logging
import threading
logging.basicConfig(level=logging.INFO)
from defs import *
def HandlerFactory(game: Game):
class Handler(BaseHTTPRequestHandler):
def body_json(self):
content_len = int(self.headers.get('Content-Length'))
post_body = self.rfile.read(content_len)
return json.loads(post_body)
def broadcast(self, s: str):
for player in game.players:
task = io.request(
'BROADCAST',
player.url(),
data=json.dumps({'message': s}))
asyncio.ensure_future(task)
def do_JOIN(self):
"""
A player wants to join the room.
Body format: { name: , port: }
IP is the same as this request is sent from.
"""
try:
addr, _ = self.client_address
body = self.body_json()
player = Player(body['name'], (addr, body['port']))
i = game.join(player)
self.send_response(HTTPStatus.OK, str(i))
self.end_headers()
except NotAllowedException:
logging.info('Room full. Not allowed to join the game.')
self.send_response(HTTPStatus.FORBIDDEN, 'Room full. You are not allowed to join.')
self.end_headers()
def do_SHUFFLED(self):
"""
A player has finished shuffling the deck.
"""
try:
body = self.body_json()
game.recv_shuffled(body['name'], body['deck'])
self.send_response(HTTPStatus.OK)
self.end_headers()
except NotAllowedException:
msg = '<{} IS TRYING TO CHEAT ITS NOT UR TURN TO SHUFFLE MATE>'.format(body['name'])
self.broadcast(msg)
self.send_response(HTTPStatus.FORBIDDEN)
self.end_headers()
def do_ENCRYPTED(self):
"""
A player has finished encrypting the deck.
"""
try:
body = self.body_json()
game.recv_encrypt(body['name'], body['deck'])
message_to_send = '{} has encrypted the deck'.format(body['name'])
self.broadcast(message_to_send)
self.send_response(HTTPStatus.OK)
self.end_headers()
except NotAllowedException:
message_to_send = '{} IS TRYING TO CHEAT ITS NOT UR TURN TO ENCRYPT MATE'.format(body['name'])
self.broadcast(message_to_send)
self.send_response(HTTPStatus.FORBIDDEN)
self.end_headers()
def do_PLAYED(self):
"""
A player has made a play decision.
"""
try:
body = self.body_json()
game.recv_played(body['name'], body['decision'], body['key'])
self.send_response(HTTPStatus.OK)
self.end_headers()
except NotAllowedException:
message_to_send = '{} IS TRYING TO CHEAT ITS NOT UR TURN TO PLAY MATE'.format(body['name'])
self.broadcast(message_to_send)
self.send_response(HTTPStatus.FORBIDDEN)
self.end_headers()
def do_REQUEST(self):
"""
Request to draw from a player.
"""
body = self.body_json()
game.recv_request_draw(body['name'], body['no'])
self.send_response(HTTPStatus.OK)
self.end_headers()
def do_RELEASE(self):
"""
A player is releasing a key
"""
body = self.body_json()
game.recv_release(body['name'], body['no'], body['key'])
self.send_response(HTTPStatus.OK)
self.end_headers()
return Handler
DEFAULT_ADDR = "127.0.0.1"
DEFAULT_PORT = 8123
def run_server(loop):
game = Game(N_PLAYERS, loop)
httpd = HTTPServer((DEFAULT_ADDR, DEFAULT_PORT), HandlerFactory(game))
logging.info('Blackjack room is open %s:%s', DEFAULT_ADDR, DEFAULT_PORT)
httpd.serve_forever()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
threading.Thread(target=loop.run_forever).start()
run_server(loop)