ShopProc.php 23 KB

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