refactor: mixin

This commit is contained in:
cha0s 2024-01-05 22:50:17 -06:00
parent b72d3b5b28
commit da6f6fbd51
8 changed files with 71 additions and 20 deletions

View File

@ -1,11 +1,29 @@
import Receiver from './receiver';
export const hooks = {
'@flecks/core.mixin': (Flecks) => (
class FlecksWithHumusAppClient extends Flecks {
constructor(...args) {
super(...args);
if (!this.humus) {
this.humus = {};
}
if (!this.humus.app) {
this.humus.app = {};
}
this.humus.app.room = undefined;
this.humus.app.selfEntity = undefined;
this.humus.app.synchronizer = undefined;
}
}
),
'@flecks/web/client.up': (flecks) => {
window.flecks = flecks;
const Synchronizer = Receiver(flecks);
const synchronizer = new Synchronizer();
flecks.set('$humus/app.synchronizer', synchronizer);
flecks.humus.app.synchronizer = synchronizer;
synchronizer.listenTo(flecks.socket.client);
},
};

View File

@ -7,16 +7,16 @@ import {useSocket} from '@flecks/socket';
export default () => {
const flecks = useFlecks();
const [room, setRoom] = useState(flecks.get('$humus/app.room'));
const [room, setRoom] = useState(flecks.humus.app.room);
const socket = useSocket();
const synchronizer = flecks.get('$humus/app.synchronizer');
const {synchronizer} = flecks.humus.app;
useEffect(() => {
if (!room) {
return undefined;
}
const onDisconnect = () => {
synchronizer.destroySynchronized(room.constructor.resourceId, room.s13nId);
flecks.set('$humus/app.room', undefined);
flecks.humus.app.room = undefined;
setRoom(undefined);
};
socket.on('disconnect', onDisconnect);
@ -33,7 +33,7 @@ export default () => {
switch (created.constructor.resourceId) {
// Track room.
case Room.resourceId: {
flecks.set('$humus/app.room', created);
flecks.humus.app.room = created;
created.universe = synchronizer.synchronized(7, 0);
setRoom(created);
break;

View File

@ -12,13 +12,13 @@ export default () => {
const flecks = useFlecks();
const room = useRoom();
const selfEntity = useSelector(selfEntitySelector);
const [entity, setEntity] = useState(flecks.get('$humus/app.selfEntity'));
const [entity, setEntity] = useState(flecks.humus.app.selfEntity);
useEffect(() => {
if (!room) {
return;
}
const onDestroying = () => {
flecks.set('$humus/app.selfEntity', undefined);
flecks.humus.app.selfEntity = undefined;
setEntity(undefined);
};
const augmentSelfEntity = async (entity) => {
@ -26,7 +26,7 @@ export default () => {
entity.initialFollow();
entity.on('destroying', onDestroying);
}
flecks.set('$humus/app.selfEntity', entity);
flecks.humus.app.selfEntity = entity;
setEntity(entity);
};
augmentSelfEntity(room.findEntity(selfEntity));

View File

@ -3,6 +3,23 @@ import flatten from 'lodash.flatten';
export const hooks = {
'@avocado/traits.traits': Flecks.provide(require.context('./traits', false, /\.js$/)),
'@flecks/core.mixin': (Flecks) => (
class FlecksWithHumusCombat extends Flecks {
constructor(...args) {
super(...args);
if (!this.humus) {
this.humus = {};
}
if (!this.humus.combat) {
this.humus.combat = {};
}
this.humus.combat.Affinities = {};
this.humus.combat.interactions = () => {};
}
}
),
'@humus/combat.affinities': () => ({
Void: Class,
Bio: Class,
@ -19,7 +36,7 @@ export const hooks = {
return context.keys().map((path) => context(path).default);
},
'@flecks/core.starting': (flecks) => {
flecks.set('$humus/combat.affinities', flecks.gather('@humus/combat.affinities'));
flecks.humus.combat.Affinities = flecks.gather('@humus/combat.affinities');
const interactions = flatten(flecks.invokeFlat('@humus/combat.interactions'))
.reduce(
(r, interaction) => {
@ -37,9 +54,8 @@ export const hooks = {
},
{},
);
flecks.set(
'$humus/combat.interactions',
(harmingAffinity, harmedAffinity) => interactions[harmingAffinity]?.[harmedAffinity] || [],
flecks.humus.combat.interactions = (harmingAffinity, harmedAffinity) => (
interactions[harmingAffinity]?.[harmedAffinity] || []
);
},
'@flecks/socket.packets': Flecks.provide(require.context('./packets', false, /\.js$/)),

View File

@ -5,7 +5,7 @@ import {Packet} from '@flecks/socket';
export default (flecks) => class HarmPacket extends Packet {
static pack(harms) {
const Affinities = flecks.get('$humus/combat.affinities');
const {Affinities} = flecks.humus.combat;
return harms.map((harm) => {
const {[harm.affinity]: Affinity} = Affinities;
harm.affinity = Affinity.id;
@ -27,7 +27,7 @@ export default (flecks) => class HarmPacket extends Packet {
}
static unpack(harms) {
const Affinities = flecks.get('$humus/combat.affinities');
const {Affinities} = flecks.humus.combat;
return harms.map((harm) => {
const {[harm.affinity]: Affinity} = Affinities;
harm.affinity = Affinity.type;

View File

@ -194,7 +194,7 @@ export default (flecks) => class Vulnerable extends Trait {
interactions(harmed, harm) {
const {Script} = flecks.avocado.resource.Resources;
const {affinity, from} = harm;
const interactions = flecks.get('$humus/combat.interactions');
const {interactions} = flecks.humus.combat;
flatten(
harmed.affinities()
.map((harmedAffinity) => interactions(affinity, harmedAffinity)),

View File

@ -14,6 +14,23 @@ const {
const debug = D('@humus/universe/server');
export const hooks = {
'@flecks/core.mixin': (Flecks) => (
class FlecksWithHumusAppClient extends Flecks {
constructor(...args) {
super(...args);
if (!this.humus) {
this.humus = {};
}
if (!this.humus.universe) {
this.humus.universe = {};
}
this.humus.universe.universe = undefined;
this.humus.universe.resourceServer = undefined;
}
}
),
'@flecks/core.starting': async (flecks) => {
const {resource} = flecks.get('@humus/universe');
const stats = await stat(resource);
@ -21,14 +38,14 @@ export const hooks = {
throw new Error(`resource root ${resource} is not a directory`);
}
Resource.root = resource;
flecks.set('$humus/universe.resource-server', express.static(resource));
flecks.humus.universe.resourceServer = express.static(resource);
debug('resource root: %s', resource);
},
'@flecks/server.up': async (flecks) => {
const {universe: path} = flecks.get('@humus/universe');
try {
const universe = await Universe.loadFrom(flecks, resolve(FLECKS_CORE_ROOT, path));
flecks.set('$humus/universe.universe', universe);
flecks.humus.universe.universe = universe;
universe.start();
debug('universe up and running!');
}
@ -39,7 +56,7 @@ export const hooks = {
'@flecks/web/server.request.socket': (flecks) => (req, res, next) => {
if (req.url.startsWith('/resource')) {
req.url = req.url.slice('/resource'.length);
flecks.get('$humus/universe.resource-server')(req, res, next);
flecks.humus.universe.resourceServer(req, res, next);
return;
}
next();
@ -48,7 +65,7 @@ export const hooks = {
Flecks.decorate(require.context('./packets/decorators', false, /\.js$/))
),
'@flecks/socket/server.request.socket': (flecks) => async ({handshake}, next) => {
const {universe} = flecks.get('$humus/universe');
const {universe} = flecks.humus.universe;
const {user} = handshake;
if (0 !== user.id) {
handshake.entity = await universe.loadOrCreateEntity(user);

View File

@ -4,7 +4,7 @@ export default (Join, flecks) => class UniverseJoinPacket extends Join {
static async respond(packet, socket) {
const {req: {entity}} = socket;
const universe = flecks.get('$humus/universe.universe');
const {universe} = flecks.humus.universe;
if (universe.hasEntity(entity)) {
throw new ValidationError({code: 409, reason: 'already logged in'});
}