Technical Approach for Embedding a Script Management System in MUD Server Admin UI - wwestlake/Labyrinth GitHub Wiki
Technical Approach for Embedding a Script Management System in MUD Server Admin UI
Overview
This document outlines the technical approach for implementing a comprehensive administrative UI within the MUD server, allowing admins and owners to manage server tasks, edit and save scripts (C# or Python), compile and execute these scripts, and attach them to server events.
Objectives
- Admin UI Integration: Provide a web-based UI within the existing React-based UI for managing server configurations, game settings, and scripting tasks.
- Script Editing: Enable admins to write, edit, and save C# or Python scripts using a code editor integrated into the UI.
- Script Storage: Store the scripts in MongoDB for persistence and easy retrieval.
- Compilation and Execution: Compile and execute scripts on the server, report errors, and manage runtime execution.
- Event Integration: Allow scripts to be triggered by specific server events.
Architecture Overview
1. Frontend (React UI)
- Code Editor: Integrate a code editor component like Monaco Editor (used in VS Code) or Ace Editor into the React admin UI.
- UI Components: Provide UI components for managing server configurations, viewing logs, and monitoring server health.
- API Integration: Use Axios or Fetch API to communicate with the backend for saving, compiling, and executing scripts.
2. Backend (C# Web API in .NET 8)
- Script Management API: Develop a set of API endpoints for CRUD operations on scripts (create, read, update, delete), compilation, execution, and event handling.
- Script Storage: Use MongoDB to store scripts and metadata (e.g., script name, language, associated events).
- Script Compilation and Execution:
- C# Scripts: Use the Roslyn Scripting API for dynamic compilation and execution.
- Python Scripts: Use IronPython for executing Python scripts within the .NET environment.
- Error Handling and Reporting: Capture compilation and runtime errors, and return detailed error messages to the frontend.
- Event Dispatcher: Implement a system for mapping server events to scripts that should be executed when those events occur.
3. Database (MongoDB)
- Script Collection: Store scripts in a MongoDB collection, each document containing fields such as:
name
: The name of the script.language
: The scripting language (C# or Python).code
: The actual script code.createdBy
: User who created the script.createdAt
: Timestamp of creation.events
: List of events that trigger this script.
- Audit Logs: Store logs of script execution for auditing purposes.
Detailed Technical Components
1. Frontend Implementation
Code Editor Integration
- Monaco Editor:
- Import and integrate the Monaco Editor into the React component responsible for script management.
- Enable syntax highlighting and autocomplete for C# and Python.
- Provide features like linting, error detection, and formatting.
- Script Management Interface:
- Create a form allowing admins to name scripts, choose the language (C# or Python), and associate events with the script.
- Implement buttons for saving scripts, running tests, and deploying scripts.
2. Backend Implementation
API Endpoints
- Script Management:
POST /api/scripts
: Create or update a script.GET /api/scripts
: Retrieve all scripts or a specific script by ID.DELETE /api/scripts/{id}
: Delete a script by ID.
- Script Compilation and Execution:
POST /api/scripts/compile
: Compile the script and return any compilation errors.POST /api/scripts/run
: Execute the script and return output or errors.
- Event Management:
POST /api/events
: Trigger an event that executes associated scripts.
Script Compilation and Execution
- C# (Roslyn API):
- Use the Roslyn Scripting API to compile and run C# scripts dynamically.
- Implement a method to execute the script in a sandboxed environment, limiting the script’s access to sensitive server resources.
- Python (IronPython):
- Embed IronPython in the .NET Core application to execute Python scripts.
- Similar to C#, ensure that Python scripts are executed in a controlled environment.
Error Handling
- Implement robust error handling in both compilation and execution phases.
- Return detailed error messages to the frontend, including line numbers, syntax issues, and runtime exceptions.
3. MongoDB Integration
Script Storage
- Define a MongoDB schema for storing script-related information.
- Implement methods for saving, updating, retrieving, and deleting scripts.
- Use MongoDB’s indexing to quickly query scripts by name, event, or other criteria.
Audit Logging
- Log every script execution attempt, including script ID, execution time, output, and any errors.
- Store logs in a separate MongoDB collection for easy querying and monitoring.
4. Event Integration
Event Dispatcher
- Create an event dispatcher in the server that listens for predefined events (e.g., player login, item pickup).
- When an event occurs, query MongoDB to retrieve and execute any scripts associated with that event.
- Allow scripts to modify the event’s behavior or data, enabling dynamic game functionality.
Security Considerations
- Sandboxing: Ensure that all scripts run in a restricted environment with limited access to the server’s file system, network, and critical APIs.
- Authentication and Authorization: Only authorized users (admins and owners) should have access to the script management UI and API endpoints.
- Input Validation: Validate all inputs to the script editor and execution endpoints to prevent injection attacks.
Deployment Strategy
- Testing Environment: Implement a staging environment where scripts can be tested safely before deployment.
- CI/CD Integration: Automate the deployment of script changes using a CI/CD pipeline, ensuring that any script modifications are tested and reviewed before going live.
- Monitoring and Alerts: Set up monitoring to detect performance issues or errors related to script execution, and alert admins if issues arise.
Conclusion
This technical approach provides a comprehensive plan for integrating script management and execution capabilities into the MUD server. By embedding a code editor in the React UI, storing scripts in MongoDB, and utilizing the Roslyn and IronPython APIs for execution, admins and owners will have powerful tools to customize and automate server behavior. The approach also emphasizes security, maintainability, and scalability to ensure long-term success.