ShopProc.php 24 KB

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