LLFS Command Execution Specification - wwestlake/Labyrinth GitHub Wiki

LLFS Command Execution Specification

1. User Context and Character Context

LLFS commands will be executed within the context of both the user (authenticated player) and the character (in-game avatar). We will manage these contexts through the following components:

1.1 User Context

The user context includes:

  • Identity: The authenticated user's ID (from the authentication system).
  • Claims: The user's claims, such as permissions, roles, etc.
  • Session: Information about the current user session, such as active connections.

The User Context will be passed along with each command as:

{
  "userId": "user-12345",
  "userClaims": {
    "roles": ["Admin", "Player"],
    "permissions": ["can_move", "can_attack"]
  }
}

1.2 Character Context

The character context includes:

  • Character ID: The in-game avatar's ID.
  • Character Attributes: Properties such as health, strength, inventory, etc.
  • Character Status: Current status (e.g., "alive", "stunned").

The Character Context is represented as:

{
  "characterId": "char-67890",
  "attributes": {
    "health": 100,
    "strength": 15,
    "inventory": ["sword", "shield"]
  },
  "status": "alive"
}

2. Command Execution Workflow

The LLFS command execution involves the following steps:

2.1 Compile Command

When a command is compiled, it will output an executable function with any variable or expression resolved. The compiled command needs to be associated with both the user and character contexts for execution.

2.2 Execute Command

To execute a compiled command, the system should:

  1. Validate the user and character contexts.
  2. Check the user's permissions to ensure they are allowed to execute the command.
  3. Execute the command with the relevant character state.

Each compiled command is passed to an executor along with the contexts:

executeCommand(compiledCommand, userContext, characterContext)

2.3 Command Validation

Before execution, the command must be validated:

  • User Role and Claims Check: Ensure the user has sufficient rights to perform the action.
  • Character Status Check: Ensure the character is able to perform the action (e.g., not stunned or dead).

Validation logic:

function validateCommand(userContext, characterContext, compiledCommand) {
  if (!userContext.userClaims.permissions.includes(compiledCommand.requiredPermission)) {
    throw new Error("Permission Denied");
  }

  if (characterContext.status !== "alive") {
    throw new Error("Character cannot act in current state");
  }
}

2.4 Command Execution

After validation, the compiled command is executed within the character's context. This may include modifying character attributes, interacting with other game objects, or sending responses to the user.

Example execution flow:

function executeCommand(compiledCommand, userContext, characterContext) {
  // Validate command first
  validateCommand(userContext, characterContext, compiledCommand);

  // Execute the compiled command logic
  const result = compiledCommand.execute(characterContext);

  // Update character state or handle results
  updateCharacterState(characterContext, result);

  return result;
}

2.5 Command Output Handling

The result of executing a command may include:

  • Updating the game world.
  • Sending feedback to the user (e.g., "You successfully moved north").
  • Triggering other events in the game.

Example output handling:

function handleCommandOutput(result, userContext) {
  // Send the result back to the user
  sendFeedbackToUser(userContext.userId, result.message);

  // Broadcast changes to the game world if applicable
  if (result.worldChanges) {
    broadcastToGameWorld(result.worldChanges);
  }
}

3. Example Flow: "go" Command

3.1 Compiled Command

The compiled form of the go <direction> command will include logic to move the character and any conditions that need to be checked before the move is executed.

const compiledGoCommand = {
  name: "go",
  requiredPermission: "can_move",
  execute: function(characterContext) {
    if (characterContext.attributes.health <= 0) {
      return { success: false, message: "You are too weak to move!" };
    }

    // Perform the move logic
    const newPosition = moveCharacter(characterContext, "north");

    return { success: true, message: "You move north.", worldChanges: { position: newPosition } };
  }
};

3.2 Execution Workflow

When the player types go north, the following steps occur:

  1. The command is parsed and compiled into compiledGoCommand.
  2. The executeCommand function is invoked with the compiled command, user context, and character context.
  3. The system checks if the user has the "can_move" permission and if the character is alive.
  4. If valid, the command is executed, and the character's position is updated.
  5. The result of the command is returned to the user and optionally broadcast to other players.
const result = executeCommand(compiledGoCommand, userContext, characterContext);
handleCommandOutput(result, userContext);

4. Error Handling and Logging

Any errors during command validation or execution should be caught and logged for auditing or debugging purposes.

function executeCommandWithErrorHandling(compiledCommand, userContext, characterContext) {
  try {
    return executeCommand(compiledCommand, userContext, characterContext);
  } catch (error) {
    logError(error, userContext, characterContext);
    return { success: false, message: "An error occurred while executing the command." };
  }
}

4.1 Command Error Logging

Logs will capture the user's identity, character context, and the specific error encountered.

function logError(error, userContext, characterContext) {
  console.error(`Error for user ${userContext.userId} on character ${characterContext.characterId}:`, error);
}

5. Summary

  • User and Character Contexts: Commands are executed in the context of both the authenticated user and their in-game character.
  • Command Validation: Commands are validated for permissions and character status before execution.
  • Execution Workflow: Commands are compiled, executed, and the results are handled, including feedback to the user and updates to the game world.
  • Error Handling: Errors during execution are logged, and the user is informed.
⚠️ **GitHub.com Fallback** ⚠️