def test_AddNametoConnection(self): server=EchoGameServer(host,port,data_size,msghand,1) self.assertEqual(server.gameList.gamersUnnamedNumber(),0) self.assertEqual(server.gameList.gamersNamedNumber(),0) client=EchoGameClient(host,port,data_size,msghand) server.handle_connection() self.assertEqual(server.gameList.gamersUnnamedNumber(),1) self.assertEqual(server.gameList.gamersNamedNumber(),0) def eff(): client.socketStatus=False return "Nick" sys.stdin.readline = MagicMock( side_effect =eff) client.sendName() ansS = server.handle_connection() self.assertEqual(ansS,2) client.socketStatus=True client.sendName() self.assertEqual(server.gameList.gamerName(server.gameList.broadcastedGamersSockets()[0]),"Nick") self.assertEqual(server.gameList.gamersUnnamedNumber(),0) self.assertEqual(server.gameList.gamersNamedNumber(),1)
def test_broadcast_data(self): server=EchoGameServer(host,port,data_size,msghand,3) client1=EchoGameClient(host,port,data_size,msghand) server.handle_connection() client2=EchoGameClient(host,port,data_size,msghand) server.handle_connection() def eff(): client1.socketStatus=False return "Nick_1" sys.stdin.readline = MagicMock( side_effect = eff ) client1.sendName() server.handle_connection() client1.socketStatus=True client1.sendName() def eff(): client2.socketStatus=False return "Nick_2" sys.stdin.readline = MagicMock( side_effect = eff ) client2.sendName() server.handle_connection() client2.socketStatus=True client2.sendName() ansC = client1.handle_connection() self.assertEqual(ansC,('S', '_info', '\\Game Server\\ Player <Nick_2> has joined the game')) server.broadcast_data("_info","Message") ansC = client1.handle_connection() self.assertEqual(ansC,('S', '_info', '\\Game Server\\ Message')) ansC = client2.handle_connection() self.assertEqual(ansC,('S', '_info', '\\Game Server\\ Message'))
def test_sendMessageFromServerToClient(self): server=EchoGameServer(host,port,data_size,msghand,1) client=EchoGameClient(host,port,data_size,msghand) server.handle_connection() def eff(): client.socketStatus=False return "Nick" sys.stdin.readline = MagicMock( side_effect =eff) client.sendName() server.handle_connection() client.socketStatus=True client.sendName() server.sendMessage("_info","Message","Nick") ansC = client.handle_connection() self.assertEqual(ansC,('S', '_info', '\\Game Server\\ Message'))
def test_DeinedConnection(self): server=EchoGameServer(host,port,data_size,msghand,0) client=EchoGameClient(host,port,data_size,msghand) ansS = server.handle_connection() self.assertEqual(ansS,-1) ansC = client.handle_connection() self.assertEqual(ansC,('S', '_error', '\\Game Server\\ There is too many players on serwer')) self.assertEqual(server.gameList.gamersUnnamedNumber(),0)
def test_NewConnection(self): server=EchoGameServer(host,port,data_size,msghand,maxPlayers) client=EchoGameClient(host,port,data_size,msghand) ansS = server.handle_connection() self.assertEqual(ansS,1) ansC = client.handle_connection() self.assertEqual(ansC,('S', '_name_request', '\\Game Server\\ Send your name')) self.assertEqual(server.gameList.gamersUnnamedNumber(),1)
host = 'localhost' port = 50015 data_size = 1024 maxPlayers=2 msghand = TTT_MessageHandler() try: server=EchoGameServer(host,port,data_size,msghand,maxPlayers) except OSError: print("Adress already in use") sys.exit() while server.gamersNumber() <2: resp = server.handle_connection() if resp is tuple: server.sendMessage("_error","Waiting on more gamers", resp[1]) print("Start game") time.sleep(0.1) server.broadcast_data("_start","Starting the TickTackToo game!") pl = server.gamersList() random.shuffle(pl) players = {"X" : pl[0], "O" : pl[1]} time.sleep(0.1) server.broadcast_data("_info","Player <%s> start as X"%pl[0])
data_size = 1024 maxPlayers=2 msghand = MessageHandler() try: server=EchoGameServer(host,port,data_size,msghand,maxPlayers) except OSError: print("Adress already in use") sys.exit() startMsg = "More Or Less Game: number can be found from %d to %d!"%(mol.minV,mol.maxV) continueGame = True while continueGame : ans = server.handle_connection(startMsg) if isinstance(ans,tuple): numGam, gamer, msgType, msgText = ans else: continue print("%d - %s - %s - %s"%(numGam, gamer, msgType, msgText)) if msgType == "_number": try: num = int(msgText) except ValueError: continue res = mol.guess(num) if res == 0: