@colyseus/redis-driver
Version:
<div align="center"> <a href="https://github.com/colyseus/colyseus"> <img src="media/logo.svg?raw=true" width="60%" height="300" /> </a> <br> <br> <a href="https://npmjs.com/package/colyseus"> <img src="https://img.shields.io/npm/dm/coly
80 lines (79 loc) • 2.07 kB
JavaScript
// packages/drivers/redis-driver/src/RoomData.ts
import { logger } from "@colyseus/core";
var RoomData = class {
constructor(initialValues, client) {
this.clients = 0;
this.locked = false;
this.private = false;
this.maxClients = Infinity;
this.unlisted = false;
this.#removed = false;
this.#client = client;
this.createdAt = initialValues && initialValues.createdAt ? new Date(initialValues.createdAt) : /* @__PURE__ */ new Date();
for (const field in initialValues) {
if (initialValues.hasOwnProperty(field)) {
this[field] = initialValues[field];
}
}
}
#client;
#removed;
toJSON() {
return {
clients: this.clients,
createdAt: this.createdAt,
maxClients: this.maxClients,
metadata: this.metadata,
name: this.name,
publicAddress: this.publicAddress,
processId: this.processId,
roomId: this.roomId
};
}
async save() {
if (this.#removed) {
return;
}
if (this.roomId) {
const toJSON = this.toJSON;
this.toJSON = void 0;
const roomcache = JSON.stringify(this);
this.toJSON = toJSON;
await this.hset("roomcaches", this.roomId, roomcache);
} else {
logger.warn("RedisDriver: can't .save() without a `roomId`");
}
}
updateOne(operations) {
if (operations.$set) {
for (const field in operations.$set) {
if (operations.$set.hasOwnProperty(field)) {
this[field] = operations.$set[field];
}
}
}
if (operations.$inc) {
for (const field in operations.$inc) {
if (operations.$inc.hasOwnProperty(field)) {
this[field] += operations.$inc[field];
}
}
}
return this.save();
}
remove() {
if (this.roomId) {
this.#removed = true;
return this.hdel("roomcaches", this.roomId);
}
}
async hset(key, field, value) {
return await this.#client.hset(key, field, value);
}
async hdel(key, field) {
return await this.#client.hdel(key, field);
}
};
export {
RoomData
};