chore: cleanup unused elements

This commit is contained in:
gh0sTedBuddy
2024-04-07 20:06:55 +01:00
parent a3c5a832b7
commit 2c72652e15
4 changed files with 50 additions and 128 deletions

View File

@@ -4,8 +4,12 @@
<title>WetSock Client Test</title> <title>WetSock Client Test</title>
<style> <style>
/* Add some basic styling */ /* Add some basic styling */
* {
box-sizing: border-box;
}
body { body {
font-family: Arial, sans-serif; font-family: Arial, sans-serif;
font-size: 16px;
margin: 20px; margin: 20px;
} }
input, button { input, button {
@@ -16,6 +20,8 @@
padding: 10px; padding: 10px;
height: 300px; height: 300px;
overflow-y: scroll; overflow-y: scroll;
letter-spacing: 0.125em;
font-size: 1rem;
} }
</style> </style>
</head> </head>
@@ -49,11 +55,19 @@
<button id="sendButton">Send</button> <button id="sendButton">Send</button>
<h2>Output:</h2> <h2>Output:</h2>
<div id="output"></div> <pre id="output"></pre>
<script type="module"> <script type="module">
import WetSockClient from './client.js'; import WetSockClient from './client.js';
const client = new WetSockClient('wss://wetsock.ghosted.id:443/chat');
const url = new URL(window.location.href);
const wetsockAddr = [
url.protocol.startsWith('https') ? 'wss' : 'ws',
'://',
url.host,
'/chat'
].join('');
const client = new WetSockClient(wetsockAddr);
const pluginIdentifierSelect = document.getElementById('pluginIdentifier'); const pluginIdentifierSelect = document.getElementById('pluginIdentifier');
const actionNameSelect = document.getElementById('actionName'); const actionNameSelect = document.getElementById('actionName');
const entityIdInput = document.getElementById('entityId'); const entityIdInput = document.getElementById('entityId');
@@ -61,19 +75,23 @@
const sendButton = document.getElementById('sendButton'); const sendButton = document.getElementById('sendButton');
const outputDiv = document.getElementById('output'); const outputDiv = document.getElementById('output');
client.on('message', (message) => {
log(message);
});
client.on('connected', () => { client.on('connected', () => {
log('Connected to WetSock server'); log('Connected to WetSock server');
}); });
client.on('pluginList', (data) => { client.on('plugins', (plugins) => {
updatePluginOptions(data.plugins); updatePluginOptions(plugins);
}); });
client.on('actionPerformed', (data) => { client.on('done', (data) => {
log(`Action performed: ${JSON.stringify(data)}`); log(`Action performed: ${JSON.stringify(data)}`);
}); });
client.on('entityCreated', (data) => { client.on('created', (data) => {
log(`Entity created: ${JSON.stringify(data)}`); log(`Entity created: ${JSON.stringify(data)}`);
}); });
@@ -83,20 +101,30 @@
const entityId = entityIdInput.value; const entityId = entityIdInput.value;
const args = argsInput.value.split(',').map(arg => arg.trim()); const args = argsInput.value.split(',').map(arg => arg.trim());
if (actionName === 'create') { client.send(JSON.stringify({
client.createEntity(pluginIdentifier, { /* entity data */ }); action: [
} else { actionName ? `/${actionName}` : null,
client.performAction(pluginIdentifier, entityId, actionName, ...args); entityId ?? null
} ].filter(v => !!v).join('.'),
message: String(args.length > 0 ? args.join(' ') : pluginIdentifier),
}));
}); });
function addOptionElement(el, config = { value: '', identifier: 'select...' }) {
const opt = document.createElement('option');
opt.setAttribute('value', config.value);
opt.textContent = config.identifier;
el.appendChild(opt);
}
function updatePluginOptions(plugins) { function updatePluginOptions(plugins) {
pluginIdentifierSelect.innerHTML = ''; pluginIdentifierSelect.innerHTML = '';
addOptionElement(pluginIdentifierSelect);
plugins.forEach((plugin) => { plugins.forEach((plugin) => {
const option = document.createElement('option'); addOptionElement(pluginIdentifierSelect, {
option.value = plugin.identifier; value: plugin.identifier,
option.textContent = plugin.identifier; identifier: plugin.identifier
pluginIdentifierSelect.appendChild(option); });
}); });
updateActionOptions(); updateActionOptions();
} }
@@ -105,17 +133,19 @@
const pluginIdentifier = pluginIdentifierSelect.value; const pluginIdentifier = pluginIdentifierSelect.value;
const actions = client.plugins.get(pluginIdentifier); const actions = client.plugins.get(pluginIdentifier);
actionNameSelect.innerHTML = ''; actionNameSelect.innerHTML = '';
addOptionElement(actionNameSelect);
if(!pluginIdentifier) return;
actions.forEach((action) => { actions.forEach((action) => {
const option = document.createElement('option'); addOptionElement(actionNameSelect, {
option.value = action; value: action,
option.textContent = action; identifier: action
actionNameSelect.appendChild(option); });
}); });
} }
function log(message) { function log(message) {
const entry = document.createElement('div'); const entry = document.createElement('div');
entry.textContent = message; entry.textContent = typeof(message) == 'string' ? message : JSON.stringify(message);
outputDiv.appendChild(entry); outputDiv.appendChild(entry);
outputDiv.scrollTop = outputDiv.scrollHeight; outputDiv.scrollTop = outputDiv.scrollHeight;
} }

View File

@@ -1,24 +0,0 @@
export default class Repository {
#entityClass = undefined;
#actions = new Map();
constructor(config) {
this.#entityClass = config.entityClass;
}
create(entityData) {
return new this.#entityClass(entityData);
}
perform(entity, actionName, ...args) {
if(!this.#actions.has(actionName)) throw new Error(`Action '${actionName}' is not registered.`);
return this.#actions.get(actionName)(entity, ...args);
}
register(actionName, actionFn) {
this.#actions.set(actionName, actionFn);
}
getActions() {
return Array.from(this.#actions.keys());
}
}

View File

@@ -1,19 +0,0 @@
export default class Room {
constructor(data) {
this.id = data.id;
this.name = data.name;
this.users = new Set();
}
addUser(userId) {
this.users.add(userId);
}
removeUser(userId) {
this.users.delete(userId);
}
getUsers() {
return Array.from(this.users);
}
}

View File

@@ -1,65 +0,0 @@
import BasePlugin from './BasePlugin';
class EntityPlugin extends BasePlugin {
#config = {};
#entities = new Map();
#repository = null;
constructor(config) {
super();
this.#config = config;
}
initialize(wetsock) {
super.initialize(wetsock);
this.#repository = this.#config.repository;
this.#config.entities.forEach((data) => {
const entity = this.#repository.create(data);
this.#entities.set(entity.id, entity);
});
}
onMessage(clientId, message) {
const { action, entityId, data } = message;
if (action === 'create') {
const entity = this.#repository.create(data);
this.#entities.set(entity.id, entity);
this.broadcast(clientId, { event: 'entityCreated', entityId: entity.id });
} else if (action === 'perform') {
const entity = this.#entities.get(entityId);
if (entity) {
const result = this.#repository.perform(entity, data.actionName, ...data.args);
this.broadcast(clientId, { event: 'actionPerformed', entityId, actionName: data.actionName, result });
}
}
}
onOpen(clientId) {
// Send the list of available entities to the client
const entityList = Array.from(this.#entities.values());
this.send(clientId, { event: 'entityList', entities: entityList });
}
onClose(clientId) {
// Handle client disconnection if needed
}
broadcast(clientId, message) {
// Broadcast the message to all connected clients
for (const [id, client] of this._getServer().clients) {
if (id !== clientId) {
client.send(JSON.stringify(message));
}
}
}
send(clientId, message) {
// Send the message to a specific client
const client = this._getServer().clients.get(clientId);
if (client) {
client.send(JSON.stringify(message));
}
}
}