mirror of
https://github.com/Such-Software/wownerogue.git
synced 2026-11-26 17:13:52 +01:00
Wownerogue
A browser-based roguelike synchronized with crypto block timing. A run is a race against the next block: reach the exit before the block lands, or the house wins. The same codebase runs the solo dungeon, the social Tavern, and operator-selected PvP and race rulesets, with separate free and paid leaderboards. Self-hostable under the MIT License. See README.md to run it, docs/ARCHITECTURE.md for the design, and docs/DEPLOY.md to operate an instance.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
const ChatProvider = require('./ChatProvider');
|
||||
|
||||
/**
|
||||
* SocketChatProvider: the default ChatProvider, backed by Socket.IO for delivery and (for
|
||||
* global chat) the Postgres-backed ChatHistoryManager for persistence.
|
||||
*
|
||||
* Behaviour matches the pre-seam chat exactly:
|
||||
* • global scope → persist via the history manager + broadcast to everyone
|
||||
* (broadcastManager.broadcastChatMessage, i.e. io.emit 'chat_broadcast').
|
||||
* • room scope → deliver only to that Socket.IO room, ephemeral (no history).
|
||||
*
|
||||
* A history manager may be injected (so ChatHandler shares its existing instance) or omitted
|
||||
* (tavern chat, which is ephemeral). When omitted, this provider persists nothing.
|
||||
*/
|
||||
class SocketChatProvider extends ChatProvider {
|
||||
constructor({ io, broadcastManager = null, debugManager = null, historyManager = null } = {}) {
|
||||
super();
|
||||
this.io = io;
|
||||
this.broadcastManager = broadcastManager;
|
||||
this.debugManager = debugManager;
|
||||
this.history = historyManager; // null → ephemeral (no persistence)
|
||||
this._ownsHistory = false; // true only if this provider created the manager
|
||||
}
|
||||
|
||||
async initialize() {
|
||||
if (this._ownsHistory && this.history) await this.history.initialize();
|
||||
}
|
||||
|
||||
async publish(msg = {}) {
|
||||
const { scope = 'global', username, text, ts = Date.now(), socketId = null, userId = null } = msg;
|
||||
|
||||
// Defense in depth (S1): never broadcast the raw full socket.id to other clients; it is
|
||||
// a hijackable handle. Derive a short, non-sensitive public id for display/attribution.
|
||||
const publicId = userId != null ? String(userId) : (socketId ? String(socketId).substring(0, 6) : null);
|
||||
|
||||
if (scope === 'global') {
|
||||
if (this.history) {
|
||||
// Fire-and-forget persistence, exactly as before (must not block delivery).
|
||||
this.history.saveMessage({ socketId, username, message: text, type: 'chat', userId })
|
||||
.catch(err => console.error('Failed to save chat message:', err.message));
|
||||
}
|
||||
if (this.broadcastManager) {
|
||||
this.broadcastManager.broadcastChatMessage(username, text, ts, publicId);
|
||||
} else {
|
||||
this.io.emit('chat_broadcast', { username, message: text, timestamp: ts, publicId });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Room-scoped (e.g. tavern): delivered only to occupants of that room. The `scope`
|
||||
// field lets a room client filter these from any global broadcasts it also receives.
|
||||
this.io.to(scope).emit('chat_broadcast', { username, message: text, timestamp: ts, publicId, scope });
|
||||
}
|
||||
|
||||
async getHistory({ scope = 'global', limit = 50 } = {}) {
|
||||
if (scope === 'global' && this.history) return this.history.getRecentMessages(limit);
|
||||
return [];
|
||||
}
|
||||
|
||||
async shutdown() {
|
||||
if (this._ownsHistory && this.history) this.history.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SocketChatProvider;
|
||||
Reference in New Issue
Block a user