# 角色扮演游戏
***
本章用 MongoDB 演示了如何设计角色扮演游戏的数据库结构,准确来说是具备了角色扮演游戏的基本特征。实际上相关的设计要更复杂和庞大。
首先要考虑存储玩家角色的基本属性。
**角色属性**:主要的固定属性,如力量,敏捷,智力等参数,还有一些临时性的属性,比如 buff 和 Debuff(角色固定属性的增强和削弱,可能是临时性或永久性)
**背包**:背包中携带的装备及已经被角色装备中的武器,防具等
**地理位置**:在游戏中角色所处的地理位置。包含地图的入口出口等信息
**日常信息**:组队状态,工会信息,金币,元宝余额等
考虑到以上信息的随时变动,以及同时在线人数。这些数据对读写延时都必须降到最低,保证在游戏期间的响应速度。
### MongoDB 设计角色集合
~~~json
{
"_id": "ObjectId(...)",
"name": "血染沙巴克",
"class": "Warrior",
"level": 54,
"health": [600, 600],
"mana": [80, 80],
"gold": 57119223,
"point": 100,
"properties": {
"force": 100,
"dexterity": 60,
"intelligence": 20
},
// "additional": {},
"additional": {
"speed": [100, 30]
// "speed": [-100, 30]
},
"location": {
"x": 238,
"y": 195
},
"map": {
"_id": "ObjectId(...)",
"name": "封魔谷",
"description": "封魔谷",
"exits":{"e": "ObjectId(...)", "w": "ObjectId(...)", "s": "ObjectId(...)", "n": "ObjectId(...)"}
},
// "guild": {},
"guild": {
"_id": "ObjectId(...)",
"name": "葬爱家族"
},
// "team": {},
"team": {
"leader": {
"_id": "ObjectId(...)",
"name": "blablabla"
},
"members": [
{"_id": "ObjectId(...)", "name": "blablabla"},
{"_id": "ObjectId(...)", "name": "血染沙巴克"},
{"_id": "ObjectId(...)", "name": "blablabla2"}
]
},
"armor": [
{"_id": "595b60a5917e874077232d81", "region": "head"},
{"_id": "595b60bb917e8768075db171", "region": "left wrist"},
{"_id": "595b61c4917e87736a72f082", "region": "neck"},
{"_id": null, "region": "right wrist"},
{"_id": null, "region": "left hand"},
{"_id": "595b60ca917e87736a72f081", "region": "right hand"},
{"_id": "595b611e917e87409779a771", "region": "body"},
{"_id": null, "region": "foot"}
],
"weapons": [
{"_id": "595b6051917e87407f617ed1", "hand": "both"}
],
"backpack": [
{"_id": "595b6051917e87407f617ed1", "name": "屠龙", "number": 1, "parameters": {"solidity": 55, "complete": 60, "attack": 110}},
{"_id": "595b61c4917e87736a72f082", "name": "圣战项链", "number": 1, "parameters": {"solidity": 10, "complete": 14}},
{"_id": "595b60bb917e8768075db171", "name": "圣战手镯", "number": 1, "parameters": {"solidity": 15, "complete": 22}},
{"_id": "595b60ca917e87736a72f081", "name": "圣战戒指", "number": 1, "parameters": {"solidity": 10, "complete": 11}},
{"_id": "595b60a5917e874077232d81", "name": "圣战头盔", "number": 1, "parameters": {"solidity": 10, "complete": 17}},
{"_id": "595b611e917e87409779a771", "name": "天魔神甲(男)", "number": 1, "parameters": {"solidity": 71, "complete": 75}},
{"_id": "595b62ae917e8740bb77d761", "name": "乌木剑", "number": 1, "parameters": {"solidity": 11, "complete": 11, "attack": 7}},
{"_id": "595b6311917e875c1f1fa7a1", "name": "乌木剑", "number": 1, "parameters": {"solidity": 12, "complete": 12, "attack": 7}},
{"_id": "595b6318917e8768075db172", "name": "万年雪霜(捆)", "number": 1, "parameters": {"health": 400, "mana": 120, "gold": 1200}},
{"_id": "595b631c917e8742836885a1", "name": "万年雪霜", "number": 7, "parameters": {"health": 400, "mana": 120, "gold": 200}}
]
}
~~~
接下来我们来逐个介绍一下集合的字段的用途。
#### 基础信息
| 列名 | 描述 |
| --- | --- |
| id | 主键 |
| name | 昵称 |
| class | 职业 |
| level | 等级 |
| health | 血量(满/当前) |
| mana | 魔法量(满/当前) |
| gold | 金币 |
| point | 元宝 |
| properties.force | 力量 |
| properties.dexterity | 敏捷 |
| properties.intelligence | 智慧 |
| additional.speed | 附加属性(影响值,持续时间) |
#### 位置与地图
| 列名 | 描述 |
| --- | --- |
| location.x | 横向坐标 |
| location.y | 纵向坐标 |
| map._id | 地图主键 |
| map.name | 地图名称 |
| map.description | 地图描述 |
| map.exits | 地图出口 |
#### 行会与团队
| 列名 | 描述 |
| --- | ---
| guild._id | 家族主键 |
| guild.name | 家族名称 |
| team.leader._id | 团队队长用户主键 |
| team.leader.name | 团队队长名称 |
| team.members | 团队成员数组 |
#### 装备和背包
| 列名 | 描述 |
| --- | ---
| armor | 装备 |
| weapons | 武器 |
| backpack | 背包 |