-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmember.py
53 lines (39 loc) · 1.36 KB
/
member.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
import asyncio
from utils import send_object, recv_object
class Member:
def __init__(self, ws, name):
self.ws = ws
self.name = name
self.hand = []
self.handLimit = 7
self.score = 0
self.queuedBroadcasts = asyncio.Queue()
self.queuedReplys = asyncio.Queue()
self.chatMessageHandler = None
def fillHand(self, deck):
while len(self.hand) < self.handLimit:
self.hand.append(deck.pop())
async def addObj(self, obj):
await self.queuedBroadcasts.put(obj)
async def getObj(self):
return await self.queuedReplys.get()
async def manualBroadcast(self):
await send_object(self.ws, await self.queuedBroadcasts.get())
async def broadcastLoop(self):
while True:
await self.manualBroadcast()
async def manualRead(self):
obj = await recv_object(self.ws)
if "action" in obj and obj["action"] == "sendChat" and self.chatMessageHandler != None:
await self.chatMessageHandler(obj, self)
else:
await self.queuedReplys.put(obj)
async def readLoop(self):
while True:
await self.manualRead()
async def runGameLoop(self):
broadcastLoop = asyncio.ensure_future(self.broadcastLoop())
readLoop = asyncio.ensure_future(self.readLoop())
done, pending = await asyncio.wait([broadcastLoop, readLoop], return_when=asyncio.FIRST_COMPLETED)
for task in pending:
task.cancel()