From ef5ed73c9ec498340266e5c8c7e9d3617b70330d Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 26 Mar 2023 15:42:22 +0200 Subject: [PATCH 1/3] added REACTION_UUID field to Reaction --- truthinquiry/ext/database/models.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/truthinquiry/ext/database/models.py b/truthinquiry/ext/database/models.py index 3535f5c..c53d077 100644 --- a/truthinquiry/ext/database/models.py +++ b/truthinquiry/ext/database/models.py @@ -1,4 +1,6 @@ -from sqlalchemy import Column, Integer, VARCHAR, Text, LargeBinary, ForeignKey +import uuid + +from sqlalchemy import Column, Integer, VARCHAR, Text, LargeBinary, ForeignKey, UUID from sqlalchemy.orm import relationship, declarative_base Base = declarative_base() @@ -198,14 +200,16 @@ class Reaction(Base): IMG = Column(LargeBinary(length=2**24), comment="Binary data of the image associated to this npc and trait") NPC = relationship("Npc") TRAIT = relationship("Trait") + REACTION_UUID = Column(UUID, unique=True, comment="ID of this reaction") def __init__(self, REACTION_ID, NPC_ID, TRAIT_ID): self.REACTION_ID = REACTION_ID self.NPC_ID = NPC_ID self.TRAIT_ID = TRAIT_ID + self.REACTION_UUID = uuid.uuid4() def __str__(self) -> str: - return f"Reaction(REACTION_ID={self.REACTION_ID}, NPC_ID={self.NPC_ID}, TRAIT_ID={self.TRAIT_ID})" + return f"Reaction(REACTION_ID={self.REACTION_ID}, NPC_ID={self.NPC_ID}, TRAIT_ID={self.TRAIT_ID}, REACTION_UUID={self.REACTION_UUID})" def __repr__(self) -> str: return self.__str__() \ No newline at end of file From daeea9cfd7837c357ecca08385fd0ddd314509b8 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 26 Mar 2023 16:12:10 +0200 Subject: [PATCH 2/3] implement api route /getReaction --- truthinquiry/routes/routes_api.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/truthinquiry/routes/routes_api.py b/truthinquiry/routes/routes_api.py index c3728a0..055ce50 100644 --- a/truthinquiry/routes/routes_api.py +++ b/truthinquiry/routes/routes_api.py @@ -1,7 +1,11 @@ import json -import json -import flask +import io +import flask +from sqlalchemy import select + +from truthinquiry.ext.database.models import * +from truthinquiry.ext.database.fsa import db from truthinquiry.ext.discord_bot import discord_bot from truthinquiry.ext.socketio import socket_io from truthinquiry.logic import game_logic @@ -154,6 +158,20 @@ def get_npc_reaction(): 'Content-Disposition', 'attachment', filename='reaction.png') return response +@routes_api.route("/getReaction", methods=["GET", "POST"]) +def get_reaction(): + input_uuid = flask.request.values.get("uuid") + results = db.session.execute(select(Reaction).where(Reaction.REACTION_UUID==uuid.UUID(input_uuid))) + + row = results.first() + if row == None: + return {"error": 1, "msg": "No such reaction"} + reaction_obj = row[0] + + return flask.send_file(io.BytesIO(reaction_obj.IMG), mimetype='image/png') + + + @routes_api.route("/gameProgress", methods=["GET", "POST"]) def game_progress(): From f3a27be97188604a0853a03fc27f57cb2e5c3b73 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 26 Mar 2023 16:22:20 +0200 Subject: [PATCH 3/3] do not use UUID type for compatibility with old MariaDB servers --- truthinquiry/ext/database/models.py | 4 ++-- truthinquiry/routes/routes_api.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/truthinquiry/ext/database/models.py b/truthinquiry/ext/database/models.py index c53d077..957a8be 100644 --- a/truthinquiry/ext/database/models.py +++ b/truthinquiry/ext/database/models.py @@ -1,6 +1,6 @@ import uuid -from sqlalchemy import Column, Integer, VARCHAR, Text, LargeBinary, ForeignKey, UUID +from sqlalchemy import Column, Integer, VARCHAR, Text, LargeBinary, ForeignKey from sqlalchemy.orm import relationship, declarative_base Base = declarative_base() @@ -200,7 +200,7 @@ class Reaction(Base): IMG = Column(LargeBinary(length=2**24), comment="Binary data of the image associated to this npc and trait") NPC = relationship("Npc") TRAIT = relationship("Trait") - REACTION_UUID = Column(UUID, unique=True, comment="ID of this reaction") + REACTION_UUID = Column(VARCHAR(255), unique=True, comment="ID of this reaction") def __init__(self, REACTION_ID, NPC_ID, TRAIT_ID): self.REACTION_ID = REACTION_ID diff --git a/truthinquiry/routes/routes_api.py b/truthinquiry/routes/routes_api.py index 055ce50..1a89169 100644 --- a/truthinquiry/routes/routes_api.py +++ b/truthinquiry/routes/routes_api.py @@ -161,7 +161,7 @@ def get_npc_reaction(): @routes_api.route("/getReaction", methods=["GET", "POST"]) def get_reaction(): input_uuid = flask.request.values.get("uuid") - results = db.session.execute(select(Reaction).where(Reaction.REACTION_UUID==uuid.UUID(input_uuid))) + results = db.session.execute(select(Reaction).where(Reaction.REACTION_UUID==input_uuid)) row = results.first() if row == None: