Example #1
0
def handle_post_secret(data):
    app.logger.info('post secret %s', data)
    if not 'coin_id' in data or not 'racer_id' in data:
        return
    coin = CopperCoin.objects(pk=data.get('coin_id')).first()
    racer = Racer.objects(pk=data['racer_id']).first()

    if racer and coin and coin.secret == data['secret']:
        emit('secret correct')
        racer.modify(inc__score=200)
        spectators = Racer.objects(pos__near=racer.pos,
                                   pos__max_distance=GLOBAL_RANGE)
        update_scores(spectators)
Example #2
0
def oldstylemap():
    if 'username' in session:
        admin = False
        #try:
        # get the main Racer.. you need to be logged in
        session['racer'] = Racer.objects(name=session['username']).first()
        mainracer = session['racer']
        mainracer.modify(is_online=True)
        mainracer.clear_nearby()  # in case it was not done on logout
        app.logger.info('loaded %s, of type %s', mainracer, type(mainracer))
        _, stuff = mainracer.get_nearby_stuff()
        racers = [o.get_info() for o in stuff if isinstance(o, Racer)]
        flags = [o.get_info() for o in stuff if isinstance(o, Flag)]
        mainracer.clear_nearby(
        )  # clear again to load all the stuff that I didn't add here on the next move
        myself = mainracer.get_info()
        racers = [myself] + racers
        app.logger.info('loading... %s', racers)
        # #     return "Failed to show map"

        data = {
            'racers': racers,
            'username': session['username'],
            'flags': flags
        }
        # detect admin access
        if session['username'] == 'Stefaan':  # todo: set party admins
            app.logger.warn('admin logged in %s', session['username'])
            admin = True
        return render_template('map.html', flaskData=data, admin=admin)
    else:
        session['newstyle'] = False
        return redirect(url_for('login'))
Example #3
0
def handle_disconnect():
    if session.get('username'):
        app.logger.info('Disconnecting %s ...', session['username'])
        leave_room(str(session['username']))
        r = Racer.objects(name=session['username']).first()
        if r:
            r.modify(is_online=False)
    else:
        app.logger.warning('Disconnecting but no username in session')
    return 'OK'
Example #4
0
def handle_connect():
    if 'username' in session:
        app.logger.info('New connection received: %s', session['username'])
        # register the handler
        join_room(str(session['username']))
        r = Racer.objects(name=session['username']).first()
        if r:
            r.modify(is_online=True)
    else:
        app.logger.warning('New connection but no username in session')
    app.logger.info('New conn rooms: %s', rooms())
    return 'OK'
Example #5
0
def move_racer():
    # DEPRECATED
    """Moves a Racer in the persistent database"""
    if request.method == 'POST':
        errors = []
        # TODO clean this
        name, lat, lng = request.get_data().split()
        try:
            # r = Racer('yolo', 'POINT(%s %s)' % (lng, lat))
            r = Racer.objects(name=name).first()
            r.pos = (float(lng), float(lat))
            r.save()
            return "OK"
        except Exception as e:
            errors.append(e)
            app.logger.error("Failed to move racer: %s", repr(e))
            return "Failed to move marker"
    else:
        return "Post me some JSON plx"
Example #6
0
def login():
    if request.method == 'POST':
        if not request.form['username']:
            return render_template('login.html',
                                   error='You must fill in a Username')
        if not all(c.isalnum() or c.isspace()
                   for c in request.form['username']):
            return render_template(
                'login.html',
                error='Only use letters, numbers and spaces in your Username')
        username = session['username'] = escape(request.form['username'])
        color = session['color'] = request.form['color']

        if username.startswith('clearall'):
            deletables = [(Bomb, 'bombs'), (CopperCoin, 'coins'),
                          (Flag, 'flags')]
            for cls, keyw in deletables:
                if username.endswith(keyw):
                    cls.objects().delete()
            return redirect(url_for('logout'))

        r = Racer.objects(name=username)
        if not r:
            # add a new Racer to the db
            r = Racer(name=username, pos=[3.7, 51], color=color)
        else:
            # update the color attribute to the db
            r = r.first()
            r.color = color
        r.is_online = True
        r.save()
        # keep track of the racer object in this session
        session['racerid'] = str(r.id)
        app.logger.debug('id is %s', session['racerid'])
        if session.get('newstyle'):
            return redirect(url_for('newstylemap'))
        else:
            return redirect(url_for('oldstylemap'))
    return render_template('login.html', error='')
Example #7
0
def handle_get_scores():
    if session.get('username'):
        app.logger.info('Get scores for %s', session['username'])
        racers = Racer.objects()  # add party
        update_scores(racers)