def doUpgrade(self, event): """Open the upgrade GUI""" try: upApp = wx.App(False) upFrame = Upgrade(None, self.getStats(self.username, self.sel), self.getXP(self.sel), self.username, self) upFrame.Show(True) upApp.MainLoop() except Exception as ex: messages.Warn(self.parent, "Please select a tank first") messages.Warn(self.parent, str(ex))
def goToBattle(self, event): """Set up the client to launch the game engine and then run the game""" try: self.a.stop() except Exception: pass try: assert (self.AddressBox.GetValue() != u"") assert (self.tankChoice.GetSelection() >= 0) instance = [ self.username, self.toInt(self.stats), self.host, self.port ] #self.Show(False) try: a = self.battleThread(instance) except error as e: #TankClient.setupEnv() print str(e) except games.GamesError as ex: games.screen.quit() print "Error with pygame: " + str(ex) except AssertionError: messages.Warn(self.parent, "Please select a tank and enter a host:port combo") except NoConnectionException: messages.Warn( self.parent, "There is no server on this port, please double check and try again." ) self.Show(True) except EndOfGame as ex: #print "EX:" + str(ex.message) ex = eval(ex.message) self.Show(True) win = ex[0] xp = ex[1] damage = ex[2] kills = ex[3] if win: messages.Info( self.parent, "You have won!\nYou recieved: " + str(xp) + " xp\nDamage dealt: " + str(damage) + " Kills: " + str(kills), "VICTORY") else: messages.Info( self.parent, "You have been defeated...\nYou recieved: " + str(xp) + " xp\nDamage dealt: " + str(damage) + " Kills: " + str(kills), "DEFEAT")
def buyTank(self, event): """Button press event to send the command to buy the tank""" if int(self.xpBox.Value) >= int(self.priceBox.Value): try: # buy the tank assert (self.TankBox.GetSelection() >= 0) conn = netComms.networkComms(self.ipAddr, int(self.port)) pastTank = self.alltanks[self.alltanks.index(self.name) - 1] conn.send(["BUY", self.name, self.username, pastTank]) a = conn.recieved conn.close() if a == "DONE": messages.Info(self.parent, "Tank purchased!") self.owner.refresh(self.owner.username) except AssertionError: messages.Warn(self.parent, "Please select a tank first") else: messages.Warn(self.parent, "You do not have the XP to purchase this tank") self.owner.refresh(self.owner.username) self.Show(False)
def process(self, message): """Process the server's response""" if message == "UsernameException": messages.Info(self.parent, "That username is in use. Please choose another.") if message == "LoginFailure": messages.Warn(self.parent, "Login failed. Wrong username/password.") if message == "COMPLETE": messages.Info(self.parent, "Account created. Please log in.") if type((0, 0)) == type(message): self.loginComplete(message)
def createAccount(self, event): """Asks the login server to add the account""" #Open the config try: r = open("login.conf", "r") except IOError: messages.Warn("Could not open login.conf from directory %s", str(os.getcwd())) sys.exit() config = r.read().split("\n") #This is stored in the config file self.ipAddr = getConfiguration(config, "ip_address") self.port = getConfiguration(config, "port") r.close() if len(self.passB.Value) < 5: messages.Warn(self.parent, "Passwords must be at least 5 characters long") elif self.userBox.Value != "" and self.passB.Value != "": conn = netComms.networkComms(self.ipAddr, self.port) conn.send([ "CREATE", [ self.userBox.Value.lower(), hashlib.sha1(self.passB.Value).hexdigest() ] ]) self.process(conn.recieved) conn.close() else: #show a warning messages.Warn( self.parent, "Please enter both a username and password to create an account" )
def readConfig(self): """Reads the configuration file for ip address and port""" #the client will provide a configuration file try: r = open("login.conf", "r") except IOError: thing = str("Could not locate login.conf in directory {0}".format( str(os.getcwd()))) messages.Warn(self.parent, thing) sys.exit([7]) config = r.read().split("\n") self.ipAddr = getConfiguration(config, "ip_address") self.port = getConfiguration(config, "port") r.close()
def suchSending(self, event): """Send the login credentials to the server""" try: self.conn = netComms.networkComms(self.ipAddr, self.port) self.conn.send([ "LOGIN", [ self.userBox.Value.lower(), hashlib.sha1(self.passBox.Value).hexdigest() ] ]) except NoConnectionException: thing = "Could not connect to login server at {0}".format( str(self.ipAddr) + ":" + str(self.port)) messages.Warn(self.parent, thing) sys.exit([7]) recv = self.conn.recieved self.conn.close() self.process(recv)