ShopProc.php 23 KB

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