52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from flask import Blueprint, g, request, jsonify, current_app
|
|
from typing import cast
|
|
from dataclasses import asdict
|
|
|
|
from app.utils.typed_flask import CoCFlask
|
|
from app.utils.session_user import SessionUser, import_g_session
|
|
from app.game.generators.entity_factory import build_char
|
|
from app.services.appwrite_db import AppwriteTables
|
|
|
|
from app.utils.logging import get_logger
|
|
|
|
logging = get_logger(__file__)
|
|
|
|
# type cast flask to my custom flask app so the app.api methods are available in the IDE / typed correctly.
|
|
app = cast(CoCFlask,current_app)
|
|
|
|
# blueprint def
|
|
char_bp = Blueprint("char", __name__, url_prefix="/char")
|
|
|
|
# return CURRENT USER
|
|
# {"user":g.appwrite_user}
|
|
|
|
@char_bp.route("/new", methods=["POST"])
|
|
def new():
|
|
api_user = import_g_session(g.appwrite_user)
|
|
|
|
data = request.get_json(silent=True)
|
|
|
|
name = data.get("name")
|
|
origin_story = data.get("origin_story")
|
|
race_id = data.get("race_id")
|
|
profession_id = data.get("profession_id")
|
|
|
|
try:
|
|
player = build_char(name=name,origin_story=origin_story,race_id=race_id,profession_id=profession_id)
|
|
player_dict = asdict(player)
|
|
uuid = player.uuid
|
|
|
|
tablesdb = AppwriteTables()
|
|
tablesdb.save_character_for_user_id(api_user.id,player_dict)
|
|
|
|
|
|
logging.info(f"Created char {uuid} for {api_user.id} - {api_user.name} - {api_user.email}")
|
|
|
|
except Exception as e:
|
|
logging.error(f"Unable to create char for user: {api_user.id} due to {e}")
|
|
player_dict = {}
|
|
|
|
uuid = player_dict.get("uuid",{})
|
|
print(f"Returned {uuid}")
|
|
return app.api.ok(uuid)
|
|
|