def DEBUG_instant_build(self, building): """Performs instant build for debugging purposes.""" self.buildings_status = self.buildings_status[:building.dep_pos] + \ str(int(self.buildings_status[building.dep_pos])+1) + \ self.buildings_status[building.dep_pos+1:] db_session.add(self) db_session.commit()
def invite(self, player_id): """Invite a player with specified id. Before doing this, check if leader sent request and if player doesn't have network.""" from wtm.models.User import Player #TODO: VERY UGLY; DAMN CIRCULAR IMPORTS. REFACTOR? :O player = Player.query.filter(Player.id == player_id).first() self.invites.append(player) db_session.add(self) db_session.commit()
def finish_building(self, building_pos): """Finish building specified building.""" #When the on tick hits 0 ETA, do this -> self.buildings_status = self.buildings_status[:building_pos] + \ str(int(self.buildings_status[building_pos])+1) + \ self.buildings_status[building_pos+1:] db_session.add(self) db_sessizoon.commit()
def deny_network(self, network_id): """Player denies an invitation to join a network.""" try: network = Network.query.filter(Network.id == network_id).first() self.invites.remove(network) db_session.add(self) db_session.commit() except: raise UnknownGameException
def remove_invite(self, player): """Removes the invite for a player with specified id.""" try: if player not in self.invites: raise NetworkBadRemoveInvite self.invites.remove(player) db_session.add(self) db_session.commit() except NetworkBadRemoveInvite as e: raise e
def create_network(self, name): """Create a new network.""" if self.network_id != None: raise NetworkAlreadyInNetwork else: new_network = Network(name, self.id) game = Game.query.filter(Game.id == self.game_id).first() game.networks.append(new_network) db_session.add(new_network) db_session.add(game) db_session.commit()
def start_building(self, building): """Start building specified building.""" if self.gold < building.cost_gold or self.metal < building.cost_metal: raise NotEnoughResources #Decrease resources and start building self.gold -= building.cost_gold self.metal -= building.cost_metal #Mark as building. self.building_now = self.building_now[:building.dep_pos] + \ str(building.ticks_to_complete) + \ self.building_now[building.dep_pos+1:] db_session.add(self) db_session.commit()
def create_user(username=None, password=None): """Creates new user, needs -u, -p parameters """ if (username and password) and not User.username_taken(username): user = User(username,password) db_session.add(user) db_session.commit() print "added user with id:", user.id else: print "error, username taken or not enough params"
def initdb(): """Creates all database tables.""" Base.metadata.create_all(bind=engine) admin = User("admin", "admin", "boris", "bogdanovich", ADMIN_PERM) test_user = User("primi", "primi", "milivoj", "novakovic", PLAYER_PERM) test_user2 = User("tomi", "tomi", "milivoj", "novakovic", PLAYER_PERM) db_session.add(admin) db_session.add(test_user) db_session.add(test_user2) db_session.commit() #Create a game start_date = datetime.datetime.now() end_date = start_date + datetime.timedelta(1*365/12) #1 Month game print "start_date:",start_date print "end_date:", end_date #end_date = datetime.datetime. game = Game("server1", 10, start_date, end_date) game2 = Game("server2", 60, start_date, end_date) db_session.add(game) db_session.add(game2) db_session.commit() #Add test_user to game p1 = test_user.join_game(game.id) p2 = test_user2.join_game(game.id) #Print all games and its players all_games = Game.query.all() for cur_game in all_games: print "Game: %s" % (cur_game.name) for player in cur_game.players: print "\t",player #Make a network try: p2.create_network(u"Testna zveza") except ALL_EXCEPTIONS as e: print e.as_console_output()
def add_player(self, player_id, network_created=False): """Adds a player to the network (if possible).""" #TODO: Check if that player even exists. try: from wtm.models.User import Player #TODO: VERY UGLY; DAMN CIRCULAR IMPORTS. REFACTOR? :O new_player = Player.query.filter_by(id = player_id).first() self.players.append(new_player) db_session.add(self) db_session.commit() if not network_created: #If a player joined a network (didnt create one), remove his invitations try: self.remove_invite(new_player) new_player.deny_all_network_invites() except ALL_EXCEPTIONS as e: raise e except ALL_EXCEPTIONS as e: raise e except: raise UnknownGameException
def join_game(self, game_id): """Join a game with specified id and become a player in that game. Returns the new player.""" #Get the game game = Game.query.filter(Game.id==game_id).first() #Create a new player new_player = Player(str(self.username)) #Give user a player to play with self.players.append(new_player) db_session.add(self) db_session.commit() #Give game another player new_player = Player.query.filter(Player.id==new_player.id).first() game.players.append(new_player) db_session.add(game) db_session.commit() print "Joined the game: %s." % (game.name) return new_player
def ticker_update(self): #Calculate the amount of gold and metal player gains gold_gained = self.asteroids_gold * 50 metal_gained = self.asteroids_metal * 50 self.gold += gold_gained self.metal += metal_gained #Decrease eta of buildings that are being built for (dep_pos, building) in enumerate(self.building_now): if int(building) != 0: self.decrease_building_eta(dep_pos) #TODO: Decrease the eta of army being built #TODO: Make attacks #TODO: Make defences #TODO: Calculate new player score db_session.add(self) db_session.commit()
def leave_network(self): """Make a player leave the network. If player is the leader... delete the network.""" if self.network.leader_id == self.id: #If this player is the leader of the network remove all network players cur_network = Network.query.filter(Network.id == self.network_id).first() for network_player in cur_network.players: network_player.network_id = None db_session.add(network_player) #Remove also all network invites for invite in cur_network.invites: cur_network.invites.remove(invite) db_session.add(cur_network) db_session.delete(cur_network) db_session.commit() self.network_id = None db_session.add(self) db_session.commit()