ShopProc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace loyalsoft;
  8. /**
  9. * Description of ShopProc
  10. *
  11. * @author c'y'zhao
  12. */
  13. class ShopProc {
  14. /**
  15. * 逻辑分发
  16. * 所有的Proc中必须有这样一个方法
  17. * @param Req $req
  18. */
  19. public static function procMain($req) {
  20. switch ($req->cmd) {
  21. case CmdCode::cmd_shop_BuyGift: # 6501 商城购买东西
  22. return ShopProc::ShopBuyGift();
  23. case CmdCode::cmd_shop_BuyTili: # 6502 买体力
  24. return ShopProc::BuyTili();
  25. default:
  26. Err(ErrCode::cmd_err);
  27. }
  28. }
  29. public static function BuyTili() {
  30. list($type) = req()->paras;
  31. $addTili = 0;
  32. if ($type == 1) {//广告
  33. $maxNum = glc()->GuanggaoGetTili_DailyMaxNum;
  34. my_Assert(ctx()->privateState->guanggaoGetTiliNum < $maxNum, ErrCode::user_shop_GuanggaoGetTiliNumLimit);
  35. my_Assert(now() - ctx()->privateState->guanggaoGetTili_ts > 300, ErrCode::user_shop_DownTsLimit);
  36. $addTili = glc()->GuanggaoGetTili;
  37. ctx()->baseInfo->Add_tili(glc()->GuanggaoGetTili);
  38. ctx()->privateState->guanggaoGetTiliNum += 1;
  39. ctx()->privateState->guanggaoGetTili_ts = now();
  40. } else {
  41. $maxNum = glc()->BuyTili_DailyMaxNum;
  42. my_Assert(ctx()->privateState->buyTiliNum < $maxNum, ErrCode::user_shop_buyTiliNumLimit);
  43. $arr = explode(',', glc()->BuyTiliCost);
  44. my_Assert(ctx()->baseInfo->cash >= $arr[0], ErrCode::user_shop_NotRepeatBuy);
  45. ctx()->baseInfo->Consume_Cash($arr[0]);
  46. $addTili = $arr[1];
  47. ctx()->baseInfo->Add_tili($arr[1]);
  48. ctx()->privateState->buyTiliNum += 1;
  49. }
  50. TaskProc::OnBuyTiliOrLookGuanggao();
  51. UserProc::updateUserInfo();
  52. return Resp::ok(array(
  53. "privateState" => ctx()->privateState,
  54. "baseInfo" => ctx()->baseInfo,
  55. "tili" => $addTili,
  56. "task" => ctx()->task,
  57. ));
  58. }
  59. /**
  60. * 商城购买东西
  61. * @return type
  62. */
  63. public static function ShopBuyGift() {
  64. list($type, $typeId, $buyNum) = req()->paras;
  65. $user = ctx();
  66. $resultArr = array();
  67. $dailyArr = array();
  68. $equip = array();
  69. $gateGift = array();
  70. switch ($type) {
  71. case EnumShopGift::GateGift_Shop://章节礼包
  72. my_Assert(!in_array($typeId, ctx()->privateState->gateGiftReceived), ErrCode::user_shop_NotRepeatBuy);
  73. $mo = GameConfig::shop_gategift_getItem($typeId);
  74. my_Assert($mo != null, ErrCode::err_const_no);
  75. $price = explode(';', $mo->reward);
  76. foreach ($price as $value) {
  77. $item = explode(',', $value);
  78. if (GameConfig::item_getItem($item[0])->itemType == EnumItemType::tuzhiBox) {
  79. $dic = GameConfig::item();
  80. $list = array();
  81. foreach ($dic as $id => $val) {
  82. if ($val->itemType == EnumItemType::tuzhi) {
  83. $list[] = $val->typeId;
  84. }
  85. }
  86. $randNum = rand(0, count($list) - 1);
  87. $tuzhiId = $list[$randNum];
  88. $gateGift[] = $tuzhiId . ',1';
  89. } else {
  90. $gateGift[] = $value;
  91. }
  92. }
  93. foreach ($gateGift as $str) {
  94. StoreProc::AddMultiItemInStore($str);
  95. }
  96. ctx()->privateState->gateGiftReceived[] = $typeId;
  97. ctx()->baseInfo->charge_amt += $mo->curPrice;
  98. break;
  99. case EnumShopGift::Daily_Shop://每日商店
  100. $mo = GameConfig::shop_daily_getItem($typeId);
  101. my_Assert($mo != null, ErrCode::err_const_no);
  102. my_Assert(in_array($typeId, $user->privateState->dailyShopRandItems), ErrCode::err_const_no);
  103. if ($mo->type == 1) {
  104. $getNum = 0;
  105. foreach ($user->privateState->dailyShopReceived as $id) {
  106. if ($id == $typeId) {
  107. $getNum += 1;
  108. }
  109. }
  110. if ($getNum >= $mo->freeNum) {
  111. my_Assert($user->privateState->dailyShop_GuangGaoNum < $mo->num, ErrCode::user_shop_GuanggaoFreeNumLimit);
  112. if ($user->privateState->dailyShop_GuangGaoNum >= 1) {
  113. my_Assert(now() - $user->privateState->dailyShop_GuangGaoTs >= $mo->downTs, ErrCode::user_shop_DownTsLimit);
  114. }
  115. $user->privateState->dailyShop_GuangGaoNum += 1;
  116. $user->privateState->dailyShop_GuangGaoTs = now();
  117. }
  118. } else {
  119. my_Assert(!in_array($typeId, $user->privateState->dailyShopReceived), ErrCode::user_shop_NotRepeatBuy);
  120. switch ($mo->costType) {
  121. case EnumShopCost::Gold:
  122. my_Assert($user->baseInfo->gold >= $mo->costNum, ErrCode::notenough_gold_msg);
  123. ctx()->baseInfo->Consume_Gold($mo->costNum);
  124. break;
  125. case EnumShopCost::Cash:
  126. my_Assert($user->baseInfo->cash >= $mo->costNum, ErrCode::notenough_cash_msg);
  127. ctx()->baseInfo->Consume_Cash($mo->costNum);
  128. break;
  129. default:
  130. break;
  131. }
  132. TaskProc::OnBuyNumDailyShop();
  133. }
  134. // $str = explode(',', $mo->reward);
  135. // if ($str[0] == 501) {//暂时还没有
  136. // }
  137. //$dailyArr
  138. StoreProc::AddMultiItemInStore($mo->reward);
  139. ctx()->privateState->dailyShopReceived[] = $typeId;
  140. TaskProc::OnBuyNumDailyShop_state();
  141. break;
  142. case EnumShopGift::GemBox_Shop ://宝石宝箱---->2024.8.14 改为可能抽出宝石或是道具
  143. $mo = GameConfig::shop_box_getItem($typeId);
  144. my_Assert($mo != null, ErrCode::err_const_no);
  145. if ($buyNum == 1) {
  146. $yaoshiArr = explode(',', $mo->cost_one_yaoshi);
  147. $itemId = $yaoshiArr[0];
  148. if (StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $yaoshiArr[1]) {
  149. ctx()->store->removeItem($itemId, $yaoshiArr[1]);
  150. } else {
  151. $cost = $mo->cost_one;
  152. my_Assert($user->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
  153. $user->baseInfo->Consume_Cash($cost);
  154. }
  155. } elseif ($buyNum == 10) {
  156. $yaoshiArr = explode(',', $mo->cost_ten_yaoshi);
  157. $itemId = $yaoshiArr[0];
  158. if (StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $yaoshiArr[1]) {
  159. ctx()->store->removeItem($itemId, $yaoshiArr[1]);
  160. } else {
  161. $cost = $mo->cost_ten;
  162. my_Assert($user->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
  163. $user->baseInfo->Consume_Cash($cost);
  164. }
  165. }
  166. if ($mo->type == 1) {
  167. if ($buyNum == 0) {
  168. $buyNum = 1;
  169. my_Assert(now() - $user->privateState->lastFreeGetTs_ShopBoxCommon >= $mo->freeDownTs * 24 * 60 * 60, ErrCode::user_shop_FreeNumLimit);
  170. $user->privateState->lastFreeGetTs_ShopBoxCommon = now();
  171. }
  172. } else if ($mo->type == 2) {
  173. if ($buyNum == 0) {
  174. $buyNum = 1;
  175. my_Assert(now() - $user->privateState->lastFreeGetTs_ShopBoxBright >= $mo->freeDownTs * 24 * 60 * 60, ErrCode::user_shop_FreeNumLimit);
  176. $user->privateState->lastFreeGetTs_ShopBoxBright = now();
  177. }
  178. }
  179. $arr = array();
  180. $arr[] = 1;
  181. $arr[] = 10;
  182. if (!in_array($buyNum, $arr)) {
  183. $buyNum = 1;
  184. }
  185. TaskProc::OnOpenNumBrightBox($typeId, $buyNum);
  186. TaskProc::OnOpenNumAnyBox($buyNum);
  187. $perList = explode(';', $mo->percent);
  188. $allNum = 0;
  189. foreach ($perList as $key => $value) {
  190. $str = explode(',', $value);
  191. $allNum += $str[1];
  192. }
  193. $allNum *= 10;
  194. for ($i = 0; $i < $buyNum; $i++) {
  195. mt_srand((double) microtime() * 1000000);
  196. $buyCount = 0;
  197. if ($mo->type == 1) {
  198. $user->privateState->buyNum_ShopBoxCommon += 1;
  199. $buyCount = $user->privateState->buyNum_ShopBoxCommon;
  200. } else {
  201. $user->privateState->buyNum_ShopBoxBright += 1;
  202. $buyCount = $user->privateState->buyNum_ShopBoxBright;
  203. }
  204. $bichu1 = explode(',', $mo->bichu1);
  205. $qual = 0;//改成道具盒子了 但是这个变量不动了
  206. if ($buyCount % $bichu1[0] == 0) {
  207. $qual = $bichu1[1];
  208. }
  209. if ($mo->bichu2 != null) {
  210. $bichu2 = explode(',', $mo->bichu2);
  211. if ($buyCount % $bichu2[0] == 0) {
  212. $qual = $bichu2[1];
  213. }
  214. }
  215. if ($qual == 0) {
  216. $start = 0;
  217. $end = 0;
  218. $randNum = mt_rand(1, $allNum);
  219. foreach ($perList as $k => $val) {
  220. $str = explode(',', $val);
  221. $end += $str[1] * 10;
  222. if ($randNum > $start && $randNum <= $end) {
  223. $qual = $str[0];
  224. break;
  225. }
  226. $start = $end;
  227. }
  228. }
  229. if ($qual > 0) {
  230. StoreProc::AddMultiItemInStore($qual.",1", Enum_StoreSourceType::ShopBox);
  231. }
  232. }
  233. TaskProc::OnOpenNumBrightBox_state($typeId, $buyNum);
  234. break;
  235. case EnumShopGift::BujiBox_Shop://补给箱
  236. $mo = GameConfig::shop_supply_getItem($typeId);
  237. my_Assert($mo != null, ErrCode::err_const_no);
  238. $list = explode(',', $mo->cost);
  239. my_Assert(StlUtil::dictHasProperty($user->store->items, $list[0]), ErrCode::notenough_item);
  240. $user->store->removeItem($list[0], $list[1]);
  241. $user->baseInfo->Add_Gold($mo->gold);
  242. if (!StlUtil::dictHasProperty($user->privateState->junbeiShopNumRecord, $typeId)) {
  243. $user->privateState->junbeiShopNumRecord->$typeId = 0;
  244. }
  245. $num = $user->privateState->junbeiShopNumRecord->$typeId += 1;
  246. if (!StlUtil::dictHasProperty($user->privateState->supplyBichuDic, $typeId)) {
  247. $user->privateState->supplyBichuDic->$typeId = 0;
  248. }
  249. $user->privateState->supplyBichuDic->$typeId += 1;
  250. my_Assert($num <= $mo->limitNum, ErrCode::user_shop_LimitNum);
  251. $list = explode(';', $mo->percent);
  252. $randNum = rand(0, 10000);
  253. $start = 0;
  254. $end = 0;
  255. $qual = null;
  256. foreach ($list as $item) {
  257. $arr = explode(',', $item);
  258. $per = $arr[2] * 100;
  259. $end += $per;
  260. if ($randNum >= $start && $randNum < $end) {
  261. $qual = $arr[0] . ',' . $arr[1]; //品阶,稀有度
  262. break;
  263. }
  264. $start = $end;
  265. }
  266. //必出
  267. $bichu = explode(',', $mo->bichu);
  268. $qArr = explode(',', $qual);
  269. if ($qArr[0] == $bichu[1] && $qArr[1] == $bichu[2]) {//得到必出,重置数量记录
  270. $user->privateState->supplyBichuDic->$typeId = 0;
  271. $qual = $bichu[1] . ',' . $bichu[2];
  272. } else {
  273. if ($user->privateState->supplyBichuDic->$typeId == $bichu[0]) {
  274. $qual = $bichu[1] . ',' . $bichu[2];
  275. $user->privateState->supplyBichuDic->$typeId = 0;
  276. }
  277. }
  278. if ($qual != null) {
  279. $str = explode(',', $qual);
  280. $equipList = array();
  281. $dic = GameConfig::equip();
  282. foreach ($dic as $tid => $item) {
  283. if ($item->qual == $str[0] && $item->rarity == $str[1]) {
  284. $equipList[] = $item->typeId;
  285. }
  286. }
  287. $rNum = rand(0, count($equipList) - 1);
  288. $equipId = $equipList[$rNum];
  289. StoreProc::PutEquipInStore($equipId, 1);
  290. $equip[] = $equipId;
  291. }
  292. break;
  293. case EnumShopGift::Cash_Shop://商城-钻石
  294. $mo = GameConfig::shop_cash_getItem($typeId);
  295. my_Assert($mo != null, ErrCode::err_const_no);
  296. $tag = false;
  297. if (in_array($typeId, $user->privateState->cashShopReceived)) {
  298. $tag = true;
  299. }
  300. $reward = $mo->reward;
  301. if (!$tag) {
  302. $reward = $mo->reward . ';' . $mo->reward;
  303. }
  304. StoreProc::AddMultiItemInStore($reward);
  305. $user->baseInfo->charge_amt += $mo->price;
  306. $user->privateState->cashShopReceived[] = $typeId;
  307. TaskProc::OnRecharge();
  308. break;
  309. case EnumShopGift::Gold_Shop://商城-金币--可以多次购买
  310. $mo = GameConfig::shop_gold_getItem($typeId);
  311. my_Assert($mo != null, ErrCode::err_const_no);
  312. $tempNum = 0;
  313. foreach ($user->privateState->goldShopReceived as $key) {
  314. if ($key == $typeId) {
  315. $tempNum += 1;
  316. }
  317. }
  318. if ($tempNum >= $mo->freeNum) {
  319. $ts = 0;
  320. switch ($typeId) {
  321. case 1:
  322. $ts = $user->privateState->goldShop_CoolDownTs_1;
  323. break;
  324. case 2:
  325. $ts = $user->privateState->goldShop_CoolDownTs_2;
  326. break;
  327. case 3:
  328. $ts = $user->privateState->goldShop_CoolDownTs_3;
  329. break;
  330. }
  331. //收费买
  332. if ($typeId == 1 && $user->privateState->goldShop_GuangGaoNum < $mo->guanggaoNum) {
  333. $user->privateState->goldShop_GuangGaoNum += 1;
  334. } else {
  335. my_Assert($user->baseInfo->cash >= $mo->price, ErrCode::notenough_cash_msg);
  336. ctx()->baseInfo->Consume_Cash($mo->price);
  337. }
  338. if ($ts == 0) {
  339. switch ($typeId) {
  340. case 1:
  341. $user->privateState->goldShop_CoolDownTs_1 = now();
  342. break;
  343. case 2:
  344. $user->privateState->goldShop_CoolDownTs_2 = now();
  345. break;
  346. case 3:
  347. $user->privateState->goldShop_CoolDownTs_3 = now();
  348. break;
  349. }
  350. //$user->privateState->goldShopReceived[] = $typeId;
  351. } else {
  352. my_Assert(now() - $ts >= $mo->downTs, ErrCode::user_shop_DownTsLimit);
  353. switch ($typeId) {
  354. case 1:
  355. $user->privateState->goldShop_CoolDownTs_1 = now();
  356. break;
  357. case 2:
  358. $user->privateState->goldShop_CoolDownTs_2 = now();
  359. break;
  360. case 3:
  361. $user->privateState->goldShop_CoolDownTs_3 = now();
  362. break;
  363. }
  364. }
  365. if ($typeId == 1 && $user->privateState->goldShop_GuangGaoNum == $mo->guanggaoNum) {
  366. $user->privateState->goldShop_GuangGaoNum += 1;
  367. $user->privateState->goldShop_CoolDownTs_1 = 0;
  368. }
  369. }
  370. TaskProc::OnBuyGold();
  371. $user->privateState->goldShopReceived[] = $typeId;
  372. StoreProc::AddMultiItemInStore($mo->reward);
  373. break;
  374. case EnumShopGift::MonthCard_Shop:
  375. $mo = GameConfig::shop_monthcard_getItem($typeId);
  376. my_Assert($mo != null, ErrCode::err_const_no);
  377. //$user->privateState->monthCardShop_Received[] = $typeId;
  378. if ($typeId == 1) {
  379. my_Assert($user->privateState->monthCardShop_ts == 0, ErrCode::err_const_no);
  380. $user->privateState->monthCardShop_ts = now();
  381. StoreProc::AddMultiItemInStore($mo->buy_reward);
  382. EmailProc::SendMonthCardRewardMail(req()->zoneid, req()->uid, $mo->daily_reward);
  383. } else {
  384. my_Assert($user->privateState->honourCardShop_ts == 0, ErrCode::err_const_no);
  385. $user->privateState->honourCardShop_ts = now();
  386. }
  387. break;
  388. default:
  389. break;
  390. }
  391. // if (ctx()->baseInfo->charge_amt > 0 && ctx()->baseInfo->fRechargePriceReceived == 0) {
  392. // StoreProc::AddMultiItemInStore(GameConfig::globalsettings()->FirstRechargePrice);
  393. // ctx()->baseInfo->fRechargePriceReceived = 1;
  394. // }
  395. ctx($user);
  396. UserProc::updateUserInfo();
  397. return Resp::ok(array(
  398. 'gemBox' => $resultArr,
  399. 'gateGift' => $gateGift,
  400. 'cash' => $user->baseInfo->cash,
  401. 'gold' => $user->baseInfo->gold,
  402. 'privateState' => $user->privateState,
  403. 'store' => $user->store,
  404. 'task' => ctx()->task,
  405. 'heros'=> ctx()->heros,
  406. 'reward' => StoreProc::$reward,
  407. 'reward_Gem' => StoreProc::$reward_Gem,
  408. ));
  409. }
  410. /**
  411. * 商城每日重置
  412. */
  413. public static function ShopDailyClear() {
  414. ctx()->privateState->dailyShopReceived = array();
  415. ctx()->privateState->goldShopReceived = array();
  416. $num = count(ctx()->privateState->goldShopReceived);
  417. $arr = ctx()->privateState->goldShopReceived;
  418. for ($i = 0; $i < $num; $i++) {
  419. if ($arr[$i] == 1) {
  420. StlUtil::arrayRemoveAt($arr, $i);
  421. }
  422. }
  423. ctx()->privateState->goldShop_CoolDownTs_1 = 0;
  424. ctx()->privateState->goldShop_GuangGaoNum = 0;
  425. ctx()->privateState->junbeiShopNumRecord = new \stdClass();
  426. ctx()->privateState->dailyShop_GuangGaoNum = 0;
  427. ctx()->privateState->dailyShop_GuangGaoTs = 0;
  428. ctx()->privateState->guanggaoGetTiliNum = 0;
  429. ctx()->privateState->buyTiliNum = 0;
  430. ctx()->privateState->lastFreeGetTs_ShopBoxBright = 0;
  431. ctx()->privateState->lastFreeGetTs_ShopBoxCommon = 0;
  432. self::ShopMonthClear();
  433. }
  434. /**
  435. * 月卡按照时间记录的一个月后清理
  436. */
  437. public static function ShopMonthClear() {
  438. $monthTs = 30 * 24 * 60 * 60;
  439. if (ctx()->privateState->monthCardShop_ts > 0) {
  440. if (now() - ctx()->privateState->monthCardShop_ts >= $monthTs) {
  441. ctx()->privateState->monthCardShop_ts = 0;
  442. } else {
  443. $mo = GameConfig::shop_monthcard_getItem(1);
  444. EmailProc::SendMonthCardRewardMail(req()->zoneid, req()->uid, $mo->daily_reward);
  445. }
  446. }
  447. if (ctx()->privateState->honourCardShop_ts > 0 && now() - ctx()->privateState->honourCardShop_ts >= $monthTs) {
  448. ctx()->privateState->honourCardShop_ts = 0;
  449. }
  450. }
  451. /**
  452. * 每日商城随机6个道具
  453. * @return type
  454. */
  455. public static function DailyShopItemRand() {
  456. $dataDic = new \stdClass();
  457. $dic = GameConfig::shop_daily();
  458. if ($dic == null) {
  459. return;
  460. }
  461. foreach ($dic as $typeId => $mo) {
  462. $type = $mo->type;
  463. if (StlUtil::dictHasProperty($dataDic, $type)) {
  464. $list = $dataDic->$type;
  465. $list[] = $typeId;
  466. $dataDic->$type = $list;
  467. } else {
  468. $list = array();
  469. $list[] = $typeId;
  470. $dataDic->$type = $list;
  471. }
  472. }
  473. $result = array();
  474. $arrType = array(1, 2, 3, 2, 4, 3);
  475. foreach ($arrType as $k) {
  476. if (StlUtil::dictHasProperty($dataDic, $k) && count($dataDic->$k) > 0) {
  477. $arr2 = $dataDic->$k;
  478. if ($k == 1) {
  479. $result[] = $arr2[0];
  480. continue;
  481. }
  482. $allNum = 0;
  483. foreach ($arr2 as $giftTypeId) {
  484. $allNum += GameConfig::shop_daily_getItem($giftTypeId)->per;
  485. }
  486. $randNum = rand(1, $allNum);
  487. $start = 0;
  488. $end = 0;
  489. $index = 0;
  490. foreach ($arr2 as $itemId) {
  491. $per = GameConfig::shop_daily_getItem($itemId)->per;
  492. $end += $per;
  493. if ($randNum >= $start && $randNum < $end) {
  494. $result[] = $itemId;
  495. break;
  496. }
  497. $start = $end;
  498. $index += 1;
  499. }
  500. unset($arr2[$index]);
  501. $dataDic->$k = array_values($arr2);
  502. }
  503. }
  504. ctx()->privateState->dailyShopRandItems = $result;
  505. UserProc::updateUserInfo();
  506. }
  507. }