ShopProc.php 16 KB

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