aboutsummaryrefslogtreecommitdiffstats
path: root/helpers/helpersBack.ts
diff options
context:
space:
mode:
authorMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-24 00:34:07 +0200
committerMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-24 00:34:07 +0200
commit13dcc2c8fc1d11e3b94432fffc4c4f186bd94d29 (patch)
tree077a963bae31729dffa6c7c678af112680bb6d38 /helpers/helpersBack.ts
parent457ee1d7caa899bbe86bf7a1390f940b5b3176b7 (diff)
downloadfia-13dcc2c8fc1d11e3b94432fffc4c4f186bd94d29.tar.gz
fia-13dcc2c8fc1d11e3b94432fffc4c4f186bd94d29.tar.zst
fia-13dcc2c8fc1d11e3b94432fffc4c4f186bd94d29.zip
Playing almost working
Diffstat (limited to 'helpers/helpersBack.ts')
-rw-r--r--helpers/helpersBack.ts92
1 files changed, 83 insertions, 9 deletions
diff --git a/helpers/helpersBack.ts b/helpers/helpersBack.ts
index d594c03..5aaae77 100644
--- a/helpers/helpersBack.ts
+++ b/helpers/helpersBack.ts
@@ -1,10 +1,20 @@
import fetch from "node-fetch";
import { Response } from "express";
import Helpers from "./helpers";
+import { global } from "../backend/ts/index";
export { Helpers };
-export async function makeQuery(query: string, params: Array<any>): Promise<any> {
- let t = await (
+export const API_RES = {
+ success: '{"success": true}',
+ failure: '{"success": false}',
+};
+export async function makeQuery(query: string, params: Array<any>): Promise<Array<any>> {
+ // console.log("params are ", JSON.stringify({
+ // apiKey: process.env.API_KEY,
+ // query: query,
+ // params: params
+ // }));
+ let t: any = await (
await fetch("https://jopek.eu/maks/szkola/apkKli/fiaFiles/fia.php", {
method: "POST",
headers: {
@@ -18,8 +28,28 @@ export async function makeQuery(query: string, params: Array<any>): Promise<any>
})
})
).text();
- // console.log(t);
- return JSON.parse(t);
+ try {
+ t = JSON.parse(t)[0];
+ } catch (e) {
+ console.log(`dbResposnse to "${query}"`, ` is '${t}' (typeof res ${typeof t})`);
+ console.log(`dbResposnse as object as string: '${JSON.stringify(t)}'`);
+ }
+ for (const p in t) {
+ if (["data", "gameBoard"].includes(p))
+ t[p] = JSON.parse(t[p]);
+ }
+ return [t];
+}
+
+export async function getData(gid: number): Promise<Data> {
+ let data = (await makeQuery("SELECT `data` FROM `fia` WHERE `id` = ?", [gid]))[0].data;
+ if (typeof data === "string") {
+ data = JSON.parse(data) as Data;
+ return data;
+ } else {
+ console.log(data);
+ throw new Error("Data is not a object");
+ }
}
export async function mFetch(url: string, body: object, res: Response): Promise<Response> {
@@ -33,6 +63,19 @@ export async function mFetch(url: string, body: object, res: Response): Promise<
}).then(fRes => Helpers.checkApiRes(fRes, res)) as Promise<Response>;
}
+export function nextTurnEndsAt(): number {
+ return Date.now() + (60 * 1000);
+}
+
+export async function sleep(ms: number) {
+ return new Promise(resolve => setTimeout(resolve, ms));
+}
+export async function waitForQueue(): Promise<void> {
+ while(global.stop === true) {
+ await sleep(200);
+ }
+}
+
export interface Colors {
blue: number;
red: number;
@@ -60,18 +103,49 @@ export type Data = Array<DataObject>;
export interface DataObject {
id: any;
index: number;
+ ready: boolean;
color: tColors;
name: string;
}
-export interface Chequer extends Coordinates {
+export interface GhostWhere {
+ from: "base" | "map" | "home";
+ oldIndex: number;
+ to: "base" | "map" | "home";
+ newIndex: number;
+}
+export interface Ghost {
+ where: GhostWhere;
color: tColors;
+ coords?: Coordinates;
+}
+export interface Chequer {
+ color: tColors;
+ ghost?: Ghost; // number; // 1 | 2 | 3 | 4 | 5 | 6;
};
-
export interface Coordinates {
- x: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
- y: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
+ x: number; //0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
+ y: number; //0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
+}
+export interface Square extends Coordinates {
+ chequers: Array<Chequer>;
+ start: -1 | tColors;
}
-export type GameBoard = Array<Chequer>;
+export interface HoBSquare extends Coordinates {
+ chequer: tColors;
+ ghost?: Ghost; // number; // 1 | 2 | 3 | 4 | 5 | 6;
+}
+export interface HomesOrBases {
+ 0: Array<HoBSquare>;
+ 1: Array<HoBSquare>;
+ 2: Array<HoBSquare>;
+ 3: Array<HoBSquare>;
+}
+
+export interface GameBoard {
+ map: Array<Square>;
+ homes: HomesOrBases;
+ bases: HomesOrBases;
+};
export interface fiaTable {
id: number;
data: Data;