Data_UserGame.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. * @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 = ObjectInit(); # 初始化新手引导结构
  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 Data_UserGame $user
  145. * @param int $amt 数量
  146. */
  147. static public function set_Cash($user, $amt) {
  148. my_Assert($amt >= 0, "数量小于0");
  149. my_Assert($user, "user为空");
  150. my_Assert(isset($user->cash), "找不到cash字段");
  151. $user->cash = $amt; # 实际逻辑
  152. }
  153. /**
  154. * 扣除玩家晶石
  155. * @param Data_UserGame $user
  156. * @param int $amt
  157. */
  158. static public function Consume_Spar($user, $amt) {
  159. if ($amt > 0 && isset($user) && isset($user->spar)) {
  160. if ($user->spar - $amt >= 0) {
  161. $user->spar -= $amt;
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. /**
  168. * 扣除玩家钻石
  169. * @param Data_UserGame $user
  170. * @param int $amt
  171. * @return bool 成功与否
  172. */
  173. static public function Consume_Cash($user, $amt) {
  174. if ($amt >= 0 && isset($user) && isset($user->cash)) {
  175. if ($user->cash - $amt >= 0) {
  176. $user->cash -= $amt;
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. /**
  183. * 扣除玩家金币
  184. * @param Data_UserGame $user
  185. * @param int $amt
  186. */
  187. static public function Consume_Gold($user, $amt) {
  188. if ($amt > 0 && isset($user) && isset($user->gold)) {
  189. if ($user->gold - $amt >= 0) {
  190. $user->gold -= $amt;
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. /**
  197. * 扣除玩家友情点
  198. * @param Data_UserGame $user
  199. * @param int $amt
  200. */
  201. static public function Consume_FriendShipPoint($user, $amt) {
  202. if ($amt > 0 && isset($user) && isset($user->friendPoint)) {
  203. if ($user->friendPoint - $amt >= 0) {
  204. $user->friendPoint -= $amt;
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. /**
  211. * 扣除玩家碎片
  212. * @param Data_UserGame $user
  213. * @param int $segmentId 碎片ID
  214. * @param int $amt 数量
  215. * @return boolean true 成功, false 失败
  216. */
  217. static public function Consume_HeroSegment($user, $segmentId, $amt) {
  218. if ($amt > 0 && isset($user)) {
  219. if (CommUtil::isPropertyExists($user->store->items, $segmentId)) {
  220. if ($user->store->items->$segmentId - $amt >= 0) {
  221. $user->store->items->$segmentId -= $amt;
  222. return true;
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. /**
  229. * 增加用户钻石
  230. * @param Data_UserGame $user
  231. * @param type $amt
  232. * @return type
  233. */
  234. static function Add_Cash($user, $amt) {
  235. my_Assert($user, "user为空");
  236. my_Assert($amt >= 0, "amt值为负");
  237. $user->cash += $amt;
  238. }
  239. /**
  240. * 增加用户友情点
  241. * @param info_userbase $user
  242. * @param type $amt
  243. * @return type
  244. */
  245. static function Add_FriendPoint($user, $amt) {
  246. my_Assert($user, "user为空");
  247. my_Assert($amt >= 0, "amt值为负");
  248. $user->friendPoint += $amt; # 业务逻辑
  249. }
  250. /**
  251. * 用户获得经验值
  252. * @param info_userbase $user Description
  253. * @param int $amt
  254. */
  255. static function Add_Exp($user, $amt) {
  256. my_Assert($user, "user为空");
  257. my_Assert($amt >= 0, "amt值为负");
  258. $cfgLVs = GameConfig::playerlevel();
  259. $user->xp += $amt;
  260. $initLevel = $curLevel = $user->level;
  261. $nextLevel = $curLevel + 1;
  262. while ($user->xp >= $cfgLVs->$nextLevel->xp_need) { # 超过升级所需经验
  263. if ($user->level < glc()->Game_MaxPlayerLevel) { # 如果未到达最大等级
  264. $user->level++;
  265. $user->xp -= $cfgLVs->$nextLevel->xp_need;
  266. $curLevel = $user->level;
  267. $nextLevel = $curLevel + 1;
  268. my_Assert(CommUtil::isPropertyExists($cfgLVs, $nextLevel), ErrCode::err_const_no); // "取英雄升级常量数据失败." . $nextLevel . "级");
  269. $user->maxXp = $cfgLVs->$nextLevel->xp_need;
  270. // StatProc::UserLevel($nowlv); // 等级统计
  271. // todo:: 发发宝箱奖励, 如果有的话
  272. } else { // 如果已到达最大等级则仅补齐缺失的经验即可
  273. $user->xp = $user->maxXp; # 经验不能超过最大值
  274. break;
  275. }
  276. }
  277. if ($user->level != $initLevel) { # 插入玩家升级的系统消息
  278. SystemProc::UserLevelUp(req()->zoneid, $user, $user->level);
  279. TaskProc::OnUserLevelUp($user->level); # 通知任务模块,这里应该有事件模块
  280. EventProc::OnUserLevelup($initLevel, $user->level); # 事件模块
  281. }
  282. }
  283. /**
  284. * 用户获得金币
  285. * @param Data_UserGame $user 玩家数据
  286. * @param int $amt
  287. */
  288. static function Add_Gold(&$user, $amt) {
  289. my_Assert($user, "参数null");
  290. my_Assert($amt >= 0, "参数为负");
  291. $user->gold += $amt;
  292. }
  293. /**
  294. * 给玩家增加体力
  295. * @param Req $req
  296. * @param int $amt
  297. * @return type
  298. */
  299. static function Add_tili($req, $amt) {
  300. $user = $req->userInfo->game;
  301. my_Assert($user, "user为空");
  302. my_Assert($amt >= 0, "体力amt小于0");
  303. ActiveProc::ChangeTili($amt);
  304. }
  305. // </editor-fold>
  306. // <editor-fold defaultstate="collapsed" desc="私有方法">
  307. //
  308. // </editor-fold>
  309. }