Add endpoints and game utilities
This commit is contained in:
parent
56f2195962
commit
6a174e4d36
7 changed files with 213 additions and 1 deletions
|
|
@ -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
46
src/utils/game.ts
Normal 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;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue