colyseus / command Goto Github PK
View Code? Open in Web Editor NEWCommand Pattern for Colyseus
Home Page: https://docs.colyseus.io/best-practices/#the-command-pattern
License: MIT License
Command Pattern for Colyseus
Home Page: https://docs.colyseus.io/best-practices/#the-command-pattern
License: MIT License
Imagine following scenario:
Creating a Unit Entity
After that create a Player Entity which can control this unit
onJoin(client: Client, options: any, auth: any) {
const unit = this.dispatcher.dispatch(new UnitAddCommand(),{})
this.dispatcher.dispatch(new PlayerAddCommand(), {
sessionId: client.sessionId,
unitId: unit.id
});
}
So i would like to create a command like this:
export class UnitAddCommand extends Command<RoomState, UnitAddPayload> {
execute(payload: UnitAddPayload): UnitState {
const unit = new UnitState()
this.state.units.set(unit.id, unit)
return unit
}
}
This would be a really nice feature IMO.
Feel free to ask questions.
The dispatch
method does not return the result
unless it actually has more commands to run."
Thus, it returns undefined if our Command is asynchronous.
We don't normally await the results, but we try to when the player exits/leaves the room, so that we ensure all commands have completed before we save the player state.
But if the promise is not returned, this isn't possible
Dispatcher will forcus cmds to execute in a queue. If I create single dispatcher for one room, dose it probabily block other players' cmds handling when one player's cmd awaiting ? So I am considering if it's necessary create dispatcher for every player in the room.
If all cmds are sync, is there no difference between single dispatcher mode and one-player-one-dispatcher mode, because of Node.js's single thread mechanism?
How should I judge how many dispatcher instance to create for one room
First of all, thank you for this library! It has been a blast so far ๐ฅ
I have a use case where I want to dispatch two (or more) commands after a command finished. They're all promises.
Example;
export class OnJoinCommand extends Command<YourState, { sessionId: string }> {
execute({ sessionId }) {
this.state.players[sessionId] = new Player();
return [
new FirstCommand().setPayload({ sessionId }),
new SecondCommand().setPayload({ sessionId })
];
}
}
In that case SecondCommand
would execute after FirstCommand
finished, how would I execute both at the same time?
Library extends the Command class with Generic Types of <Room, Payload>
However it should be <Room, Payload>
Solution:
export abstract class Command<
R extends Room<any, any> = Room,
Payload = unknown,
> {
payload: Payload;
PR coming soon
The current typing of the optional validate
method of commands assumes that the payload data is already checked for correct data types. As incoming messages by clients must never be trusted without data type checking, there should be a canonical way of doing so, e.g.
validate
before setting the payload and have validate
receive the payload as unknown
data type. If the method returns true, the data can be trusted and is of the specified payload data type.Either way the interface of Dispatcher.dispatch
should then allow to pass unknown data.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.