ShopProc.php 17 KB

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