rkgk/static/online-users.js

81 lines
2 KiB
JavaScript
Raw Normal View History

2024-08-15 20:01:23 +02:00
import { Haku } from "./haku.js";
import { Painter } from "./painter.js";
export class User {
nickname = "";
brush = "";
reticle = null;
isBrushOk = false;
constructor(wallInfo, nickname) {
this.nickname = nickname;
this.haku = new Haku(wallInfo.hakuLimits);
this.painter = new Painter(wallInfo.paintArea);
}
destroy() {
this.haku.destroy();
}
2024-08-15 20:01:23 +02:00
setBrush(brush) {
2024-09-03 22:16:28 +02:00
console.groupCollapsed("setBrush", this.nickname);
2024-08-15 20:01:23 +02:00
let compileResult = this.haku.setBrush(brush);
console.log("compiling brush complete", compileResult);
console.groupEnd();
2024-08-15 20:01:23 +02:00
this.isBrushOk = compileResult.status == "ok";
2024-08-15 20:01:23 +02:00
return compileResult;
}
renderBrushToChunks(wall, x, y) {
2024-09-03 22:16:28 +02:00
console.groupCollapsed("renderBrushToChunks", this.nickname);
let result = this.painter.renderBrushToWall(this.haku, x, y, wall);
console.log("rendering brush to chunks complete");
console.groupEnd();
return result;
2024-08-15 20:01:23 +02:00
}
}
2024-08-10 23:13:20 +02:00
export class OnlineUsers extends EventTarget {
2024-08-15 20:01:23 +02:00
#wallInfo;
2024-08-10 23:13:20 +02:00
#users = new Map();
2024-08-15 20:01:23 +02:00
constructor(wallInfo) {
2024-08-10 23:13:20 +02:00
super();
2024-08-15 20:01:23 +02:00
this.#wallInfo = wallInfo;
2024-08-10 23:13:20 +02:00
}
2024-08-15 20:01:23 +02:00
addUser(sessionId, { nickname, brush }) {
if (!this.#users.has(sessionId)) {
console.info("user added", sessionId, nickname);
let user = new User(this.#wallInfo, nickname);
user.setBrush(brush);
this.#users.set(sessionId, user);
return user;
} else {
console.info("user already exists", sessionId, nickname);
return this.#users.get(sessionId);
}
2024-08-10 23:13:20 +02:00
}
getUser(sessionId) {
return this.#users.get(sessionId);
}
removeUser(sessionId) {
2024-08-15 20:01:23 +02:00
if (this.#users.has(sessionId)) {
let user = this.#users.get(sessionId);
user.destroy();
2024-08-15 20:01:23 +02:00
console.info("user removed", sessionId, user.nickname);
// TODO: Cleanup reticles
this.#users.delete(sessionId);
}
2024-08-10 23:13:20 +02:00
}
}