2023-10-18 15:01:15 -07:00
|
|
|
export function toReadableString(str: string): string {
|
|
|
|
return str.replace(/\_/g, ' ').split(' ').map(s => `${s.slice(0, 1)}${s.slice(1).toLowerCase()}`).join(' ');
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:31:59 -08:00
|
|
|
export function randomString(length: integer, seeded: boolean = false) {
|
2023-10-18 15:01:15 -07:00
|
|
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
let result = '';
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2024-01-02 18:31:59 -08:00
|
|
|
const randomIndex = seeded ? randSeedInt(characters.length) : Math.floor(Math.random() * characters.length);
|
2023-10-18 15:01:15 -07:00
|
|
|
result += characters[randomIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function shiftCharCodes(str: string, shiftCount: integer) {
|
|
|
|
if (!shiftCount)
|
|
|
|
shiftCount = 0;
|
|
|
|
|
|
|
|
let newStr = '';
|
|
|
|
|
|
|
|
for (let i = 0; i < str.length; i++) {
|
|
|
|
let charCode = str.charCodeAt(i);
|
|
|
|
let newCharCode = charCode + shiftCount;
|
|
|
|
newStr += String.fromCharCode(newCharCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newStr;
|
2023-03-28 11:54:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function clampInt(value: integer, min: integer, max: integer): integer {
|
|
|
|
return Math.min(Math.max(value, min), max);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function randGauss(value: number): number {
|
|
|
|
let rand = 0;
|
|
|
|
for(var i = value; i > 0; i--)
|
|
|
|
rand += Math.random();
|
|
|
|
return rand / value;
|
|
|
|
}
|
|
|
|
|
2023-10-26 18:42:53 -07:00
|
|
|
export function randSeedGauss(value: number): number {
|
|
|
|
let rand = 0;
|
|
|
|
for(var i = value; i > 0; i--)
|
|
|
|
rand += Phaser.Math.RND.realInRange(0, 1);
|
|
|
|
return rand / value;
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:54:52 -07:00
|
|
|
export function padInt(value: integer, length: integer, padWith?: string): string {
|
|
|
|
if (!padWith)
|
|
|
|
padWith = '0';
|
|
|
|
let valueStr = value.toString();
|
|
|
|
while (valueStr.length < length)
|
|
|
|
valueStr = `${padWith}${valueStr}`;
|
|
|
|
return valueStr;
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:31:59 -08:00
|
|
|
export function randInt(range: integer, min: integer = 0): integer {
|
2023-05-18 08:11:06 -07:00
|
|
|
if (range === 1)
|
|
|
|
return min;
|
2023-03-28 11:54:52 -07:00
|
|
|
return Math.floor(Math.random() * range) + min;
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:31:59 -08:00
|
|
|
export function randSeedInt(range: integer, min: integer = 0): integer {
|
2023-10-18 15:01:15 -07:00
|
|
|
if (range === 1)
|
|
|
|
return min;
|
|
|
|
return Phaser.Math.RND.integerInRange(min, (range - 1) + min);
|
|
|
|
}
|
|
|
|
|
2023-10-04 14:24:28 -07:00
|
|
|
export function randIntRange(min: integer, max: integer): integer {
|
|
|
|
return randInt(max - min, min);
|
|
|
|
}
|
|
|
|
|
2023-12-19 20:51:48 -08:00
|
|
|
export function getSunday(date: Date): Date {
|
|
|
|
const day = date.getDay(),
|
|
|
|
diff = date.getDate() - day;
|
|
|
|
const newDate = new Date(date.setDate(diff));
|
|
|
|
return new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate());
|
|
|
|
}
|
|
|
|
|
2023-06-04 18:47:43 -07:00
|
|
|
export function getFrameMs(frameCount: integer): integer {
|
|
|
|
return Math.floor((1 / 60) * 1000 * frameCount);
|
|
|
|
}
|
|
|
|
|
2023-12-29 18:04:40 -08:00
|
|
|
export function getCurrentTime(): number {
|
|
|
|
const date = new Date();
|
|
|
|
return (((date.getHours() * 60 + date.getMinutes()) / 1440) + 0.675) % 1;
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:54:52 -07:00
|
|
|
export function binToDec(input: string): integer {
|
2023-11-12 20:47:04 -08:00
|
|
|
let place: integer[] = [];
|
|
|
|
let binary: string[] = [];
|
2023-03-28 11:54:52 -07:00
|
|
|
|
|
|
|
let decimalNum = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
binary.push(input[i]);
|
|
|
|
place.push(Math.pow(2, i));
|
|
|
|
decimalNum += place[i] * parseInt(binary[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return decimalNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decToBin(input: integer): string {
|
|
|
|
let bin = '';
|
|
|
|
let intNum = input;
|
|
|
|
while (intNum > 0) {
|
|
|
|
bin = intNum % 2 ? `1${bin}` : `0${bin}`;
|
|
|
|
intNum = Math.floor(intNum * 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin;
|
|
|
|
}
|
|
|
|
|
2023-04-11 10:00:04 -07:00
|
|
|
export function getEnumKeys(enumType): string[] {
|
|
|
|
return Object.values(enumType).filter(v => isNaN(parseInt(v.toString()))).map(v => v.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getEnumValues(enumType): integer[] {
|
2023-03-28 11:54:52 -07:00
|
|
|
return Object.values(enumType).filter(v => !isNaN(parseInt(v.toString()))).map(v => parseInt(v.toString()));
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:09:33 -07:00
|
|
|
export function executeIf<T>(condition: boolean, promiseFunc: () => Promise<T>): Promise<T> {
|
|
|
|
return condition ? promiseFunc() : new Promise<T>(resolve => resolve(null));
|
|
|
|
}
|
|
|
|
|
2023-12-30 15:41:25 -08:00
|
|
|
export const sessionIdKey = 'pokerogue_sessionId';
|
|
|
|
export const isLocal = window.location.hostname === 'localhost';
|
|
|
|
export const serverUrl = isLocal ? 'http://localhost:8001' : '';
|
|
|
|
export const apiUrl = isLocal ? serverUrl : 'api';
|
|
|
|
|
|
|
|
export function setCookie(cName: string, cValue: string): void {
|
|
|
|
document.cookie = `${cName}=${cValue};SameSite=Strict;path=/`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCookie(cName: string): string {
|
|
|
|
const name = `${cName}=`;
|
|
|
|
const ca = document.cookie.split(';');
|
|
|
|
for (let i = 0; i < ca.length; i++) {
|
|
|
|
let c = ca[i];
|
|
|
|
while (c.charAt(0) === ' ')
|
|
|
|
c = c.substring(1);
|
|
|
|
if (c.indexOf(name) === 0)
|
|
|
|
return c.substring(name.length, c.length);
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function apiFetch(path: string): Promise<Response> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const sId = getCookie(sessionIdKey);
|
|
|
|
const headers = sId ? { 'Authorization': sId } : {};
|
|
|
|
fetch(`${apiUrl}/${path}`, { headers: headers })
|
|
|
|
.then(response => resolve(response))
|
|
|
|
.catch(err => reject(err));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:31:59 -08:00
|
|
|
export function apiPost(path: string, data?: any, contentType: string = 'application/json'): Promise<Response> {
|
2023-12-30 15:41:25 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const headers = {
|
|
|
|
'Accept': contentType,
|
|
|
|
'Content-Type': contentType,
|
|
|
|
};
|
|
|
|
const sId = getCookie(sessionIdKey);
|
|
|
|
if (sId)
|
|
|
|
headers['Authorization'] = sId;
|
|
|
|
fetch(`${apiUrl}/${path}`, { method: 'POST', headers: headers, body: data })
|
|
|
|
.then(response => resolve(response))
|
|
|
|
.catch(err => reject(err));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:09:15 -07:00
|
|
|
export class BooleanHolder {
|
|
|
|
public value: boolean;
|
|
|
|
|
|
|
|
constructor(value: boolean) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 20:02:35 -07:00
|
|
|
export class NumberHolder {
|
|
|
|
public value: number;
|
|
|
|
|
|
|
|
constructor(value: number) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 08:30:47 -07:00
|
|
|
|
2023-11-11 21:31:40 -08:00
|
|
|
export class IntegerHolder extends NumberHolder {
|
2023-03-28 11:54:52 -07:00
|
|
|
constructor(value: integer) {
|
2023-11-11 21:31:40 -08:00
|
|
|
super(value);
|
2023-03-28 11:54:52 -07:00
|
|
|
}
|
2023-04-12 08:30:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class FixedInt extends IntegerHolder {
|
|
|
|
constructor(value: integer) {
|
|
|
|
super(value);
|
|
|
|
}
|
2023-10-25 20:15:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function fixedInt(value: integer): integer {
|
|
|
|
return new FixedInt(value) as unknown as integer;
|
2023-11-23 20:52:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function rgbToHsv(r: integer, g: integer, b: integer) {
|
|
|
|
let v = Math.max(r, g, b);
|
|
|
|
let c = v - Math.min(r, g, b);
|
|
|
|
let h = c && ((v === r) ? (g - b) / c : ((v === g) ? 2 + (b - r) / c : 4 + (r - g) / c));
|
|
|
|
return [ 60 * (h < 0 ? h + 6 : h), v && c / v, v];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare color difference in RGB
|
|
|
|
* @param {Array} rgb1 First RGB color in array
|
|
|
|
* @param {Array} rgb2 Second RGB color in array
|
|
|
|
*/
|
|
|
|
export function deltaRgb(rgb1: integer[], rgb2: integer[]): integer {
|
|
|
|
const [ r1, g1, b1 ] = rgb1;
|
|
|
|
const [ r2, g2, b2 ] = rgb2;
|
|
|
|
const drp2 = Math.pow(r1 - r2, 2);
|
|
|
|
const dgp2 = Math.pow(g1 - g2, 2);
|
|
|
|
const dbp2 = Math.pow(b1 - b2, 2);
|
|
|
|
const t = (r1 + r2) / 2;
|
|
|
|
|
|
|
|
return Math.ceil(Math.sqrt(2 * drp2 + 4 * dgp2 + 3 * dbp2 + t * (drp2 - dbp2) / 256));
|
2023-03-28 11:54:52 -07:00
|
|
|
}
|