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'))
Esempio n. 2
0
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])


continueGame = True
while continueGame :
    if server.gamersNumber() < 2:
        print("There is not enough players to continue the game")
        server.broadcast_data("_stop", "There is not enough players to continue the game")
        continueGame = False
Esempio n. 3
0
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:
            server.broadcast_data("_stop", "Player <%s> win! Correct number %d"%(gamer,mol.curr))
            continueGame = False
        elif res == 1:
            server.broadcast_data("_info", "The number %d is too big"%num)
        else:
            server.broadcast_data("_info", "The number %d is too small"%num)

server.closeSocket()