ShopProc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. mt_srand((double)microtime()*1000000);
  145. $allNum *= 10;
  146. for ($i = 0; $i < $buyNum; $i++) {
  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. break;
  176. }
  177. $start = $end;
  178. }
  179. }
  180. if ($qual > 0) {
  181. $gemDic = GameConfig::gem();
  182. $gemList = array();
  183. foreach ($gemDic as $key => $value) {
  184. if ($value->qual == $qual) {
  185. $gemList[] = $value;
  186. }
  187. }
  188. $n = rand(0, count($gemList) - 1);
  189. $gem = StoreProc::initGem($gemList[$n]->typeId);
  190. $resultArr[] = $gem->uid;
  191. StoreProc::PutGemInStore2($gem);
  192. }
  193. }
  194. break;
  195. case EnumShopGift::BujiBox_Shop://补给箱
  196. $mo = GameConfig::shop_supply_getItem($typeId);
  197. my_Assert($mo != null, ErrCode::err_const_no);
  198. $list = explode(',', $mo->cost);
  199. my_Assert(StlUtil::dictHasProperty($user->store->items, $list[0]), ErrCode::notenough_item);
  200. $user->store->removeItem($list[0], $list[1]);
  201. $user->baseInfo->Add_Gold($mo->gold);
  202. if (!StlUtil::dictHasProperty($user->privateState->junbeiShopNumRecord, $typeId)) {
  203. $user->privateState->junbeiShopNumRecord->$typeId = 0;
  204. }
  205. $num = $user->privateState->junbeiShopNumRecord->$typeId += 1;
  206. if (!StlUtil::dictHasProperty($user->privateState->supplyBichuDic, $typeId)) {
  207. $user->privateState->supplyBichuDic->$typeId = 0;
  208. }
  209. $user->privateState->supplyBichuDic->$typeId += 1;
  210. my_Assert($num <= $mo->limitNum, ErrCode::user_shop_LimitNum);
  211. $list = explode(';', $mo->percent);
  212. $randNum = rand(0, 10000);
  213. $start = 0;
  214. $end = 0;
  215. $qual = null;
  216. foreach ($list as $item) {
  217. $arr = explode(',', $item);
  218. $per = $arr[2] * 100;
  219. $end += $per;
  220. if ($randNum >= $start && $randNum < $end) {
  221. $qual = $arr[0] . ',' . $arr[1]; //品阶,稀有度
  222. break;
  223. }
  224. $start = $end;
  225. }
  226. //必出
  227. $bichu = explode(',', $mo->bichu);
  228. $qArr = explode(',', $qual);
  229. if ($qArr[0] == $bichu[1] && $qArr[1] == $bichu[2]) {//得到必出,重置数量记录
  230. $user->privateState->supplyBichuDic->$typeId = 0;
  231. $qual = $bichu[1] . ',' . $bichu[2];
  232. } else {
  233. if ($user->privateState->supplyBichuDic->$typeId == $bichu[0]) {
  234. $qual = $bichu[1] . ',' . $bichu[2];
  235. $user->privateState->supplyBichuDic->$typeId = 0;
  236. }
  237. }
  238. if ($qual != null) {
  239. $str = explode(',', $qual);
  240. $equipList = array();
  241. $dic = GameConfig::equip();
  242. foreach ($dic as $tid => $item) {
  243. if ($item->qual == $str[0] && $item->rarity == $str[1]) {
  244. $equipList[] = $item->typeId;
  245. }
  246. }
  247. $rNum = rand(0, count($equipList) - 1);
  248. $equipId = $equipList[$rNum];
  249. StoreProc::PutEquipInStore($equipId, 1);
  250. $equip[] = $equipId;
  251. }
  252. break;
  253. case EnumShopGift::Cash_Shop://商城-钻石
  254. $mo = GameConfig::shop_cash_getItem($typeId);
  255. my_Assert($mo != null, ErrCode::err_const_no);
  256. $tag = false;
  257. if (in_array($typeId, $user->privateState->cashShopReceived)) {
  258. $tag = true;
  259. }
  260. $reward = $mo->reward;
  261. if (!$tag) {
  262. $reward = $mo->reward . ';' . $mo->reward;
  263. }
  264. StoreProc::AddMultiItemInStore($reward);
  265. $user->baseInfo->charge_amt += $mo->price;
  266. $user->privateState->cashShopReceived[] = $typeId;
  267. break;
  268. case EnumShopGift::Gold_Shop://商城-金币--可以多次购买
  269. $mo = GameConfig::shop_gold_getItem($typeId);
  270. my_Assert($mo != null, ErrCode::err_const_no);
  271. $tempNum = 0;
  272. foreach ($user->privateState->goldShopReceived as $key) {
  273. if ($key == $typeId) {
  274. $tempNum += 1;
  275. }
  276. }
  277. if ($tempNum >= $mo->freeNum) {
  278. if ($typeId == 1 && $user->privateState->goldShop_GuangGaoNum < $mo->guanggaoNum) {
  279. //广告
  280. //$user->privateState->goldShopReceived[] = $typeId;
  281. $user->privateState->goldShop_GuangGaoNum += 1;
  282. } else {
  283. $ts = 0;
  284. switch ($typeId) {
  285. case 1:
  286. $ts = $user->privateState->goldShop_GuangGaoTs_1;
  287. break;
  288. case 2:
  289. $ts = $user->privateState->goldShop_GuangGaoTs_2;
  290. break;
  291. case 3:
  292. $ts = $user->privateState->goldShop_GuangGaoTs_3;
  293. break;
  294. }
  295. if ($ts == 0) {
  296. //收费买
  297. my_Assert($user->baseInfo->cash >= $mo->price, ErrCode::notenough_cash_msg);
  298. ctx()->baseInfo->Consume_Cash($mo->price);
  299. //$user->privateState->goldShopReceived[] = $typeId;
  300. } else {
  301. my_Assert(now() - $ts >= $mo->downTs, ErrCode::user_shop_FreeNumLimit);
  302. switch ($typeId) {
  303. case 1:
  304. $user->privateState->goldShop_GuangGaoTs_1 = now();
  305. break;
  306. case 2:
  307. $user->privateState->goldShop_GuangGaoTs_2 = now();
  308. break;
  309. case 3:
  310. $user->privateState->goldShop_GuangGaoTs_3 = now();
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. $user->privateState->goldShopReceived[] = $typeId;
  317. StoreProc::AddMultiItemInStore($mo->reward);
  318. break;
  319. default:
  320. break;
  321. }
  322. if (ctx()->baseInfo->charge_amt > 0 && ctx()->baseInfo->fRechargePriceReceived == 0) {
  323. StoreProc::AddMultiItemInStore(GameConfig::globalsettings()->FirstRechargePrice);
  324. ctx()->baseInfo->fRechargePriceReceived = 1;
  325. }
  326. ctx($user);
  327. UserProc::updateUserInfo();
  328. return Resp::ok(array(
  329. 'gemBox' => $resultArr,
  330. 'gateGift' => $gateGift,
  331. 'cash' => $user->baseInfo->cash,
  332. 'gold' => $user->baseInfo->gold,
  333. 'privateState' => $user->privateState,
  334. 'store' => $user->store,));
  335. }
  336. /**
  337. * 商城每日重置
  338. */
  339. public static function ShopDailyClear() {
  340. ctx()->privateState->dailyShopReceived = array();
  341. ctx()->privateState->goldShopReceived = array();
  342. ctx()->privateState->junbeiShopNumRecord = new \stdClass();
  343. ctx()->privateState->dailyShop_GuangGaoNum = 0;
  344. ctx()->privateState->dailyShop_GuangGaoTs = 0;
  345. }
  346. /**
  347. * 每日商城随机6个道具
  348. * @return type
  349. */
  350. public static function DailyShopItemRand() {
  351. $dataDic = new \stdClass();
  352. $dic = GameConfig::shop_daily();
  353. if ($dic == null) {
  354. return;
  355. }
  356. foreach ($dic as $typeId => $mo) {
  357. $type = $mo->type;
  358. if (StlUtil::dictHasProperty($dataDic, $type)) {
  359. $list = $dataDic->$type;
  360. $list[] = $typeId;
  361. $dataDic->$type = $list;
  362. } else {
  363. $list = array();
  364. $list[] = $typeId;
  365. $dataDic->$type = $list;
  366. }
  367. }
  368. $result = array();
  369. $arrType = array(1, 2, 3, 2, 4, 3);
  370. foreach ($arrType as $k) {
  371. if (StlUtil::dictHasProperty($dataDic, $k) && count($dataDic->$k) > 0) {
  372. $arr2 = $dataDic->$k;
  373. if ($k == 1) {
  374. $result[] = $arr2[0];
  375. continue;
  376. }
  377. $allNum = 0;
  378. foreach ($arr2 as $giftTypeId) {
  379. $allNum += GameConfig::shop_daily_getItem($giftTypeId)->per;
  380. }
  381. $randNum = rand(1, $allNum);
  382. $start = 0;
  383. $end = 0;
  384. $index = 0;
  385. foreach ($arr2 as $itemId) {
  386. $per = GameConfig::shop_daily_getItem($itemId)->per;
  387. $end += $per;
  388. if ($randNum >= $start && $randNum < $end) {
  389. $result[] = $itemId;
  390. break;
  391. }
  392. $start = $end;
  393. $index += 1;
  394. }
  395. unset($arr2[$index]);
  396. $dataDic->$k = array_values($arr2);
  397. }
  398. }
  399. ctx()->privateState->dailyShopRandItems = $result;
  400. UserProc::updateUserInfo();
  401. }
  402. }