async def draw(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_draw(1)) return if not roommgr().roomexists(c.room): c.room = None await c.ws.send(protoassembler.get_resp_draw(2)) return r = roommgr().getroom(c.room) if not r.clientinroom(c) or not r.clientisdrawer(c): await c.ws.send(protoassembler.get_resp_draw(3)) return if not r.isinround(): await c.ws.send(protoassembler.get_resp_draw(4)) return if 'STEPS' not in req: await c.ws.send(protoassembler.get_resp_draw(5)) return steps = req['STEPS'] if type(steps) is not list: await c.ws.send(protoassembler.get_resp_draw(6)) return r.draw(steps) l = r.getbroadcastclientlist() await broadcastmsg(l, protoassembler.get_broadcast_draw(steps))
async def oncancelready(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_cancel_ready(1)) return if not roommgr().roomexists(c.room): c.room = None await c.ws.send(protoassembler.get_resp_cancel_ready(2)) return r = roommgr().getroom(c.room) if not r.clientinroom(c): c.room = None await c.ws.send(protoassembler.get_resp_cancel_ready(3)) return if r.clientisviewer(c): await c.ws.send(protoassembler.get_resp_cancel_ready(4)) return if r.isinmatch(): await c.ws.send(protoassembler.get_resp_cancel_ready(5)) return r.setplayerready(c, False) await c.ws.send(protoassembler.get_resp_cancel_ready(0)) l = r.getbroadcastclientlist() await broadcastmsg( l, protoassembler.get_broadcast_player_cancel_ready( r.getplayerstat(c).getinfo()))
async def quitroom(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_quit_room(1)) return if not roommgr().roomexists(c.room): c.room = None await c.ws.send(protoassembler.get_resp_quit_room(2)) return r = roommgr().getroom(c.room) if not r.clientinroom(c): c.room = None await c.ws.send(protoassembler.get_resp_quit_room(1)) return isviewer = r.clientisviewer(c) isdrawer = r.clientisdrawer(c) r.quitroom(c) await c.ws.send(protoassembler.get_resp_quit_room(0)) if isviewer: await broadcastmsg(r.getbroadcastclientlist(), protoassembler.get_broadcast_viewer_exit(c)) else: await broadcastmsg(r.getbroadcastclientlist(), protoassembler.get_broadcast_player_exit(c)) if isdrawer and r.isinround(): await processroundover(r) if r.isempty(): roommgr().removeroom(r.id)
async def ondisconnected(ws, quitroom=True): print('unregister ws: ', ws) if ws not in CLIENTS: return c = CLIENTS[ws] if quitroom: if c.room is not None: if roommgr().roomexists(c.room): r = roommgr().getroom(c.room) isviewer = r.clientisviewer(c) isdrawer = r.clientisdrawer(c) r.quitroom(c) if isviewer: await broadcastmsg( r.getbroadcastclientlist(), protoassembler.get_broadcast_viewer_exit(c)) else: await broadcastmsg( r.getbroadcastclientlist(), protoassembler.get_broadcast_player_exit(c)) if isdrawer and r.isinround(): await processroundover(r) if r.isempty(): roommgr().removeroom(r.id) del CLIENTS[ws]
async def joingame(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_join_game(1)) return if not roommgr().roomexists(c.room): await c.ws.send(protoassembler.get_resp_join_game(2)) return r = roommgr().getroom(c.room) if r.isplayerfull(): await c.ws.send(protoassembler.get_resp_join_game(3)) return if not r.clientisviewer(c): await c.ws.send(protoassembler.get_resp_join_game(4)) return if r.isinmatch(): await c.ws.send(protoassembler.get_resp_join_game(5)) return r.quitroom(c) r.joinasplayer(c) await c.ws.send(protoassembler.get_resp_join_game(0)) l = r.getbroadcastclientlist() await broadcastmsg( l, protoassembler.get_broadcast_viewer_join_game( r.getplayerstat(c).getinfo()))
async def oncreateroom(c, req): if 'NAME' not in req: await c.ws.send(protoassembler.get_resp_create_room(1, None)) return rname = req['NAME'] if type(rname) is not str: await c.ws.send(protoassembler.get_resp_create_room(1, None)) return if 'ICON' not in req: await c.ws.send(protoassembler.get_resp_create_room(2, None)) return ricon = req['ICON'] if type(ricon) is not str: await c.ws.send(protoassembler.get_resp_create_room(2, None)) return if 'MAXPLAYERS' not in req: await c.ws.send(protoassembler.get_resp_create_room(3, None)) return maxplayers = req['MAXPLAYERS'] if type(maxplayers) is not int and type(maxplayers) is not float: await c.ws.send(protoassembler.get_resp_create_room(3, None)) return maxplayers = int(maxplayers) if 'MATCHOVERSCORE' not in req: await c.ws.send(protoassembler.get_resp_create_room(4, None)) return matchoverscore = req['MATCHOVERSCORE'] if type(matchoverscore) is not int and type(matchoverscore) is not float: await c.ws.send(protoassembler.get_resp_create_room(4, None)) return matchoverscore = int(matchoverscore) if c.room is not None: await c.ws.send(protoassembler.get_resp_create_room(5, None)) return r = None if 'PWD' in req: r = roommgr().createroom(rname, ricon, maxplayers, matchoverscore, req['PWD']) else: r = roommgr().createroom(rname, ricon, maxplayers, matchoverscore, None) r.joinasplayer(c) await c.ws.send( protoassembler.get_resp_create_room(0, r.getroombriefinfo()))
async def send_answer(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_send_answer(1, False)) return if not roommgr().roomexists(c.room): c.room = None await c.ws.send(protoassembler.get_resp_send_answer(2, False)) return r = roommgr().getroom(c.room) if not r.clientinroom(c): c.room = None await c.ws.send(protoassembler.get_resp_send_answer(3, False)) return if r.clientisviewer(c): await c.ws.send(protoassembler.get_resp_send_answer(4, False)) return if r.clientisdrawer(c): await c.ws.send(protoassembler.get_resp_send_answer(5, False)) return if not r.isinround(): await c.ws.send(protoassembler.get_resp_send_answer(6, False)) return if r.answer is None: await c.ws.send(protoassembler.get_resp_send_answer(7, False)) return answer = req['ANSWER'] l = r.getbroadcastclientlist() if answer != r.answer: await c.ws.send(protoassembler.get_resp_send_answer(0, False)) await broadcastmsg( l, protoassembler.get_broadcast_answer_wrong(c.getinfo(), answer)) else: r.playeranswercorrect(c) playerstat = r.getplayerstat(c) drawerstat = r.getdrawerstat() await c.ws.send(protoassembler.get_resp_send_answer(0, True)) await broadcastmsg( l, protoassembler.get_broadcast_answer_correct(c.getinfo())) await broadcastmsg( l, protoassembler.get_broadcast_player_stat_update( [playerstat.getinfo(), drawerstat.getinfo()])) if r.isallplayercorrect(): await processroundover(r)
async def onreadyforplay(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_ready_for_play(1)) return if not roommgr().roomexists(c.room): c.room = None await c.ws.send(protoassembler.get_resp_ready_for_play(2)) return r = roommgr().getroom(c.room) if not r.clientinroom(c): c.room = None await c.ws.send(protoassembler.get_resp_ready_for_play(3)) return if r.clientisviewer(c): await c.ws.send(protoassembler.get_resp_ready_for_play(4)) return if r.isinmatch(): await c.ws.send(protoassembler.get_resp_ready_for_play(5)) return r.setplayerready(c, True) await c.ws.send(protoassembler.get_resp_ready_for_play(0)) l = r.getbroadcastclientlist() await broadcastmsg( l, protoassembler.get_broadcast_player_ready( r.getplayerstat(c).getinfo())) if r.playercount() >= configmgr().getminplayers() and r.isallready(): r.gennewquestion() r.matchstart() await broadcastmsg( l, protoassembler.get_broadcast_nextdrawer( r.getdrawerstat().getinfo())) drawerstat = r.getdrawerstat() answer = r.getanswer() try: await drawerstat.player.ws.send( protoassembler.get_notify_round_answer(answer)) except websockets.exceptions.connectionclosed: print('error: \n', sys.exc_info()[0], '\n', sys.exc_info()[1]) ondisconnected(drawerstat.player.ws, True) await processroundover(r) except: print('error: \n', sys.exc_info()[0], '\n', sys.exc_info()[1]) await processroundover(r)
async def joinroom_asplayer(c, req): if c.room is not None and roommgr().roomexists(c.room): await c.ws.send(protoassembler.get_resp_join_room_as_player(8)) return if 'ROOMID' not in req: await c.ws.send(protoassembler.get_resp_join_room_as_player(1, None)) return roomid = req['ROOMID'] if not roommgr().roomexists(roomid): await c.ws.send(protoassembler.get_resp_join_room_as_player(2, None)) return r = roommgr().getroom(roomid) if r.isplayerfull(): await c.ws.send(protoassembler.get_resp_join_room_as_player(3, None)) return if r.clientinroom(c): await c.ws.send(protoassembler.get_resp_join_room_as_player(4, None)) return if r.pwd is not None: if 'PWD' not in req: await c.ws.send( protoassembler.get_resp_join_room_as_player(5, None)) return pwd = req['PWD'] if pwd != r.pwd: await c.ws.send( protoassembler.get_resp_join_room_as_player(5, None)) return if r.isinmatch(): await c.ws.send(protoassembler.get_resp_join_room_as_player(6, None)) return if r.isplayerfull(): await c.ws.send(protoassembler.get_resp_join_room_as_player(7, None)) return l = r.getbroadcastclientlist() r.joinasplayer(c) await c.ws.send( protoassembler.get_resp_join_room_as_player(0, r.getroominfo())) await broadcastmsg( l, protoassembler.get_broadcast_player_joined( r.getplayerstat(c).getinfo()))
async def joinrandomroom(c, req): if c.room is not None and roommgr().roomexists(c.room): await c.ws.send(protoassembler.get_resp_join_random_room(1)) return r = roommgr().getjoinalbleroom() if r is None: await c.ws.send(protoassembler.get_resp_join_random_room(2)) return l = r.getbroadcastclientlist() r.joinasplayer(c) await c.ws.send( protoassembler.get_resp_join_random_room(0, r.getroominfo())) await broadcastmsg( l, protoassembler.get_broadcast_player_joined( r.getplayerstat(c).getinfo()))
async def send_chat(c, req): if c.room is None: return if not roommgr().roomexists(c.room): return r = roommgr().getroom(c.room) if not r.clientinroom(c): c.room = None return msg = req['MSG'] if type(msg) is not str: return if len(msg) <= 0: return await broadcastmsg(r.getbroadcastclientlist(), msg)
async def drawundo(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_draw_undo(1)) return if not roommgr().roomexists(c.room): c.room = None await c.ws.send(protoassembler.get_resp_draw_undo(2)) return r = roommgr().getroom(c.room) if not r.clientinroom(c) or not r.clientisdrawer(c): await c.ws.send(protoassembler.get_resp_draw_undo(3)) return if not r.isinround(): await c.ws.send(protoassembler.get_resp_draw_undo(4)) return r.undodraw() l = r.getbroadcastclientlist() await broadcastmsg(l, protoassembler.get_broadcast_draw_undo())
async def startround(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_start_round(1)) return if not roommgr().roomexists(c.room): c.room = None await c.ws.send(protoassembler.get_resp_start_round(2)) return r = roommgr().getroom(c.room) if not r.clientinroom(c): c.room = None await c.ws.send(protoassembler.get_resp_start_round(3)) return isviewer = r.clientisviewer(c) isdrawer = r.clientisdrawer(c) if isviewer or not isdrawer: await c.ws.send(protoassembler.get_resp_start_round(4)) return if r.isinround(): await c.ws.send(protoassembler.get_resp_start_round(5)) return if not r.isinmatch(): await c.ws.send(protoassembler.get_resp_start_round(6)) return if not r.isallready(): await c.ws.send(protoassembler.get_resp_start_round(7)) return r.roundstart() await c.ws.send(protoassembler.get_resp_start_round(0)) l = r.getbroadcastclientlist() await broadcastmsg( l, protoassembler.get_broadcast_roundstart(r.getcurrentroundinfo()))
async def skipround(c, req): if c.room is None: await c.ws.send(protoassembler.get_resp_skip_round(1)) return if not roommgr().roomexists(c.room): c.room = None await c.ws.send(protoassembler.get_resp_skip_round(2)) return r = roommgr().getroom(c.room) if not r.clientinroom(c): c.room = None await c.ws.send(protoassembler.get_resp_skip_round(3)) return isviewer = r.clientisviewer(c) isdrawer = r.clientisdrawer(c) if isviewer or not isdrawer: await c.ws.send(protoassembler.get_resp_skip_round(4)) return if r.isinround(): await c.ws.send(protoassembler.get_resp_skip_round(5)) return if not r.isinmatch(): await c.ws.send(protoassembler.get_resp_skip_round(6)) return if not r.isallready(): await c.ws.send(protoassembler.get_resp_skip_round(7)) return await c.ws.send(protoassembler.get_resp_skip_round(0)) await processroundover(r, True)
async def update(tickcount, tm): # print (tickcount, tm) l = roommgr().update(tickcount, tm) for r in l: await processroundover(r)
async def test(): wslist = [] for i in range(30): ws = fakews() wslist.append(ws) c = client(ws) worker.CLIENTS[ws] = c # c.name = "CLIENT" + i # c.icon = i % 10 i = 0 for k in worker.CLIENTS: i = i + 1 c = worker.CLIENTS[k] name = 'CLIENT_' + str(i) req = {'PROTO': 'REQ_REGISTER', 'NAME': name, 'ICON': i % 10} await worker.onregister(c, req) printallclientinfo() player0 = worker.CLIENTS[wslist[0]] await worker.oncreateroom( player0, { 'PROTO': 'REQ_CREATE_ROOM', 'NAME': 'ROOM_1', 'ICON': '10001', 'MAXPLAYERS': 5, 'MATCHOVERSCORE': 20 }) player0.printinfo() # printallclientinfo() r0 = roommgr().getroom("1000000") player1 = worker.CLIENTS[wslist[1]] await worker.getroomlist(player1, {}) await worker.joinroom_asplayer(player1, { 'PROTO': 'REQ_JOIN_ROOM_AS_PLAYER', 'ROOMID': "1000000" }) player1.printinfo() viewer0 = worker.CLIENTS[wslist[2]] await worker.joinroom_asviewer(viewer0, { 'PROTO': 'REQ_JOIN_ROOM_AS_VIEWER', 'ROOMID': "1000000" }) viewer0.printinfo() player2 = worker.CLIENTS[wslist[3]] await worker.joinroom_asplayer(player2, { 'PROTO': 'REQ_JOIN_ROOM_AS_PLAYER', 'ROOMID': "1000000" }) player2.printinfo() await worker.quitroom(player2, {}) player3 = worker.CLIENTS[wslist[4]] viewer1 = worker.CLIENTS[wslist[5]] # await worker.joinroom_asplayer(player3, { # 'PROTO': 'REQ_JOIN_ROOM_AS_PLAYER', # 'ROOMID': "1000000" # }) await worker.joinrandomroom(player3, { 'PROTO': 'REQ_JOIN_RANDOM_ROOM', }) # await worker.joingame(viewer0, { 'PROTO': 'REQ_JOIN_GAME' }) # return await worker.onreadyforplay(player0, {'PROTO': 'REQ_READY_FOR_PLAY'}) await worker.onreadyforplay(player1, {'PROTO': 'REQ_READY_FOR_PLAY'}) await worker.oncancelready(player1, {'PROTO': 'REQ_CANCEL_PLAY'}) await worker.onreadyforplay(player1, {'PROTO': 'REQ_READY_FOR_PLAY'}) # await worker.onreadyforplay(viewer0, {'PROTO': 'REQ_READY_FOR_PLAY'}) await worker.onreadyforplay(player2, {'PROTO': 'REQ_READY_FOR_PLAY'}) await worker.onreadyforplay(player3, {'PROTO': 'REQ_READY_FOR_PLAY'}) # await worker.quitroom(player3, {'PROTO': 'REQ_QUIT_ROOM'}) await worker.startround(player0, {'PROTO': 'REQ_START_ROUND'}) # await worker.quitroom(player0, {'PROTO': 'REQ_QUIT_ROOM'}) await worker.send_answer(player3, { 'PROTO': 'REQ_SEND_ANSWER', 'ANSWER': r0.answer }) # await worker.quitroom(player1, { # 'PROTO': 'REQ_QUIT_ROOM', # }) await worker.send_answer(player1, { 'PROTO': 'REQ_SEND_ANSWER', 'ANSWER': r0.answer }) # await worker.startround(player1, {'PROTO': 'REQ_START_ROUND'}) # await worker.send_answer(player3, { # 'PROTO': 'REQ_SEND_ANSWER', # 'ANSWER': r0.answer # }) # await worker.send_answer(player0, { # 'PROTO': 'REQ_SEND_ANSWER', # 'ANSWER': r0.answer # }) await worker.skipround(player1, {'PROTO': 'REQ_SKIP_ROUND'}) await worker.startround(player3, {'PROTO': 'REQ_START_ROUND'}) await worker.drawstart(player3, {'PROTO': 'REQ_DRAW_START'}) await worker.draw(player3, {'PROTO': 'REQ_DRAW', 'STEPS': [1]}) await worker.draw(player3, {'PROTO': 'REQ_DRAW', 'STEPS': [2]}) await worker.draw(player3, {'PROTO': 'REQ_DRAW', 'STEPS': [3, 4, 5]}) await worker.drawundo(player3, {}) await worker.drawundo(player3, {}) await worker.drawundo(player3, {}) await worker.drawundo(player3, {}) await worker.drawstart(player3, {}) await worker.draw(player3, {'PROTO': 'REQ_DRAW', 'STEPS': [1]}) await worker.drawstart(player3, {}) await worker.draw(player3, {'PROTO': 'REQ_DRAW', 'STEPS': [2]}) await worker.joinroom_asviewer(viewer1, { 'PROTO': 'REQ_JOIN_ROOM_AS_VIEWER', 'ROOMID': "1000000" }) await worker.draw(player3, {'PROTO': 'REQ_DRAW', 'STEPS': [3, 4, 5]}) await worker.send_answer(player1, { 'PROTO': 'REQ_SEND_ANSWER', 'ANSWER': r0.answer }) await worker.send_answer(player0, { 'PROTO': 'REQ_SEND_ANSWER', 'ANSWER': r0.answer }) # await worker.quitroom(player0, {}) # await worker.quitroom(player1, {}) # await worker.quitroom(player3, {}) # await worker.quitroom(viewer0, {}) await worker.getroomlist(player2, {})
async def getroomlist(c, req): infolist = roommgr().getsortedroomlist() await c.ws.send(protoassembler.get_resp_get_room_list(0, infolist))