Add Directus db wrapper
This commit is contained in:
parent
74073a0a9b
commit
dafaf8d4ee
3 changed files with 1134 additions and 6 deletions
10
package.json
10
package.json
|
|
@ -8,12 +8,16 @@
|
|||
"start": "node dist/index.js",
|
||||
"dev": "concurrently \"npx tsc --watch\" \"nodemon -q dist/index.js\"",
|
||||
"buildDocker": "docker build -t nuark/huacu_server:latest .",
|
||||
"pushDocker": "docker push nuark/huacu_server:latest"
|
||||
"pushDocker": "docker push nuark/huacu_server:latest",
|
||||
"test": "vitest",
|
||||
"coverage": "vitest run --coverage"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@directus/sdk": "^10.3.1",
|
||||
"@noble/hashes": "^1.2.0",
|
||||
"@socket.io/admin-ui": "^0.5.1",
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
|
|
@ -24,8 +28,10 @@
|
|||
"@types/express": "^4.17.17",
|
||||
"@types/node": "^18.14.4",
|
||||
"@types/uuid": "^9.0.1",
|
||||
"@vitest/coverage-c8": "^0.29.2",
|
||||
"concurrently": "^7.6.0",
|
||||
"nodemon": "^2.0.21",
|
||||
"typescript": "^4.9.5"
|
||||
"typescript": "^4.9.5",
|
||||
"vitest": "^0.29.2"
|
||||
}
|
||||
}
|
||||
1014
pnpm-lock.yaml
generated
1014
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
116
src/directus_db.ts
Normal file
116
src/directus_db.ts
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
import { Directus } from "@directus/sdk";
|
||||
import { blake3 } from "@noble/hashes/blake3";
|
||||
|
||||
export type HuacuUser = {
|
||||
id: string;
|
||||
login: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
export type HuacuStat = {
|
||||
id: string;
|
||||
dateCreated: string;
|
||||
dateUpdated: string;
|
||||
belongs_to: string;
|
||||
games_won: number;
|
||||
games_lost: number;
|
||||
};
|
||||
|
||||
export type HuacuCollection = {
|
||||
huacu_users: HuacuUser;
|
||||
huacu_stats: HuacuStat;
|
||||
};
|
||||
|
||||
function hexify(data: Uint8Array): string {
|
||||
return Buffer.from(data).toString("hex");
|
||||
}
|
||||
|
||||
export class DirectusDB {
|
||||
private directus: Directus<HuacuCollection>;
|
||||
|
||||
constructor() {
|
||||
const staticAccessToken = "dDAa9RSZKLuKESwMTfTWUQZcEQWqsKJn";
|
||||
this.directus = new Directus<HuacuCollection>("https://directus.nuark.xyz", {
|
||||
auth: {
|
||||
staticToken: staticAccessToken,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async registerUser(login: string, password: string) {
|
||||
const hash = hexify(blake3(password));
|
||||
const user = {
|
||||
login: login,
|
||||
password: hash,
|
||||
};
|
||||
try {
|
||||
const newUser = await this.directus.items("huacu_users").createOne(user);
|
||||
// Now we need to create a stats entry for this user
|
||||
const stats = {
|
||||
belongs_to: newUser!.id,
|
||||
};
|
||||
await this.directus.items("huacu_stats").createOne(stats);
|
||||
return newUser;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async authenticateUser(login: string, password: string): Promise<HuacuUser | null> {
|
||||
const hash = hexify(blake3(password));
|
||||
const response = await this.directus.items("huacu_users").readByQuery({
|
||||
filter: {
|
||||
login: {
|
||||
_eq: login,
|
||||
},
|
||||
password: {
|
||||
_eq: hash,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (response.data?.length === 1) {
|
||||
return response.data[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async removeUser(login: string, password: string) {
|
||||
const user = await this.authenticateUser(login, password);
|
||||
if (user === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await this.directus.items("huacu_users").deleteOne(user.id);
|
||||
return true;
|
||||
}
|
||||
|
||||
async getStats(userId: string) {
|
||||
try {
|
||||
const response = await this.directus.items("huacu_stats").readByQuery({
|
||||
filter: {
|
||||
belongs_to: {
|
||||
_eq: userId,
|
||||
},
|
||||
},
|
||||
});
|
||||
return response!.data![0];
|
||||
} catch (e) {
|
||||
// Actually, this should never happen in real life
|
||||
// because we create a stats entry for every user
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async updateStats(userId: string, incrementWin: boolean, incrementLost: boolean) {
|
||||
try {
|
||||
const stats = (await this.getStats(userId))!;
|
||||
await this.directus.items("huacu_stats").updateOne(stats.id, {
|
||||
games_won: stats.games_won + (incrementWin ? 1 : 0),
|
||||
games_lost: stats.games_lost + (incrementLost ? 1 : 0),
|
||||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue