Data_UserGame.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of UserGameModel
  5. * 游戏数据
  6. * @author gwang (mail@wanggangzero.cn)
  7. */
  8. class Data_UserGame extends HashSaver {
  9. /**
  10. * @var Info_UserBase 基础数据
  11. */
  12. public $baseInfo;
  13. /**
  14. * 玩家仓库
  15. * @var Info_Store
  16. */
  17. public $store;
  18. /**
  19. * 商城
  20. * @var {}
  21. */
  22. public $shopdata;
  23. /**
  24. * 英雄
  25. * @var Info_UserGameHero
  26. */
  27. public $heros;
  28. /**
  29. * 私有字段
  30. * @var Info_PrivateState
  31. */
  32. public $privateState;
  33. /**
  34. * @var Info_UserSecretshop 玩家神秘商城数据
  35. */
  36. public $userSecretshop;
  37. /**
  38. * 关卡
  39. * @var Info_UserGateDifficulty
  40. */
  41. public $gates;
  42. // /**
  43. // * @var Info_UserTask 玩家任务数据
  44. // */
  45. // public $task;
  46. /**
  47. *
  48. * @var Info_UserPVP pvp信息
  49. */
  50. public $pvp;
  51. /**
  52. * 战斗队伍配置信息(使用时必须先json_decode)
  53. * @var dictionary
  54. */
  55. public $heroTeamConfig;
  56. /**
  57. * 新手引导
  58. * @var object
  59. */
  60. public $NewbieGuide;
  61. /**
  62. * 新手引导是否已经结束
  63. * @var type
  64. */
  65. public $NewbieGuideOver = 1;
  66. /**
  67. * @var Data_UserProfile 角色画像
  68. */
  69. public $profile;
  70. /**
  71. * 用于给各个字段赋默认初始值
  72. * @return \UserGameModel
  73. */
  74. public function initialize() {
  75. my_Assert(GameConfig::primordial_data(), "找不到账号初始化数据"); # 防御
  76. $this->baseInfo = new Info_UserBase();
  77. $this->baseInfo->initialize();
  78. # 添加默认战队设置
  79. $this->heroTeamConfig = JsonUtil::encode(GameConfig::primordial_data()->User_HeroTeamConfig);
  80. $this->store->initialize(); # 添加默认物品
  81. $this->privateState->currentId = count((array) $this->store->equipment) + 1;
  82. $this->heros->InitializeHero(); # 添加初始英雄
  83. $this->pvp = new Info_UserPVP();
  84. // $this->task->initialize(); # 任务初始化
  85. }
  86. /**
  87. * 构造函数
  88. * @param type $arg
  89. */
  90. public function __construct($arg = null) {
  91. if (null === $arg) { # 未传参数的情况下
  92. $this->shopdata = ObjectInit(); # 商城数据
  93. $this->privateState = new Info_PrivateState(); # 私有字段
  94. $this->privateState->initialize(); # 初始化默认数据
  95. $this->store = new Info_Store(); # 背包数据
  96. // $this->store->initialize(); # 添加默认物品
  97. $this->heros = new Info_UserGameHero(); # 英雄数据
  98. // $this->heros->InitializeHero(); # 添加初始英雄
  99. $this->gates = new Info_UserGateDifficulty(); # 初始化关卡默认数据
  100. // $this->heroManual = new HeroManualModel(); # 初始化图鉴数据结构
  101. $this->NewbieGuide = ObjectInit(); # 初始化新手引导结构
  102. // $this->pvp = new UserPVPModel(); # 初始化pvp模块
  103. $this->userSecretshop = new Info_UserSecretshop(); # 神秘商店
  104. // $this->task = new Info_UserTask(); # 任务数据
  105. } else { # 实参
  106. parent::__construct($arg); # 调用Object的构造函数
  107. }
  108. $this->profile = new Data_UserProfile(); # 初始化用户画像模块
  109. }
  110. // <editor-fold defaultstate="collapsed" desc=" functions ">
  111. /**
  112. * 设置钻石数
  113. * @param Data_UserGame $user
  114. * @param int $amt 数量
  115. */
  116. static public function set_Cash($user, $amt) {
  117. my_Assert($amt >= 0, "数量小于0");
  118. my_Assert($user, "user为空");
  119. my_Assert(isset($user->cash), "找不到cash字段");
  120. $user->cash = $amt; # 实际逻辑
  121. }
  122. /**
  123. * 扣除玩家晶石
  124. * @param Data_UserGame $user
  125. * @param int $amt
  126. */
  127. static public function Consume_Spar($user, $amt) {
  128. if ($amt > 0 && isset($user) && isset($user->spar)) {
  129. if ($user->spar - $amt >= 0) {
  130. $user->spar -= $amt;
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. /**
  137. * 扣除玩家钻石
  138. * @param Data_UserGame $user
  139. * @param int $amt
  140. * @return bool 成功与否
  141. */
  142. static public function Consume_Cash($user, $amt) {
  143. if ($amt >= 0 && isset($user) && isset($user->cash)) {
  144. if ($user->cash - $amt >= 0) {
  145. $user->cash -= $amt;
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. /**
  152. * 扣除玩家金币
  153. * @param Data_UserGame $user
  154. * @param int $amt
  155. */
  156. static public function Consume_Gold($user, $amt) {
  157. if ($amt > 0 && isset($user) && isset($user->gold)) {
  158. if ($user->gold - $amt >= 0) {
  159. $user->gold -= $amt;
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. /**
  166. * 扣除玩家友情点
  167. * @param Data_UserGame $user
  168. * @param int $amt
  169. */
  170. static public function Consume_FriendShipPoint($user, $amt) {
  171. if ($amt > 0 && isset($user) && isset($user->friendPoint)) {
  172. if ($user->friendPoint - $amt >= 0) {
  173. $user->friendPoint -= $amt;
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. /**
  180. * 扣除玩家碎片
  181. * @param Data_UserGame $user
  182. * @param int $segmentId 碎片ID
  183. * @param int $amt 数量
  184. * @return boolean true 成功, false 失败
  185. */
  186. static public function Consume_HeroSegment($user, $segmentId, $amt) {
  187. if ($amt > 0 && isset($user)) {
  188. if (CommUtil::isPropertyExists($user->store->items, $segmentId)) {
  189. if ($user->store->items->$segmentId - $amt >= 0) {
  190. $user->store->items->$segmentId -= $amt;
  191. return true;
  192. }
  193. }
  194. }
  195. return false;
  196. }
  197. /**
  198. * 增加用户钻石
  199. * @param Data_UserGame $user
  200. * @param type $amt
  201. * @return type
  202. */
  203. static function Add_Cash($user, $amt) {
  204. my_Assert($user, "user为空");
  205. my_Assert($amt >= 0, "amt值为负");
  206. $user->cash += $amt;
  207. }
  208. /**
  209. * 增加用户友情点
  210. * @param Data_UserGame $user
  211. * @param type $amt
  212. * @return type
  213. */
  214. static function Add_FriendPoint($user, $amt) {
  215. my_Assert($user, "user为空");
  216. my_Assert($amt >= 0, "amt值为负");
  217. $user->friendPoint += $amt; # 业务逻辑
  218. }
  219. /**
  220. * 用户获得经验值
  221. * @param Req $req Description
  222. * @param int $amt
  223. */
  224. static function Add_Exp($user, $amt) {
  225. my_Assert($user, "user为空");
  226. my_Assert($amt >= 0, "amt值为负");
  227. $cfgLVs = GameConfig::playerlevel();
  228. $user->xp += $amt;
  229. $initLevel = $curLevel = $user->level;
  230. $nextLevel = $curLevel + 1;
  231. while ($user->xp >= $cfgLVs->$nextLevel->xp_need) { # 超过升级所需经验
  232. if ($user->level < glc()->Game_MaxPlayerLevel) { # 如果未到达最大等级
  233. $user->level++;
  234. $user->xp -= $cfgLVs->$nextLevel->xp_need;
  235. $curLevel = $user->level;
  236. $nextLevel = $curLevel + 1;
  237. my_Assert(CommUtil::isPropertyExists($cfgLVs, $nextLevel), ErrCode::err_const_no); // "取英雄升级常量数据失败." . $nextLevel . "级");
  238. $user->maxXp = $cfgLVs->$nextLevel->xp_need;
  239. // StatProc::UserLevel($nowlv); // 等级统计
  240. // todo:: 发发宝箱奖励, 如果有的话
  241. } else { // 如果已到达最大等级则仅补齐缺失的经验即可
  242. $user->xp = $user->maxXp; # 经验不能超过最大值
  243. break;
  244. }
  245. }
  246. if ($user->level != $initLevel) { # 插入玩家升级的系统消息
  247. SystemProc::UserLevelUp(req()->zoneid, $user, $user->level);
  248. TaskProc::OnUserLevelUp($user->level); # 通知任务模块,这里应该有事件模块
  249. }
  250. }
  251. /**
  252. * 用户获得金币
  253. * @param Data_UserGame $user 玩家数据
  254. * @param int $amt
  255. */
  256. static function Add_Gold(&$user, $amt) {
  257. my_Assert($user, "参数null");
  258. my_Assert($amt >= 0, "参数为负");
  259. $user->gold += $amt;
  260. }
  261. /**
  262. * 给玩家增加体力
  263. * @param Req $req
  264. * @param int $amt
  265. * @return type
  266. */
  267. static function Add_tili($req, $amt) {
  268. $user = $req->userInfo->game;
  269. my_Assert($user, "user为空");
  270. my_Assert($amt >= 0, "体力amt小于0");
  271. ActiveProc::ChangeTili($amt);
  272. }
  273. // </editor-fold>
  274. // <editor-fold defaultstate="collapsed" desc="私有方法">
  275. //
  276. // </editor-fold>
  277. }