def challenge(self, ide): HOST, PORT = "localhost", 9999 data = networking.DataFrame(networking.Header(1, "challenge"), { "id": self.player.getID(), "lobby": ide }).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(4096) finally: sock.close() received = ast.literal_eval(received) if received["header"]["reqtype"] == "heroes": p = party.Party(self.player) for h in received["data"]["heroes"]: p.addHero(hero.createFromJSON(ast.literal_eval(h))) print self.player.getID() self.createGame(ide, received["data"]["playerID"], self.player.getID()) b = networkedbattlescene.NetworkedBattleScene( ide, self.surface, self.player, player.Player(received["data"]["playerID"], "Opponent", p), self.party, p) self.error = False else: self.error = True
def getLobbies(self): HOST, PORT = "localhost", 9999 data = networking.DataFrame(networking.Header( 1, "fetchLobbies")).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(1024) finally: sock.close() received = ast.literal_eval(received) self.lobbies = [] if received["header"]["reqtype"] == "sendLobbies": i = 0 for x in received["data"]["lobbies"]: x = ast.literal_eval(x) l = lobby.Lobby(x["id"], x["name"], self.challenge, x["id"]) l.setRect(pygame.Rect(0, i * 100, 600, 100)) self.lobbies.append(l) i += 1 self.error = False else: self.error = True
def createGame(self, ide, player1, player2, rows=16, cols=8): HOST, PORT = "localhost", 9999 data = networking.DataFrame( networking.Header(1, "createMatch"), { "gameID": ide, "player1": str(player1), "player2": str(player2), "rows": str(rows), "cols": str(cols) }).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(4096) finally: sock.close() received = ast.literal_eval(received) if received["header"]["reqtype"] == "success": pass self.error = False else: self.error = True
def __init__(self, ide, surface, localplayer, networkedplayer, party1=party.getAi(), party2=party.getAi()): self.ide = ide self.player = localplayer self.networkedplayer = networkedplayer self.party1 = party1 self.DataFrameFactory = lambda header, data: networking.DataFrame( networking.Header(1, header), data).toString() self.sendHeroChoices() time.sleep(5) self.sendToServer( self.DataFrameFactory("connectToGame", { "gameID": self.ide, "playerID": self.player.getID() })) battlescene.BattleScene.__init__(self, surface, party1=party2, party2=party1, ai=False)
def connectToGame(self, ide): HOST, PORT = "localhost", 9999 data = networking.DataFrame(networking.Header(1, "testConnectToGame"), { "playerID": ide }).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(4096) print received finally: sock.close() print received received = ast.literal_eval(received) if received["header"]["reqtype"] == "success": p = party.Party( player.Player(received["data"]["playerID"], "Opponent", None)) for h in received["data"]["heroes"]: p.addHero(hero.createFromJSON(ast.literal_eval(h))) b = networkedbattlescene.NetworkedBattleScene( received["data"]["gameID"], self.surface, self.player, player.Player(received["data"]["playerID"], "Opponent", p), self.party, p) return 1 else: return 0
def sendHeroToDB(self, hero): HOST, PORT = "localhost", 9999 data = networking.DataFrame( networking.Header(1, "createHero"), { "playerID": str(self.player.getID()), "hero": ast.literal_eval(hero.getJSON()) }).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(1024) finally: sock.close() received = ast.literal_eval(received) if received["header"]["reqtype"] == "success": print received["data"]["heroID"] hero.setID(int(received["data"]["heroID"])) self.error = False else: self.error = True
def getAllHeroes(self): HOST, PORT = "localhost", 9999 data = networking.DataFrame(networking.Header(1, "getHeroes"), { "playerID": str(self.player.getID()) }).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(4096) finally: sock.close() received = ast.literal_eval(received) if received["header"]["reqtype"] == "heroes": for heroJSON in received["data"]["heroes"]: j = ast.literal_eval(heroJSON) self.party.addHero(hero.createFromJSON(j)) self.error = False else: self.error = True
def register(self): HOST, PORT = "localhost", 9999 print self.passwordChars data = networking.DataFrame(networking.Header(1, "registerAccount"), { "name": self.userChars, "password": self.passwordChars }).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(1024) finally: sock.close() received = ast.literal_eval(received) if received["header"]["reqtype"] == "success": print "Success" self.error = False else: self.error = True
def login(self): HOST, PORT = "localhost", 9999 data = networking.DataFrame(networking.Header(1, "login"), { "name": self.userChars, "password": self.passwordChars }).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(1024) finally: sock.close() received = ast.literal_eval(received) if received["header"]["reqtype"] == "success": n = networkedpartyscene.HeroCreationScreen( self.surface, player.Player(received["data"]["id"], received["data"]["username"])) self.error = False else: self.error = True
def askServerForBoard(self): data = networking.DataFrame(networking.Header(1, "askForBoard"), {"gameID": self.ide}) data = self.sendToServer(data) if data != 0 and len(self.heroes) != 0: for column, row, heroid in data: self.hexBoard[column][row].setGameObject( self.getHeroByID(heroid))
def deleteLobby(self): HOST, PORT = "localhost", 9999 data = networking.DataFrame(networking.Header(1, "deleteLobby"), { "id": self.player.getID() }).toString() # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((HOST, PORT)) sock.sendall(data + "\n") # Receive data from the server and shut down received = sock.recv(1024) finally: sock.close() received = ast.literal_eval(received) if received["header"]["reqtype"] == "success": return 1 else: return 0