ShopProc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. $equip = array();
  36. $gateGift = array();
  37. switch ($type) {
  38. case EnumShopGift::GateGift_Shop://章节礼包
  39. my_Assert(!in_array($typeId, ctx()->privateState->gateGiftReceived), ErrCode::user_shop_NotRepeatBuy);
  40. $mo = GameConfig::shop_gategift_getItem($typeId);
  41. my_Assert($mo != null, ErrCode::err_const_no);
  42. $price = explode(';', $mo->reward);
  43. foreach ($price as $value) {
  44. $item = explode(',', $value);
  45. if(GameConfig::item_getItem($item[0])->itemType == EnumItemType::tuzhiBox){
  46. $dic = GameConfig::item();
  47. $list = array();
  48. foreach ($dic as $id => $val) {
  49. if($val->itemType == EnumItemType::tuzhi){
  50. $list[] = $val->typeId;
  51. }
  52. }
  53. $randNum = rand(0, count($list)-1);
  54. $tuzhiId = $list[$randNum];
  55. $gateGift[] = $tuzhiId.',1';
  56. } else {
  57. $gateGift[] = $value;
  58. }
  59. }
  60. foreach ($gateGift as $str) {
  61. StoreProc::AddMultiItemInStore($str);
  62. }
  63. ctx()->privateState->gateGiftReceived[] = $typeId;
  64. ctx()->baseInfo->charge_amt += $mo->curPrice;
  65. break;
  66. case EnumShopGift::Daily_Shop://每日商店
  67. $mo = GameConfig::shop_daily_getItem($typeId);
  68. my_Assert($mo != null, ErrCode::err_const_no);
  69. my_Assert(in_array($typeId, $user->privateState->dailyShopRandItems), ErrCode::err_const_no);
  70. if($typeId == 1001){
  71. $getNum = 0;
  72. foreach ($user->privateState->dailyShopReceived as $id) {
  73. if($id == $typeId){
  74. $getNum+=1;
  75. }
  76. }
  77. if($getNum >= $mo->num){
  78. my_Assert($user->privateState->dailyShop_GuangGaoNum < $mo->num, ErrCode::user_shop_FreeNumLimit);
  79. my_Assert(now() - $user->privateState->dailyShop_GuangGaoTs >= $mo->downTs, ErrCode::user_shop_FreeNumLimit);
  80. $user->privateState->dailyShop_GuangGaoNum += 1;
  81. $user->privateState->dailyShop_GuangGaoTs = now();
  82. }
  83. } else {
  84. my_Assert(!in_array($typeId, $user->privateState->dailyShopReceived), ErrCode::user_shop_NotRepeatBuy);
  85. switch ($mo->costType) {
  86. case EnumShopCost::Gold:
  87. my_Assert($user->baseInfo->gold>= $mo->costNum, ErrCode::notenough_gold_msg);
  88. ctx()->baseInfo->Consume_Gold($mo->costNum);
  89. break;
  90. case EnumShopCost::Cash:
  91. my_Assert($user->baseInfo->cash>= $mo->costNum, ErrCode::notenough_cash_msg);
  92. ctx()->baseInfo->Consume_Cash($mo->costNum);
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. StoreProc::AddMultiItemInStore($mo->reward);
  99. ctx()->privateState->dailyShopReceived[] = $typeId;
  100. break;
  101. case EnumShopGift::GemBox_Shop ://宝石宝箱
  102. $mo = GameConfig::shop_box_getItem($typeId);
  103. my_Assert($mo != null, ErrCode::err_const_no);
  104. if($buyNum > 0){
  105. $cost = $mo->cost_ten;
  106. if($buyNum == 1){
  107. $cost = $mo->cost_one;
  108. }
  109. my_Assert( $user->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
  110. $user->baseInfo->Consume_Cash($cost);
  111. }
  112. if($mo->type == 1){
  113. if($buyNum == 0){
  114. $buyNum = 1;
  115. my_Assert(now()-$user->privateState->lastFreeGetTs_ShopBoxCommon >= $mo->freeDownTs*24*60*60, ErrCode::user_shop_FreeNumLimit);
  116. $user->privateState->lastFreeGetTs_ShopBoxCommon = now();
  117. }
  118. } else if($mo->type == 2){
  119. if($buyNum == 0){
  120. $buyNum = 1;
  121. my_Assert(now()-$user->privateState->lastFreeGetTs_ShopBoxBright >= $mo->freeDownTs*24*60*60, ErrCode::user_shop_FreeNumLimit);
  122. $user->privateState->lastFreeGetTs_ShopBoxBright = now();
  123. }
  124. }
  125. $arr = array();
  126. $arr[] = 1;
  127. $arr[] = 10;
  128. if(!in_array($buyNum, $arr)){
  129. $buyNum = 1;
  130. }
  131. $perList = explode(';',$mo->percent);
  132. $allNum = 0;
  133. foreach ($perList as $key => $value) {
  134. $str = explode(',', $value);
  135. $allNum += $str[1];
  136. }
  137. $allNum *= 1000;
  138. for ($i = 0; $i < $buyNum; $i++) {
  139. $buyCount = 0;
  140. if($mo->type == 1){
  141. $user->privateState->buyNum_ShopBoxCommon += 1;
  142. $buyCount = $user->privateState->buyNum_ShopBoxCommon;
  143. } else {
  144. $user->privateState->buyNum_ShopBoxBright += $buyNum;
  145. $buyCount = $user->privateState->buyNum_ShopBoxBright;
  146. }
  147. $bichu1 = explode(',',$mo->bichu1);
  148. $qual = 0;
  149. if($buyCount % $bichu1[0] == 0){
  150. $qual = $bichu1[1];
  151. }
  152. if($mo->bichu2 != null){
  153. $bichu2 = explode(',',$mo->bichu2);
  154. if($buyCount % $bichu2[0] == 0){
  155. $qual = $bichu2[1];
  156. }
  157. }
  158. if($qual == 0){
  159. $start = 0;
  160. $end = 0;
  161. $randNum = rand (1,$allNum);
  162. foreach ($perList as $k => $val) {
  163. $str = explode(',', $value);
  164. $end += $str[1]*1000;
  165. if($randNum > $start && $randNum <= $end){
  166. $qual = $str[0];
  167. break;
  168. }
  169. $start = $end;
  170. }
  171. }
  172. if($qual >0){
  173. $gemDic = GameConfig::gem();
  174. $gemList = array();
  175. foreach ($gemDic as $key => $value) {
  176. if($value->qual == $qual){
  177. $gemList[] = $value;
  178. }
  179. }
  180. $n = rand(0, count($gemList)-1);
  181. $gem = StoreProc::initGem($gemList[$n]->typeId);
  182. $resultArr[] = $gem->uid;
  183. StoreProc::PutGemInStore2($gem);
  184. }
  185. }
  186. break;
  187. case EnumShopGift::BujiBox_Shop://补给箱
  188. $mo = GameConfig::shop_supply_getItem($typeId);
  189. my_Assert($mo != null, ErrCode::err_const_no);
  190. $list = explode(',', $mo->cost);
  191. my_Assert(StlUtil::dictHasProperty($user->store->items, $list[0]), ErrCode::notenough_item);
  192. $user->store->removeItem($list[0], $list[1]);
  193. $user->baseInfo->Add_Gold($mo->gold);
  194. if(!StlUtil::dictHasProperty($user->privateState->junbeiShopNumRecord, $typeId)){
  195. $user->privateState->junbeiShopNumRecord->$typeId = 0;
  196. }
  197. $num = $user->privateState->junbeiShopNumRecord->$typeId += 1;
  198. if(!StlUtil::dictHasProperty($user->privateState->supplyBichuDic, $typeId)){
  199. $user->privateState->supplyBichuDic->$typeId = 0;
  200. }
  201. $user->privateState->supplyBichuDic->$typeId += 1;
  202. my_Assert($num <= $mo->limitNum, ErrCode::user_shop_LimitNum);
  203. $list = explode(';', $mo->percent);
  204. $randNum = rand(0,10000);
  205. $start = 0;
  206. $end = 0;
  207. $qual = null;
  208. foreach ($list as $item) {
  209. $arr = explode(',', $item);
  210. $per = $arr[2]*100;
  211. $end += $per;
  212. if($randNum >= $start && $randNum <$end){
  213. $qual = $arr[0].','.$arr[1];//品阶,稀有度
  214. break;
  215. }
  216. $start = $end;
  217. }
  218. //必出
  219. $bichu = explode(',', $mo->bichu);
  220. $qArr = explode(',', $qual);
  221. if($qArr[0]==$bichu[1] && $qArr[1] == $bichu[2]){//得到必出,重置数量记录
  222. $user->privateState->supplyBichuDic->$typeId = 0;
  223. $qual = $bichu[1].','.$bichu[2];
  224. } else {
  225. if($user->privateState->supplyBichuDic->$typeId == $bichu[0]){
  226. $qual = $bichu[1].','.$bichu[2];
  227. $user->privateState->supplyBichuDic->$typeId = 0;
  228. }
  229. }
  230. if($qual != null){
  231. $str = explode(',', $qual);
  232. $equipList = array();
  233. $dic = GameConfig::equip();
  234. foreach ($dic as $tid => $item) {
  235. if($item->qual == $str[0] && $item->rarity == $str[1]){
  236. $equipList[] = $item->typeId;
  237. }
  238. }
  239. $rNum = rand(0, count($equipList)-1);
  240. $equipId = $equipList[$rNum];
  241. StoreProc::PutEquipInStore($equipId, 1);
  242. $equip[] = $equipId;
  243. }
  244. break;
  245. case EnumShopGift::Cash_Shop://商城-钻石
  246. $mo = GameConfig::shop_cash_getItem($typeId);
  247. my_Assert($mo != null, ErrCode::err_const_no);
  248. $tag = false;
  249. if(in_array($typeId,$user->privateState->cashShopReceived)){
  250. $tag = true;
  251. }
  252. $reward = $mo->reward;
  253. if(!$tag){
  254. $reward = $mo->reward.';'.$mo->reward;
  255. }
  256. StoreProc::AddMultiItemInStore($reward);
  257. $user->baseInfo->charge_amt += $mo->price;
  258. $user->privateState->cashShopReceived[] = $typeId;
  259. break;
  260. case EnumShopGift::Gold_Shop://商城-金币--可以多次购买
  261. $mo = GameConfig::shop_gold_getItem($typeId);
  262. my_Assert($mo != null, ErrCode::err_const_no);
  263. $tempNum = 0;
  264. foreach ($user->privateState->goldShopReceived as $key) {
  265. if($key == $typeId){
  266. $tempNum += 1;
  267. }
  268. }
  269. if($tempNum >= $mo->freeNum){
  270. my_Assert($user->baseInfo->cash>= $mo->costNum, ErrCode::notenough_cash_msg);
  271. ctx()->baseInfo->Consume_Cash($mo->price);
  272. }
  273. $user->privateState->goldShopReceived[] = $typeId;
  274. StoreProc::AddMultiItemInStore($mo->reward);
  275. break;
  276. default:
  277. break;
  278. }
  279. if(ctx()->baseInfo->charge_amt > 0 && ctx()->baseInfo->fRechargePriceReceived == 0){
  280. StoreProc::AddMultiItemInStore(GameConfig::globalsettings()->FirstRechargePrice);
  281. ctx()->baseInfo->fRechargePriceReceived = 1;
  282. }
  283. ctx($user);
  284. UserProc::updateUserInfo();
  285. return Resp::ok(array(
  286. 'gemBox' => $resultArr,
  287. 'gateGift'=>$gateGift,
  288. 'cash'=>$user->baseInfo->cash,
  289. 'gold'=>$user->baseInfo->gold,
  290. 'privateState' => $user->privateState,
  291. 'store' => $user->store,));
  292. }
  293. /**
  294. * 商城每日重置
  295. */
  296. public static function ShopDailyClear() {
  297. ctx()->privateState->dailyShopReceived = array();
  298. ctx()->privateState->goldShopReceived = array();
  299. ctx()->privateState->junbeiShopNumRecord = new \stdClass();
  300. ctx()->privateState->dailyShop_GuangGaoNum = 0;
  301. ctx()->privateState->dailyShop_GuangGaoTs = 0;
  302. }
  303. /**
  304. * 每日商城随机6个道具
  305. * @return type
  306. */
  307. public static function DailyShopItemRand() {
  308. $dataDic = new \stdClass();
  309. $dic = GameConfig::shop_daily();
  310. if($dic == null){
  311. return;
  312. }
  313. foreach ($dic as $typeId => $mo) {
  314. $type = $mo->type;
  315. if(StlUtil::dictHasProperty($dataDic,$type) ){
  316. $list = $dataDic->$type;
  317. $list[] = $typeId;
  318. $dataDic->$type = $list;
  319. } else {
  320. $list = array();
  321. $list[] = $typeId;
  322. $dataDic->$type = $list;
  323. }
  324. }
  325. $result = array();
  326. $arrType = array(1,2,3,2,4,3);
  327. foreach ($arrType as $k) {
  328. if(StlUtil::dictHasProperty($dataDic,$k) && count($dataDic->$k) > 0){
  329. $arr2 = $dataDic->$k;
  330. if($k == 1){
  331. $result[] = $arr2[0];
  332. continue;
  333. }
  334. $allNum = 0;
  335. foreach ($arr2 as $giftTypeId) {
  336. $allNum += GameConfig::shop_daily_getItem($giftTypeId)->per;
  337. }
  338. $randNum = rand(1,$allNum);
  339. $start = 0;
  340. $end = 0;
  341. $index = 0;
  342. foreach ($arr2 as $itemId) {
  343. $per = GameConfig::shop_daily_getItem($itemId)->per;
  344. $end += $per;
  345. if($randNum >= $start && $randNum < $end){
  346. $result[] = $itemId;
  347. break;
  348. }
  349. $start = $end;
  350. $index += 1;
  351. }
  352. unset($arr2[$index]);
  353. $dataDic->$k = array_values($arr2);
  354. }
  355. }
  356. ctx()->privateState->dailyShopRandItems = $result;
  357. UserProc::updateUserInfo();
  358. }
  359. }