From 03e422e97c59a55545460188ad6441b0a4456a86 Mon Sep 17 00:00:00 2001
From: Paolo Brasolin <paolo.brasolin@eurac.edu>
Date: Thu, 10 Mar 2022 09:12:34 +0100
Subject: [PATCH] feat: #fe #be fetch actual data

---
 backend/src/index.ts           | 28 ++++++++++++-------------
 frontend/.env.development      |  2 +-
 frontend/src/js/fight_scene.js | 38 ++++++++++++++--------------------
 3 files changed, 30 insertions(+), 38 deletions(-)

diff --git a/backend/src/index.ts b/backend/src/index.ts
index d258ed7..ba40061 100644
--- a/backend/src/index.ts
+++ b/backend/src/index.ts
@@ -26,34 +26,32 @@ server.route({
   method: "POST",
   url: "/GetImage",
   schema: {
-    body: {
-      type: "object",
-      properties: {
-        sessionImages: {
-          type: "array",
-          items: { type: "number" },
-        },
-      },
-    },
+    // body: {},
     response: {
       200: {
         type: "object",
         properties: {
-          id: { type: "number" },
+          id: { type: "string", format: "uuid" },
           image: { type: "string" },
+          ocr_confidence: { type: "number", minimum: 0, maximum: 1 },
+          ocr_transcript: { type: "string" },
         },
       },
     },
   },
   handler: async function (request, reply) {
-    // TODO: skip images already used in current game
-    const image = await connection
-      .table("images")
+    // TODO: skip images already used in current game (overkill? we prolly have thousands so collisions should be irrelevant...)
+    const word = await connection
+      .table("words")
+      .whereBetween("ocr_confidence", [0.4, 0.8]) // i.e. needs improvement but it's not trash
+      .whereRaw(`"ocr_transcript" ~ '^[[:alpha:]]+$'`) // i.e. no numbers nor symbols
       .orderByRaw("RANDOM()")
       .first();
     reply.send({
-      id: image.id,
-      image: image.image,
+      id: word.id,
+      image: word.image,
+      ocr_confidence: word.ocr_confidence,
+      ocr_transcript: word.ocr_transcript,
     });
   },
 });
diff --git a/frontend/.env.development b/frontend/.env.development
index 92a72bc..7dd94c4 100644
--- a/frontend/.env.development
+++ b/frontend/.env.development
@@ -1 +1 @@
-BACKEND_URL=http://localhost:8080/
+BACKEND_URL=http://localhost:8080
diff --git a/frontend/src/js/fight_scene.js b/frontend/src/js/fight_scene.js
index 53fbe84..6cf0be5 100644
--- a/frontend/src/js/fight_scene.js
+++ b/frontend/src/js/fight_scene.js
@@ -320,28 +320,22 @@ function dispatchEnemy(scene) {
   let e = new enemy(scene);
   window.onscreenEnemies.push(e);
 
-  backend
-    .post("GetImage", {
-      sessionImages: [],
-    })
-    .then(function (response) {
-      e.refData = response.data;
-
-      const imageData = `data:image/png;base64,${response.data.image}`;
-      scene.textures.addBase64(`WORD-${response.data.id}`, imageData);
-
-      scene.textures.once(
-        "addtexture",
-        function () {
-          e.run((v) => {
-            setTimeout(() => {
-              dispatchEnemy(scene);
-            }, Math.floor(Math.random() * 10000 + 3000));
-          });
-        },
-        scene,
-      );
-    });
+  backend.post("GetImage", {}).then(function (response) {
+    e.refData = response.data;
+
+    scene.textures.addBase64(`WORD-${response.data.id}`, response.data.image);
+    scene.textures.once(
+      "addtexture",
+      function () {
+        e.run((v) => {
+          setTimeout(() => {
+            dispatchEnemy(scene);
+          }, Math.floor(Math.random() * 10000 + 3000));
+        });
+      },
+      scene,
+    );
+  });
 }
 
 export default fightScene;
-- 
GitLab