feat: 集成单词对战(wordduel)与数独(numberplace),中心扩到 7 个游戏
- games.json 新增两条进程型配置:wordduel(../en,/wordduel/)与 numberplace(/numberplace/),启动器自动多出两张卡片 - build-games.sh 改为「二进制名:目录名」映射循环,wordduel 住在 ../en - 重写规则全部走代理层,未改游戏源码: - numberplace 的 api(`/api/…`) 反引号/单引号两种写法补 rewrites - wordduel 邀请链接 location.origin + '/#' + c 补前缀 - WS 地址与 index.html 的 href/src 走内置规则 - README 同步路由表、重写规则表、kill -9 兜底命令; 并更正 games.json 需重启中心才生效(config 启动时一次性加载)
This commit is contained in:
1 parent
dd2935366e
commit
53c6018bed
3 files changed
+63
-14
No files matched your search
@@ -7,6 +7,8 @@ http://localhost:1818/ 入口页(只显示状态,点卡片
|
||||
http://localhost:1818/cnchess/ 中国象棋 ← 进程型,冷启动 / 空闲释放
|
||||
http://localhost:1818/cngo/ 围棋 ← 进程型
|
||||
http://localhost:1818/cs/ 枪火对决 ← 进程型
|
||||
http://localhost:1818/wordduel/ 单词对战 ← 进程型(源码在 ../en)
|
||||
http://localhost:1818/numberplace/ 数独 ← 进程型
|
||||
http://localhost:1818/alonelyroad/ 沙漠狂飙 ← 静态目录
|
||||
http://localhost:1818/myword/ 我的世界 ← 静态目录
|
||||
```
|
||||
@@ -21,7 +23,7 @@ http://localhost:1818/myword/ 我的世界 ← 静态目录
|
||||
## 跑起来
|
||||
|
||||
```bash
|
||||
bash scripts/build-games.sh # 第一次:把 cnchess / cngo / cs 编成 release 二进制
|
||||
bash scripts/build-games.sh # 第一次:把 cnchess / cngo / cs / wordduel / numberplace 编成 release 二进制
|
||||
bash run.sh # 起中心,前台跑在 1818
|
||||
```
|
||||
|
||||
@@ -65,7 +67,7 @@ bash run.sh # 起中心,前台跑在 1818
|
||||
- **请求作用域计数**。`Lease` 在进入代理时 `inflight+1`、Drop 时 `-1`,慢请求不会被回收器
|
||||
从中间掐断。
|
||||
- **中心退出即清场**。Ctrl-C / SIGTERM / SIGHUP 都注册了退出处理,先把所有子进程 release 掉再走,
|
||||
不留孤儿。万一被 `kill -9`:`pkill -f 'game/(cnchess|cngo|cs)/target/release'` 收一遍尾巴。
|
||||
不留孤儿。万一被 `kill -9`:`pkill -f 'game/(cnchess|cngo|cs|en|numberplace)/target/release'` 收一遍尾巴。
|
||||
|
||||
## 代理怎么做的
|
||||
|
||||
@@ -82,10 +84,12 @@ hop-by-hop 头,`Host` 改写成 `127.0.0.1:<port>`,`Accept-Encoding: identit
|
||||
|
||||
| 原文 | 重写为 | 出现在 |
|
||||
| --- | --- | --- |
|
||||
| `href="/` `src="/` `action="/` | `…="/cnchess/…` | 三个 Rust 游戏的 index.html |
|
||||
| `href="/` `src="/` `action="/` | `…="/cnchess/…` | 各 Rust 游戏的 index.html |
|
||||
| `fetch("/` | `fetch("/cnchess/…` | cs 取 `/level.json` |
|
||||
| `location.host}/` | `location.host}/cnchess/` | 三个游戏的 `${proto}//${location.host}/ws` |
|
||||
| `location.host}/` | `location.host}/cnchess/` | 各游戏的 `${proto}//${location.host}/ws` |
|
||||
| `url(/` | `url(/cnchess/…` | CSS 里的绝对资源 |
|
||||
| `api(\`/api/` `api('/api/` | `api(\`/numberplace/api/…` | 数独(反引号 / 单引号两种写法都要补) |
|
||||
| `location.origin + '/#' + c` | `location.origin + '/wordduel/#' + c` | 单词对战的邀请链接(`{prefix}` 自带斜杠,`to` 里别再写) |
|
||||
|
||||
另外 `/cnchess`(少个斜杠)会 301 到 `/cnchess/`,否则页面里的相对路径会解析到根。
|
||||
要加新的重写规则,在 `games.json` 里给该游戏写 `rewrites: [["from", "to"]]`,`to` 里可用
|
||||
|
||||
+31
@@ -61,6 +61,37 @@
|
||||
"kind": "static",
|
||||
"dir": "../MyWord",
|
||||
"entry": "index.html"
|
||||
},
|
||||
{
|
||||
"id": "wordduel",
|
||||
"name": "单词对战",
|
||||
"tagline": "双人抢答背单词:拼写 / 释义 / 混合三模式",
|
||||
"icon": "词",
|
||||
"accent": "#8b5cf6",
|
||||
"stack": "Rust · axum",
|
||||
"meta": ["2 人 · 观战", "WebSocket", "4418 词库"],
|
||||
"kind": "process",
|
||||
"cwd": "../en",
|
||||
"bin": "target/release/wordduel",
|
||||
"rewrites": [
|
||||
["location.origin + '/#' + c", "location.origin + '{prefix}#' + c"]
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "numberplace",
|
||||
"name": "数独",
|
||||
"tagline": "四档难度,服务端出题、唯一解保证",
|
||||
"icon": "数",
|
||||
"accent": "#14b8a6",
|
||||
"stack": "Rust · axum",
|
||||
"meta": ["单人", "HTTP/JSON", "提示 / 撤销"],
|
||||
"kind": "process",
|
||||
"cwd": "../numberplace",
|
||||
"bin": "target/release/numberplace",
|
||||
"rewrites": [
|
||||
["api(`/api/", "api(`{prefix}api/"],
|
||||
["api('/api/", "api('{prefix}api/"]
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
+24
-10
@@ -1,27 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# 把三个「进程型」游戏编成 release 单文件二进制。
|
||||
# 游戏中心只在第一次访问时才拉起它们,所以先得有这么三个可执行文件。
|
||||
# 把「进程型」游戏编成 release 单文件二进制。
|
||||
# 游戏中心只在第一次访问时才拉起它们,所以先得有这么些可执行文件。
|
||||
#
|
||||
# 条目格式「二进制名:目录名」——两者不一致时以目录名定位(如 wordduel 住在 ../en)。
|
||||
set -euo pipefail
|
||||
|
||||
here="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
|
||||
for g in cnchess cngo cs; do
|
||||
dir="$here/../$g"
|
||||
games=(
|
||||
cnchess:cnchess
|
||||
cngo:cngo
|
||||
cs:cs
|
||||
wordduel:en
|
||||
numberplace:numberplace
|
||||
)
|
||||
|
||||
for entry in "${games[@]}"; do
|
||||
g="${entry%%:*}"
|
||||
sub="${entry##*:}"
|
||||
dir="$here/../$sub"
|
||||
if [ ! -d "$dir" ]; then
|
||||
echo "!! 找不到 $dir,跳过"
|
||||
echo "!! 找不到 $dir($g),跳过"
|
||||
continue
|
||||
fi
|
||||
echo "==> 构建 $g($(basename "$dir"))"
|
||||
echo "==> 构建 $g($dir)"
|
||||
( cd "$dir" && cargo build --release )
|
||||
done
|
||||
|
||||
echo
|
||||
echo "完成。二进制:"
|
||||
for g in cnchess cngo cs; do
|
||||
bin="$here/../$g/target/release/$g"
|
||||
for entry in "${games[@]}"; do
|
||||
g="${entry%%:*}"
|
||||
sub="${entry##*:}"
|
||||
bin="$here/../$sub/target/release/$g"
|
||||
if [ -x "$bin" ]; then
|
||||
printf ' %-10s %s\n' "$g" "$(du -h "$bin" | cut -f1)"
|
||||
printf ' %-12s %s\n' "$g" "$(du -h "$bin" | cut -f1)"
|
||||
else
|
||||
printf ' %-10s 缺失\n' "$g"
|
||||
printf ' %-12s 缺失\n' "$g"
|
||||
fi
|
||||
done
|
||||
Reference in new issue
Block a user