def player_in_match(user_id, channel_id): result = False session = Session() m = session.query(Player).join(Match).filter(Match.channel_id == channel_id).all() for p in m: if p.user_id == user_id: result = True session.close() return result
def get_player_count(channel_id): count = 0 session = Session() m = session.query(Player).join(Match).filter(Match.channel_id == channel_id).all() for p in m: if p.user_id is not None: count += 1 session.close() return count
def get_global_hiscores(): session = Session() h = session.query(HiscoresReport.winner_id, User.name, func.sum(HiscoresReport.count).label('wins')) \ .filter(User.id == HiscoresReport.winner_id) \ .group_by(HiscoresReport.winner_id, User.name) \ .order_by(text('wins DESC')) \ .limit(10) \ .all() session.close() return h
def remove_roles(serv, role): session = Session() r = session.query(Role).filter(Role.id == role.id and Role.server_id == serv.id).first() session.delete(r) session.commit() session.close()
def remove_channel(channel, server): session = Session() chan = session.query(Channel).filter( Channel.id == channel.id and Channel.server_id == server.id).first() session.delete(chan) session.commit() session.close()
def create_roles(serv, role): session = Session() s = session.query(Server).filter(Server.id == serv.id).first() r = Role(role.id, role.name, s) session.add(r) session.commit() session.close()
def get_global_wins(user_id): wins = 0 session = Session() p = session.query(HiscoresReport).filter(HiscoresReport.winner_id == user_id) for row in p.all(): wins += row.count session.close() return wins
def get_user_inventory(user_id): inv = [] session = Session() w = session.query(ItemInventory).filter(ItemInventory.user_id == user_id) if w.count() > 0: for item in w: d = {} d['name'] = item.item.name d['count'] = item.count inv.append(d) session.close() return inv
def match_data(channel_id): data = {} session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() if m.turn % 2 == 0: # Even turns are player 2, odd is player 1 attacker = 2 defender = 1 else: attacker = 1 defender = 2 data['match'] = {'turn': m.turn, 'rules': m.rules, 'attacker': str(attacker), 'defender': str(defender)} players = session.query(Player).filter(Player.match_id == m.id).all() data['player'] = {} for player in players: data['player'][str(player.position)] = {'id': player.user_id, 'name': player.user.name, 'hp': player.hp, 'special': player.special, 'prayer_points': player.prayer_points, 'food': player.food, 'frozen': player.frozen, 'poison': player.poison, 'prayer': player.prayer, } session.commit() session.close() return data
def poison(channel_id, player_id, poisoned): session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() player = session.query(Player).filter(Player.match_id == m.id).filter( Player.user_id == player_id).first() player.poison = poisoned session.commit() session.close()
def frozen(channel_id, player_id, freeze): session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() player = session.query(Player).filter(Player.match_id == m.id).filter( Player.user_id == player_id).first() player.frozen = freeze session.commit() session.close()
def get_items(): items = [] session = Session() i = session.query(Item).all() for item in i: d = {} d['name'] = item.name d['description'] = item.description d['cost'] = item.price d['id'] = item.id items.append(d) session.close() return items
def damage(channel_id, player_id, dmg): session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() player = session.query(Player).filter(Player.match_id == m.id).filter( Player.user_id == player_id).first() player.hp -= dmg session.commit() session.close()
def give_item(user_id, item_id): session = Session() i = session.query(Item).filter(Item.id == item_id).first() w = session.query(ItemInventory).filter( ItemInventory.user_id == user_id).filter(ItemInventory.item_id == i.id) if w.count() > 0: w.first().count += 1 else: n = ItemInventory(user_id, item_id, 1) session.add(n) session.commit() session.close()
def update_attacker(channel_id, player_id, special, heal): session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() player = session.query(Player).filter(Player.match_id == m.id).filter( Player.user_id == player_id).first() player.special -= special player.hp += heal session.commit() session.close()
def next_turn(channel_id): session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() m.turn += 1 m.last_play = func.now() session.commit() session.close()
def add_player_to_match(channel_id, user_id, position, rule, stake=0): session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() u = session.query(User).filter(User.id == user_id).first() p = session.query(Player).filter(Player.match_id == m.id).filter( Player.position == position).first() p.user_id = user_id m.stake = stake u.gp -= stake if rule: m.rules = rule session.commit() session.close()
def win_lose_history(winner_id, loser_id, server_id): session = Session() h = session.query(HiscoresReport).filter(HiscoresReport.winner_id == winner_id) \ .filter(HiscoresReport.loser_id == loser_id) \ .filter(HiscoresReport.server_id == server_id) if h.count() > 0: h.first().count += 1 else: r = HiscoresReport(winner_id, loser_id, server_id) r.count = 1 session.add(r) session.commit() session.close()
def clear_match(channel_id, winner=False): # If there is no winner, return the staked amount back to each player # If there is a winner add double the staked amount to the winner. session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() players = session.query(Player).filter(Player.match_id == m.id).all() if m.rules == "stake" and not winner: for player in players: if player.user_id: player.user.gp += m.stake for player in players: player.user_id = None m.stake = 0 session.commit() session.close()
def player_owns_weapon(player_id, weapon_name): session = Session() r = False i = session.query(Item).filter(Item.name == weapon_name).first() w = session.query(ItemInventory).filter(ItemInventory.user_id == player_id).filter(ItemInventory.item_id == i.id) if w.count() > 0: if w.first().count > 0: r = True session.close() return r
def user_turn(user_id, channel_id): session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() if m.turn % 2 == 0: # Even turns are player 2, odd is player 1 turn = 2 else: turn = 1 s = session.query(exists() .where(Player.match_id == m.id) .where(Player.position == turn) .where(Player.user_id == user_id)).scalar() session.close() return s
def player_data(channel_id): data = {} session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() if m.turn % 2 == 0: # Even turns are player 2, odd is player 1 turn = 2 else: turn = 1 player = session.query(Player).filter(Player.match_id == m.id).filter(Player.position == turn).first() data['player'] = {'name': player.user.name, 'hp': player.hp, 'special': player.special, 'prayer_points': player.prayer_points, 'food': player.food, 'frozen': player.frozen, 'poison': player.poison, 'prayer': player.prayer, } session.commit() session.close() return data
def begin_match(channel_id): # Match: Set the begin and last play time. Randomize the turn between 1,2 # Players: Reset the stats session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() m.start_time = func.now() m.last_play = func.now() m.turn = random.choice([1, 2]) players = session.query(Player).filter(Player.match_id == m.id).all() for player in players: player.hp = 99 player.special = 100 player.prayer_points = 99 player.food = 3 player.poison = False player.frozen = False player.prayer = None session.commit() session.close()
def user_exists(user_id): session = Session() s = session.query(exists().where(User.id == user_id)).scalar() session.close() return s
def role_exists(role_id, server_id): session = Session() s = session.query(exists().where(Role.id == role_id and Role.server_id == server_id)).scalar() session.close() return s
def server_roles(server_id): session = Session() s = session.query(Server).filter(Server.id == server_id).first() results = [str(role.id) for role in s.roles] session.close() return results
def channel_exists(channel_id, server_id): session = Session() s = session.query(exists().where(Channel.id == channel_id and Channel.server_id == server_id)).scalar() session.close() return s
def server_exists(server_id): session = Session() s = session.query(exists().where(Server.id == server_id)).scalar() session.close() return s
def get_stake(channel_id): session = Session() m = session.query(Match).filter(Match.channel_id == channel_id).first() r = m.stake session.close() return r
def get_user_gp(user_id): session = Session() p = session.query(User).filter(User.id == user_id).first() gp = p.gp session.close() return gp