Adapt the world's evolution - BuggleInc/PLM GitHub Wiki
The world's evolution
Before going into full details, here is the workflow of the world's evolution:
- When the user submits its code, a new entity is created and run
- Each time the world or the entity are modified by the user's code, a corresponding operation is created
- For example, MoveBuggleOperation when a buggle moves, ChangeCellColor when a buggle paints a cell.
- These new operations are send to the browser after each stepUI().
- The client identifies the operations according to their type and generates corresponding objects.
- These objects own 2 main methods:
- apply(), which updates the world or the entity model according to the action
- reverse(), which allows to reset the world or the entity to their previous state
Generate operations server-side
As stated previously, the operations are generated by PLM's entities:
- Start by creating a new abstract class <entity's name>Operation extending Operation
- Browse the entity's file to identify which methods update the world or the entity and have to be displayed to the user
- For each case, create a new operation class corresponding to the action
- Set carefully the operation's name since it will be used to identify the operation's type client-side!
- Don't forget to pass enough data to the operation's constructor to be able to apply the operation, but also to reverse it.
- Use addOperation(Operation operation) to record using your operations the world's evolution.
- Make sure stepUI() is called afterward otherwise the operations won't be sent.
Now, we need to send the new operations to the client.
Send operations to the client
To send the new operations, we need to update webPLM:
- In the folder /app/json/operation, add a new file <entity's name>OperationToJson. This file, like BuggleOperationToJson, will provide methods to format the new operations into JSON.
- In OperationToJson, add to the pattern matching a new case to handle this new kind of operations.
Handle operations client-side
- In the folder /public/app/models/universe/<your universe>, add a new directory operations
- In this folder, add Javascript models corresponding to operations
- The constructor's parameter will be an object resulting of the received JSON.
- Both methods apply() and reverse() take as parameter the world's model.
- Make sure that the methods apply() and reverse() cancel each other out!
- In the world's model, update generateOperation(operation) to generate the correct operation according to the received one's type.