Files

107 lines
4.2 KiB
PowerShell
Raw Permalink Normal View History

# FunMC 局域网联机测试脚本 (Windows PowerShell)
# 用于快速验证 FunMC 服务是否正常运行
Write-Host "============================================" -ForegroundColor Cyan
Write-Host " FunMC 局域网联机测试脚本" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
# 检查必要的工具
function Test-Command($cmd) {
return [bool](Get-Command $cmd -ErrorAction SilentlyContinue)
}
# 1. 检查 Rust 环境
Write-Host "[1/6] 检查 Rust 环境..." -ForegroundColor Yellow
if (Test-Command "cargo") {
$rustVersion = cargo --version
Write-Host " ✓ Rust 已安装: $rustVersion" -ForegroundColor Green
} else {
Write-Host " ✗ Rust 未安装,请访问 https://rustup.rs 安装" -ForegroundColor Red
exit 1
}
# 2. 检查 Node.js 环境
Write-Host "[2/6] 检查 Node.js 环境..." -ForegroundColor Yellow
if (Test-Command "node") {
$nodeVersion = node --version
Write-Host " ✓ Node.js 已安装: $nodeVersion" -ForegroundColor Green
} else {
Write-Host " ✗ Node.js 未安装,请访问 https://nodejs.org 安装" -ForegroundColor Red
exit 1
}
# 3. 检查数据库连接
Write-Host "[3/6] 检查数据库连接..." -ForegroundColor Yellow
$dbRunning = docker ps --filter "name=funmc-db" --format "{{.Names}}" 2>$null
if ($dbRunning -eq "funmc-db") {
Write-Host " ✓ PostgreSQL 容器运行中" -ForegroundColor Green
} else {
Write-Host " ! PostgreSQL 容器未运行,尝试启动..." -ForegroundColor Yellow
docker start funmc-db 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Host " ! 创建新的 PostgreSQL 容器..." -ForegroundColor Yellow
docker run -d --name funmc-db -p 5432:5432 -e POSTGRES_PASSWORD=password -e POSTGRES_DB=funmc postgres:14
}
Start-Sleep -Seconds 3
Write-Host " ✓ PostgreSQL 容器已启动" -ForegroundColor Green
}
# 4. 检查端口占用
Write-Host "[4/6] 检查端口占用..." -ForegroundColor Yellow
$ports = @(3000, 3001, 7900, 5432)
$allFree = $true
foreach ($port in $ports) {
$used = netstat -an | Select-String ":$port\s"
if ($used) {
Write-Host " ! 端口 $port 已被占用" -ForegroundColor Yellow
$allFree = $false
} else {
Write-Host " ✓ 端口 $port 可用" -ForegroundColor Green
}
}
# 5. 获取本机 IP
Write-Host "[5/6] 获取本机 IP..." -ForegroundColor Yellow
$localIP = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notlike "*Loopback*" -and $_.IPAddress -notlike "169.*" } | Select-Object -First 1).IPAddress
if ($localIP) {
Write-Host " ✓ 本机局域网 IP: $localIP" -ForegroundColor Green
} else {
Write-Host " ✗ 无法获取本机 IP" -ForegroundColor Red
}
# 6. 显示测试说明
Write-Host "[6/6] 测试说明..." -ForegroundColor Yellow
Write-Host ""
Write-Host "============================================" -ForegroundColor Cyan
Write-Host " 局域网联机测试步骤" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "1. 启动服务端:" -ForegroundColor White
Write-Host " cd server" -ForegroundColor Gray
Write-Host " cargo run" -ForegroundColor Gray
Write-Host ""
Write-Host "2. 启动客户端 (新终端):" -ForegroundColor White
Write-Host " cd client/ui && npm install" -ForegroundColor Gray
Write-Host " cd .. && cargo tauri dev" -ForegroundColor Gray
Write-Host ""
Write-Host "3. 配置其他电脑的客户端:" -ForegroundColor White
Write-Host " 修改 client/src/config.rs:" -ForegroundColor Gray
Write-Host " DEFAULT_SERVER_URL = `"http://${localIP}:3000`"" -ForegroundColor Gray
Write-Host ""
Write-Host "4. 测试流程:" -ForegroundColor White
Write-Host " - 主机: 启动 MC 服务器 -> FunMC 创建房间 -> 开始托管" -ForegroundColor Gray
Write-Host " - 玩家: FunMC 加入房间 -> 连接 -> 复制地址到 MC" -ForegroundColor Gray
Write-Host ""
Write-Host "============================================" -ForegroundColor Cyan
# 询问是否启动服务
Write-Host ""
$start = Read-Host "是否立即启动服务端? (y/n)"
if ($start -eq "y") {
Write-Host ""
Write-Host "启动服务端..." -ForegroundColor Green
Set-Location server
cargo run
}