Example #1
0
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
Example #2
0
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()
Example #3
0
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()
Example #4
0
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()
Example #5
0
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()
Example #6
0
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
Example #7
0
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()
Example #8
0
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
Example #9
0
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()
Example #10
0
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()
Example #11
0
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()
Example #12
0
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()
Example #13
0
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()
Example #14
0
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()
Example #15
0
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
Example #16
0
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
Example #17
0
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
Example #18
0
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
Example #19
0
def create_channel(channel, server):
    session = Session()
    s = session.query(Server).filter(Server.id == server.id).first()
    chan = Channel(channel.id, channel.name, s)
    match = Match(chan)
    p1 = Player(match, 1)
    p2 = Player(match, 2)
    session.add(chan)
    session.add(match)
    session.add(p1)
    session.add(p2)
    session.commit()
    session.close()
Example #20
0
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
Example #21
0
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
Example #22
0
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
Example #23
0
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()
Example #24
0
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()
Example #25
0
def user_exists(user_id):
    session = Session()
    s = session.query(exists().where(User.id == user_id)).scalar()
    session.close()
    return s
Example #26
0
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
Example #27
0
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
Example #28
0
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
Example #29
0
def server_exists(server_id):
    session = Session()
    s = session.query(exists().where(Server.id == server_id)).scalar()
    session.close()
    return s
Example #30
0
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