-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_controller.py
97 lines (74 loc) · 2.86 KB
/
game_controller.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
import game, asyncio, json, uuid
from player import Player
class GameController:
def __init__(self, debug=False):
self.waiting_players = {} # Mapping of Player ID to Player object
self.active_games = {} # Mapping of Game ID to Game object
self.active_players = {} # Mapping of PlayerID to Game ID
self.debug = debug
def new_player(self, name, ws):
"""
Whenever a player is created, it is automatically added to the waiting area
:param name:
:param ws:
:return:
"""
p_id = uuid.uuid4().hex
p = Player(p_id, name, ws)
self.waiting_players[p_id] = p
self.send_personal(ws, "handshake", name, p_id)
self.render_active_games()
return p
def player_disconnected(self, player):
game_id = self.active_players[player.id]
self.active_games[game_id].player_disconnected(player)
del self.active_players[player.id]
if self.active_games[game_id].finished:
del self.active_games[game_id]
def add_to_waiting_area(self, player):
self.waiting_players[player.id] = player
def add_to_existing_game(self, game_id, player):
"""
Adds a player to the game with id = game_id
:param game_id:
:param player:
:return: Game instance
"""
self.active_games[game_id].join(player)
del self.waiting_players[player.id]
self.active_players[player.id] = game_id
self.send_personal(player.ws, "joined_game")
self.render_active_games()
return self.active_games[game_id]
def create_new_game(self):
game_id = uuid.uuid4().hex
new_game = game.Game(game_id, debug=self.debug)
self.active_games[game_id] = new_game
return new_game
def terminate_game(self, game_id):
"""
Happens when there are 0 players left in a game.
:param game_id:
:return:
"""
del self.active_games[game_id]
def render_active_games(self):
games = {}
for g_id, g in self.active_games.items():
players = [player.name for player in g.players.values()]
games[g_id] = players
message = "render_active_games"
self.send_all(message, games)
def send_personal(self, ws, *args):
msg = json.dumps(args)
if self.debug:
print("(gamecontroller) sending message: " + msg)
asyncio.ensure_future(ws.send_str(msg))
def send_all(self, *args):
# TODO: make this asynchronous, await the send_str
msg = json.dumps(args)
if self.debug:
n = len(self.waiting_players.values())
print("(gamecontroller) sending message to all ({} players): {}".format(n, msg))
for player in self.waiting_players.values():
asyncio.ensure_future(player.ws.send_str(msg))