Concept Document: Enhancing the Command Context System for a MUD - wwestlake/Labyrinth GitHub Wiki

Concept Document: Enhancing the Command Context System for a MUD

Overview

The Command Context system is designed to manage the state and execution environment for commands in a Multi-User Dungeon (MUD) game. This system ensures that commands are executed with the appropriate permissions, and it provides the necessary infrastructure to handle user interactions, administrative tasks, and system operations. To enhance this system, we need to incorporate variable management, script execution, and action history tracking to support features like command undoing and state restoration.

Key Components

  1. Context Classes:

    • UserContext: Manages the state and permissions for commands executed by a normal user.
    • AdminContext: Extends UserContext, allowing administrators to act on behalf of other users and perform elevated actions.
    • SystemContext: A special context with permissions to execute system-level commands that affect the entire game or server.
  2. Variable Management:

    • Each context (User, Admin, System) should maintain a dictionary or similar structure to store variables and their values. These variables could be defined by scripts or commands and need to persist throughout the context's lifetime.
    • Variables may be local (specific to the current command or script) or global (available across multiple commands or sessions, depending on the context).
    • Example variables might include currentHealth, selectedWeapon, targetEnemy, etc.
  3. Script Execution:

    • Contexts should support the execution of multi-step commands or scripts, which may define and use variables.
    • The script execution engine should interact with the context to retrieve and update variable values dynamically.
    • Scripts should be able to control the flow of execution based on the context (e.g., branching, looping, conditional execution).
  4. History and Undo Mechanism:

    • Action History: Each context should maintain a history of actions performed during its lifetime. This history could include executed commands, changes to variables, and modifications to the game state.
    • Undo Capability: For certain actions (depending on user privileges), the context should allow undoing previous actions. This could involve reverting variable values, canceling commands, or restoring entities to their previous states.
    • Permissions: The ability to undo actions should be tightly controlled, with only users or admins with the appropriate privileges able to perform these operations.
  5. State Management:

    • Contexts should be responsible for managing and storing the state of entities within their scope, such as the current room, inventory, or health of the user.
    • The state management system should be capable of saving and restoring states, which is particularly important for undo functionality and context switching.
  6. Context Switching:

    • Admins should be able to switch between different contexts (e.g., acting as a different user) without losing the state of the original context. This requires the ability to pause and resume contexts seamlessly.
    • The system should support nested contexts, where a command might temporarily elevate to an AdminContext or SystemContext before returning to the original UserContext.
  7. Security and Permissions:

    • Each context should enforce strict security and permission checks before executing any command. These checks should consider the context's type, the command being executed, and the current user's role and privileges.
    • The system should also log actions performed in elevated contexts to ensure transparency and accountability.

Detailed Considerations

  1. Variable Scoping:

    • Determine the scope of variables—whether they are session-based, context-specific, or global across multiple contexts. This scoping impacts how variables are stored, accessed, and managed.
    • Ensure that variable names are unique within their scope to prevent conflicts.
  2. Action History Structure:

    • Define a structure for recording actions that includes metadata such as the command executed, variables affected, and the previous state of those variables.
    • Consider the memory and performance implications of maintaining a detailed history, particularly in long-running sessions.
  3. Undo Mechanism:

    • Identify which commands or actions are reversible and the conditions under which they can be undone.
    • Develop strategies for rolling back complex actions, especially those that involve multiple steps or external effects (e.g., sending messages, updating the game world).
  4. Integration with Existing Entities:

    • Ensure that the context system integrates seamlessly with the existing ApplicationUser, Room, Exit, and other entities in the game.
    • The context should be able to query and modify these entities as needed while respecting the boundaries set by the current context type.
  5. Testing and Validation:

    • Implement thorough testing of the context system, particularly focusing on security (e.g., ensuring that users cannot elevate their privileges inappropriately) and stability (e.g., ensuring that undo operations do not leave the system in an inconsistent state).
    • Validate that context switching, script execution, and variable management work correctly across different scenarios and use cases.

Conclusion

The enhanced Command Context system will be a powerful tool for managing the execution environment in the MUD game, providing robust support for variable management, script execution, and action history tracking. By carefully designing and implementing these features, we can ensure that the system remains flexible, secure, and performant, capable of handling the diverse needs of normal users, admins, and system-level operations.