Files
ritzenbergen-frontend/src/ritzenbergenlib.ts
2026-02-02 12:18:27 +01:00

176 lines
3.6 KiB
TypeScript

import $ from "jquery";
import { GraphQLClient } from 'graphql-request';
const graphqlClient = new GraphQLClient('http://localhost:3000/graphql', {
headers: {
// optional: Authorization: 'Bearer TOKEN'
},
});
const apiBaseUrl = import.meta.env.VITE_BACKEND_URL;
class User {
public username: string;
public kuerzel: string;
public points: number;
public id: number;
constructor(username: string, kuerzel: string, points: number, id: number) {
this.username = username;
this.kuerzel = kuerzel;
this.points = points;
this.id = id;
}
hatgetippt(): boolean {
var hatgetippt = false;
$.ajax(
RitzenbergenLib.api("/bulitipp/hatgetippt.php?userid=" + this.id),
{
async: false,
success(response): any {
hatgetippt = response == 1;
console.log(response);
},
}
);
return hatgetippt;
}
}
class SpieltagSumme {
public user: User;
public tsPoints: number;
constructor(user: User, tsPoints: number) {
this.user = user;
this.tsPoints = tsPoints;
}
}
class RitzenbergenLib {
static get_img(mypath: string) {
if (mypath.startsWith("/"))
return "https://bilder.ritzenbergen.de" + mypath;
else return "https://bilder.ritzenbergen.de/" + mypath;
}
static api(path: string) {
if (path.startsWith("/"))
return apiBaseUrl + path;
else return apiBaseUrl+"/" + path;
}
static async checkInternetConnection(): Promise<boolean> {
try{
return await fetch(this.api("/addhit.php")).then((response)=>response.ok);
} catch(e){
return false;
}
}
}
class Paarung {
public heim: string;
public gast: string;
public id: number;
constructor(heim: string, gast: string, id: number) {
this.heim = heim;
this.gast = gast;
this.id = id;
}
}
class Ergebnis {
paarung: Paarung;
heim: number;
gast: number;
constructor(paarung: Paarung, heim: number, gast: number) {
this.paarung = paarung;
this.heim = heim;
this.gast = gast;
}
}
class Tipp {
heim: number | null;
gast: number | null;
paarung: Paarung | null;
constructor(
paarung: Paarung | null,
heim: number | null,
gast: number | null
) {
this.heim = heim;
this.gast = gast;
this.paarung = paarung;
}
}
class Spiel {
public paarung: Paarung;
public result: [number, number] | null;
public tipp: [number, number] | null;
constructor(
paarung: Paarung,
result: [number, number],
tipp: [number, number] | null
) {
this.paarung = paarung;
this.result = result;
this.tipp = tipp;
}
public calcPoints() {
if (this.tipp == null) return 0;
if (this.result[0] == this.tipp[0] && this.result[1] == this.tipp[1])
return 3;
let differenztipp = this.tipp[0] - this.tipp[1];
let differenz = this.result[0] - this.result[1];
if (differenztipp == differenz) return 2;
if (
(this.tipp[0] > this.tipp[1] && this.result[0] > this.result[1]) ||
(this.tipp[0] < this.tipp[1] && this.result[0] < this.result[1])
)
return 1;
return 0;
}
public static async fetchSpiele(spieltag: number, userid: number): Spiel[] {
let params = new URLSearchParams({
spieltag: spieltag + 1,
user: userid,
});
return fetch(
RitzenbergenLib.api("/bulitipp/spieltag-user.php?") +
params.toString()
)
.then((response) => {
return response.json();
})
.then((data) => {
return data.data;
})
.then((data) => {
return data.map((el) => {
return new Spiel(
new Paarung(
el.paarung.heim,
el.paarung.gast,
el.paarung.id
),
el.result,
el.tipp
);
});
});
}
}
export default {
RitzenbergenLib,
User,
SpieltagSumme,
Paarung,
Ergebnis,
Tipp,
Spiel,
graphqlClient
};