Ejemplo n.º 1
0
    def on_connect(self):
        """ Return False if player does not have permission. Called when a player attempts to connect to the socket. """
        if session.get('username') is None:
            return False
        player = get_current_player(session.get('username'))
        if player is None:
            return False
        self.players_logged_in.add(player.get_name())

        self.emit(self.STATISTICS, self._get_stats())
Ejemplo n.º 2
0
    def on_answer_option(self, data):
        """ data is formatted as {"answer_number": 1} """
        if data['answer_number'] == self.correct_answer_idx:

            # time taken to answer the question = end time - start time
            time_taken = time.time() - self.starting_time

            print(f"{session['username']} answered in {time_taken}")

            player = get_current_player(session.get('username'))
            if player.player_name not in self.answered:
                self.answered.add(player.player_name)
                # score for this round = 30 seconds - time taken
                player.score += Game.calculate_score(time_taken)
                db.session.add(player)
                db.session.commit()

        self.emit(self.STATISTICS, self._get_stats())
Ejemplo n.º 3
0
def login():
    """ Login to obtain session cookie if available """
    # regular user
    if request.form.get('nickname') is None:
        return gen_response({
            'success': True,
            'message': 'please input nickname'
        })
    if request.form.get('username') is not None:
        username = request.form.get('username').lower().strip()
        nickname = request.form.get('nickname').lower().strip()
        player = get_current_player(username)

        # player can be found
        if player is not None:
            # update nickname
            if nickname is not None and len(nickname) > 0:
                player.nickname = nickname
                db.session.add(player)
                try:
                    db.session.commit()
                except IntegrityError:
                    return gen_response({
                        'success': False,
                        'message': 'nickname not unique'
                    })

            # set user session
            session['username'] = username

            return gen_response({
                'success': True,
                'username': username,
                'nickname': nickname
            })
        else:
            # player cannot be found in database
            return gen_response({
                'success': False,
                'message': 'must use registered email prefix'
            })
Ejemplo n.º 4
0
    def on_disconnect(self):
        """ Called when a player disconnects from the socket. """
        player = get_current_player(session.get('username'))
        self.players_logged_in.discard(player.get_name())

        self.emit(self.STATISTICS, self._get_stats())