Data_UserGame.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 Info_UserShop
  21. */
  22. public $shopdata;
  23. /**
  24. * @var Info_UserSecretshop 玩家神秘商城数据
  25. */
  26. public $userSecretshop;
  27. /**
  28. * 英雄
  29. * @var Info_UserGameHero
  30. */
  31. public $heros;
  32. /**
  33. * 私有字段
  34. * @var Info_PrivateState
  35. */
  36. public $privateState;
  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. * @var Info_College
  83. */
  84. public $college;
  85. /**
  86. * 用于给各个字段赋默认初始值
  87. * @return \UserGameModel
  88. */
  89. public function initialize() {
  90. my_Assert(GameConfig::primordial_data(), "找不到账号初始化数据"); # 防御
  91. $this->baseInfo = new Info_UserBase();
  92. $this->baseInfo->initialize();
  93. # 添加默认战队设置
  94. $this->heroTeamConfig = JsonUtil::encode(GameConfig::primordial_data()->User_HeroTeamConfig);
  95. $this->store->initialize(); # 添加默认物品
  96. $this->privateState->currentId = count((array) $this->store->equipment) + 1;
  97. $this->heros->InitializeHero(); # 添加初始英雄
  98. $this->pvp = new Info_UserPVP();
  99. // $this->task->initialize(); # 任务初始化
  100. $this->taskCardShop = new Info_TaskCard_Shop();
  101. $this->college->initialize();
  102. }
  103. /**
  104. * 构造函数
  105. * @param type $arg
  106. */
  107. public function __construct($arg = null) {
  108. if (null === $arg) { # 未传参数的情况下
  109. $this->shopdata = ObjectInit(); # 商城数据
  110. $this->privateState = new Info_PrivateState(); # 私有字段
  111. $this->privateState->initialize(); # 初始化默认数据
  112. $this->store = new Info_Store(); # 背包数据
  113. // $this->store->initialize(); # 添加默认物品
  114. $this->heros = new Info_UserGameHero(); # 英雄数据
  115. // $this->heros->InitializeHero(); # 添加初始英雄
  116. $this->gates = new Info_UserGateDifficulty(); # 初始化关卡默认数据
  117. $this->map = new Info_Map(); # 初始化地图解锁数据
  118. // $this->heroManual = new HeroManualModel(); # 初始化图鉴数据结构
  119. $this->NewbieGuide = new Info_NewbieGuide(); # 初始化新手引导结构
  120. // $this->pvp = new UserPVPModel(); # 初始化pvp模块
  121. $this->userSecretshop = new Info_UserSecretshop(); # 神秘商店
  122. // $this->task = new Info_UserTask(); # 任务数据
  123. $this->taskCardShop = new Info_TaskCard_Shop(); # 任务卡商店
  124. $this->college = new Info_College();
  125. } else { # 实参
  126. parent::__construct($arg); # 调用Object的构造函数
  127. // $this->shopdata = new Info_UserShop($this->shopdata);
  128. //// $this->NewbieGuide= new info_us
  129. // $this->baseInfo = new Info_UserBase($this->baseInfo);
  130. // $this->gates = new Info_UserGateDifficulty($this->gates);
  131. // $this->heros = new Info_UserGameHero($this->heros);
  132. // $this->privateState = new Info_PrivateState($this->privateState);
  133. //// $this->profile = n
  134. // $this->pvp = new Info_UserPVP($this->pvp);
  135. // $this->store = new Info_Store($this->store);
  136. // $this->taskCardShop = new Info_TaskCard_Shop($this->taskCardShop);
  137. // $this->userSecretshop = new Info_UserSecretshop($this->userSecretshop);
  138. }
  139. $this->profile = new Data_UserProfile(); # 初始化用户画像模块
  140. }
  141. // <editor-fold defaultstate="collapsed" desc=" functions ">
  142. /**
  143. * 扣除玩家钻石
  144. * @param Info_UserBase $user
  145. * @param int $amt
  146. * @return bool 成功与否
  147. */
  148. static public function Consume_Cash($user, $amt) {
  149. if ($amt >= 0 && isset($user) && isset($user->cash)) {
  150. if ($user->cash - $amt >= 0) {
  151. $user->cash -= $amt;
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. * 扣除玩家金币
  159. * @param Info_UserBase $user
  160. * @param int $amt
  161. * @return boolean true:成功, false:金币不足
  162. */
  163. static public function Consume_Gold($user, $amt) {
  164. if ($amt > 0 && isset($user) && isset($user->gold)) {
  165. if ($user->gold - $amt >= 0) {
  166. $user->gold -= $amt;
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. /**
  173. * 扣除资源点
  174. * @param Info_UserBase $user
  175. * @param type $amt
  176. * @return boolean true:成功, false:资源点不足
  177. */
  178. static public function Consume_ResPoint($user, $amt) {
  179. if ($amt > 0 && isset($user) && isset($user->resPoint)) {
  180. if ($user->resPoint - $amt >= 0) {
  181. $user->resPoint -= $amt;
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. /**
  188. * 扣除玩家友情点
  189. * @param Info_UserBase $user
  190. * @param int $amt
  191. */
  192. static public function Consume_FriendShipPoint($user, $amt) {
  193. if ($amt > 0 && isset($user) && isset($user->friendPoint)) {
  194. if ($user->friendPoint - $amt >= 0) {
  195. $user->friendPoint -= $amt;
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. /**
  202. * 扣除玩家碎片
  203. * @param Data_UserGame $user
  204. * @param int $segmentId 碎片ID
  205. * @param int $amt 数量
  206. * @return boolean true 成功, false 失败
  207. */
  208. static public function Consume_HeroSegment($user, $segmentId, $amt) {
  209. if ($amt > 0 && isset($user)) {
  210. if (CommUtil::isPropertyExists($user->store->items, $segmentId)) {
  211. if ($user->store->items->$segmentId - $amt >= 0) {
  212. $user->store->items->$segmentId -= $amt;
  213. return true;
  214. }
  215. }
  216. }
  217. return false;
  218. }
  219. /**
  220. * 增加用户钻石
  221. * @param Info_UserBase $user
  222. * @param type $amt
  223. * @return type
  224. */
  225. static function Add_Cash($user, $amt) {
  226. my_Assert($user, "user为空");
  227. my_Assert($amt >= 0, "amt值为负");
  228. $user->cash += $amt;
  229. }
  230. /**
  231. * 增加用户友情点
  232. * @param Info_UserBase $user
  233. * @param type $amt
  234. * @return type
  235. */
  236. static function Add_FriendPoint($user, $amt) {
  237. my_Assert($user, "user为空");
  238. my_Assert($amt >= 0, "amt值为负");
  239. $user->friendPoint += $amt; # 业务逻辑
  240. }
  241. /**
  242. * 用户获得经验值
  243. * @param Info_UserBase $user Description
  244. * @param int $amt
  245. */
  246. static function Add_Exp($user, $amt) {
  247. my_Assert($user, "user为空");
  248. my_Assert($amt >= 0, "amt值为负");
  249. $cfgLVs = GameConfig::playerlevel();
  250. $user->xp += $amt;
  251. $initLevel = $curLevel = $user->level;
  252. $nextLevel = $curLevel + 1;
  253. while ($user->xp >= $cfgLVs->$nextLevel->xp_need) { # 超过升级所需经验
  254. if ($user->level < glc()->Game_MaxPlayerLevel) { # 如果未到达最大等级
  255. $user->level++;
  256. $user->xp -= $cfgLVs->$nextLevel->xp_need;
  257. $curLevel = $user->level;
  258. $nextLevel = $curLevel + 1;
  259. my_Assert(CommUtil::isPropertyExists($cfgLVs, $nextLevel), ErrCode::err_const_no); // "取英雄升级常量数据失败." . $nextLevel . "级");
  260. $user->maxXp = $cfgLVs->$nextLevel->xp_need;
  261. // StatProc::UserLevel($nowlv); // 等级统计
  262. // todo:: 发发宝箱奖励, 如果有的话
  263. } else { // 如果已到达最大等级则仅补齐缺失的经验即可
  264. $user->xp = $user->maxXp; # 经验不能超过最大值
  265. break;
  266. }
  267. }
  268. if ($user->level != $initLevel) { # 插入玩家升级的系统消息
  269. SystemProc::UserLevelUp(req()->zoneid, $user, $user->level);
  270. TaskProc::OnUserLevelUp($user->level); # 通知任务模块,这里应该有事件模块
  271. EventProc::OnUserLevelup($initLevel, $user->level); # 事件模块
  272. }
  273. }
  274. /**
  275. * 用户获得金币
  276. * @param Info_UserBase $user 玩家数据
  277. * @param int $amt
  278. */
  279. static function Add_Gold(&$user, $amt) {
  280. my_Assert($user, "参数null");
  281. my_Assert($amt >= 0, "参数为负");
  282. $user->gold += $amt;
  283. }
  284. /**
  285. * 给玩家增加体力
  286. * @param int $amt
  287. * @return type
  288. */
  289. static function Add_tili($amt) {
  290. $user = req()->userInfo->game;
  291. my_Assert($user, "user为空");
  292. my_Assert($amt >= 0, "体力amt小于0");
  293. ActiveProc::ChangeTili($amt);
  294. }
  295. /**
  296. * 增加资源点
  297. * @param Info_UserBase $user
  298. * @param type $amt
  299. */
  300. static function Add_resPoint(&$user, $amt) {
  301. my_Assert($user, "参数null");
  302. my_Assert($amt >= 0, "参数为负");
  303. $user->resPoint += $amt;
  304. }
  305. // </editor-fold>
  306. // <editor-fold defaultstate="collapsed" desc="实例方法">
  307. /**
  308. * 基础信息
  309. * @param bool $save 是否需要回存
  310. * @return Info_UserBase
  311. */
  312. public function base($save = false) {
  313. $this->baseInfo = new Info_UserBase($this->baseInfo);
  314. if ($save) {
  315. self::save_tag("baseInfo");
  316. }
  317. return $this->baseInfo;
  318. }
  319. /**
  320. * 玩家仓库
  321. * @param bool $save 是否需要回存
  322. * @return Info_Store
  323. */
  324. public function store($save = false) {
  325. $this->store = new Info_Store($this->store);
  326. if ($save) {
  327. self::save_tag("store");
  328. }
  329. return $this->store;
  330. }
  331. /**
  332. * 商店数据
  333. * @param bool $save 是否需要回存
  334. * @return Info_UserShop
  335. */
  336. public function shop($save = false) {
  337. $this->shopdata = new Info_UserShop($this->shopdata);
  338. if ($save) {
  339. self::save_tag("shopdata");
  340. }
  341. return $this->shopdata;
  342. }
  343. /**
  344. * 神秘商店
  345. * @param bool $save 是否需要回存
  346. * @return Info_UserSecretshop
  347. */
  348. public function secretshop($save = false) {
  349. $this->userSecretshop = new Info_UserSecretshop($this->userSecretshop);
  350. if ($save) {
  351. self::save_tag("userSecretshop");
  352. }
  353. return $this->userSecretshop;
  354. }
  355. /**
  356. * 英雄数据
  357. * @param bool $save 是否需要回存
  358. * @return Info_UserGameHero
  359. */
  360. public function heros($save = false) {
  361. $this->heros = new Info_UserGameHero($this->heros);
  362. if ($save) {
  363. self::save_tag("heros");
  364. }
  365. return $this->heros;
  366. }
  367. /**
  368. * 私有数据
  369. * @param bool $save 是否需要回存
  370. * @return Info_PrivateState
  371. */
  372. public function private($save = false) {
  373. $this->privateState = new Info_PrivateState($this->privateState);
  374. if ($save) {
  375. self::save_tag("privateState");
  376. }
  377. return $this->privateState;
  378. }
  379. /**
  380. * 关卡数据
  381. * @param bool $save 是否需要回存
  382. * @return Info_UserGateDifficulty
  383. */
  384. public function gates($save = false) {
  385. $this->gates = new Info_UserGateDifficulty($this->gates);
  386. if ($save) {
  387. self::save_tag("gates");
  388. }
  389. return $this->gates;
  390. }
  391. /**
  392. * 地图数据
  393. * @param bool $save 是否需要回存
  394. * @return Info_Map
  395. */
  396. public function map($save = false) {
  397. $this->map = new Info_Map($this->map);
  398. if ($save) {
  399. self::save_tag("map");
  400. }
  401. return $this->map;
  402. }
  403. /**
  404. * 任务卡商店
  405. * @param bool $save 是否需要回存
  406. * @return Info_TaskCard_Shop
  407. */
  408. public function taskCardShop($save = false) {
  409. $this->taskCardShop = new Info_TaskCard_Shop($this->taskCardShop);
  410. if ($save) {
  411. self::save_tag("taskCardShop");
  412. }
  413. return $this->taskCardShop;
  414. }
  415. /**
  416. * pvp数据
  417. * @param bool $save 是否需要回存
  418. * @return Info_UserPVP
  419. */
  420. public function pvp($save = false) {
  421. $this->pvp = new Info_UserPVP($this->pvp);
  422. if ($save) {
  423. self::save_tag("pvp");
  424. }
  425. return $this->pvp;
  426. }
  427. /**
  428. * 角色画像数据
  429. * @param bool $save 是否需要回存
  430. * @return Data_UserProfile
  431. */
  432. public function profile($save = false) {
  433. $this->profile = new Data_UserProfile($this->profile);
  434. if ($save) {
  435. self::save_tag("profile");
  436. }
  437. return $this->profile;
  438. }
  439. /**
  440. * 圣哲学院
  441. * @param bool $save 是否需要回存
  442. * @return Info_College
  443. */
  444. public function college($save = false) {
  445. $this->college = new Info_College($this->college);
  446. if ($save) {
  447. self::save_tag("college");
  448. }
  449. return $this->college;
  450. }
  451. /**
  452. * 新手引导
  453. * @param bool $save 是否需要回存
  454. * @return Info_NewbieGuide
  455. */
  456. public function newbieGuide($save = false) {
  457. $this->NewbieGuide = new Info_NewbieGuide($this->NewbieGuide);
  458. if ($save) {
  459. self::save_tag("NewbieGuide");
  460. }
  461. return $this->NewbieGuide;
  462. }
  463. /**
  464. * 战队配置
  465. * @param bool $save 是否需要回存
  466. * @return Info_NewbieGuide
  467. */
  468. public function &teamConfig($save = false) {
  469. // $this->NewbieGuide = new Info_NewbieGuide($this->NewbieGuide);
  470. if ($save) {
  471. self::save_tag("heroTeamConfig");
  472. }
  473. return $this->heroTeamConfig;
  474. }
  475. //
  476. // </editor-fold>
  477. }