ShopProc.php 21 KB

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