aboutsummaryrefslogtreecommitdiffstats
path: root/helpers
diff options
context:
space:
mode:
authorMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-18 00:03:32 +0200
committerMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-18 00:03:32 +0200
commitc3c3b770b037cb80a37b3508ac890699d7391083 (patch)
tree4d82e69aaba488f1a6aecf7ada4ce1747e9ca307 /helpers
parentdd66b8ad239d5ceae219746a702963645259b784 (diff)
downloadfia-c3c3b770b037cb80a37b3508ac890699d7391083.tar.gz
fia-c3c3b770b037cb80a37b3508ac890699d7391083.tar.zst
fia-c3c3b770b037cb80a37b3508ac890699d7391083.zip
Everything till generating empty game board
Diffstat (limited to 'helpers')
-rw-r--r--helpers/helpers.ts143
-rw-r--r--helpers/helpersBack.ts86
2 files changed, 146 insertions, 83 deletions
diff --git a/helpers/helpers.ts b/helpers/helpers.ts
index de6744f..03bc7d7 100644
--- a/helpers/helpers.ts
+++ b/helpers/helpers.ts
@@ -1,92 +1,69 @@
-import fetch from "node-fetch";
-import { Response } from "express";
+import { Colors, Color, tColors, Data, fiaTable, dbRes } from "./helpersBack";
+import { Request, Response } from "express";
-export async function makeQuery(query: string, params: Array<any>): Promise<object> {
- let t = await (
- await fetch("https://jopek.eu/maks/szkola/apkKli/fiaFiles/fia.php", {
- method: "POST",
- headers: {
- Accept: "application/json",
- "Content-Type": "application/json"
- },
- body: JSON.stringify({
- apiKey: process.env.API_KEY,
- query: query,
- params: params
+export default class Helpers {
+ static async makeQuery(query: string, params: Array<any>): Promise<object> {
+ let t = await (
+ //@ts-ignore - this doesn't exist for backend, but its never used there
+ await fetch("https://jopek.eu/maks/szkola/apkKli/fiaFiles/fia.php", {
+ method: "POST",
+ headers: {
+ Accept: "application/json",
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify({
+ apiKey: process.env.API_KEY,
+ query: query,
+ params: params
+ })
})
- })
- ).text();
- // console.log(t);
- return JSON.parse(t);
-}
+ ).text();
+ // console.log(t);
+ return JSON.parse(t);
+ }
-export function dbResToFiaTable(dbRes: dbRes): fiaTable {
- if (typeof dbRes.data === "string")
- dbRes.data = JSON.parse(dbRes.data);
- else
- throw new Error("dbResToFiaTable : wrong $1 type; ");
+ static dbResToFiaTable(dbRes: dbRes): fiaTable {
+ if (typeof dbRes.data === "string")
+ dbRes.data = JSON.parse(dbRes.data);
+ else
+ throw new Error("dbResToFiaTable : wrong $1 type; ");
- // @ts-ignore
- return dbRes;
-}
+ // @ts-ignore
+ return dbRes;
+ }
-export function checkApiRes(res: any, nRes = {} as Response): void {
- if (res.api === false) {
- if (Object.keys(nRes).length === 0) {
- alert("API Error!");
- throw new Error("API Error!");
- } else {
- nRes.status(500);
- res.send("");
+ static checkApiRes(res: any, nRes = {} as Response): any {
+ if (res.api === 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!");
+ throw new Error("API Error!");
+ } else {
+ nRes.status(500);
+ nRes.send("");
+ return false;
+ }
}
+ if (Object.keys(nRes).length !== 0)
+ nRes.status(200);
+ return res;
}
- if (Object.keys(nRes).length !== 0)
- nRes.status(200);
- return res;
-}
-
-export async function mFetch(url: string, body: object) /* : Promise<Response> */ {
- return fetch(url, {
- method: "POST",
- headers: {
- Accept: "application/json",
- "Content-Type": "application/json"
- },
- body: JSON.stringify(body)
- }).then(checkApiRes);
-}
-export interface Colors {
- blue: number;
- red: number;
- green: number;
- yellow: number;
- 0: number;
- 1: number;
- 2: number;
- 3: number;
-}
-
-export const Color: Colors = {
- blue: 0,
- red: 1,
- green: 2,
- yellow: 3,
- 0: 0,
- 1: 1,
- 2: 2,
- 3: 3
-};
+ 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, {
+ method: "POST",
+ headers: {
+ Accept: "application/json",
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify(body)
+ }).then(Helpers.checkApiRes);
+ }
-export type tColors = typeof Color[keyof typeof Color];
-export type Data = Array<{ id: any; color: tColors; name: string }>;
-export interface fiaTable {
- id: number;
- data: Data;
- started: boolean;
-}
-export interface dbRes {
- id: number;
- data: string | Data;
- started: boolean;
-}
+ static getRandomInt(min: number, max: number) {
+ min = Math.ceil(min);
+ max = Math.floor(max);
+ return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
+ }
+} \ No newline at end of file
diff --git a/helpers/helpersBack.ts b/helpers/helpersBack.ts
new file mode 100644
index 0000000..d594c03
--- /dev/null
+++ b/helpers/helpersBack.ts
@@ -0,0 +1,86 @@
+import fetch from "node-fetch";
+import { Response } from "express";
+import Helpers from "./helpers";
+
+export { Helpers };
+export async function makeQuery(query: string, params: Array<any>): Promise<any> {
+ let t = await (
+ await fetch("https://jopek.eu/maks/szkola/apkKli/fiaFiles/fia.php", {
+ method: "POST",
+ headers: {
+ Accept: "application/json",
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify({
+ apiKey: process.env.API_KEY,
+ query: query,
+ params: params
+ })
+ })
+ ).text();
+ // console.log(t);
+ return JSON.parse(t);
+}
+
+export async function mFetch(url: string, body: object, res: Response): Promise<Response> {
+ return fetch(url, {
+ method: "POST",
+ headers: {
+ Accept: "application/json",
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify(body)
+ }).then(fRes => Helpers.checkApiRes(fRes, res)) as Promise<Response>;
+}
+
+export interface Colors {
+ blue: number;
+ red: number;
+ green: number;
+ yellow: number;
+ 0: number;
+ 1: number;
+ 2: number;
+ 3: number;
+}
+
+export const Color: Colors = {
+ blue: 0,
+ red: 1,
+ green: 2,
+ yellow: 3,
+ 0: 0,
+ 1: 1,
+ 2: 2,
+ 3: 3
+};
+
+export type tColors = typeof Color[keyof typeof Color];
+export type Data = Array<DataObject>;
+export interface DataObject {
+ id: any;
+ index: number;
+ color: tColors;
+ name: string;
+}
+export interface Chequer extends Coordinates {
+ color: tColors;
+};
+
+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;
+}
+export type GameBoard = Array<Chequer>;
+export interface fiaTable {
+ id: number;
+ data: Data;
+ gameBoard: GameBoard;
+ playersAreReady: 1 | 2 | 3 | 4;
+ playersCount: 1 | 2 | 3 | 4;
+ currentPlayer: 1 | 2 | 3 | 4;
+ started: boolean;
+}
+export interface dbRes extends Omit<fiaTable, 'data'> {
+ data: string | Data;
+}