def join(self): print("plyr: Join") self.cfg.Profile.playerName = self.playerName.get() self.cfg.Profile.color = self.playerColor.get() self.cfg.Profile.bases = self.startBases.get() if (self.hCon): sendJson = cmds.warpWarCmds().playerLeave(self.cfg.Profile.plid()) self.hCon.sendCmd(sendJson) sendJson = cmds.warpWarCmds().newPlayer( self.cfg.Profile.plid(), self.cfg.Profile.playerName, self.cfg.Profile.bases.split(" "), self.cfg.Profile.color) self.hCon.sendCmd(sendJson) self.playerName.config(state="disabled") self.playerColor.config(state="disabled") self.playerBases.config(state="disabled") self.config(text="Playing") self.joinUnjoin.config(text="Disconnect", command=lambda: self.disconnect()) self.cfg.saveConfig() # record the PlayerID self.plid = self.cfg.Profile.plid()
def newGame(self): # print(self.ip, self.port, self.msg) tmp = warpWarCmds() sendJson = tmp.newGame("CTest", "foo") print(" client sending: ", sendJson) self.hCOM = comThrd(self.ip.get(), int(self.port.get())) self.hCOM.sendCmd(sendJson)
def sendCombatReady(tkRoot): print("readyMenu") if (tkRoot.hCon is not None): if (tkRoot.battleOrders): sendJson = warpWarCmds().combatOrders(tkRoot.plid, tkRoot.battleOrders) print(" main sending: ", sendJson) tkRoot.hCon.sendCmd(sendJson) # We've sent our orders, erase them tkRoot.battleOrders = {} # Send ready because we are done with this round of combat sendJson = warpWarCmds().ready(tkRoot.plid) print(" main sending: ", sendJson) tkRoot.hCon.sendCmd(sendJson)
def selectDamageAll(self, game): print("playerAi: select damage") # Find ships with damage for ship in game['objects']['shipList']: if (ship['owner'] == self.plid) and (ship['damage'] > 0): self.selectDamageShip(ship) sendJson = warpWarCmds().acceptDamage(self.plid, ship) self.hCon.sendCmd(sendJson)
def change(self): print("connect.py: change ") self.plyr.useConnection(self.conn.hCon, {}) self.gp.useConnection(self.conn.hCon) if (self.conn.hCon): self.conn.hCon.setCallback(lambda data: self.newDataForGame(data)) sendJson = warpWarCmds().ping(self.plid) self.conn.hCon.sendCmd(sendJson)
def exitProgram(tkRoot): print("quitMain") if (tkRoot.hCon): sendJson = warpWarCmds().playerLeave(tkRoot.plid) tkRoot.hCon.sendCmd(sendJson) sendJson = warpWarCmds().quitGame(tkRoot.plid) tkRoot.hCon.sendCmd(sendJson) tkRoot.hCon.quitCmd() if (tkRoot.hPlayerAi): tkRoot.hPlayerAi.quit() # We should wait for the playerLeave and quit command # to finish, right? tkRoot.destroy() tkRoot.quit()
def newGame(tkRoot): print("newGame") print("What does newGame event mean? Kill old game and reconnect?") if (tkRoot.hCon is not None): sendJson = warpWarCmds().newGame( tkRoot.plid, dataModel.playerNameGet(tkRoot.game, tkRoot.plid), "foo") print(" main sending: ", sendJson) tkRoot.hCon.sendCmd(sendJson)
def restoreGame(self): """ This sends the loaded game. I Note that this DOES NOT populate self.loadedGame """ sendJson = warpWarCmds().restoreGame(self.plid, self.loadedGame) self.hCon.sendCmd(sendJson) resp = self.hCon.waitFor(5) self.game = json.loads(resp)
def loadCargo(tkRoot, star, ship): print("cargoMenu") if (star and ship and tkRoot.hCon is not None): cargoResult = loadCargoMenu(tkRoot, star, ship) if (cargoResult is not None and cargoResult.shipment != 0): sendJson = warpWarCmds().loadCargo(tkRoot.plid, star['name'], ship['name'], cargoResult.shipment) print(" main sending: ", sendJson) tkRoot.hCon.sendCmd(sendJson)
def damageAllocationMenu(tkRoot, shipName): print("damageAllocationMenu", shipName) if (tkRoot.hCon is not None): ship = findShip(tkRoot.game, shipName) allocationResult = damageAllocation(tkRoot, ship) if (allocationResult is not None and allocationResult.finished): sendJson = warpWarCmds().acceptDamage(tkRoot.plid, allocationResult.ship) print(" main sending: ", sendJson) tkRoot.hCon.sendCmd(sendJson)
def create(self): print("gameCreate") if (self.hCon): sendJson = warpWarCmds().newGame(self.plid, "dummy", self.gameName.get()) self.hCon.sendCmd(sendJson) self.gameName.config(state="disabled") self.config(text="Playing") self.owner.change()
def loadShip(tkRoot, ship, shipList): print("loadShipMenu") if (tkRoot.hCon is not None): #I don't like sending the whole tkRoot here. if you have an idea, do it. loadResult = loadShipMenu(tkRoot, ship, shipList) if (loadResult is not None and loadResult.finished): if loadResult.motherVar.get() == "unload": motherName = findMother(tkRoot.game['objects']['shipList'], loadResult.ship['name']) print(motherName) sendJson = warpWarCmds().unloadShip(tkRoot.plid, loadResult.ship['name'], motherName) else: sendJson = warpWarCmds().loadShip(tkRoot.plid, loadResult.ship['name'], loadResult.motherVar.get()) print(" main sending: ", sendJson) tkRoot.hCon.sendCmd(sendJson)
def buildShip(tkRoot, baseName): print("buildMenu", baseName) base = findBase(tkRoot.game, baseName) if (base and tkRoot.hCon is not None): buildResult = build(tkRoot, base) if (buildResult is not None and buildResult.ship): sendJson = warpWarCmds().buildShip(tkRoot.plid, buildResult.ship, baseName) print(" main sending: ", sendJson) tkRoot.hCon.sendCmd(sendJson)
def quit(self): print("server: Sending quit msg to server") try: tmpS = socket.socket() tmpS.connect((self.ipAddr, self.port)) tmp = warpWarCmds() sendJson = tmp.quitGame('STest') tmpS.send(zlib.compress(sendJson.encode())) tmpS.shutdown(socket.SHUT_RDWR) tmpS.close() tmpS = None except Exception as error: print("server.py Socket error: ", error, "\n")
def retreatOnClick(private, x, y): tkRoot = private[0] shipName = private[1] original = private[2] warpEnds = private[3] # should retreat allow along warp lines? #write the ship where it should be. print("Left click to retreat", shipName, "to", x, y) ship = dataModel.findShip(tkRoot.game, shipName) #can we move there? moveLeft = 1 cur_x = ship['location']['x'] cur_y = ship['location']['y'] # How far away is the mouse click? si, sj, sk = XYtoIJK(x, y) fi, fj, fk = XYtoIJK(cur_x, cur_y) delta = int((abs(si - fi) + abs(sj - fj) + abs(sk - fk)) / 2) if [x, y] in warpEnds: delta = 1 if (delta <= moveLeft): # ok, it's less than 1 away but is it legal for me to move there # during a retreat? enemyPresent = False objList = dataModel.findObjectsAt(tkRoot.game, x, y) for obj in objList: if (obj['owner'] != tkRoot.cfg.Profile.plid()): enemyPresent = True if (not enemyPresent): ship['location']['x'] = x ship['location']['y'] = y # Restore the old call back tkRoot.hexMap.setLeftPrivateCallBack(original[0], original[1]) # Send the move command to the server # We could also Queue up all the move commands # and play them out to the server later. # That would permit the user to change their # mind about a move and cancel it before it is # permanent if (tkRoot.hCon is not None): sendJson = warpWarCmds().acceptRetreat( tkRoot.cfg.Profile.plid(), shipName, x, y) tkRoot.hCon.sendCmd(sendJson)
def buildThings(self, game): # Loop through every base I own baseList = dataModel.getOwnedListOfType(game, self.plid, 'starBaseList') for base in baseList: # Take the points at that base and build a ship there print("playerAI: build something at ", base['name'], " for ", base['BP']['cur']) ship = self.createShip(base['BP']['cur'], base['location']['x'], base['location']['y']) if ship: sendJson = warpWarCmds().buildShip(self.plid, ship, base['name']) self.hCon.sendCmd(sendJson)
def disconnect(self): print("plyr: Disconnection") # clear the PlayerID slef.plid = None self.playerName.config(state="normal") self.playerColor.config(state="normal") self.playerBases.config(state="normal") self.config(text="Join Game") self.joinUnjoin.config(text="Join", command=lambda: self.join()) if (self.hCon): sendJson = cmds.warpWarCmds().playerLeave(self.cfg.Profile.plid()) self.hCon.sendCmd(sendJson) print("plyr: stopped playing")
def loadGame(tkRoot): print("loadGame") #so we need to open a file select menu, filtering for .wwr #then we just parse the string to our dict. loadFileName = filedialog.askopenfilename(title="Select file", filetypes=(("warpWar files", "*.wwr"), ("all files", "*.*"))) print(loadFileName) loadFile = open(loadFileName, 'r') gameString = loadFile.read() loadFile.close() gameDict = json.loads(gameString) #send the game to the server. sendJson = warpWarCmds().restoreGame(tkRoot.plid, gameDict) print(sendJson) tkRoot.hCon.sendCmd(sendJson)
def __init__(self, plid=845733184055071504452410, ipAddr="127.0.1.1", port=12345): self.plid = plid self.server = ServerTestApp() self.ipAddr = ipAddr self.port = port self.hCon = comThrd(self.ipAddr, self.port) self.loadedGame = None #game loaded from file self.game = None #game received from server print("Server is live! Wait 1 second and ping") time.sleep(1) sendJson = warpWarCmds().ping(plid) self.hCon.sendCmd(sendJson) resp = self.hCon.waitFor(5) print("We pinged the server. Remember to Load a game!")
def moveOnClick(private, x, y): tkRoot = private[0] shipName = private[1] original = private[2] warpEnds = private[3] #write the ship where it should be. print("Left Click to Move", shipName) ship = dataModel.findShip(tkRoot.game, shipName) #can we move there? moveLeft = ship['moves']['cur'] cur_x = ship['location']['x'] cur_y = ship['location']['y'] si, sj, sk = XYtoIJK(x, y) fi, fj, fk = XYtoIJK(cur_x, cur_y) delta = int((abs(si - fi) + abs(sj - fj) + abs(sk - fk)) / 2) if [x, y] in warpEnds: delta = 1 if (delta <= moveLeft): ship['location']['x'] = x ship['location']['y'] = y #find the ijk stuff for decrementing the right number of moves ship['moves']['cur'] = ship['moves']['cur'] - delta # Restore the old call back tkRoot.hexMap.setLeftPrivateCallBack(original[0], original[1]) # Send the move command to the server # We could also Queue up all the move commands # and play them out to the server later. # That would permit the user to change their # mind about a move and cancel it before it is # permanent if (tkRoot.hCon is not None): sendJson = warpWarCmds().moveShip(tkRoot.cfg.Profile.plid(), shipName, x, y) tkRoot.hCon.sendCmd(sendJson)
def connect(self): print("conn: Connect") self.cfg.Client.serverIP = self.serverEntry.get() self.cfg.Client.serverPort = self.portEntry.get() self.hCon = client.comThrd(self.cfg.Client.serverIP, int(self.cfg.Client.serverPort), "connection") if (self.hCon): pingCmd = cmds.warpWarCmds().ping('connectDlg') self.hCon.sendCmd(pingCmd) resp = self.hCon.waitFor(5) resp = json.loads(resp) else: resp = {} if (resp): print("Connection success!", len(resp)) self.serverEntry.config(state="disabled") self.portEntry.config(state="disabled") self.config(text="Connected") self.conDiscon.config(text="Disconnect", command=lambda: self.disconnect()) self.cfg.saveConfig() else: print("Connection failed:", len(resp)) self.config(text="Connection Failed") self.hCon.quitCmd() self.hCon = None # Inform parent Connection might have changed self.owner.change()
def ping(self): sendJson = warpWarCmds().ping(self.plid) self.hCon.sendCmd(sendJson)
def newPlayer(self): print("playerAi: newPlayer") sendJson = warpWarCmds().newPlayer(self.plid, self.playerName, self.startingBases, self.color) self.hCon.sendCmd(sendJson)
def playerJoinMenu(tkRoot): print("playerJoinMenu") print("Should launch dialog to pick") if (tkRoot.hCon is not None): sendJson = warpWarCmds().playerLeave(tkRoot.plid) tkRoot.hCon.sendCmd(sendJson)
def refresh(tkRoot): print("refresh") if (tkRoot.hCon is not None): sendJson = warpWarCmds().ping(tkRoot.plid) tkRoot.hCon.sendCmd(sendJson)
def sendStart(event, tkRoot): print("sendStart:", event) if (tkRoot.hCon is not None): sendJson = warpWarCmds().start(tkRoot.plid) print(" main sending: ", sendJson) tkRoot.hCon.sendCmd(sendJson)
def leave(self): print("playerAi: leaveGame") sendJson = warpWarCmds().playerLeave(self.plid) self.hCon.sendCmd(sendJson)
def ping(self): # print(self.ip, self.port, self.msg) tmp = warpWarCmds() sendJson = tmp.ping("CTest") print(" client sending: ", sendJson) self.hCOM.sendCmd(sendJson)
def ready(self): print("playerAi: ready") sendJson = warpWarCmds().ready(self.plid) self.hCon.sendCmd(sendJson)
from test.unitTest import UnitTest from cmds import warpWarCmds _plid = 845733184055071504452410 shipName = "foobar" foo = UnitTest(plid=_plid) foo.loadGame("test/move_unit_test.wwr") assert (foo.loadedGame['objects']['shipList'][0]['location']['x'] == 2 and foo.loadedGame['objects']['shipList'][0]['location']['y'] == 12) foo.restoreGame() assert (foo.game['objects']['shipList'][0]['location']['x'] == 2 and foo.game['objects']['shipList'][0]['location']['y'] == 12) foo.sendCmd(warpWarCmds().moveShip(_plid, shipName, 5, 14)) assert (foo.game['objects']['shipList'][0]['location']['x'] == 5 and foo.game['objects']['shipList'][0]['location']['y'] == 14) foo.finishTest()