Binato/server/ChatHistory.ts

34 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2022-11-16 15:25:46 +00:00
import { readFileSync } from "fs";
2023-09-10 12:59:22 +01:00
export default abstract class ChatHistory {
2022-11-16 15:25:46 +00:00
private static _history:Array<string> = new Array<string>();
private static _lastGeneratedPage:string;
private static _hasChanged:boolean = true;
private static readonly HISTORY_LENGTH = 10;
private static readonly PAGE_TEMPLATE = readFileSync("./web/chatPageTemplate.html").toString();
public static AddMessage(message:string) : void {
2023-09-10 12:59:22 +01:00
if (this._history.length === this.HISTORY_LENGTH) {
2022-11-16 15:25:46 +00:00
this._history.splice(0, 1);
}
this._history.push(message);
this._hasChanged = true;
}
public static GenerateForWeb() : string {
2023-09-10 12:59:22 +01:00
if (this._hasChanged) {
let lines = "", flip = false;
2022-11-16 15:25:46 +00:00
2023-09-10 12:59:22 +01:00
for (let i = 0; i < this.HISTORY_LENGTH; i++) {
lines += `<div class="line line${flip ? 1 : 0}">${this._history[i] == null ? "<hidden>blank</hidden>" : this._history[i].replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\n", "<br>")}</div>`
flip = !flip;
}
2022-11-16 15:25:46 +00:00
this._lastGeneratedPage = this.PAGE_TEMPLATE.toString().replace("|content|", lines);
this._hasChanged = false;
}
return this._lastGeneratedPage;
}
}