ActiveProc.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. <?php
  2. namespace loyalsoft;
  3. require_once __DIR__ . '/ActiveProc/CipheredBase32.php'; # 算法库
  4. /**
  5. * 活动模块
  6. */
  7. class ActiveProc {
  8. /**
  9. * 兑换码礼包使用记录
  10. */
  11. const tab_toke_gift = 'tab_token_gift';
  12. /**
  13. * 逻辑分发
  14. * 所有的Proc中必须有这样一个方法
  15. */
  16. public static function procMain() {
  17. switch (req()->cmd) {
  18. case CmdCode::active_day7_drawreward: # 6502 领取七日签到奖励
  19. return self::Day7_DrawReward();
  20. case CmdCode::active_getzonePublicTs: # 6503 查询开服时间戳
  21. return ActiveProc::GetZonePublicTS();
  22. case CmdCode::active_getTodayOnlineInfos: # 6504 查询当天在线时长
  23. return self::getTotayOnlineInfos();
  24. case CmdCode::Task_Tili: # 6508 体力变化
  25. return ActiveProc::RecoveryTili();
  26. case CmdCode::active_token_drawReward: # 6512 兑换码礼包
  27. return self::drawActivePackageByCode();
  28. case CmdCode::active_draw_onlinegift: # 6513 领取在线(时长)礼包
  29. return self::drawOnlineGift();
  30. case CmdCode::active_draw_reggift: # 6514 领取全服注册礼包
  31. return self::drawRegGift();
  32. case CmdCode::active_presentTili: # 6515 领取在线赠送体力
  33. return self::drawTiliGift();
  34. case CmdCode::active_get_regnum: # 6516 查询当前全服注册人数
  35. return self::getRegNumber();
  36. case CmdCode::active_shenmiaoDrawPrize: # 6505 什么抽奖
  37. return self::shenmiaoDrawPrize();
  38. case CmdCode::active_GetShenmiaoData:
  39. return self::GetShenmiaoData();
  40. default:
  41. return Resp::err(ErrCode::cmd_err);
  42. }
  43. }
  44. /**
  45. * 每天重置
  46. */
  47. static function DailyReset() {
  48. self::DailyResetDay7Task();
  49. self::ClearOnlineGiftTs();
  50. self::ClearDailyTiliGift();
  51. }
  52. // <editor-fold defaultstate="collapsed" desc=" 赠送体力 ">
  53. /**
  54. * 获取神庙的主界面信息
  55. */
  56. static function GetShenmiaoData() {
  57. $req = req();
  58. //$shenmiaoInfo = $req->userInfo->game->shenmiao;
  59. $shenmiaoInfo = new Info_ShenMiao($req->userInfo->game->shenmiao);
  60. $shenmiaoInfo->initActiveItem();
  61. $req->userInfo->game->shenmiao = $shenmiaoInfo;
  62. UserProc::updateUserInfo();
  63. return Resp::ok(array(# # 返回值
  64. 'shenmiaoInfo' => $shenmiaoInfo,
  65. ));
  66. }
  67. /**
  68. * 神庙抽奖
  69. */
  70. static function ShenmiaoDrawPrize() {
  71. $req = req();
  72. list($type,$num) = $req->paras;
  73. $shenmiaoInfo = $req->userInfo->game->shenmiao;
  74. my_Assert(StlUtil::dictHasProperty($shenmiaoInfo->activeDic, $type), ErrCode::active_activeNoExistent);
  75. $list = array();
  76. if(Enum_Active::NewWish == $type){
  77. my_Assert($shenmiaoInfo->activeDic->$type->num < glc()->UserWishNumLimit, ErrCode::active_NewUserActiveNumLimit);
  78. my_Assert($num == 10, ErrCode::active_NewUserActiveNumLimit);
  79. $shenmiaoInfo->activeDic->$type->num += $num;
  80. $shenmiaoInfo->activeDic->$type->totalNum += $num;
  81. $shenmo = GameConfig::shenmiao_getItem($shenmiaoInfo->activeDic->$type->id);
  82. self::cost($req, $shenmo,$num);
  83. $tag = false;
  84. $n = 0;
  85. for ($index = 0; $index < $num; $index++) {
  86. $n+=1;
  87. $itemType = self::activeRandom($shenmo->per, $shenmo);
  88. if($itemType >= 2){
  89. $tag = true;
  90. }
  91. if($n==10 && $tag == false && $shenmiaoInfo->activeDic->$type->totalNum > 10){
  92. $list[] =self::getActiveRandomItem(Enum_ActiveItemType::Ssr, $shenmo);//self::randSsrUr($shenmo->per)
  93. } else {
  94. if($shenmiaoInfo->activeDic->$type->totalNum <= 10){
  95. $itemType = Enum_ActiveItemType::Sr;
  96. }
  97. $nId =self::getActiveRandomItem($itemType, $shenmo);
  98. $list[] = $nId;
  99. }
  100. }
  101. if($shenmiaoInfo->activeDic->$type->totalNum == 10 && !in_array($shenmo->must, $list)){//首次10连抽有必出--临时
  102. $list[0] = $shenmo->must;
  103. }
  104. if($shenmiaoInfo->activeDic->$type->totalNum >= glc()->UserWishNumLimit){
  105. $shenmiaoInfo->isComNewWish = 1;
  106. StlUtil::dictRemove($shenmiaoInfo->activeDic, $type);
  107. }
  108. } else if(Enum_Active::OpenWish == $type){
  109. $shenmo = GameConfig::shenmiao_getItem($shenmiaoInfo->activeDic->$type->id);
  110. if($num == 1){
  111. $shenmiaoInfo->activeDic->$type->num += $num;
  112. self::cost($req, $shenmo);
  113. $list[] = self::drawOneLottery($shenmo, $type);
  114. } else {//10连抽
  115. self::cost($req, $shenmo,$num);
  116. $shenmiaoInfo->activeDic->$type->totalNum += $num;
  117. $list = self::drawTenLottery($shenmo, $type, $num);
  118. }
  119. } else {//不定期活动
  120. $shenmo = GameConfig::shenmiao_getItem($shenmiaoInfo->activeDic->$type->id);
  121. if($num == 1){
  122. self::cost($req, $shenmo,$num);
  123. $shenmiaoInfo->activeDic->$type->num += $num;
  124. $list[] = self::drawOneLottery($shenmo, $type);
  125. } else {
  126. self::cost($req, $shenmo,$num);
  127. $shenmiaoInfo->activeDic->$type->totalNum += $num;
  128. $list = self::drawTenLottery($shenmo, $type, $num);
  129. }
  130. }
  131. foreach ($list as $id) {
  132. $s = $id.',1';
  133. StoreProc::AddMultiItemInStore($s);
  134. }
  135. $req->userInfo->game->shenmiao = $shenmiaoInfo;
  136. UserProc::updateUserInfo();
  137. return Resp::ok(array(# # 返回值
  138. 'shenmiaoInfo' => $shenmiaoInfo,
  139. 'list'=>$list,
  140. 'store'=>$req->userInfo->game->store,
  141. ));
  142. }
  143. /**
  144. * 1连抽
  145. * @param type $shenmo
  146. * @param type $type
  147. * @param type $num
  148. * @return type
  149. */
  150. public static function drawOneLottery($shenmo,$type){
  151. $req = req();
  152. $shenmiaoInfo = $req->userInfo->game->shenmiao;
  153. $itemType = self::activeRandom($shenmo->per, $shenmo);
  154. if($type == Enum_Active::OpenWish){
  155. $aid =self::getActiveRandomItem($itemType, $shenmo);
  156. } else {
  157. $aid =self::getActiveRandomItem_3($itemType, $shenmo,$type);//随机出的id
  158. }
  159. if($shenmiaoInfo->activeDic->$type->num % glc()->Active_UR_Num == 0 && $itemType != Enum_ActiveItemType::Ur){
  160. if($type == Enum_Active::OpenWish){
  161. $aid =self::getActiveRandomItem(Enum_ActiveItemType::Ur, $shenmo);
  162. } else {
  163. $aid =self::getActiveRandomItem_3(Enum_ActiveItemType::Ur, $shenmo,$type);
  164. }
  165. }
  166. return $aid;
  167. }
  168. /**
  169. * 10连抽
  170. * @param type $type
  171. * @param type $num
  172. */
  173. public static function drawTenLottery($shenmo,$type,$num){
  174. $req = req();
  175. $shenmiaoInfo = $req->userInfo->game->shenmiao;
  176. $tag = false;
  177. $n = 0;
  178. $list = array();
  179. for ($index1 = 0; $index1 < $num; $index1++) {
  180. $n+=1;
  181. $itemType = self::activeRandom($shenmo->per, $shenmo);
  182. if($itemType >= 2){
  183. $tag = true;
  184. }
  185. if($n==10 && $tag == false){
  186. if($type == Enum_Active::OpenWish){
  187. $list[] =self::getActiveRandomItem(Enum_ActiveItemType::Ssr, $shenmo);//self::randSsrUr($shenmo->per)
  188. } else {
  189. $list[] =self::getActiveRandomItem_3(Enum_ActiveItemType::Ssr, $shenmo,$type);
  190. }
  191. } else {
  192. if($type == Enum_Active::OpenWish){
  193. $list[] =self::getActiveRandomItem($itemType, $shenmo);
  194. } else {
  195. $list[] =self::getActiveRandomItem_3($itemType, $shenmo,$type);
  196. }
  197. }
  198. }
  199. if($shenmiaoInfo->activeDic->$type->totalNum >= glc()->Active_UR_Num){//第90次必得
  200. if($type == Enum_Active::OpenWish){
  201. $aid =self::getActiveRandomItem(Enum_ActiveItemType::Ur, $shenmo);
  202. } else {
  203. $aid =self::getActiveRandomItem_3(Enum_ActiveItemType::Ur, $shenmo,$type);
  204. }
  205. $list[0] = $aid;
  206. $shenmiaoInfo->activeDic->$type->totalNum = 0;
  207. }
  208. return $list;
  209. }
  210. public static function prizeTypeRand($str) {
  211. if(strpos($str,';')){//包含;说明里面奖品类型不同获得的概率不同
  212. $sList = explode(';',$str);
  213. $dic= new \stdClass();
  214. $rand = rand(1,100);
  215. $start = 0;
  216. $end = 0;
  217. foreach ($sList as $val) {
  218. $itemList = explode('-', $val);
  219. $end = $itemList[1] + $start;
  220. if($rand > $start && $rand <= $end){
  221. $id = self::randList(explode(',', $itemList[0]));
  222. break;
  223. }
  224. $start = $end;
  225. }
  226. } else {
  227. $id = self::randList(explode(',',$str));
  228. }
  229. return $id;
  230. }
  231. /**
  232. * 抽奖消耗
  233. */
  234. public static function cost($req,$shenmo,$num = 1) {
  235. $sList = explode(',', $shenmo->cost);
  236. $costVal = $sList[1];
  237. if($shenmo->discount != 0){
  238. $costVal = $sList[1]*$num*$shenmo->discount/100;
  239. }
  240. StoreProc::removeItemFromStore($req->userInfo->game->store,$sList[0],$costVal);
  241. }
  242. /**
  243. *
  244. * @param type $randCtx
  245. * @param sm_shenmiao $mo
  246. * @return int
  247. */
  248. public static function activeRandom($randCtx,$mo) {
  249. $rand = rand(1,10000);
  250. $type = 1;
  251. $list = explode(',', $randCtx);
  252. $start = 0;
  253. foreach ($list as $val) {
  254. $end = $val + $start;
  255. if($rand > $start && $rand <= $end){
  256. break;
  257. }
  258. $type += 1;
  259. $start = $end;
  260. }
  261. return $type;
  262. }
  263. /**
  264. *
  265. * @param type $arr
  266. * @return type
  267. */
  268. public static function randList($arr) {
  269. $rand = rand(0, count($arr)-1);
  270. return $arr[$rand];
  271. }
  272. /**
  273. *
  274. * @param type $type
  275. * @param type $mo
  276. * @return type
  277. */
  278. public static function getActiveRandomItem($type,$mo) {
  279. $id = 0;
  280. switch ($type) {
  281. case Enum_ActiveItemType::Sr:
  282. $id = self::prizeTypeRand($mo->srList);
  283. //$id = self::randList(explode(',', $mo->srList));
  284. break;
  285. case Enum_ActiveItemType::Ssr:
  286. $id = self::prizeTypeRand($mo->ssrList);
  287. //$id = self::randList(explode(',', $mo->ssrList));
  288. break;
  289. case Enum_ActiveItemType::Ur:
  290. $id = self::prizeTypeRand($mo->urList);
  291. //$id = self::randList(explode(',', $mo->urList));
  292. break;
  293. default:
  294. break;
  295. }
  296. return $id;
  297. }
  298. /**
  299. *
  300. * @param type $type
  301. * @param sm_shenmiao $mo
  302. * @return type
  303. */
  304. public static function getActiveRandomItem_3($type,$mo,$activeType) {
  305. $shenmiaoInfo = req()->userInfo->game->shenmiao;
  306. $id = 0;
  307. switch ($type) {
  308. case Enum_ActiveItemType::Sr:
  309. $id = self::prizeTypeRand($mo->srList);
  310. //$id = self::randList(explode(',', $mo->srList));
  311. break;
  312. case Enum_ActiveItemType::Ssr:
  313. if($mo->ssrUp != null){
  314. $dic = new \stdClass();
  315. $ssrList = explode(';', $mo->ssrUp);
  316. $tNum = 0;
  317. foreach ($ssrList as $val) {
  318. $sList = explode(',', $val);
  319. $sid = $sList[0];
  320. $dic->$sid = $sList[1];
  321. $tNum += $sList[1];
  322. }
  323. if($shenmiaoInfo->activeDic->$activeType->isUpSsr == 1){
  324. $rand = rand(1,$tNum);
  325. $start = 0;
  326. foreach ($dic as $k=>$val) {
  327. $end = $val + $start;
  328. if($rand > $start && $rand <= $end){//随机到Up得id
  329. $id = $k;
  330. break;
  331. }
  332. $start = $end;
  333. }
  334. $shenmiaoInfo->activeDic->$activeType->isUpSsr = 0;
  335. } else {
  336. $rand = rand(1,10000);
  337. $start = 0;
  338. $sNum = 0;
  339. $count = StlUtil::count($dic);
  340. foreach ($dic as $k=>$val) {
  341. $sNum += 1;
  342. $end = $val + $start;
  343. if($rand > $start && $rand <= $end){//随机到Up得id
  344. $id = $k;
  345. break;
  346. }
  347. if($sNum >= $count){ //随机到普通的id
  348. $shenmiaoInfo->activeDic->$activeType->isUpSsr = 1;
  349. $id =self::prizeTypeRand($mo->ssrList);
  350. //$id = self::randList(explode(',', $mo->ssrList));
  351. break;
  352. }
  353. $start = $end;
  354. }
  355. }
  356. } else {
  357. $id =self::prizeTypeRand($mo->ssrList);
  358. //$id = self::randList(explode(',', $mo->ssrList));
  359. }
  360. break;
  361. case Enum_ActiveItemType::Ur:
  362. if($mo->ssrUp != null){
  363. $dic = new \stdClass();
  364. $urList = explode(';', $mo->urUp);
  365. $tNum = 0;
  366. foreach ($urList as $val) {
  367. $sList = explode(',', $val);
  368. $sid = $sList[0];
  369. $dic->$sid = $sList[1];
  370. $tNum += $sList[1];
  371. }
  372. if($shenmiaoInfo->activeDic->$activeType->isUpUr == 1){
  373. $rand = rand(1,$tNum);
  374. $start = 0;
  375. foreach ($dic as $k=>$val) {
  376. $end = $val + $start;
  377. if($rand > $start && $rand <= $end){//随机到Up得id
  378. $id = $k;
  379. break;
  380. }
  381. $start = $end;
  382. }
  383. $shenmiaoInfo->activeDic->$activeType->$isUpUr = 0;
  384. } else {
  385. $rand = rand(1,10000);
  386. $start = 0;
  387. $sNum = 0;
  388. $count = StlUtil::count($dic);
  389. foreach ($dic as $k=>$val) {
  390. $sNum += 1;
  391. $end = $val + $start;
  392. if($rand > $start && $rand <= $end){
  393. $id = $k;
  394. break;
  395. }
  396. if($sNum >= $count){
  397. $shenmiaoInfo->activeDic->$activeType->isUpUr = 1;
  398. $id =self::prizeTypeRand($mo->urList);
  399. //$id = self::randList(explode(',', $mo->urList));
  400. break;
  401. }
  402. $start = $end;
  403. }
  404. }
  405. } else {
  406. //$id = self::randList(explode(',', $mo->urList));
  407. $id =self::prizeTypeRand($mo->urList);
  408. }
  409. break;
  410. default:
  411. break;
  412. }
  413. return $id;
  414. }
  415. /**
  416. * 从SSr 和 uR中得一个
  417. * @param type $param
  418. */
  419. public static function randSsrUr($randCtx) {
  420. $list = explode(',', $randCtx);
  421. $list = array_splice($list,1);
  422. $total = $list[0]+$list[1];
  423. $rand = rand(1,$total);
  424. $start = 0;
  425. //$type = Enum_ActiveItemType::Ssr;
  426. $type = 2;
  427. foreach ($list as $val) {
  428. $end = $val + $start;
  429. if($rand > $start && $rand <= $end){
  430. break;
  431. }
  432. $type += 1;
  433. $start = $end;
  434. }
  435. return $type;
  436. }
  437. /**
  438. * [6515] 领取 赠送体力
  439. */
  440. static function drawTiliGift() {
  441. $user = ctx();
  442. $privateState = ctx()->privateData();
  443. $base = $user->base();
  444. $hour = date('G');
  445. my_Assert(!in_array($hour, $privateState->dailyDrawedTiliGift), ErrCode::active_hasgetted); # 判断是否已领取
  446. $arr = explode(',', glc()->activity_presentTili_times); # 判断是否满足领取条件
  447. my_Assert(in_array($hour, $arr), ErrCode::active_illegal); # 不满足条件
  448. $base->Add_tili(glc()->activity_presentTili_number); # 增加体力
  449. $privateState->dailyDrawedTiliGift[] = $hour;
  450. UserProc::updateUserInfo(); # 回存
  451. return Resp::ok(array(# # 返回值
  452. 'key' => 'ok',
  453. 'record' => $privateState->dailyDrawedTiliGift,
  454. 'tili' => $base->tili,
  455. 'time' => $user->privateState->TiliTime,
  456. ));
  457. }
  458. ////
  459. // </editor-fold>
  460. // <editor-fold defaultstate="collapsed" desc=" 全服注册礼包 ">
  461. /**
  462. * [6516] 查询当前全服注册人数
  463. */
  464. static function getRegNumber() {
  465. return Resp::ok(array(
  466. 'num' => gMem()->get(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid))
  467. ));
  468. }
  469. /**
  470. * [6514] 领取全服注册礼包
  471. */
  472. static function drawRegGift() {
  473. $user = new Data_UserGame(req()->userInfo->game);
  474. $privateState = new Info_PrivateState($user->privateState);
  475. $giftId = req()->paras[0]; # 礼包id
  476. $giftModel = GameConfig::activity_reggift_getItem($giftId); # 取礼包常亮数据
  477. if (null == $giftModel) {
  478. return Resp::err(ErrCode::err_const_no);
  479. }
  480. if (in_array($giftId, $privateState->drawedRegGift)) { # 判断是否已领取
  481. return Resp::err(ErrCode::active_hasgetted);
  482. }
  483. // 判断是否满足领取条件
  484. if (gMem()->get(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid)) < $giftModel->regNumber) {
  485. return Resp::err(ErrCode::active_illegal); # 不满足条件
  486. }
  487. $err = StoreProc::AddMultiItemInStore($giftModel->rewardstr); # 发放奖励
  488. if ($err) {
  489. return Resp::err($err); # 失败
  490. }
  491. $privateState->drawedRegGift[] = $giftId;
  492. $user->privateState = $privateState;
  493. req()->userInfo->game = $user;
  494. UserProc::updateUserInfo(); # 回存
  495. return Resp::ok(array(# # 返回值
  496. 'key' => 'ok',
  497. 'store' => $user->store
  498. ));
  499. }
  500. // </editor-fold>
  501. // <editor-fold defaultstate="collapsed" desc=" 在线礼包 ">
  502. /**
  503. * 【6504】查询当天在线时长信息
  504. */
  505. static function getTotayOnlineInfos() {
  506. return Resp::ok(array(
  507. "onlineGiftIDs" => req()->userInfo->game->privateState->onlineGiftIDs,
  508. "onlineGiftts" => req()->userInfo->game->privateState->onlineGiftts
  509. ));
  510. }
  511. /**
  512. * [6513] 领取连续在线礼包
  513. */
  514. static function drawOnlineGift() {
  515. $giftid = req()->paras[0]; # 提取参数: 礼包的编号
  516. $user = ctx();
  517. $privateState = ctx()->privateData();
  518. $giftData = GameConfig::activity_onlinegift_getItem($giftid); # 在线礼包常量数据
  519. my_Assert(null != $giftData, ErrCode::onlinegift_constno_err); # 防御找不到配置数据
  520. my_Assert($privateState->onlineGiftts >= $giftData->onlineSec, # # 时间未到,不可以领取
  521. ErrCode::onlinegift_timenotenough);
  522. my_default_Arr($privateState->onlineGiftIDs); # 防御未初始化领取记录
  523. my_Assert(!in_array($giftid, $privateState->onlineGiftIDs), # # 判断礼包ID是否已经领取
  524. ErrCode::active_hasgetted);
  525. $err = StoreProc::AddMultiItemInStore($giftData->rewardstr); # 发放奖励
  526. my_Assert(ErrCode::ok == $err, $err); # 发放奖励失败
  527. $privateState->onlineGiftIDs[] = $giftid; # 记录已领礼包ID
  528. UserProc::updateUserInfo(); # 回写用户数据
  529. return Resp::ok(array(
  530. 'onlineGiftts' => $privateState->onlineGiftts,
  531. 'onlineGiftIDs' => $privateState->onlineGiftIDs,
  532. "reward" => $giftData->rewardstr,
  533. 'gold' => $user->base()->gold,
  534. 'cash' => $user->base()->cash,
  535. 'tili' => $user->base()->tili,
  536. 'store' => $user->store(),
  537. 'hero' => $user->heros()
  538. )); # 设置返回值
  539. }
  540. /**
  541. * 清理/重置 在线礼包时间戳
  542. */
  543. static private function ClearOnlineGiftTs() {
  544. req()->userInfo->game->privateState->onlineGiftts = 0;
  545. req()->userInfo->game->privateState->onlineGiftIDs = array();
  546. }
  547. // </editor-fold>
  548. // <editor-fold defaultstate="collapsed" desc=" 兑换码 ">
  549. /**
  550. * [6512]凭兑换码领取礼包
  551. */
  552. static function drawActivePackageByCode() {
  553. $user = new Data_UserGame(req()->userInfo->game);
  554. $privateState = new Info_PrivateState($user->privateState); # 快速访问
  555. list( $activeId, $codestring) = req()->paras; # 取参数 活动id, 兑换码
  556. $active = GameConfig::activity_getItem($activeId); # 活动数据
  557. my_Assert(null != $active, ErrCode::active_const_no_err);
  558. my_Assert($active->startts <= now() && $active->endts >= now(), ErrCode::active_time); # 校验开放时间
  559. my_Assert(preg_match("/^[a-kmnp-z2-9]{10}$/", $codestring), ErrCode::active_activecode_format); # 基础格式校验(10个特定字符)
  560. $activeCode = CipheredBase32::Decode($codestring); # 解码
  561. $codePlatStr = GameConstants::GetPlatStringByActivteCode($activeCode); # platstr
  562. my_Assert(GameConstants::AllPlatStr == $codePlatStr # # 忽略全平台礼包
  563. || req()->userInfo->getPlatStr() == $codePlatStr, # # 平台字符串必须相符
  564. ErrCode::active_activecode_plat); # # 平台错误
  565. my_Assert(is_int($activeCode->number) # # 编号为int值
  566. && $activeCode->number >= 1 && $activeCode->number <= 50000, # # 检查 兑换码的编号范围0~50000
  567. ErrCode::active_activecode_format);
  568. $packageID = $activeCode->package; # 礼包id
  569. $packageInfo = GameConfig::tokenGift_getItem($packageID); # 礼包常量数据
  570. my_Assert(null != $packageInfo, ErrCode::err_const_no); # 防御
  571. my_Assert($packageInfo->expirets >= now() && $packageInfo->startTs <= now(),
  572. ErrCode::active_activecode_outtime); # 激活码已经失效,或者礼包尚未开启
  573. my_Assert(!in_array($packageID, $privateState->usedTokens), ErrCode::active_hasgetted); # 已经领取过该礼包了
  574. my_Assert(!self::checkActiveCodeIsUsed($activeCode), ErrCode::active_activecode_used); # 检查 该激活码是否已经使用过了
  575. $err = StoreProc::AddMultiItemInStore($packageInfo->reward); # 发放礼包
  576. my_Assert(ErrCode::ok == $err, $err); # 防御发放礼包过程出错
  577. req()->userInfo->game->privateState->usedTokens[] = $packageID; # 记录领取记录
  578. $ok = self::setActiveCodeUserRecord($activeCode, req()->uid); # 插入数据库
  579. my_Assert($ok, ErrCode::err_db); # 数据库操作失败- 重试
  580. UserProc::updateUserInfo(); # 回存玩家数据
  581. $ret = array(# # 返回值
  582. "plat" => $codePlatStr,
  583. "packageId" => $activeCode->package,
  584. "reward" => $packageInfo->reward,
  585. 'gold' => $user->base()->gold,
  586. 'cash' => $user->base()->cash,
  587. 'tili' => $user->base()->tili,
  588. 'store' => $user->store,
  589. 'hero' => $user->heros
  590. );
  591. return Resp::ok($ret); # 返回成功信息
  592. }
  593. /**
  594. * 检查兑换码是否已经使用过了
  595. * @param ActiveCode $activeCode
  596. * @return boolean true : 已经用过了, false : 激活码尚未使用过.
  597. */
  598. static function checkActiveCodeIsUsed($activeCode) {
  599. $sql = sprintf("`plat`='%d' and `package`='%d' and `number`='%d'", # # where 子句
  600. $activeCode->plat, $activeCode->package, $activeCode->number);
  601. $rows = daoInst()
  602. ->select()
  603. ->from(self::tab_toke_gift) # 表名
  604. ->where($sql)
  605. ->count();
  606. return $rows > 0;
  607. }
  608. /**
  609. * 将兑换码添加到使用记录中
  610. * @param ActiveCode $actvieCode
  611. * @param string $uid
  612. * @return boolean true: 操作成功, false: 操作失败
  613. */
  614. static function setActiveCodeUserRecord($actvieCode, $uid) {
  615. $actvieCode->uid = $uid; # 添加uid
  616. $ret = daoInst()
  617. ->insert(self::tab_toke_gift) # 表名
  618. ->data($actvieCode)
  619. ->exec();
  620. return $ret > 0;
  621. }
  622. // </editor-fold>
  623. /**
  624. * [6503] 查询当前分区开放时间戳
  625. */
  626. private static function GetZonePublicTS() {
  627. $zoneInfo = GameConfig::zonelist_getItem(req()->zoneid);
  628. return Resp::ok($zoneInfo->publicTs);
  629. }
  630. // <editor-fold defaultstate="collapsed" desc=" 体力 ">
  631. /**
  632. * [6508]
  633. */
  634. public static function RecoveryTili() {
  635. $useTili = req()->paras[0];
  636. ActiveProc::ChangeTili($useTili);
  637. UserProc::updateUserInfo();
  638. $result = array(
  639. 'time' => req()->userInfo->game->privateState->TiliTime,
  640. 'tili' => req()->userInfo->game->baseInfo->tili
  641. );
  642. $resp = Resp::ok($result);
  643. return $resp;
  644. }
  645. /**
  646. * 清理每天的赠送体力领取记录
  647. */
  648. private static function ClearDailyTiliGift() {
  649. req()->userInfo->game->privateState->dailyDrawedTiliGift = ArrayInit();
  650. }
  651. /**
  652. * 修改玩家体力
  653. * @param int $useTili $useTili>0代表增加体力, $useTili<0代表消耗体力
  654. */
  655. public static function ChangeTili($useTili) {
  656. $user = req()->userInfo->game;
  657. $moreTili = 0; # 超上限的体力值
  658. // 如果没有tilitime这个值,说明是以前老用户新登录的,为了纠错在判断里面补值.(正常用户不会进入判断内部)
  659. if (!CommUtil::isPropertyExists($user->privateState, "TiliTime")) {
  660. $user->privateState->TiliTime = time() - 120;
  661. }
  662. if ($user->baseInfo->tili > 120) { # 如果该账户体力值本身是大于上限的,获取一下超出的体力值
  663. $moreTili = $user->baseInfo->tili - 120; # 用moreTili保存一下额外的体力值
  664. }
  665. $useTime = time() - $user->privateState->TiliTime; # 距离上次修改体力值的时间
  666. if ($useTime > 300) { # 该账户超过了单位体力时间,需要重新计算体力值
  667. if ($moreTili <= 0) {
  668. $user->baseInfo->tili += (int) ( (int) ($useTime / 300));
  669. if ($user->baseInfo->tili > 120) { # 体力值上限
  670. $user->baseInfo->tili = 120;
  671. } // 这代码真烂,以后用的时候再修复吧... -gwang 20200110154858
  672. }
  673. $user->privateState->TiliTime = time();
  674. }
  675. if ($useTili < 0) { # 如果是消耗体力
  676. my_Assert(abs($useTili) < $user->baseInfo->tili, ErrCode::notenough_tili); # 体力不足
  677. $user->baseInfo->tili += $useTili; # 扣除体力
  678. } else if ($useTili > 0) { # 如果是购买增加体力
  679. $user->baseInfo->tili += $useTili;
  680. }
  681. return ErrCode::ok;
  682. }
  683. // </editor-fold>
  684. //
  685. //
  686. // <editor-fold defaultstate="collapsed" desc=" 7日签到 ">
  687. /**
  688. * 重置7日签到相关计数器
  689. */
  690. static function DailyResetDay7Task() {
  691. req()->userInfo->game->gates->Times = 0;
  692. $typeID = 6; # 7日活动
  693. $ac = GameConfig::activity_getItem($typeID);
  694. my_Assert(null != $ac, ErrCode::err_const_no);
  695. $startDay = tsDay($ac->startts);
  696. $newLoginDays = array_filter(req()->userInfo->game->privateState->LoginDays, # 剔除不符合时间的登录记录
  697. function ($tsday)use ($startDay) {
  698. return $startDay <= $tsday;
  699. }
  700. );
  701. req()->userInfo->game->privateState->LoginDays = $newLoginDays; #
  702. if (count(req()->userInfo->game->privateState->LoginDays) < 7) {
  703. req()->userInfo->game->privateState->LoginDays[] = tsDay();
  704. }
  705. }
  706. /**
  707. * [6502] 领取七日签到奖励
  708. */
  709. static function Day7_DrawReward() {
  710. // 设计7日数据结构
  711. list($day) = req()->paras; # 参数: 领取第x天的奖励
  712. $user = req()->userInfo->game;
  713. $private = new Info_PrivateState($user->privateState); # 私有数据
  714. my_Assert(count($private->LoginDays) >= $day, ErrCode::active_day7_totaldays);
  715. my_Assert(!in_array($day, $private->day7_drawed), ErrCode::active_hasgetted);
  716. $day_rwd = GameConfig::activity_day7_getItem($day); # 查询奖励数据
  717. my_Assert(null != $day_rwd, ErrCode::err_const_no); # 防御找不到配置
  718. StoreProc::AddMultiItemInStore($day_rwd->reward); # 发放奖励
  719. $private->day7_drawed[] = $day; # 添加领取记录
  720. $user->privateState = $private; # 更新数据
  721. UserProc::updateUserInfo(); # 回存
  722. return Resp::ok(array(
  723. 'gold' => $user->baseInfo->gold,
  724. 'cash' => $user->baseInfo->cash,
  725. 'tili' => $user->baseInfo->tili,
  726. 'store' => $user->store,
  727. 'hero' => $user->heros
  728. )); # 返回值
  729. }
  730. // </editor-fold>
  731. //
  732. }