ShopProc.php 23 KB

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