Data_UserGame.php 11 KB

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