def startGame(user, gameid):
    userData = getUserData(user)
    if user:
        session = Session()
        game = session.query(GameModel) \
                .filter(GameModel.id==gameid).first()
        if ((user == game.owner) and (user in game.players)
                and game.status == "new"
                and (len(game.players) == game.nplayers)):
            players = []
            for player in game.players:
                players.append({'name': player.username, 'id': player.id})
                notes = Notes(player, game)
                session.add(notes)
            newgame = Game(players, game.advanced, game.id)
            activePlayer = newgame.getActivePlayer()
            active = session.query(User) \
                    .filter(User.id==activePlayer.id).first()
            game.active = active
            game.status = "running"
            game.game = jsonpickle.encode(newgame)
            session.commit()
            emails = [player.email for player in game.players]
            send_start_game_email(emails, gameid)
        session.close()
    return gameList(userData)
def createGame(user, nplayers, advanced):
    userData = getUserData(user)
    if user:
        session = Session()
        newgame = GameModel(advanced=advanced, nplayers=nplayers, owner=user)
        newgame.players = [user]
        session.add(newgame)
        session.commit()
        session.close()
    return gameList(userData)
def signup():
    session = Session()
    req = request.get_json()
    email = req['email']
    password = req['password']
    username = req['username']
    url = req['url']
    data = {}
    if exists_email(email):
        data['status'] = 'email_exists'
    elif exists_username(username):
        data['status'] = 'username_exists'
    else:
        user = User(username=username, email=email, password=password)
        session.add(user)
        # now first commit before sending email, else the new user has no id
        # yet...
        session.commit()
        send_registration_email(user, url)
        data['status'] = 'success'
    json_data = json.dumps(data)
    return json_data