ShopProc.php 18 KB

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