Lua/Cpp Phira-mp已停止维护,请移步至Phira-mp-plus
Lua/Cpp Phira-mp
A complete, feature-rich multiplayer game server implementation in C++20, providing full compatibility with the phira-mp protocol and extending it with a powerful Lua plugin system and RESTful HTTP API.
中文说明:这是一个用C++20实现的多人在线游戏服务器,完全兼容phira-mp协议,并扩展了强大的Lua插件系统和RESTful HTTP API。
📋 Features / 功能特性
Core Server / 核心服务器
- Binary Protocol Support - Full implementation of the phira-mp binary command protocol with 16 command types
- Multi-threaded Architecture - Efficient session handling with dedicated connection threads
- Room Management - Complete room system with creation, joining, and management capabilities
- User Session Management - Robust user connection handling with UUID-based identification
Plugin System / 插件系统
- Lua 5.4 Integration - Dynamic plugin loading with Lua scripting support
- Event Hooks - Comprehensive hook system (
on_enable,on_disable,on_user_join,on_before_command, etc.) - Plugin API - Full Lua API exposed through global
phiratable for server manipulation - Hot Reload - Plugins can be enabled/disabled at runtime
HTTP API / HTTP接口
- RESTful Endpoints - Complete HTTP API on port 61234 (configurable)
- Public Endpoints -
/room,/statsfor client applications - Replay System -
/replay/*endpoints for replay authentication and retrieval - Admin Interface - Full administrative control via HTTP (
/admin/*) - CORS Support - Cross-origin requests enabled for web clients
Replay System / 回放系统
- Recording & Storage - Automatic replay recording during gameplay
- File-based Storage - Replays stored in
replays/directory as binary files - Metadata Management - Replay information tracking (player, song, timestamp, size)
- HTTP Access - Replays accessible via authenticated HTTP endpoints
Admin & Management / 管理员功能
- Token-based Authentication - Simple admin token system (awaiting HSN integration)
- Server Configuration - Dynamic configuration via HTTP API
- Room Controls - Room creation, banning, and management
- User Management - User banning, disconnection, and monitoring
- Broadcast System - Server-wide message broadcasting
Interactive CLI / 交互式命令行界面
- Real-time Command Input - Interactive console for server administration
- Server Status Monitoring - View connected users, active rooms, and server stats
- Room Management - List, disband, and configure rooms directly
- User Management - Kick, ban, unban, and view user details
- Plugin Control - Hot-reload plugins without restarting server
- Broadcast Messaging - Send messages to all rooms or specific rooms
🚀 Quick Start / 快速开始
Prerequisites / 环境要求
- g++ 13+ with C++20 support
- Lua 5.4 development libraries
- libuuid for UUID generation
- pthread for threading support
Ubuntu/Debian:
sudo apt-get update
sudo apt-get install -y g++ make liblua5.4-dev uuid-devCentOS/RHEL:
sudo yum install -y gcc-c++ make lua-devel libuuid-develBuilding / 编译
# Clone the repository (if not already)
git clone <repository-url>
cd cpp-phira-mp
# Build the server
make clean
makeThis will produce the phira-mp-server binary.
Running / 运行
# Start with default port (12346 for game protocol, 61234 for HTTP)
./phira-mp-server
# Start with custom game port
./phira-mp-server --port 8080
# Start in background
nohup ./phira-mp-server > server.log 2>&1 &The server will:
- Load configuration from
server_config.yml(if exists) - Scan and load plugins from
plugins/directory - Start game server on specified port (default: 12346)
- Start HTTP API server on port 61234
- Start interactive CLI for console administration
- Begin accepting connections
Interactive CLI Usage / 交互式CLI使用
When you start the server, you'll see a CLI prompt appear:
=== Phira MP Server CLI ===
Type 'help' for available commands
==============================
>Available Commands / 可用命令
General Commands:
help,?- Show help messagestatus,info- Show server statusstop,shutdown- Show shutdown instructions
Room Management:
list,rooms- List all active roomsdisband <roomId>- Disband a roommaxusers <roomId> <count>- Set room max users (1-64)roomcreation <on|off|status>- Control room creation
User Management:
users- List all online usersuser <userId>- Show user detailskick <userId>- Kick a user from the serverban <userId>- Ban a user from the serverunban <userId>- Unban a userbanlist- Show banned users list
Communication:
broadcast <msg>- Broadcast message to all roomssay <msg>- Alias for broadcastroomsay <roomId> <msg>- Send message to specific room
Server Management:
reload- Reload all pluginsreplay <on|off|status>- Control replay recording
Command Examples / 命令示例
# View server status
> status
# List all active rooms
> list
# List all online users
> users
# Kick a user
> kick 12345
# Broadcast server message
> broadcast Server will restart in 10 minutes
# Send message to specific room
> roomsay room1 Please follow the game rules
# Disband a room
> disband room1
# Reload all plugins
> reload
# Control replay recording
> replay on
> replay statusNote: For advanced commands (contest mode, IP blacklist management, etc.), use the HTTP API on port 61234.
⚙️ Configuration / 配置
Server Configuration / 服务器配置
Create server_config.yml in the working directory:
# Game server port
port: 12346
# HTTP API server port
http_port: 61234
# Admin token for API access (simplified, awaiting HSN integration)
admin_token: "your-secure-admin-token-here"
# Replay system enabled
replay_enabled: true
# Room creation enabled
room_creation_enabled: true
# Monitor IDs (game-specific)
monitors:
- 2
- 42If no configuration file is found, default values will be used.
Plugin Configuration / 插件配置
Each plugin in the plugins/ directory requires:
plugin.json- Plugin metadata (id, name, version, author, enabled flag)init.lua- Main plugin script
Example plugin.json:
{
"id": "my-plugin",
"name": "My Plugin",
"version": "1.0.0",
"description": "Plugin description",
"author": "Your Name",
"enabled": true,
"dependencies": []
}🔌 Plugin System / 插件系统
Available Plugins / 内置插件
The server comes with 5 built-in plugins:
- http-admin-api - HTTP Admin API endpoints
- replay-recorder - Game replay recording and management
- admin-commands - Administrative command system
- advanced-room-management - Enhanced room controls and features
- virtual-room - Virtual room creation and management
Plugin Development / 插件开发
Plugins are written in Lua and have access to the server through the phira global table.
For complete plugin development documentation, see PLUGIN_DEVELOPMENT.md
The plugin system provides:
- 40+ Server Management APIs - Full control over users, rooms, messages, bans, contests, and server state
- Comprehensive Event Hooks - Real-time notifications for user joins/leaves, room creation/destruction, kicks, bans, and command filtering
- HTTP Route Registration - Extend the HTTP API with custom endpoints
- Thread-Safe Operations - All APIs are properly synchronized for concurrent access
New Event Hooks (v2.0+)
on_user_kick(user, room, reason)- When a user is kickedon_user_ban(user, reason, duration)- When a user is bannedon_user_unban(user_id)- When a user is unbannedon_room_create(room)- When a room is createdon_room_destroy(room)- When a room is destroyed
New Management APIs (v2.0+)
-- User management
phira.kick_user(user_id, preserve_room)
phira.ban_user(user_id)
phira.unban_user(user_id)
phira.is_user_banned(user_id)
phira.get_banned_users()
-- Room management
phira.disband_room(room_id)
phira.set_max_users(room_id, max_users)
phira.get_room_max_users(room_id)
-- Messaging
phira.broadcast_message(message)
phira.roomsay_message(room_id, message)
-- Server control
phira.shutdown_server()
phira.reload_plugins()
-- Status queries
phira.get_connected_user_count()
phira.get_active_room_count()
phira.get_room_list()
-- IP blacklist management
phira.add_ip_to_blacklist(ip, is_admin)
phira.remove_ip_from_blacklist(ip, is_admin)
phira.is_ip_banned(ip)
-- Contest management
phira.enable_contest(room_id, manual_start, auto_disband)
phira.disable_contest(room_id)
phira.start_contest(room_id, force)See the complete API reference in PLUGIN_DEVELOPMENT.md for detailed documentation and examples.
🌐 HTTP API Reference / HTTP接口文档
Public Endpoints / 公共端点
GET /room
Get list of all available rooms.
Response:
{
"rooms": [
{
"id": "room-uuid",
"name": "Room Name",
"players": 3,
"maxPlayers": 8,
"status": "waiting"
}
],
"total": 1
}GET /stats
Get server statistics.
Response:
{
"users": 5,
"sessions": 5,
"rooms": 2,
"uptime": 3600,
"version": "1.0.0"
}Replay Endpoints / 回放端点
POST /replay/auth
Authenticate for replay access (stub implementation).
Request:
{
"token": "user-token"
}Response:
{
"ok": true,
"userId": 12345,
"charts": [],
"sessionToken": "mock_session_token",
"expiresAt": 1678886400
}GET /replay/download
Download a replay file (requires authentication).
Query Parameters:
id- Replay IDtoken- Session token
POST /replay/delete
Delete a replay (stub implementation).
Admin Endpoints / 管理员端点
All admin endpoints require authentication via admin_token parameter (query string or request body).
Authentication Methods / 认证方式
- Query Parameter:
?admin_token=your-token - Request Body:
{"admin_token": "your-token"}
Available Admin Endpoints / 可用管理端点
Configuration Management:
GET /admin/replay/config- Get replay configurationPOST /admin/replay/config- Update replay configurationGET /admin/room-creation/config- Get room creation configPOST /admin/room-creation/config- Update room creation config
Room Management:
GET /admin/rooms- List all rooms with detailsPOST /admin/ban/room- Ban a roomPOST /admin/rooms/max_users- Set room max usersPOST /admin/rooms/disband- Disband a roomPOST /admin/rooms/chat- Send room chat message
User Management:
GET /admin/users/info- Get user informationPOST /admin/ban/user- Ban a userPOST /admin/users/disconnect- Disconnect a userPOST /admin/users/move- Move user to different room
Server Controls:
POST /admin/broadcast- Broadcast message to all usersGET /admin/ip-blacklist- Get IP blacklistPOST /admin/ip-blacklist/remove- Remove IP from blacklistPOST /admin/ip-blacklist/clear- Clear IP blacklistGET /admin/log-rate- Get log rate configuration
OTP Endpoints (Simplified - Awaiting HSN Integration):
POST /admin/otp/request- Request OTP (returns dummy session)POST /admin/otp/verify- Verify OTP (accepts "123456" for testing)
🔐 Authentication & Security / 认证与安全
Current Implementation / 当前实现
The current authentication system is simplified and awaits integration with the HSN (HyperSynapseNetwork) unified user system.
Admin Authentication:
- Single
admin_tokenin configuration - Token passed via query parameter or request body
- No OTP/IP banning in current simplified version
Future HSN Integration:
- Unified user accounts across services
- OTP-based admin authentication
- IP-based rate limiting and banning
- Session management
Security Notes / 安全说明
- Production Use: The current simplified auth is for development/testing only
- Token Security: Keep
admin_tokensecure and rotate regularly - Network Security: Run behind firewall/reverse proxy in production
- HTTPS: For production, use HTTPS termination at reverse proxy
📊 Replay System / 回放系统
How It Works / 工作原理
- Recording: During gameplay, the server records game events
- Storage: Replays are saved as binary files in
replays/directory - Metadata: Replay information stored in server memory for quick access
- Retrieval: Replays accessible via authenticated HTTP endpoints
File Structure / 文件结构
replays/
├── replay_1234567890_1678886400.bin
├── replay_1234567891_1678886500.bin
└── ...Replay Information / 回放信息
Each replay includes:
- Unique replay ID
- Player name
- Song ID
- Creation timestamp
- File size
- Binary game data
🛠️ Development / 开发
Building from Source / 从源码构建
# Clone repository
git clone <repository-url>
cd cpp-phira-mp
# Install dependencies (Ubuntu/Debian example)
sudo apt-get install -y g++ make liblua5.4-dev uuid-dev
# Build
make
# Run tests (if available)
make testCode Structure / 代码结构
cpp-phira-mp/
├── include/ # Header files
│ ├── server.h # Server core definitions
│ ├── session.h # Session management
│ ├── room.h # Room system
│ ├── commands.h # Binary protocol commands
│ ├── http_server.h # HTTP server
│ ├── lua_bindings.h # Lua API bindings
│ └── ...
├── src/ # Source files
│ ├── server.cpp # Server implementation
│ ├── session.cpp # Session handling
│ ├── http_server.cpp # HTTP API implementation
│ ├── lua_bindings.cpp # Lua integration
│ └── ...
├── plugins/ # Lua plugins
│ ├── http-admin-api/
│ ├── replay-recorder/
│ ├── admin-commands/
│ ├── advanced-room-management/
│ └── virtual-room/
├── replays/ # Replay storage
├── locales/ # Localization files
├── Makefile # Build configuration
├── server_config.yml # Server configuration
└── README.md # This fileExtending the Server / 扩展服务器
Adding New Commands / 添加新命令
- Define command in
include/commands.h - Implement handling in
src/session.cpp - Add plugin hooks if needed
Adding HTTP Endpoints / 添加HTTP端点
- Add route registration in
src/http_server.cpp - Implement handler function
- Test with curl or HTTP client
Creating New Plugins / 创建新插件
- Create directory in
plugins/ - Add
plugin.jsonwith metadata - Write
init.luawith plugin logic - Enable in configuration
🐛 Troubleshooting / 故障排除
Common Issues / 常见问题
Server won't start:
- Check port availability:
sudo lsof -i :12346 - Verify dependencies:
ldd phira-mp-server - Check permissions:
chmod +x phira-mp-server
Plugins not loading:
- Verify plugin directory structure
- Check
plugin.jsonsyntax - Enable plugin in configuration
- Check Lua version compatibility
HTTP API inaccessible:
- Verify HTTP server is running on port 61234
- Check firewall rules
- Test locally:
curl http://localhost:61234/stats
High memory usage:
- Check for memory leaks in plugins
- Monitor with
toporhtop - Adjust connection limits if needed
Logging / 日志
The server outputs logs to stdout. Important events include:
- Server startup and shutdown
- User connections/disconnections
- Room creation/deletion
- Plugin loading/enabling
- HTTP request processing
For production, redirect logs to a file:
./phira-mp-server > /var/log/phira-server.log 2>&1 &🤝 Contributing / 贡献指南
Development Process / 开发流程
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
Code Standards / 代码标准
- C++20 with modern practices
- RAII for resource management
- const-correctness where applicable
- Meaningful naming for variables/functions
- Comments for complex logic
Testing / 测试
- Test new features thoroughly
- Verify backward compatibility
- Test with multiple simultaneous clients
- Validate HTTP API responses
📄 License / 许可证
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2026 HyperSynapseNetwork
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.🙏 Acknowledgments / 致谢
- Original phira-mp - For the protocol specification and inspiration
- Lua Community - For the powerful scripting language
- Open Source Contributors - For various libraries and tools used
- HyperSynapseNetwork - For project sponsorship and development
- tphira-mp - provided a large amount of reference English for this project
- jphira-mp - provided a large amount of reference English for this project
Note: This server is under active development. Features and APIs may change as development progresses. Always check the documentation for your specific version.
注意: 本服务器正在积极开发中。功能和API可能会随着开发进展而变化。请始终查阅您特定版本的文档。