Ejemplo n.º 1
0
def import_alliances():
    click.echo("Start to import alliances from http://www.leagueofautomatednations.com/alliances.js")
    import requests as r
    import screeps_loan.models.alliances as alliances_model
    import screeps_loan.models.users as users_model
    import screeps_loan.auth_user
    import screeps_loan.services.users as users_service

    alliance_query = alliances_model.AllianceQuery()
    users_query = users_model.UserQuery()
    screeps = get_client()
    auth_user = screeps_loan.auth_user.AuthPlayer(screeps)
    resp = r.get('http://www.leagueofautomatednations.com/alliances.js')
    data = json.loads(resp.text)
    for shortname, info in data.items():
        print(shortname)
        members = info['members']
        fullname = info['name']
        color = None
        if 'color' in info:
            color = info['color']
        slack = None
        if 'slack' in info:
            slack = info['slack']
        alliance = alliance_query.find_by_shortname(shortname)
        if (alliance is None):
            alliance_query.insert_alliance(shortname, fullname, color, slack)
            alliance = shortname

        existing_member = [i['name'] for i in users_query.find_name_by_alliances([shortname])]
        new_members = [name for name in members if name not in existing_member]
        for member in new_members:
            print(member)
            id = users_service.player_id_from_api(member)
            users_query.update_alliance_by_screeps_id(id, shortname)
Ejemplo n.º 2
0
def login_as_user(user):
    api = screeps_client.get_client()
    auth = AuthPlayer(api)
    (id, token) = auth.auth_token(user)
    if (id is not None):
        message = "%s/auth/%s" % (app.config['WEB_ROOT'], token)
        click.echo(message)
Ejemplo n.º 3
0
def kick_from_alliance():
    my_id = session['my_id']
    alliance = users_model.alliance_of_user(my_id)
    if alliance is None:
        return "You are not in an alliance, can't kick anyone"

    username = request.form['username']

    # Make sure user is in database and game world.
    api = screeps_client.get_client()
    auth = AuthPlayer(api)
    ign = auth.id_from_name(username)

    # # Check if user is not leader
    # leader_list = alliance['leaders']
    # if my_id not in leader_list:
    #     flash('Only the alliance leader can remove members.')
    #     return redirect(url_for("my_alliance"))

    if not ign:
        flash(
            'User not present in game - remember usernames are case sensitive.'
        )
        return redirect(url_for("my_alliance"))

    # Get database id
    user_id = users_model.user_id_from_db(username)

    if not user_id:
        flash('User not present in system - please try again later.')
        return redirect(url_for("my_alliance"))

    # Is user already in an alliance?
    current_alliance = users_model.alliance_of_user(user_id)
    if not current_alliance:
        flash('User is not in an alliance.')
        return redirect(url_for("my_alliance"))
    if current_alliance[1] != alliance[1]:
        flash('User is not in your alliance.')
        return redirect(url_for("my_alliance"))

    users_model.update_alliance_by_user_id(user_id, None)

    # # Remove user from leader list if there
    # if user_id in leader_list:
    #     leader_list.remove(user_id)
    #     alliances_model.update_leader_of_alliance(alliance['shortname'], leader_list)

    flash('Successfully kicked {} from your alliance'.format(username))
    kicker_name = users_model.user_name_from_db_id(my_id)

    # Get player id for message and send
    player_id = users_model.player_id_from_db(username)
    api.msg_send(
        player_id,
        'You have been removed from {} by {}.'.format(alliance['fullname'],
                                                      kicker_name))
    return redirect(url_for("my_alliance"))
Ejemplo n.º 4
0
def login():
    if request.method == 'POST':
        #TODO: Authenticate via url
        username = request.form['username']
        api = screeps_client.get_client()
        auth = AuthPlayer(api)
        (id, token) = auth.auth_token(username)
        if (id is not None):
            url = url_for('auth', token=token, _external=True)
            message = "Login to the League of Automated Nations %s" % url
            api.msg_send(id, message)
        return redirect(url_for('auth_request'))
    return render_template("login.html")
Ejemplo n.º 5
0
def invite_to_alliance():
    my_id = session['my_id']
    alliance = users_model.alliance_of_user(my_id)
    if alliance is None:
        return "You are not in an alliance."

    # # Check if user is not leader or if leader has not been set yet
    # leader_list = alliance['leader']
    # if my_id not in leader_list:
    #     flash('Only the alliance leaders can invite.')
    #     return redirect(url_for("my_alliance"))

    username = request.form['username']

    # Make sure user is in database and game world.
    api = screeps_client.get_client()
    auth = AuthPlayer(api)
    ign = auth.id_from_name(username)

    if not ign:
        flash(
            'User not present in game - remember usernames are case sensitive.'
        )
        return redirect(url_for("my_alliance"))

    # Get database id
    id = users_model.user_id_from_db(username)

    if not id:
        flash('User not present in system - please try again later.')
        return redirect(url_for("my_alliance"))

    # Is user already in an alliance?
    current_alliance = users_model.alliance_of_user(id)
    if current_alliance:
        flash('User is already in an alliance.')
        return redirect(url_for("my_alliance"))

    invites.add_invite(id, alliance['shortname'], my_id)
    api.msg_send(
        ign, "You are invited to join %s \n\n%s" %
        (alliance['fullname'], url_for('list_invites', _external=True)))

    flash('Successfully invited {} to your alliance'.format(username))
    return redirect(url_for("my_alliance"))
Ejemplo n.º 6
0
    def run(self):
        self.start()
        api = get_client()
        shards = api.get_shards()

        for shard in shards:
            click.echo('Importing from %s' % (shard,))
            queue = []
            user_map = {}
            roomCount = 0
            api_worldsize = api.worldsize(shard)
            maxroom = int((api_worldsize['width']-2)/2)
            for x in range(1, maxroom + 1):
                for y in range(1, maxroom + 1):
                    for horizontal in ['E', 'W']:
                        for vertical in ['N', 'S']:
                            room = "%s%s%s%s" % (horizontal, x, vertical, y)
                            if self.isNPC(room):
                                continue
                            queue.append(room)
                    if len(queue) < self.queueLimit:
                        if y < maxroom or x < maxroom:
                            continue
                    room_statistics = api.map_stats(queue, 'claim0', shard)
                    roomCount += self.queueLimit
                    click.echo(str(roomCount) + " rooms requested")
                    #self.calls = self.calls + 1
                    queue = []

                    for id, user_info in room_statistics['users'].items():
                        username = user_info['username']
                        user_map[id] = username

                    for room, statistics in room_statistics['stats'].items():
                        if 'own' in statistics:
                            if 'user' in statistics['own']:
                                user_id = statistics['own']['user']
                                level = statistics['own']['level']
                                self.update(user_id, user_map[user_id], room, level, shard)

        conn = get_conn()
        self.finish()
        conn.commit()
Ejemplo n.º 7
0
def export_to_segments():
    click.echo("Exporting Alliance and Bot Data To Segment")

    r = requests.get(
        'http://www.leagueofautomatednations.com/vk/bots/league.json')
    if r.status_code == requests.codes.ok:
        clone_data = r.text
    else:
        clone_data = False

    import screeps_loan.models.alliances as alliances
    import screeps_loan.models.users as users

    alliance_query = alliances.AllianceQuery()
    all_alliances = alliance_query.getAll()

    alliances_name = [item["shortname"] for item in all_alliances]
    users_with_alliance = users.UserQuery().find_name_by_alliances(
        alliances_name)

    alliances_aux = {}
    for alliance in all_alliances:
        members = [
            user['name'] for user in users_with_alliance
            if user['alliance'] == alliance['shortname']
        ]
        if len(members) < 2:
            continue
        alliances_aux[alliance['shortname']] = members

    alliance_json = json.dumps(alliances_aux)

    screeps = get_client()
    shards = screeps.get_shards()
    for shard in shards:
        screeps.set_segment(alliance_segment, alliance_json, shard)
        if clone_data:
            screeps.set_segment(clone_segment, clone_data, shard)
Ejemplo n.º 8
0
def send_message():
    my_id = session['my_id']
    message = request.form['alliance-message']
    alliance = users_model.alliance_of_user(my_id)
    if alliance is None:
        return "You are not in an alliance."

    # # Check if user is not leader or if leader has not been set yet
    # leader_list = alliance['leader']
    # if my_id not in leader_list:
    #     flash('Only the alliance leaders can send alliance wide messages.')
    #     return redirect(url_for("my_alliance"))

    alliance_members = users_model.find_player_id_by_alliance(
        alliance['shortname'])
    api = screeps_client.get_client()

    sender_name = users_model.user_name_from_db_id(my_id)
    for player_id in alliance_members:
        api.msg_send(player_id,
                     '{}\n\nSent By: {}'.format(message, sender_name))

    flash('Message sent.')
    return redirect(url_for("my_alliance"))
Ejemplo n.º 9
0
def player_id_from_api(name):
    screeps = get_client()
    resp = screeps.user_find(name)
    if ((resp['ok'] == 1) and (resp['user'] is not None)):
        return resp['user']['_id']
    return None
Ejemplo n.º 10
0
def getUserInfo(username):
    screeps = get_client()
    sleep(1)
    return screeps.user_find(username)
Ejemplo n.º 11
0
def get_shard_size(shard):
    api = get_client()
    api_worldsize = api.worldsize(shard)
    return int((api_worldsize['width'] - 2) / 2)
Ejemplo n.º 12
0
def get_shards():
    api = get_client()
    shards = api.get_shards()
    if not shards:
        shards = ['shard0']
    return shards