游戏服务器设计之属性管理器

  游戏中角色拥有的属性值很多,运营多年的游戏,往往会有很多个成长线,每个属性都有可能被N个成长线模块增减数值。举例当角色戴上武器时候hp+100点,卸下武器时HP-100点,这样加减逻辑只有一处还比较好控制,如果某天有个特殊功能当被某技能攻击时,角色武器会被击落,这样就会出现减数值的操作不止一处。如果逻辑处理不当,比如击落的时候没有恰当的减数值,再次穿戴武器就导致属性值加了两边,也就是玩家经常说的刷属性。这种bug对游戏平衡性影响很大,反响很恶劣,bug又很难被测试发现。本文将介绍一种管理属性的思路,最大限度的避免此类bug,如果出现bug,也能够很好的排查。

设计思路

  刷属性bug的核心原因是某功能的模块数值加了N次,所以各个模块加的属性要被记录,加过了必须不能重复加。设计这样的数据结构。

//!各个属性对应一个总值
//!各个属性对应各个模块的分值
template<typename T>
class PropCommonMgr
{
public:
typedef T ObjType;
typedef int64_t (*functorGet)(ObjType);
typedef void (*functorSet)(ObjType, int64_t);
struct PropGetterSetter
{
PropGetterSetter():fGet(NULL), fSet(NULL){}
functorGet fGet;
functorSet fSet;
std::map<std::string, int64_t> moduleRecord;
};
void regGetterSetter(const std::string& strName, functorGet fGet, functorSet fSet){
PropGetterSetter info;
info.fGet = fGet;
info.fSet = fSet;
propName2GetterSetter[strName] = info;
}
public:
std::map<std::string, PropGetterSetter> propName2GetterSetter;
};
  1. 关于数据结构的get和set,我们为每个属性命名一个名字,这样处理数据的时候会非常方便(比如道具配增加属性等等),角色属性有很多种,这里不能一一定义,所以属性管理器只是映射属性,并不创建属性值。通过regGetterSetter接口,注册get和set的操作映射。为什么不需要提供add和sub接口能,因为add和sub可以通过get和set组合实现。get和set的接口实现如下:
int64_t get(ObjType obj, const std::string& strName) {
typename std::map<std::string, PropGetterSetter>::iterator it = propName2GetterSetter.find(strName);
if (it != propName2GetterSetter.end() && it->second.fGet){
return it->second.fGet(obj);
}
return 0;
}
bool set(ObjType obj, const std::string& strName, int64_t v) {
typename std::map<std::string, PropGetterSetter>::iterator it = propName2GetterSetter.find(strName);
if (it != propName2GetterSetter.end() && it->second.fSet){
it->second.fSet(obj, v);
return true;
}
return false;
}
  1. 关于add和sub,前面提到要避免刷属性,就必须避免重复加属性。所以每个模块再加属性前必须检查一下是否该模块已经加了属性,如果加过一定要先减后加。因为每次模块加属性都记录在属性管理器中,那么减掉的数值一定是正确的。这样可以避免另外一种常见bug,如加了100,减的时候计算错误减了80,也会积少成多造成刷属性。add和sub的代码如下:
int64_t addByModule(ObjType obj, const std::string& strName, const std::string& moduleName, int64_t v) {
typename std::map<std::string, PropGetterSetter>::iterator it = propName2GetterSetter.find(strName);
if (it != propName2GetterSetter.end() && it->second.fGet && it->second.fSet){
int64_t ret =it->second.fGet(obj);
std::map<std::string, int64_t>::iterator itMod = it->second.moduleRecord.find(moduleName);
if (itMod != it->second.moduleRecord.end()){
ret -= itMod->second;
itMod->second = v;
}
else{
it->second.moduleRecord[moduleName] = v;
}
ret += v;
it->second.fSet(obj, ret);
return ret;
}
return 0;
}
int64_t subByModule(ObjType obj, const std::string& strName, const std::string& moduleName) {
typename std::map<std::string, PropGetterSetter>::iterator it = propName2GetterSetter.find(strName);
if (it != propName2GetterSetter.end() && it->second.fGet && it->second.fSet){
int64_t ret =it->second.fGet(obj);
std::map<std::string, int64_t>::iterator itMod = it->second.moduleRecord.find(moduleName);
if (itMod == it->second.moduleRecord.end()){
return ret;
}
ret -= itMod->second;
it->second.moduleRecord.erase(itMod);
it->second.fSet(obj, ret);
return ret;
}
return 0;
}
int64_t getByModule(ObjType obj, const std::string& strName, const std::string& moduleName) {
typename std::map<std::string, PropGetterSetter>::iterator it = propName2GetterSetter.find(strName);
if (it != propName2GetterSetter.end() && it->second.fGet && it->second.fSet){
int64_t ret =it->second.fGet(obj);
std::map<std::string, int64_t>::iterator itMod = it->second.moduleRecord.find(moduleName);
if (itMod != it->second.moduleRecord.end()){
return itMod->second;
}
}
return 0;
}
std::map<std::string, int64_t> getAllModule(ObjType obj, const std::string& strName) {
std::map<std::string, int64_t> ret;
typename std::map<std::string, PropGetterSetter>::iterator it = propName2GetterSetter.find(strName);
if (it != propName2GetterSetter.end() && it->second.fGet && it->second.fSet){
ret = it->second.moduleRecord;
}
return ret;
}

  如上代码所示,addByModule和subByModule必须提供模块名,比如穿装备的时候加血量:addByModule('HP', 'Weapon', 100),而卸下武器的时候只要subByModule('HP', 'Weapon'),因为属性管理器知道减多少。

总结

  1. 属性提供一个名字映射有很多好处,比如装备配属性,buff配属性的,有名字相关联会特别方便
  2. 提供一个get和set接口的映射,这样属性管理器就和具体的对象的属性字段解耦了。即使是现有的功能模块也可以集成这个属性管理器。
  3. 属性的add和sub操作,都在属性管理器中留下记录,这样即使出现问题,通过getByModule getAllModule两个接口亦可以辅助查找问题。
  4. 属性管理已经集成到H2Engine中,github地址: https://github.com/fanchy/h2engine

H2Engine游戏服务器设计之属性管理器的更多相关文章

  1. h2engine游戏服务器设计之聊天室示例

    游戏服务器设计之聊天室示例 简介 h2engine引擎建群以后,有热心网友向我反馈,想尝试h2engine但是没有服务器开发经验觉得无从入手,希望我能提供一个简单明了的示例.由于前一段时间工作实在忙碌 ...

  2. 游戏服务器设计之NPC系统

    游戏服务器设计之NPC系统 简介 NPC系统是游戏中非常重要的系统,设计的好坏很大程度上影响游戏的体验.NPC在游戏中有如下作用: 引导玩家体验游戏内容,一般游戏内有很多主线.支线任务,而任务的介绍. ...

  3. JMeter学习(二十五)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults

    Test Plan的配置元件中有一些和HTTP属性相关的元件:HTTP Cache Manager.HTTP Authorization Manager.HTTP Cookie Manager.HTT ...

  4. 【jmeter】HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults

    Test Plan的配置元件中有一些和HTTP属性相关的元件:HTTP Cache Manager.HTTP Authorization Manager.HTTP Cookie Manager.HTT ...

  5. HTTP属性管理器详解

      1)HTTP Cache Manager 2)HTTP Cookie 管理器 3)HTTP 信息头管理器 4)HTTP 授权管理器 5)HTTP 请求默认值 为什么会有这些http属性的配置元件? ...

  6. HTTP属性管理器 初探

      1)HTTP Cache Manager 2)HTTP Cookie 管理器 3)HTTP 信息头管理器 4)HTTP 授权管理器 5)HTTP 请求默认值 为什么会有这些http属性的配置元件? ...

  7. <转>jmeter(十九)HTTP属性管理器

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  8. jmeter(十九)HTTP属性管理器

    jmeter是一个开源灵活的接口和性能测试工具,当然也能利用jmeter进行接口自动化测试.在我们利用它进行测试过程中,最常用的sampler大概就是Http Request, 使用这个sampler ...

  9. JMeter学习(二十四)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults(转载)

    转载自 http://www.cnblogs.com/yangxia-test Test Plan的配置元件中有一些和HTTP属性相关的元件:HTTP Cache Manager.HTTP Autho ...

随机推荐

  1. 【本地资源路径&&网络资源路径&&正反斜杠在Java中的用法】

    一.概念和用法 左正右反 先来看看转义字符的概念:通过 \ ,?来转变后面字母或符号的含义.意思就是改变字母本身的含义. 以"\"符号为例,JAVA中有很多操作,例如文件操作等,需 ...

  2. python爬虫(六)_urllib2:handle处理器和自定义opener

    本文将介绍handler处理器和自定义opener,更多内容请参考:python学习指南 opener和handleer 我们之前一直使用的是urllib2.urlopen(url)这种形式来打开网页 ...

  3. elasticsearch 学习笔记

    基本概念 集群和节点的概念1.集群是由节点组成的2.每个集群都有唯一的名字默认是elasticsearch3.cluster.name: niubiwali //集群的名字很重要因为每个节点只是集群的 ...

  4. 基于Handler架构的录音程序

    近期我的app须要一个录音功能,于是搜到这篇文章 文章中录音线程与主线程间的通讯是通过内部类訪问外部类成员变量的方式来实现 while (isRecord == true) { //isRecord是 ...

  5. Android View视图系统分析和Scroller和OverScroller分析

    Android  View视图系统分析和Scroller和OverScroller分析 View  视图分析         首先,我们知道.在Android中全部的视图资源(无论是Layout还是V ...

  6. 聊聊属性方法property的用法

    写之前随便百度了一下博客,又看到廖雪峰的博客了.果然置顶的能力很强. 我想说其实property的用法并不是主要用来做类型检查.反而更多应该是用于简化操作的目的. 写之前想聊一个古老的话题.年初的时候 ...

  7. Intellij IDEA 安装和配置jrebel进行项目的热部署

    Jrebel 先介绍一下jrebel,jrebel是可以热部署项目的一个工具,更改代码自动部署并不需要重启项目(在spring中的controller中,增加.修改方法都是可以进行热部署而不需要重启的 ...

  8. iOS开发之JSON转PLIST(把存储json格式的文件转换成plist文件)

    p.p1 { margin: 0.0px 0.0px 0.0px 60.0px; font: 16.0px "PingFang SC"; color: #586e75 } p.p2 ...

  9. On-Heap与Off-Heap

    和C#里的托管代码.非托管代码类似

  10. APP闪退问题

    1.iOS-中app启动闪退的原因 2.iOS开发-闪退问题-解决之前上架的 App 在 iOS 9 会闪退问题 3.iOS-应用闪退总结 4.iOS开发-捕获程序崩溃日志 5.iOS开发-应用崩溃日 ...