def data_received_helper(self,pkt): pktID = pkt.DEFINITION_IDENTIFIER # 1: respond to game init pkt, ask payment if pktID == GameInitPacket.DEFINITION_IDENTIFIER: self.unique_id = self.generateRandomString() self.dataHandler.sendPkt(create_game_require_pay_packet(self.unique_id,MY_ACCOUNT,AMOUNT)) # 2: respond to game payment response pkt, confirm payment and start game elif pktID == GamePayPacket.DEFINITION_IDENTIFIER: receipt, receipt_sig = process_game_pay_packet(pkt) if(True): # TODO: fix this # if(self.bankManager.receipt_verify(receipt,receipt_sig,MY_ACCOUNT,AMOUNT,self.unique_id)): printx("payment confirmed") self.payStatus = True self.game.start() for a in self.game.agents: self.tasks.append(asyncio.create_task(a)) else: printError("client's payment confirm failed") # 3: respond to game command pkt, send game response elif pktID == GameCommandPacket.DEFINITION_IDENTIFIER: if self.game.status != "playing": printError("client tried to play the game while status is not playing") return elif self.payStatus==False: printError("client tried to play game before the payment is confirmed!") return else: self.game.command(pkt.command) time.sleep(0.25) return else: printError("unknown pkt:" + pktID)
def cancleTasks(self): # tell client the transport is closed self.dataHandler.sendPkt(GameCommandPacket(command="{},You didn't response in the past {} seconds, please reconnect again".format(self.peername,TIME_OUT))) self.transport.close() for task in self.tasks: task.cancel() printx("{}, timer is up, server transport close".format(self.peername[0]))
def data_received(self, data): self.resetTimer() pkts = self.dataHandler.recvPkt(data) for pkt in pkts: self.data_received_helper(pkt) if self.game.status == "escaped": self.payStatus = False # NOTE: here prevent user play multiple times printx('Student server side finished!')
def connection_made(self, transport): timer = Timer() printx('Connection made') self.transport = transport self.cmdHandler = ServerCmdHandler(transport) # NOTE:py3.7 for a in self.cmdHandler.game.agents: asyncio.create_task(a)
def connection_made(self, transport): printx('Connection made') self.transport = transport self.cmdHandler = ServerCmdHandler(transport) timer = Timer(10,lambda:self.transport.close(),printx("server transport close")) timer.start() # NOTE:py3.7 for a in self.cmdHandler.game.agents: asyncio.create_task(a)
def connection_made(self, transport): self.transport = transport self.dataHandler = DataHandler(transport) self.peername = self.transport.get_extra_info('peername') printx('Connection from:{}'.format(self.peername)) self.game = EscapeRoomGame(output=self.sendGameResPkt) self.game.create_game() self.payStatus = False self.bankManager = BankManager() self.timer = None self.tasks = []
def main(args): loop = asyncio.get_event_loop() coro = playground.create_server(ServerProtocol, "localhost", PORT_NUM) server = loop.run_until_complete(coro) printx('Servering on{}'.format(server.sockets[0].getsockname())) # loop.set_debug(1) try: loop.run_forever() except KeyboardInterrupt: pass server.close() loop.run_until_complete(server.wait_closed()) loop.close()
def connection_lost(self, exc): printx('The server closed the connction') printx('Stop the event loop') self.loop.stop()
def connection_made(self, transport): printx("Connection made!") self.transport = transport self.cmdHandler = ClientCmdHandler(transport, self.firstPkt)
def connection_made(self, transport): printx("Connection made!") self.transport = transport self.cmdHandler = ClientCmdHandler(transport) self.cmdHandler.sendGameInitRequestPkt()