0
0
Fork 0

Add endpoints and game utilities

This commit is contained in:
Oliver Akins 2022-12-25 16:52:15 -06:00
parent 56f2195962
commit 6a174e4d36
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99
7 changed files with 213 additions and 1 deletions

View file

@ -2,7 +2,7 @@ import { databaseOptions } from "$/types/config";
import fs from "fs";
export class JSONDatabase {
private data = {};
private data: any = {};
private conf: databaseOptions;
constructor(conf: databaseOptions) {
@ -22,4 +22,17 @@ export class JSONDatabase {
public shutdown() {
fs.writeFileSync(this.conf.uri, JSON.stringify(this.data));
};
public async createChannel(name: string) {
this.data[name] = {
current: null,
solution: null,
key: null,
incorrect: 0,
};
};
public async getChannel(name: string) {
return this.data[name];
};
};

46
src/utils/game.ts Normal file
View file

@ -0,0 +1,46 @@
export function addLetter(key: any, current: string, letter: string) {
let indexes = key[letter];
for (const i of indexes) {
current = current.slice(0, i) + letter.toUpperCase() + current.slice(i+1);
};
return current;
};
export function convertToKey(phrase: string) {
let key: {[index: string]: number[]} = {};
for (var i = 0; i < phrase.length; i++) {
let letter = phrase[i].toUpperCase();
if (!letter.match(/[a-zA-Z]/)) { continue };
if (key[letter] == null) {
key[letter] = [];
};
key[letter].push(i);
};
return key;
};
export function anonymizePhrase(phrase: string) {
let anon = ``;
for (const letter of phrase) {
if (letter.match(/[A-Za-z]/)) {
anon += `_`;
} else {
anon += letter;
};
};
return anon;
};
export function spacePhrase(phrase: string) {
let spaced = ``;
for (const letter of phrase) {
if (letter.match(/[A-Za-z]/)) {
spaced += `${letter} `;
} else if (letter == ` `) {
spaced += ``;
} else {
spaced += letter;
};
};
return spaced;
};