TravBot-v3/src/defs/user.ts

155 lines
4.6 KiB
TypeScript

import {db} from "../modules/database";
import {Collection} from "discord.js";
const upsert = (column: string, bindParameter: string) =>
`INSERT INTO Users (ID, ${column}) VALUES (:id, :${bindParameter}) ON CONFLICT (ID) DO UPDATE SET ${column} = :${bindParameter} WHERE ID = :id`;
export class User {
public readonly id: string;
private _money: number;
private _lastReceived: number;
private _lastMonday: number;
private _timezoneOffset: number | null; // This is for the standard timezone only, not the daylight savings timezone
private _daylightSavingsRegion: "na" | "eu" | "sh" | "none";
private _ecoBetInsurance: number;
private _todoList: Collection<number, string>;
constructor(id: string) {
this.id = id;
const data = db.prepare("SELECT * FROM Users WHERE ID = ?").get(id);
const todoList = db.prepare("SELECT Timestamp, Entry FROM TodoLists WHERE UserID = ?").all(id) ?? [];
if (data) {
const {Money, LastReceived, LastMonday, TimezoneOffset, DaylightSavingsRegion, EcoBetInsurance} = data;
this._money = Money;
this._lastReceived = LastReceived;
this._lastMonday = LastMonday;
this._timezoneOffset = TimezoneOffset;
this._daylightSavingsRegion = "none";
this._ecoBetInsurance = EcoBetInsurance;
switch (DaylightSavingsRegion) {
case 1:
this._daylightSavingsRegion = "na";
break;
case 2:
this._daylightSavingsRegion = "eu";
break;
case 3:
this._daylightSavingsRegion = "sh";
break;
}
} else {
this._money = 0;
this._lastReceived = -1;
this._lastMonday = -1;
this._timezoneOffset = null;
this._daylightSavingsRegion = "none";
this._ecoBetInsurance = 0;
}
this._todoList = new Collection();
for (const {Timestamp, Entry} of todoList) {
this._todoList.set(Timestamp, Entry);
}
}
static all(): User[] {
const IDs = db
.prepare("SELECT ID FROM Users")
.all()
.map((user) => user.ID);
const users = [];
for (const id of IDs) {
users.push(new User(id));
}
return users;
}
get money() {
return this._money;
}
set money(money) {
this._money = money;
db.prepare(upsert("Money", "money")).run({
id: this.id,
money
});
}
get lastReceived() {
return this._lastReceived;
}
set lastReceived(lastReceived) {
this._lastReceived = lastReceived;
db.prepare(upsert("LastReceived", "lastReceived")).run({
id: this.id,
lastReceived
});
}
get lastMonday() {
return this._lastMonday;
}
set lastMonday(lastMonday) {
this._lastMonday = lastMonday;
db.prepare(upsert("LastMonday", "lastMonday")).run({
id: this.id,
lastMonday
});
}
get timezoneOffset() {
return this._timezoneOffset;
}
set timezoneOffset(timezoneOffset) {
this._timezoneOffset = timezoneOffset;
db.prepare(upsert("TimezoneOffset", "timezoneOffset")).run({
id: this.id,
timezoneOffset
});
}
get daylightSavingsRegion() {
return this._daylightSavingsRegion;
}
set daylightSavingsRegion(daylightSavingsRegion) {
this._daylightSavingsRegion = daylightSavingsRegion;
let dstInfo = 0;
switch (daylightSavingsRegion) {
case "na":
dstInfo = 1;
break;
case "eu":
dstInfo = 2;
break;
case "sh":
dstInfo = 3;
break;
}
db.prepare(upsert("DaylightSavingsRegion", "dstInfo")).run({
id: this.id,
dstInfo
});
}
get ecoBetInsurance() {
return this._ecoBetInsurance;
}
set ecoBetInsurance(ecoBetInsurance) {
this._ecoBetInsurance = ecoBetInsurance;
db.prepare(upsert("EcoBetInsurance", "ecoBetInsurance")).run({
id: this.id,
ecoBetInsurance
});
}
get todoList() {
return this._todoList;
}
// NOTE: Need to figure out an actual ID system
setTodoEntry(timestamp: number, entry: string) {
db.prepare("INSERT INTO TodoLists VALUES (?, ?, ?)").run(this.id, timestamp, entry);
}
}