ShopProc.php 23 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. $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 ($typeId == 10001) {
  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. }
  131. $str = explode(',', $mo->reward);
  132. if ($str[0] == 501) {//暂时还没有
  133. }
  134. //$dailyArr
  135. StoreProc::AddMultiItemInStore($mo->reward);
  136. ctx()->privateState->dailyShopReceived[] = $typeId;
  137. TaskProc::OnBuyNumDailyShop();
  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. if ($typeId == 1 && $user->privateState->goldShop_GuangGaoNum < $mo->guanggaoNum) {
  331. //广告
  332. //$user->privateState->goldShopReceived[] = $typeId;
  333. $user->privateState->goldShop_GuangGaoNum += 1;
  334. } else {
  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. if ($ts == 0) {
  348. //收费买
  349. my_Assert($user->baseInfo->cash >= $mo->price, ErrCode::notenough_cash_msg);
  350. ctx()->baseInfo->Consume_Cash($mo->price);
  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. //$user->privateState->goldShopReceived[] = $typeId;
  363. } else {
  364. my_Assert(now() - $ts >= $mo->downTs, ErrCode::user_shop_FreeNumLimit);
  365. switch ($typeId) {
  366. case 1:
  367. $user->privateState->goldShop_CoolDownTs_1 = now();
  368. break;
  369. case 2:
  370. $user->privateState->goldShop_CoolDownTs_2 = now();
  371. break;
  372. case 3:
  373. $user->privateState->goldShop_CoolDownTs_3 = now();
  374. break;
  375. }
  376. }
  377. }
  378. }
  379. $user->privateState->goldShopReceived[] = $typeId;
  380. StoreProc::AddMultiItemInStore($mo->reward);
  381. break;
  382. case EnumShopGift::MonthCard_Shop:
  383. $mo = GameConfig::shop_monthcard_getItem($typeId);
  384. my_Assert($mo != null, ErrCode::err_const_no);
  385. //$user->privateState->monthCardShop_Received[] = $typeId;
  386. if ($typeId == 1) {
  387. my_Assert($user->privateState->monthCardShop_ts == 0, ErrCode::err_const_no);
  388. $user->privateState->monthCardShop_ts = now();
  389. StoreProc::AddMultiItemInStore($mo->buy_reward);
  390. } else {
  391. my_Assert($user->privateState->honourCardShop_ts == 0, ErrCode::err_const_no);
  392. $user->privateState->honourCardShop_ts = now();
  393. }
  394. break;
  395. default:
  396. break;
  397. }
  398. // if (ctx()->baseInfo->charge_amt > 0 && ctx()->baseInfo->fRechargePriceReceived == 0) {
  399. // StoreProc::AddMultiItemInStore(GameConfig::globalsettings()->FirstRechargePrice);
  400. // ctx()->baseInfo->fRechargePriceReceived = 1;
  401. // }
  402. ctx($user);
  403. UserProc::updateUserInfo();
  404. return Resp::ok(array(
  405. 'gemBox' => $resultArr,
  406. 'gateGift' => $gateGift,
  407. 'cash' => $user->baseInfo->cash,
  408. 'gold' => $user->baseInfo->gold,
  409. 'privateState' => $user->privateState,
  410. 'store' => $user->store,
  411. 'task'=> ctx()->task,
  412. ));
  413. }
  414. /**
  415. * 商城每日重置
  416. */
  417. public static function ShopDailyClear() {
  418. ctx()->privateState->dailyShopReceived = array();
  419. ctx()->privateState->goldShopReceived = array();
  420. $num = count(ctx()->privateState->goldShopReceived);
  421. $arr = ctx()->privateState->goldShopReceived;
  422. for ($i = 0; $i < $num; $i++) {
  423. if($arr[$i] == 1){
  424. StlUtil::arrayRemoveAt($arr, $i);
  425. }
  426. }
  427. ctx()->privateState->goldShop_CoolDownTs_1 = 0;
  428. ctx()->privateState->goldShop_GuangGaoNum = 0;
  429. ctx()->privateState->junbeiShopNumRecord = new \stdClass();
  430. ctx()->privateState->dailyShop_GuangGaoNum = 0;
  431. ctx()->privateState->dailyShop_GuangGaoTs = 0;
  432. ctx()->privateState->guanggaoGetTiliNum = 0;
  433. ctx()->privateState->buyTiliNum = 0;
  434. self::ShopMonthClear();
  435. }
  436. /**
  437. * 月卡按照时间记录的一个月后清理
  438. */
  439. public static function ShopMonthClear() {
  440. $monthTs = 30 * 24 * 60 * 60;
  441. if (ctx()->privateState->monthCardShop_ts > 0) {
  442. if (now() - ctx()->privateState->monthCardShop_ts >= $monthTs) {
  443. ctx()->privateState->monthCardShop_ts = 0;
  444. } else {
  445. $mo = GameConfig::shop_monthcard_getItem(1);
  446. EmailProc::SendMonthCardRewardMail(req()->zoneid, req()->uid, $mo->daily_reward);
  447. }
  448. }
  449. if (ctx()->privateState->honourCardShop_ts > 0 && now() - ctx()->privateState->honourCardShop_ts >= $monthTs) {
  450. ctx()->privateState->honourCardShop_ts = 0;
  451. }
  452. }
  453. /**
  454. * 每日商城随机6个道具
  455. * @return type
  456. */
  457. public static function DailyShopItemRand() {
  458. $dataDic = new \stdClass();
  459. $dic = GameConfig::shop_daily();
  460. if ($dic == null) {
  461. return;
  462. }
  463. foreach ($dic as $typeId => $mo) {
  464. $type = $mo->type;
  465. if (StlUtil::dictHasProperty($dataDic, $type)) {
  466. $list = $dataDic->$type;
  467. $list[] = $typeId;
  468. $dataDic->$type = $list;
  469. } else {
  470. $list = array();
  471. $list[] = $typeId;
  472. $dataDic->$type = $list;
  473. }
  474. }
  475. $result = array();
  476. $arrType = array(1, 2, 3, 2, 4, 3);
  477. foreach ($arrType as $k) {
  478. if (StlUtil::dictHasProperty($dataDic, $k) && count($dataDic->$k) > 0) {
  479. $arr2 = $dataDic->$k;
  480. if ($k == 1) {
  481. $result[] = $arr2[0];
  482. continue;
  483. }
  484. $allNum = 0;
  485. foreach ($arr2 as $giftTypeId) {
  486. $allNum += GameConfig::shop_daily_getItem($giftTypeId)->per;
  487. }
  488. $randNum = rand(1, $allNum);
  489. $start = 0;
  490. $end = 0;
  491. $index = 0;
  492. foreach ($arr2 as $itemId) {
  493. $per = GameConfig::shop_daily_getItem($itemId)->per;
  494. $end += $per;
  495. if ($randNum >= $start && $randNum < $end) {
  496. $result[] = $itemId;
  497. break;
  498. }
  499. $start = $end;
  500. $index += 1;
  501. }
  502. unset($arr2[$index]);
  503. $dataDic->$k = array_values($arr2);
  504. }
  505. }
  506. ctx()->privateState->dailyShopRandItems = $result;
  507. UserProc::updateUserInfo();
  508. }
  509. }