Data_UserGame.php 11 KB

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