feat: 实现资源(图片、音频)查询,完善README

This commit is contained in:
2026-01-20 09:34:24 +08:00
parent 70574c8a77
commit 9dce8988b3
10 changed files with 107 additions and 12 deletions
+61 -6
View File
@@ -1,14 +1,54 @@
# 后端
# 纸片化学社区版:数据后端
## 项目介绍
纸片化学社区版Paper Chemis Community是由 Tiger 开发的一款纸片化学游戏。该游戏使用纸片化学玩法,并带来了更高的自由度。
本项目为纸片化学社区版的数据后端,为游戏本体提供数据源服务。
## 项目结构
```text
backend/
- run.py
- test/
- test_*.py
- app/
- __init__.py
- models/
- routes/
- assets.py
- cards.py
- matters.py
- pages.py
- reactions.py
- utils/
- assets.py
- cards.py
- matters.py
- reactions.py
- resources/
- assets/
- index.json
- pics/
- sounds/
- cards/
- list.json
- conditions/
- list.json
- matters/
- list.json
- reactions/
- list.json
- match.json
```
## 部分文件格式
### list.json
`cards` `matters``reactions` 下的 `list.json` 有相同的格式。每个文件中都只有一个对象。对象的键的内容均是字符串,代表 ID。对象的值的内容均是字符串代表对应JSON文件的相对路径不用加 `.json` 后缀名。例如:
`cards` `matters``reactions` `conditions` 下的 `list.json` 有相同的格式。每个文件中都只有一个对象。对象的键的内容均是字符串,代表 ID。对象的值的内容均是字符串代表对应JSON文件的相对路径不用加 `.json` 后缀名。例如:
```json
{
@@ -18,14 +58,29 @@
}
```
### assets/index.json
### reactions/match.json
`assets` 目录下的 `index.json` 是资源文件的索引目录文件。该文件的内容是一个对象,每一个键值对对应一个资源文件。键对应的是资源的 ID值对应的是资源的相对文件路径不加 `.json` 后缀名。例如:
`reactions` 目录下的 `match.json` 支持通过反应物匹配反应。该文件的内容是一个对象。对象的键由反应物构成,每个反应物(包括最后一个)后面都有一个 `-` 以间隔开各项反应物。值是反应的 ID`list.json` 中对象的键。例如:
```json
{
"Carbon": "cards/carbon.png",
"Carbon-Oxygen-": "CarbonDioxide"
}
```
### assets/index.json
`assets` 目录下的 `index.json` 是资源文件的索引目录文件。该文件的内容是一个对象,每一个键值对对应一个资源文件。键对应的是资源的 ID值对应的是资源的相对文件路径不加 `pics/``sounds/` 前缀。例如:
```json
{
"pics": {
"Carbon": "cards/Carbon.png",
"Oxygen": "cards/Oxygen.png"
},
"sounds": {
// ...
}
}
```
@@ -43,4 +98,4 @@
对于路径和 ID均应当使用英文字母、下划线和数字不应当使用包括中文和短横杠在内的其他字符。由此引发的问题后果自负。
所有 JSON 文件中的缩进均应当使用2个或4个空格,而非制表符(`\t`)。
所有 JSON 文件中的缩进均应当使用 2 个或 4 个空格,而非制表符(`\t`)。
+3
View File
@@ -17,4 +17,7 @@ def create_app(config_class='config.Config'):
from app.routes.matters import matter_bp
app.register_blueprint(matter_bp, url_prefix="/matter")
from app.routes.assets import asset_bp
app.register_blueprint(asset_bp, url_prefix="/asset")
return app
+14
View File
@@ -0,0 +1,14 @@
from flask import Blueprint, send_file
from ..utils import assets
asset_bp = Blueprint("asset", __name__, "/asset")
@asset_bp.route("/pic/<id>")
def search_matter(id: str):
path = "../resources/assets/pics/" + assets.readPicPath(id)
return send_file(path)
@asset_bp.route("/sound/<id>")
def select_matter(id: str):
path = "../resources/assets/sounds/" + assets.readSoundPath(id)
return send_file(path)
+16
View File
@@ -0,0 +1,16 @@
import json
def readAssetsList() -> dict:
with open("resources/assets/index.json", "r", encoding="utf8") as f:
text = f.read()
result = json.loads(text)
return result
assets_list = readAssetsList()
def readPicPath(id: str):
return assets_list["pics"][id]
def readSoundPath(id: str):
return assets_list["sounds"][id]
+3 -1
View File
@@ -7,8 +7,10 @@ def readCardList() -> dict:
return result
card_list = readCardList()
def readCard(id: str):
path = readCardList().get(id, -1)
path = card_list.get(id, -1)
if (path == -1):
return {"message": f"No Card {id}"}
with open(f"resources/cards/{path}.json", "r", encoding="utf8") as f:
+5 -2
View File
@@ -14,8 +14,11 @@ def readMatchList() -> dict:
return result
matter_list = readMatterList()
match_list = readMatchList()
def readMatter(id: str):
path = readMatterList().get(id, -1)
path = matter_list.get(id, -1)
if (path == -1):
return {"message": f"No Matter {id}"}
with open(f"resources/matters/{path}.json", "r", encoding="utf8") as f:
@@ -25,7 +28,7 @@ def readMatter(id: str):
return {"message": "OK", "content": result}
def selectMatter(id: str):
matter_id = readMatchList().get(id, -1)
matter_id = match_list.get(id, -1)
if (matter_id == -1):
return {"message": f"No Matter {id}"}
+3 -1
View File
@@ -7,8 +7,10 @@ def readReactionList() -> dict:
return result
reaction_list = readReactionList()
def readReaction(id: str):
path = readReactionList().get(id, -1)
path = reaction_list.get(id, -1)
if (path == -1):
return {"message": f"No Reaction {id}"}
with open(f"resources/reactions/{path}.json", "r", encoding="utf8") as f:
+1 -1
View File
@@ -1,6 +1,6 @@
{
"pics": {
"Oxygen": "Oxygen.png"
},
"sounds": {
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File