ShopProc.php 16 KB

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