def populate(self): """Put the tank names into the list""" self.xpBox.SetValue("Select a tank to see XP") self.TankBox.SetValue("Select a tank") r = open("login.conf", "r") config = r.read().split("\n") self.ipAddr = getConfiguration(config, "ip_address") self.port = getConfiguration(config, "port") r.close() conn = netComms.networkComms(self.ipAddr, int(self.port)) conn.send(["OWNED", self.username]) owned = conn.recieved conn.close() relaventOwned = owned[1:-2] dataconn = sqlite3.Connection("TankStats.db") cursor = dataconn.cursor() tankname = cursor.execute("SELECT name FROM Tanks;").fetchall() tankname = [x[0] for x in tankname] dataconn.close() conn = netComms.networkComms(self.ipAddr, int(self.port)) conn.send(["COSTS"]) self.costs = conn.recieved conn.close() self.TankBox.Clear() for i in range(len(tankname)): if relaventOwned[i] == 0: self.TankBox.Append(str(tankname[i]))
def refresh(self, username): """Reloads all components""" #Pull up all known tanks self.conn = sqlite3.Connection("TankStats.db") self.cur = self.conn.cursor() self.tanks = self.cur.execute("SELECT * FROM Tanks;").fetchall() #modify the client conn = netComms.networkComms(self.ipAddr, int(self.port)) #print "Connection to loginServer made on " + str(self.ipAddr) + ":" + str(self.port) conn.send(["OWNED", self.username]) self.owned = conn.recieved #print "From server: " + str(self.owned) conn.close() dataconn = sqlite3.Connection("TankStats.db") cur = dataconn.cursor() self.names = cur.execute("SELECT name FROM Tanks").fetchall() dataconn.close() self.names = [x[0] for x in self.names] #print "All names: " + str(self.names) self.tankChoice.Clear() for x in range(len(self.owned) - 2, 1, -1): if self.owned[x] == 0: #print "Pop val is " + str(self.owned[x]) + " " + str(x-1) + ":" + str(self.names[x-1]) self.names.pop(x - 1) for i in self.names: self.tankChoice.Append(i)
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 getStats(self, username, tankName): """Get the users stats for that specific tank""" conn = netComms.networkComms(self.ipAddr, int(self.port)) conn.send(["GET", username, tankName]) a = (conn.recieved) conn.close() #print self.tankChoice.GetSelection() return a.split(":")
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)
def confirmEdit(self, event): """Ask the user if they are really sure they want to upgrade""" r = open("login.conf", "r") config = r.read().split("\n") self.ipAddr = getConfiguration(config, "ip_address") self.port = getConfiguration(config, "port") r.close() conn = netComms.networkComms(self.ipAddr, int(self.port)) toSend = ["Update", self.username] toSend.append(self.getNewStats()) toSend.append(self.xp) if messages.YesNo(self.parent, "Confirm changes?"): conn.send(toSend) #print "Sent: " + str(toSend) messages.Info(self.parent, "Changes sent.") conn.close() #print "Closed connection to loginserver" self.Show(False) else: pass
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 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)
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 setupGame(self, stats, host, port, username): """A method to load in all resources and set the game in motion""" #Create a connection self.connection = netComms.networkComms(host, int(port)) self.stats = stats self.username = username #Open resources self.fire = games.load_sound("res/Sounds/ms-1-45mm.ogg") self.idle = games.load_sound("res/Sounds/marder-idle.ogg") self.moskau = games.load_sound("res/Sounds/Moskau.ogg") self.moving = games.load_sound("res/Sounds/marder-moving.ogg") self.loadingSongs = [ games.load_sound("res/Sounds/WoT-Opening-1.ogg"), games.load_sound("res/Sounds/WoT-Opening-2.ogg"), games.load_sound("res/Sounds/WoT-Opening-3.ogg") ] self.battleSongs = [ games.load_sound("res/Sounds/WoT-Battle-1.ogg"), games.load_sound("res/Sounds/WoT-Battle-2.ogg"), games.load_sound("res/Sounds/WoT-Battle-3.ogg"), games.load_sound("res/Sounds/WoT-Battle-4.ogg"), games.load_sound("res/Sounds/WoT-Battle-5.ogg") ] self.despawnToServer = [] self.damageDone = [] name = self.stats[0] hp = self.stats[1] damage = self.stats[2] penetration = self.stats[3] reload = self.stats[4] armour = self.stats[5] hull_traverse = self.stats[6] turret_trav = self.stats[7] speed = self.stats[8] self.bg = games.load_image("background.png") self.update_ticks = 100 self.bulletType = Bullet(0, 0, 0, 0, 0, 0, 0) #Create local instances of player and turret #First we have to handshake self.connection.send(["handshake", name, hp, self.username]) #This will return us the currently connected players and our ID #print "RECV: "+str(self.connection.recieved) self.id = self.connection.recieved[0] if int(self.id) % 2 == 0: self.team = 0 else: self.team = 1 self.serverPlayers = self.connection.recieved[1] #Start countdown to the game self.countdown = self.connection.recieved[2] #Add a timer self.timerTop = games.Text(value="Game will start in:", x=400, y=300, size=50, color=colour.white) self.timerMain = games.Text(value=str(self.countdown), x=400, y=400, size=50, color=colour.white) if self.countdown == 0: self.close() #Set the map self.map = self.connection.recieved[3] self.drawMap(self.map) #Create these players, excluding us, as our movement is handled locally #Add us #We will get the data in the form [x,y,angle,turret angle] self.clientTurret = LocalTurret(self.serverPlayers[self.id][0], self.serverPlayers[self.id][1], self.serverPlayers[self.id][3], turret_trav, name) self.client = LocalPlayer(self.serverPlayers[self.id][0], self.serverPlayers[self.id][1], self.serverPlayers[self.id][2], self.clientTurret, speed, hull_traverse, hp, reload, armour, name, self.id, damage, penetration, self.username, self.team) #Start the countdown self.countdownThread = Thread(target=self.countingDown) self.countdownThread.start() #Add us to the screen games.screen.add(self.client) games.screen.add(self.clientTurret) games.screen.add(self.timerTop) games.screen.add(self.timerMain) #We have handled us, remove us from the list self.serverPlayers.pop(self.id) #Create some variables to hold the players self.serverInstances = [] self.serverInstancesTurret = [] self.bullets = [] self.toDespawn = [] self.toRebound = [] #Add everyone for p in self.serverPlayers: #Add a new turret instance self.serverInstancesTurret.append(Turret(p[0], p[1], p[3], p[4])) #Add a new player instance self.serverInstances.append( Player(p[0], p[1], p[2], p[4], p[5], p[6], self.serverInstancesTurret[-1], p[7], self.client.team)) #add them games.screen.add(self.serverInstances[-1]) games.screen.add(self.serverInstancesTurret[-1])
def getAllXP(self): """Get the users progress on all tanks from the server""" conn = netComms.networkComms(self.ipAddr, int(self.port)) conn.send(["ALLXP", self.username]) return conn.recieved
def getXP(self, name): """Get the users progress on one specific tank from the server""" conn = netComms.networkComms(self.ipAddr, int(self.port)) conn.send(["XP", str(self.username), str(name)]) return conn.recieved
def setupGame(self,stats, host, port, username): """A method to load in all resources and set the game in motion""" #Create a connection self.connection = netComms.networkComms(host, int(port)) self.stats = stats self.username = username #Open resources self.fire = games.load_sound("res/Sounds/ms-1-45mm.ogg") self.idle = games.load_sound("res/Sounds/marder-idle.ogg") self.moskau = games.load_sound("res/Sounds/Moskau.ogg") self.moving = games.load_sound("res/Sounds/marder-moving.ogg") self.loadingSongs = [games.load_sound("res/Sounds/WoT-Opening-1.ogg"), games.load_sound("res/Sounds/WoT-Opening-2.ogg"), games.load_sound("res/Sounds/WoT-Opening-3.ogg")] self.battleSongs = [games.load_sound("res/Sounds/WoT-Battle-1.ogg"), games.load_sound("res/Sounds/WoT-Battle-2.ogg"), games.load_sound("res/Sounds/WoT-Battle-3.ogg"), games.load_sound("res/Sounds/WoT-Battle-4.ogg"), games.load_sound("res/Sounds/WoT-Battle-5.ogg")] self.despawnToServer = [] self.damageDone = [] name = self.stats[0] hp = self.stats[1] damage = self.stats[2] penetration = self.stats[3] reload = self.stats[4] armour = self.stats[5] hull_traverse = self.stats[6] turret_trav = self.stats[7] speed = self.stats[8] self.bg = games.load_image("background.png") self.update_ticks = 100 self.bulletType = Bullet(0, 0, 0, 0, 0, 0, 0) #Create local instances of player and turret #First we have to handshake self.connection.send(["handshake", name, hp, self.username]) #This will return us the currently connected players and our ID #print "RECV: "+str(self.connection.recieved) self.id = self.connection.recieved[0] if int(self.id) % 2 == 0: self.team = 0 else: self.team = 1 self.serverPlayers = self.connection.recieved[1] #Start countdown to the game self.countdown = self.connection.recieved[2] #Add a timer self.timerTop = games.Text(value="Game will start in:", x=400, y=300, size=50, color=colour.white) self.timerMain = games.Text(value=str(self.countdown), x=400, y=400, size=50, color=colour.white) if self.countdown == 0: self.close() #Set the map self.map = self.connection.recieved[3] self.drawMap(self.map) #Create these players, excluding us, as our movement is handled locally #Add us #We will get the data in the form [x,y,angle,turret angle] self.clientTurret = LocalTurret(self.serverPlayers[self.id][0], self.serverPlayers[self.id][1], self.serverPlayers[self.id][3], turret_trav, name) self.client = LocalPlayer(self.serverPlayers[self.id][0], self.serverPlayers[self.id][1], self.serverPlayers[self.id][2], self.clientTurret, speed, hull_traverse, hp, reload, armour, name, self.id, damage, penetration, self.username, self.team) #Start the countdown self.countdownThread = Thread(target=self.countingDown) self.countdownThread.start() #Add us to the screen games.screen.add(self.client) games.screen.add(self.clientTurret) games.screen.add(self.timerTop) games.screen.add(self.timerMain) #We have handled us, remove us from the list self.serverPlayers.pop(self.id) #Create some variables to hold the players self.serverInstances = [] self.serverInstancesTurret = [] self.bullets = [] self.toDespawn = [] self.toRebound = [] #Add everyone for p in self.serverPlayers: #Add a new turret instance self.serverInstancesTurret.append(Turret(p[0], p[1], p[3], p[4])) #Add a new player instance self.serverInstances.append( Player(p[0], p[1], p[2], p[4], p[5], p[6], self.serverInstancesTurret[-1], p[7], self.client.team)) #add them games.screen.add(self.serverInstances[-1]) games.screen.add(self.serverInstancesTurret[-1])