forked from mailgyc/doudizhu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.py
73 lines (57 loc) · 1.83 KB
/
room.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
import logging
from core import Singleton
from core.table import Table
class Room(object):
def __init__(self, uid, allow_robot=True):
self.uid = uid
self.__waiting_tables = {}
self.__playing_tables = {}
self.allow_robot = allow_robot
self.entrance_fee = 100
logging.info('ROOM[%d] CREATED', uid)
def rsp_tables(self):
rsp = []
for _, t in self.waiting_tables.items():
rsp.append([t.uid, t.size()])
return rsp
def new_table(self):
t = Table(RoomManager.gen_table_id(), self)
self.waiting_tables[t.uid] = t
return t
def find_waiting_table(self, uid):
if uid == -1:
for _, table in self.waiting_tables.items():
return table
return self.new_table()
return self.waiting_tables.get(uid)
def on_table_changed(self, table):
if table.is_full():
self.waiting_tables.pop(table.uid, None)
self.playing_tables[table.uid] = table
if table.is_empty():
self.playing_tables.pop(table.uid, None)
self.waiting_tables[table.uid] = table
@property
def waiting_tables(self):
return self.__waiting_tables
@property
def playing_tables(self):
return self.__playing_tables
class RoomManager(object):
__metaclass__ = Singleton
__room_dict = {
1: Room(1, True),
2: Room(2, False),
}
__current_table_id = 0
@staticmethod
def gen_table_id():
RoomManager.__current_table_id += 1
return RoomManager.__current_table_id
@staticmethod
def find_room(uid, created=False):
room = RoomManager.__room_dict.get(uid)
if not room and created:
room = Room(uid)
RoomManager.__room_dict[uid] = room
return room