ShopProc.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. break;
  141. case EnumShopGift::GemBox_Shop ://宝石宝箱---->2024.8.14 改为可能抽出宝石或是道具
  142. $mo = GameConfig::shop_box_getItem($typeId);
  143. my_Assert($mo != null, ErrCode::err_const_no);
  144. if ($buyNum == 1) {
  145. $yaoshiArr = explode(',', $mo->cost_one_yaoshi);
  146. $itemId = $yaoshiArr[0];
  147. if (StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $yaoshiArr[1]) {
  148. ctx()->store->removeItem($itemId, $yaoshiArr[1]);
  149. } else {
  150. $cost = $mo->cost_one;
  151. my_Assert($user->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
  152. $user->baseInfo->Consume_Cash($cost);
  153. }
  154. } elseif ($buyNum == 10) {
  155. $yaoshiArr = explode(',', $mo->cost_ten_yaoshi);
  156. $itemId = $yaoshiArr[0];
  157. if (StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $yaoshiArr[1]) {
  158. ctx()->store->removeItem($itemId, $yaoshiArr[1]);
  159. } else {
  160. $cost = $mo->cost_ten;
  161. my_Assert($user->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
  162. $user->baseInfo->Consume_Cash($cost);
  163. }
  164. }
  165. if ($mo->type == 1) {
  166. if ($buyNum == 0) {
  167. $buyNum = 1;
  168. my_Assert(now() - $user->privateState->lastFreeGetTs_ShopBoxCommon >= $mo->freeDownTs * 24 * 60 * 60, ErrCode::user_shop_FreeNumLimit);
  169. $user->privateState->lastFreeGetTs_ShopBoxCommon = now();
  170. }
  171. } else if ($mo->type == 2) {
  172. if ($buyNum == 0) {
  173. $buyNum = 1;
  174. my_Assert(now() - $user->privateState->lastFreeGetTs_ShopBoxBright >= $mo->freeDownTs * 24 * 60 * 60, ErrCode::user_shop_FreeNumLimit);
  175. $user->privateState->lastFreeGetTs_ShopBoxBright = now();
  176. }
  177. }
  178. $arr = array();
  179. $arr[] = 1;
  180. $arr[] = 10;
  181. if (!in_array($buyNum, $arr)) {
  182. $buyNum = 1;
  183. }
  184. TaskProc::OnOpenNumBrightBox($typeId, $buyNum);
  185. TaskProc::OnOpenNumAnyBox($buyNum);
  186. $perList = explode(';', $mo->percent);
  187. $allNum = 0;
  188. foreach ($perList as $key => $value) {
  189. $str = explode(',', $value);
  190. $allNum += $str[1];
  191. }
  192. $allNum *= 10;
  193. for ($i = 0; $i < $buyNum; $i++) {
  194. mt_srand((double) microtime() * 1000000);
  195. $buyCount = 0;
  196. if ($mo->type == 1) {
  197. $user->privateState->buyNum_ShopBoxCommon += 1;
  198. $buyCount = $user->privateState->buyNum_ShopBoxCommon;
  199. } else {
  200. $user->privateState->buyNum_ShopBoxBright += 1;
  201. $buyCount = $user->privateState->buyNum_ShopBoxBright;
  202. }
  203. $bichu1 = explode(',', $mo->bichu1);
  204. $qual = 0;//改成道具盒子了 但是这个变量不动了
  205. if ($buyCount % $bichu1[0] == 0) {
  206. $qual = $bichu1[1];
  207. }
  208. if ($mo->bichu2 != null) {
  209. $bichu2 = explode(',', $mo->bichu2);
  210. if ($buyCount % $bichu2[0] == 0) {
  211. $qual = $bichu2[1];
  212. }
  213. }
  214. if ($qual == 0) {
  215. $start = 0;
  216. $end = 0;
  217. $randNum = mt_rand(1, $allNum);
  218. foreach ($perList as $k => $val) {
  219. $str = explode(',', $val);
  220. $end += $str[1] * 10;
  221. if ($randNum > $start && $randNum <= $end) {
  222. $qual = $str[0];
  223. break;
  224. }
  225. $start = $end;
  226. }
  227. }
  228. if ($qual > 0) {
  229. StoreProc::AddMultiItemInStore($qual, Enum_StoreSourceType::ShopBox);
  230. }
  231. }
  232. break;
  233. case EnumShopGift::BujiBox_Shop://补给箱
  234. $mo = GameConfig::shop_supply_getItem($typeId);
  235. my_Assert($mo != null, ErrCode::err_const_no);
  236. $list = explode(',', $mo->cost);
  237. my_Assert(StlUtil::dictHasProperty($user->store->items, $list[0]), ErrCode::notenough_item);
  238. $user->store->removeItem($list[0], $list[1]);
  239. $user->baseInfo->Add_Gold($mo->gold);
  240. if (!StlUtil::dictHasProperty($user->privateState->junbeiShopNumRecord, $typeId)) {
  241. $user->privateState->junbeiShopNumRecord->$typeId = 0;
  242. }
  243. $num = $user->privateState->junbeiShopNumRecord->$typeId += 1;
  244. if (!StlUtil::dictHasProperty($user->privateState->supplyBichuDic, $typeId)) {
  245. $user->privateState->supplyBichuDic->$typeId = 0;
  246. }
  247. $user->privateState->supplyBichuDic->$typeId += 1;
  248. my_Assert($num <= $mo->limitNum, ErrCode::user_shop_LimitNum);
  249. $list = explode(';', $mo->percent);
  250. $randNum = rand(0, 10000);
  251. $start = 0;
  252. $end = 0;
  253. $qual = null;
  254. foreach ($list as $item) {
  255. $arr = explode(',', $item);
  256. $per = $arr[2] * 100;
  257. $end += $per;
  258. if ($randNum >= $start && $randNum < $end) {
  259. $qual = $arr[0] . ',' . $arr[1]; //品阶,稀有度
  260. break;
  261. }
  262. $start = $end;
  263. }
  264. //必出
  265. $bichu = explode(',', $mo->bichu);
  266. $qArr = explode(',', $qual);
  267. if ($qArr[0] == $bichu[1] && $qArr[1] == $bichu[2]) {//得到必出,重置数量记录
  268. $user->privateState->supplyBichuDic->$typeId = 0;
  269. $qual = $bichu[1] . ',' . $bichu[2];
  270. } else {
  271. if ($user->privateState->supplyBichuDic->$typeId == $bichu[0]) {
  272. $qual = $bichu[1] . ',' . $bichu[2];
  273. $user->privateState->supplyBichuDic->$typeId = 0;
  274. }
  275. }
  276. if ($qual != null) {
  277. $str = explode(',', $qual);
  278. $equipList = array();
  279. $dic = GameConfig::equip();
  280. foreach ($dic as $tid => $item) {
  281. if ($item->qual == $str[0] && $item->rarity == $str[1]) {
  282. $equipList[] = $item->typeId;
  283. }
  284. }
  285. $rNum = rand(0, count($equipList) - 1);
  286. $equipId = $equipList[$rNum];
  287. StoreProc::PutEquipInStore($equipId, 1);
  288. $equip[] = $equipId;
  289. }
  290. break;
  291. case EnumShopGift::Cash_Shop://商城-钻石
  292. $mo = GameConfig::shop_cash_getItem($typeId);
  293. my_Assert($mo != null, ErrCode::err_const_no);
  294. $tag = false;
  295. if (in_array($typeId, $user->privateState->cashShopReceived)) {
  296. $tag = true;
  297. }
  298. $reward = $mo->reward;
  299. if (!$tag) {
  300. $reward = $mo->reward . ';' . $mo->reward;
  301. }
  302. StoreProc::AddMultiItemInStore($reward);
  303. $user->baseInfo->charge_amt += $mo->price;
  304. $user->privateState->cashShopReceived[] = $typeId;
  305. TaskProc::OnRecharge();
  306. break;
  307. case EnumShopGift::Gold_Shop://商城-金币--可以多次购买
  308. $mo = GameConfig::shop_gold_getItem($typeId);
  309. my_Assert($mo != null, ErrCode::err_const_no);
  310. $tempNum = 0;
  311. foreach ($user->privateState->goldShopReceived as $key) {
  312. if ($key == $typeId) {
  313. $tempNum += 1;
  314. }
  315. }
  316. if ($tempNum >= $mo->freeNum) {
  317. $ts = 0;
  318. switch ($typeId) {
  319. case 1:
  320. $ts = $user->privateState->goldShop_CoolDownTs_1;
  321. break;
  322. case 2:
  323. $ts = $user->privateState->goldShop_CoolDownTs_2;
  324. break;
  325. case 3:
  326. $ts = $user->privateState->goldShop_CoolDownTs_3;
  327. break;
  328. }
  329. //收费买
  330. if ($typeId == 1 && $user->privateState->goldShop_GuangGaoNum < $mo->guanggaoNum) {
  331. $user->privateState->goldShop_GuangGaoNum += 1;
  332. } else {
  333. my_Assert($user->baseInfo->cash >= $mo->price, ErrCode::notenough_cash_msg);
  334. ctx()->baseInfo->Consume_Cash($mo->price);
  335. }
  336. if ($ts == 0) {
  337. switch ($typeId) {
  338. case 1:
  339. $user->privateState->goldShop_CoolDownTs_1 = now();
  340. break;
  341. case 2:
  342. $user->privateState->goldShop_CoolDownTs_2 = now();
  343. break;
  344. case 3:
  345. $user->privateState->goldShop_CoolDownTs_3 = now();
  346. break;
  347. }
  348. //$user->privateState->goldShopReceived[] = $typeId;
  349. } else {
  350. my_Assert(now() - $ts >= $mo->downTs, ErrCode::user_shop_DownTsLimit);
  351. switch ($typeId) {
  352. case 1:
  353. $user->privateState->goldShop_CoolDownTs_1 = now();
  354. break;
  355. case 2:
  356. $user->privateState->goldShop_CoolDownTs_2 = now();
  357. break;
  358. case 3:
  359. $user->privateState->goldShop_CoolDownTs_3 = now();
  360. break;
  361. }
  362. }
  363. if ($typeId == 1 && $user->privateState->goldShop_GuangGaoNum == $mo->guanggaoNum) {
  364. $user->privateState->goldShop_GuangGaoNum += 1;
  365. $user->privateState->goldShop_CoolDownTs_1 = 0;
  366. }
  367. }
  368. $user->privateState->goldShopReceived[] = $typeId;
  369. StoreProc::AddMultiItemInStore($mo->reward);
  370. break;
  371. case EnumShopGift::MonthCard_Shop:
  372. $mo = GameConfig::shop_monthcard_getItem($typeId);
  373. my_Assert($mo != null, ErrCode::err_const_no);
  374. //$user->privateState->monthCardShop_Received[] = $typeId;
  375. if ($typeId == 1) {
  376. my_Assert($user->privateState->monthCardShop_ts == 0, ErrCode::err_const_no);
  377. $user->privateState->monthCardShop_ts = now();
  378. StoreProc::AddMultiItemInStore($mo->buy_reward);
  379. EmailProc::SendMonthCardRewardMail(req()->zoneid, req()->uid, $mo->daily_reward);
  380. } else {
  381. my_Assert($user->privateState->honourCardShop_ts == 0, ErrCode::err_const_no);
  382. $user->privateState->honourCardShop_ts = now();
  383. }
  384. break;
  385. default:
  386. break;
  387. }
  388. // if (ctx()->baseInfo->charge_amt > 0 && ctx()->baseInfo->fRechargePriceReceived == 0) {
  389. // StoreProc::AddMultiItemInStore(GameConfig::globalsettings()->FirstRechargePrice);
  390. // ctx()->baseInfo->fRechargePriceReceived = 1;
  391. // }
  392. ctx($user);
  393. UserProc::updateUserInfo();
  394. return Resp::ok(array(
  395. 'gemBox' => $resultArr,
  396. 'gateGift' => $gateGift,
  397. 'cash' => $user->baseInfo->cash,
  398. 'gold' => $user->baseInfo->gold,
  399. 'privateState' => $user->privateState,
  400. 'store' => $user->store,
  401. 'task' => ctx()->task,
  402. 'reward' => StoreProc::$reward,
  403. 'reward_Gem' => StoreProc::$reward_Gem,
  404. ));
  405. }
  406. /**
  407. * 商城每日重置
  408. */
  409. public static function ShopDailyClear() {
  410. ctx()->privateState->dailyShopReceived = array();
  411. ctx()->privateState->goldShopReceived = array();
  412. $num = count(ctx()->privateState->goldShopReceived);
  413. $arr = ctx()->privateState->goldShopReceived;
  414. for ($i = 0; $i < $num; $i++) {
  415. if ($arr[$i] == 1) {
  416. StlUtil::arrayRemoveAt($arr, $i);
  417. }
  418. }
  419. ctx()->privateState->goldShop_CoolDownTs_1 = 0;
  420. ctx()->privateState->goldShop_GuangGaoNum = 0;
  421. ctx()->privateState->junbeiShopNumRecord = new \stdClass();
  422. ctx()->privateState->dailyShop_GuangGaoNum = 0;
  423. ctx()->privateState->dailyShop_GuangGaoTs = 0;
  424. ctx()->privateState->guanggaoGetTiliNum = 0;
  425. ctx()->privateState->buyTiliNum = 0;
  426. ctx()->privateState->lastFreeGetTs_ShopBoxBright = 0;
  427. ctx()->privateState->lastFreeGetTs_ShopBoxCommon = 0;
  428. self::ShopMonthClear();
  429. }
  430. /**
  431. * 月卡按照时间记录的一个月后清理
  432. */
  433. public static function ShopMonthClear() {
  434. $monthTs = 30 * 24 * 60 * 60;
  435. if (ctx()->privateState->monthCardShop_ts > 0) {
  436. if (now() - ctx()->privateState->monthCardShop_ts >= $monthTs) {
  437. ctx()->privateState->monthCardShop_ts = 0;
  438. } else {
  439. $mo = GameConfig::shop_monthcard_getItem(1);
  440. EmailProc::SendMonthCardRewardMail(req()->zoneid, req()->uid, $mo->daily_reward);
  441. }
  442. }
  443. if (ctx()->privateState->honourCardShop_ts > 0 && now() - ctx()->privateState->honourCardShop_ts >= $monthTs) {
  444. ctx()->privateState->honourCardShop_ts = 0;
  445. }
  446. }
  447. /**
  448. * 每日商城随机6个道具
  449. * @return type
  450. */
  451. public static function DailyShopItemRand() {
  452. $dataDic = new \stdClass();
  453. $dic = GameConfig::shop_daily();
  454. if ($dic == null) {
  455. return;
  456. }
  457. foreach ($dic as $typeId => $mo) {
  458. $type = $mo->type;
  459. if (StlUtil::dictHasProperty($dataDic, $type)) {
  460. $list = $dataDic->$type;
  461. $list[] = $typeId;
  462. $dataDic->$type = $list;
  463. } else {
  464. $list = array();
  465. $list[] = $typeId;
  466. $dataDic->$type = $list;
  467. }
  468. }
  469. $result = array();
  470. $arrType = array(1, 2, 3, 2, 4, 3);
  471. foreach ($arrType as $k) {
  472. if (StlUtil::dictHasProperty($dataDic, $k) && count($dataDic->$k) > 0) {
  473. $arr2 = $dataDic->$k;
  474. if ($k == 1) {
  475. $result[] = $arr2[0];
  476. continue;
  477. }
  478. $allNum = 0;
  479. foreach ($arr2 as $giftTypeId) {
  480. $allNum += GameConfig::shop_daily_getItem($giftTypeId)->per;
  481. }
  482. $randNum = rand(1, $allNum);
  483. $start = 0;
  484. $end = 0;
  485. $index = 0;
  486. foreach ($arr2 as $itemId) {
  487. $per = GameConfig::shop_daily_getItem($itemId)->per;
  488. $end += $per;
  489. if ($randNum >= $start && $randNum < $end) {
  490. $result[] = $itemId;
  491. break;
  492. }
  493. $start = $end;
  494. $index += 1;
  495. }
  496. unset($arr2[$index]);
  497. $dataDic->$k = array_values($arr2);
  498. }
  499. }
  500. ctx()->privateState->dailyShopRandItems = $result;
  501. UserProc::updateUserInfo();
  502. }
  503. }