<commit_before>import flask
import flask_socketio as fsio
from . import socketio
from .tasks import play_game
from .models import db, GameSetup
from .routes import GLOBAL_DICT
import sqlalchemy

NAMESPACE = '/game-session'
REQUIRED_PLAYER_COUNT = 4

@socketio.on('joined', namespace=NAMESPACE)
def joined(message):
    """Sent by clients when they enter a room."""
    game_id = flask.session.get('game_id')
    fsio.join_room(game_id)

    # TODO: move to database and guard against errors
    player_count = GLOBAL_DICT[game_id]['player_count']

    fsio.emit('player_count', '{\"count\" : ' + str(REQUIRED_PLAYER_COUNT-player_count) + '}', room=game_id, namespace=NAMESPACE)

    # If there are enough players, start the game
    if player_count == REQUIRED_PLAYER_COUNT:
        fsio.emit('game_start', {}, room=game_id, namespace=NAMESPACE)
        result = play_game.delay(game_id).get()
        fsio.emit('result', result, room=game_id, namespace=NAMESPACE)

@socketio.on('left', namespace=NAMESPACE)
def left(message):
    """Sent by clients when they leave a room."""
    room = flask.session.get('game_id')
    fsio.leave_room(room)
<commit_msg>[NG] Guard against undefined game id on lobby joiners.<commit_after>import flask
import flask_socketio as fsio
from . import socketio
from .tasks import play_game
from .models import db, GameSetup
from .routes import GLOBAL_DICT # TODO: need to get rid of
import sqlalchemy

NAMESPACE = '/game-session'
REQUIRED_PLAYER_COUNT = 4

@socketio.on('joined', namespace=NAMESPACE)
def joined(message):
    """Sent by clients when they enter a room."""
    game_id = flask.session.get('game_id')
    fsio.join_room(game_id)

    # TODO global dict needs to go
    if game_id not in GLOBAL_DICT:
        flask.flash("sorry something isn't quite right... try joining another game")
        return flask.redirect(flask.url_for('.lobby'))

    player_count = GLOBAL_DICT[game_id]['player_count']

    # TODO: use json dumps and make less ugly
    fsio.emit('player_count', '{\"count\" : ' + str(REQUIRED_PLAYER_COUNT-player_count) + '}', room=game_id, namespace=NAMESPACE)

    # If there are enough players, start the game
    if player_count == REQUIRED_PLAYER_COUNT:
        fsio.emit('game_start', {}, room=game_id, namespace=NAMESPACE)
        result = play_game.delay(game_id).get()
        fsio.emit('result', result, room=game_id, namespace=NAMESPACE)

@socketio.on('left', namespace=NAMESPACE)
def left(message):
    """Sent by clients when they leave a room."""
    room = flask.session.get('game_id')
    fsio.leave_room(room)

{'fraction_non_alphanumeric': 0.1173963580007749, 'fraction_numerical': 0.0007748934521503294, 'mean_word_length': 4.31275720164609, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '71283', 'n_tokens_mistral': 838, 'n_tokens_neox': 838, 'n_words': 222}