diff options
Diffstat (limited to 'helpers')
| -rw-r--r-- | helpers/helpers.ts | 33 | ||||
| -rw-r--r-- | helpers/helpersBack.ts | 92 |
2 files changed, 109 insertions, 16 deletions
diff --git a/helpers/helpers.ts b/helpers/helpers.ts index 03bc7d7..80fb5ab 100644 --- a/helpers/helpers.ts +++ b/helpers/helpers.ts @@ -23,8 +23,11 @@ export default class Helpers { } static dbResToFiaTable(dbRes: dbRes): fiaTable { - if (typeof dbRes.data === "string") + if (typeof dbRes.data === "string") { + console.log("dbRes.data: ", dbRes.data); + console.log("dbRes.data as string: ", JSON.stringify(dbRes.data)); dbRes.data = JSON.parse(dbRes.data); + } else throw new Error("dbResToFiaTable : wrong $1 type; "); @@ -32,11 +35,11 @@ export default class Helpers { return dbRes; } - static checkApiRes(res: any, nRes = {} as Response): any { - if (res.api === false) { + static checkApiRes(res: any, nRes = {} as Response): boolean { + if (res.success === false) { if (Object.keys(nRes).length === 0) { // @ts-ignore - here error is because backend tsc is checking this file, where lib: DOM is not present - alert("API Error!"); + // alert("API Error!"); throw new Error("API Error!"); } else { nRes.status(500); @@ -46,19 +49,24 @@ export default class Helpers { } if (Object.keys(nRes).length !== 0) nRes.status(200); - return res; + return true; } static async mFetch(url: string, body: object): Promise<any> { //@ts-ignore - this doesn't exist for backend, but its never used there - return fetch(url, { + let res: any = await (await fetch(url, { method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" }, body: JSON.stringify(body) - }).then(Helpers.checkApiRes); + })).json(); + if (this.checkApiRes(res) == true) + return res; + else + throw new Error("API Error!");; + } static getRandomInt(min: number, max: number) { @@ -66,4 +74,15 @@ export default class Helpers { max = Math.floor(max); return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive } + + static Color: Colors = { + blue: 0, + red: 1, + green: 2, + yellow: 3, + 0: 0, + 1: 1, + 2: 2, + 3: 3 + }; }
\ No newline at end of file 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; |
