Lotterys.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * 抽奖 模块
  5. * @version
  6. * 1.0.1 英雄抽奖, 奖品限定为英雄, 2017.07.20 by --gwang
  7. * 1.0.0 从active模块拆分出来, Created at 2017-7-20. by --gwang
  8. * @author gwang (mail@wanggangzero.cn)
  9. * @copyright ? 2017-7-20, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  10. */
  11. class Lotterys {
  12. /**
  13. * 改造方案:
  14. * 1. 抽奖添加卡槽参数, 告诉服务端结果放到哪个卡槽中.
  15. * 2. 抽奖时验证卡槽是否为空, 不空不可继续.
  16. * 3. 抽奖结果不直接发放到玩家身上, 而是放到卡槽中, 等待读条完毕, 手动领取.
  17. * 4. 增加领取抽奖结果api, 验证冷却完毕, 提取到玩家身上.
  18. */
  19. /**
  20. * [6511] 抽奖 - 领取保底奖励, 奖品直接发放到玩家身上, 不计冷却时间
  21. * @param Req $req
  22. */
  23. static function baodiLottery($req) {
  24. # 保底奖励领取方案: 不需要扣除花费, 只需清零抽奖次数.
  25. $user = $req->userInfo->game;
  26. $lottery = new UserLotteryModel($req->userInfo->game->privateState->lottery);
  27. if (count($req->paras) < 1) { # 提取参数
  28. return Resp::err(ErrCode::parasnotenough_msg);
  29. }
  30. $lot_type = $req->paras[0]; # 抽奖类型: 金币,钻石,友情点... (物品id)
  31. $cishu = 10; # 想要领取的奖励id(1,2,3)等阶对应需要满足的抽奖次数
  32. if (count($req->paras) > 1) { # 如果没有传第二个参数, 默认给1阶奖励(10次),
  33. $cishu = $req->paras[1];
  34. }
  35. if ($lottery->baodiLotterys->$lot_type < $cishu) { # 检查累计次数
  36. return Resp::err(ErrCode::lottery_baodi_times_lt);
  37. }
  38. $baodi = GameConfig::choujiang_baodi_getItem($lot_type, $cishu); # 保底奖池
  39. if (null == $baodi) { # 找不到
  40. return Resp::err(ErrCode::err_const_no);
  41. }
  42. $settings = GameConfig::choujiang_settings_getItem($lot_type); # 设定
  43. if (null == $settings) { # 找不到
  44. return Resp::err(ErrCode::err_const_no);
  45. }
  46. # 当达到, 最后一次的时候, 执行减法扣除对应的值?或者其他算法.
  47. # 保底的奖励物品, 要切换, 换成不同抽奖道具, 对应不同的奖品池, 因为道具价值不一样.
  48. if ($lottery->baodiDrawed->$lot_type >= $cishu) {
  49. return Resp::err(ErrCode::active_hasgetted);
  50. }
  51. $lottery->baodiDrawed->$lot_type = $cishu;
  52. if ($cishu == $settings->baodiMax) {
  53. $lottery->baodiDrawed->$lot_type = 0; # 防刷, 已经领过的次数要加记录
  54. $lottery->baodiLotterys->$lot_type -= $cishu; # 清零次数
  55. }
  56. $err = self::Dice($baodi, 1, $rewardstr); # 投骰子
  57. if ($err) {
  58. return Resp::err($err);
  59. }
  60. if (strlen($rewardstr) <= 0) { # 抽奖结果为空
  61. return Resp::err(ErrCode::err_innerfault);
  62. }
  63. $err = StoreProc::AddMultiItemInStore($req, $rewardstr, 3); # 发放物品
  64. if ($err) {
  65. return Resp::err($cishu);
  66. }
  67. $req->userInfo->game->privateState->lottery = $lottery;
  68. UserProc::updateUserInfo($req); # 回存数据
  69. // var_dump($lottery);
  70. return Resp::ok(array('err' => 0, # # 返回值
  71. 'store' => $user->store,
  72. 'heros' => $user->heros,
  73. 'reward' => $rewardstr,
  74. 'baodi' => $lottery->baodiLotterys,
  75. 'baodidrawed' => $lottery->baodiDrawed
  76. ));
  77. }
  78. /**
  79. * [6510] 活动 - 抽奖 - 新手引导定制版
  80. * @param Req $req
  81. */
  82. static function demoLottery($req) {
  83. $user = $req->userInfo->game;
  84. if (count($req->paras) < 1) { # 参数数量不足
  85. return Resp::err(ErrCode::parasnotenough_msg);
  86. }
  87. # # 提取参数
  88. $type = $req->paras[0]; # 金币,钻石,友情点...
  89. $number = 1; # 抽奖次数
  90. if (count($req->paras) > 1) { # 可选参数
  91. $number = $req->paras[1];
  92. }
  93. # 记录抽奖次数
  94. $err = self::AddLotteryTimes($user->privateState->lottery, $type, $left_cishu, $number);
  95. if ($err) {
  96. return Resp::err($err);
  97. }
  98. $err = self::ConsumeLotteryCost($user, $type, $number);
  99. if ($err) {
  100. return Resp::err($err);
  101. }
  102. $req->userInfo->game = $user; // 回写
  103. $err = self::Dice(JsonUtil::decode(glc()->choujiang_guide_items), $number, $rewardstr);
  104. if ($err) { // 出错了
  105. return Resp::err($err);
  106. }
  107. if (strlen($rewardstr) <= 0) {
  108. return Resp::err(ErrCode::err_innerfault);
  109. }
  110. $err = StoreProc::AddMultiItemInStore($req, $rewardstr, 3); # 发放物品
  111. if ($err) {
  112. return Resp::err($err);
  113. }
  114. UserProc::updateUserInfo($req); # 回存数据
  115. return Resp::ok(array('err' => 0, # 返回值
  116. 'store' => $user->store,
  117. 'heros' => $user->heros,
  118. 'reward' => $rewardstr,
  119. 'baodi' => $user->privateState->lottery->baodiLotterys,
  120. 'left_cishu' => $left_cishu));
  121. }
  122. //
  123. // <editor-fold defaultstate="collapsed" desc=" 抽奖 - 经典版">
  124. /**
  125. * [6509] 活动 - 抽奖
  126. * @param Req $req
  127. * @ deprecated since version 1.0.1 新版只出英雄, . . .
  128. * Ps. 1.0.0版可支持产出道具
  129. */
  130. static function Lottery($req) {
  131. $user = $req->userInfo->game;
  132. if (count($req->paras) < 1) { # 提取参数
  133. return Resp::err(ErrCode::parasnotenough_msg);
  134. }
  135. $type = $req->paras[0]; # 金币,钻石,友情点... (物品id)
  136. $number = 1; # 抽奖次数
  137. if (count($req->paras) > 1) { # 可选参数
  138. $number = $req->paras[1];
  139. }
  140. # 记录抽奖次数
  141. $err = self::AddLotteryTimes($user->privateState->lottery, $type, $left_cishu, $number);
  142. if ($err) {
  143. return Resp::err($err);
  144. }
  145. # 扣除抽奖花费
  146. $err = self::ConsumeLotteryCost($user, $type, $number);
  147. if ($err) {
  148. return Resp::err($err);
  149. }
  150. # 回写user数据
  151. $req->userInfo->game = $user;
  152. # 对奖池依解锁等级进行过滤
  153. $arr = self::FilterPrizepool($req, GameConfig::choujiang_getItem($type));
  154. $err = self::Dice($arr, $number, $rewardstr); # 投骰子
  155. if ($err) { # 出错了
  156. return Resp::err($err);
  157. }
  158. if (strlen($rewardstr) <= 0) { # 抽奖结果为空
  159. return Resp::err(ErrCode::err_innerfault);
  160. }
  161. $err = StoreProc::AddMultiItemInStore($req, $rewardstr, 3); # 发放物品
  162. if ($err) { # 出错了
  163. return Resp::err($err);
  164. }
  165. UserProc::updateUserInfo($req); # 回存数据
  166. return Resp::ok(array('err' => 0, # # 返回值
  167. 'store' => $user->store,
  168. 'heros' => $user->heros,
  169. 'reward' => $rewardstr,
  170. 'baodi' => $user->privateState->lottery->baodiLotterys,
  171. 'left_cishu' => $left_cishu));
  172. }
  173. // </editor-fold>
  174. //
  175. // <editor-fold defaultstate="collapsed" desc=" 抽奖 - 新生到校版">
  176. /**
  177. * [6513] 活动 - 抽奖 查询信息(卡槽)
  178. * @param Req $req
  179. */
  180. static function Lottery_Info($req) {
  181. return Resp::ok(array(
  182. 'lottery' => new UserLotteryModel($req->userInfo->game->privateState->lottery)
  183. ));
  184. }
  185. /**
  186. * [6515] 活动 - 抽奖 缩短搜索时间
  187. * @param Req $req
  188. */
  189. static function Lottery_speedup_search($req) {
  190. if (count($req->paras) < 1) { # 提取参数
  191. return Resp::err(ErrCode::parasnotenough_msg);
  192. }
  193. $id = $req->paras[0]; # 卡槽id: 0,1,2,
  194. $slotid = 'slot' . $id;
  195. $lottery = new UserLotteryModel($req->userInfo->game->privateState->lottery);
  196. $slot = new LotterySlotModel($lottery->$slotid);
  197. if ($slot->locked) { # 卡槽尚未解锁
  198. return Resp::err(ErrCode::err_lottery_slot_locked);
  199. }
  200. if (!$slot->herotype) { # 卡槽空
  201. return Resp::err(ErrCode::err_lottery_slot_emputy);
  202. }
  203. $itemid = glc()->choujiang_dachequan_itemid; # 打车券的itemid
  204. if ($slot->opents > now()) { # 已经到时间了,无需扣除,
  205. # 先不替客户省钱了, 只要客户端发送请求就执行.
  206. $err = StoreProc::removeItemFromStore($req->userInfo->game->store, $itemid, 1);
  207. if ($err) {
  208. return Resp::err($err);
  209. }
  210. $slot->opents = now(-15); # 往前偏移15秒
  211. }
  212. $lottery->$slotid = $slot;
  213. $req->userInfo->game->privateState->lottery = $lottery;
  214. UserProc::updateUserInfo($req); # 回写数据
  215. return Resp::ok(array('slot' => $slot)); # 返回
  216. }
  217. /**
  218. * [6514] 活动 - 抽奖 解锁卡槽
  219. * @param Req $req
  220. */
  221. static function Lottery_unlock_Slot($req) {
  222. if (count($req->paras) < 1) { # 提取参数
  223. return Resp::err(ErrCode::parasnotenough_msg);
  224. }
  225. $id = $req->paras[0]; # 卡槽id: 0,1,2,
  226. $slotid = 'slot' . $id;
  227. $lottery = new UserLotteryModel($req->userInfo->game->privateState->lottery);
  228. $slot = new LotterySlotModel($lottery->$slotid);
  229. if (!$slot->locked) { # 卡槽已经解锁
  230. return Resp::err(ErrCode::err_lottery_slot_unlocked);
  231. }
  232. # 判断等级, 等级不足则扣除钻石
  233. $sm_slot = GameConfig::choujiang_slot_getItem($id); # slot 配置
  234. if ($req->userInfo->game->level < $sm_slot->level) { # 等级不足时需要扣除钻石
  235. if (!UserGameModel::Consume_Cash($req->userInfo->game, $sm_slot->cash)) {
  236. return Resp::err(ErrCode::notenough_cash_msg);
  237. }
  238. }
  239. $slot->locked = false; # 解锁卡槽
  240. $lottery->$slotid = $slot;
  241. $req->userInfo->game->privateState->lottery = $lottery;
  242. UserProc::updateUserInfo($req); # 回写数据
  243. return Resp::ok(array('slot' => $slot)); # 返回
  244. }
  245. /**
  246. * [6512] 活动 - 抽奖 领取到校英雄
  247. * @param Req $req
  248. */
  249. static function drawReachHero($req) {
  250. if (count($req->paras) < 1) { # 提取参数
  251. return Resp::err(ErrCode::parasnotenough_msg);
  252. }
  253. $slotid = 'slot' . $req->paras[0]; # 卡槽id: 0,1,2,
  254. $lottery = new UserLotteryModel($req->userInfo->game->privateState->lottery);
  255. $slot = new LotterySlotModel($lottery->$slotid);
  256. if ($slot->locked) { # 卡槽尚未解锁
  257. return Resp::err(ErrCode::err_lottery_slot_locked);
  258. }
  259. if ($slot->opents > now()) { # 卡槽时间未到
  260. return Resp::err(ErrCode::err_lottery_slot_time);
  261. }
  262. if (!$slot->herotype || !$slot->herostar) { # 卡槽数据异常
  263. return Resp::err(ErrCode::err_lottery_slot_emputy);
  264. }
  265. # 提取英雄...
  266. $he = HeroProc::AddHeroWithStar($req, $slot->herotype, $slot->herostar);
  267. $slot->Clear();
  268. $lottery->$slotid = $slot;
  269. $req->userInfo->game->privateState->lottery = $lottery; # 数据回写
  270. UserProc::updateUserInfo($req);
  271. return Resp::ok(array(
  272. 'heros' => $req->userInfo->game->heros,
  273. 'hero' => $he,
  274. 'slot' => $slot
  275. ));
  276. }
  277. /**
  278. * [6509] 活动 - 抽奖
  279. * @param Req $req
  280. */
  281. static function Lottery_Hero($req) {
  282. if (count($req->paras) < 1) { # 提取参数
  283. return Resp::err(ErrCode::parasnotenough_msg);
  284. }
  285. $type = $req->paras[0]; # 金币,钻石,友情点... (物品id)
  286. $slotid = 'slot' . $req->paras[1]; # 卡槽id: 0,1,2,3
  287. #
  288. // 检查卡槽是否空闲
  289. $user = $req->userInfo->game;
  290. $lottery = new UserLotteryModel($user->privateState->lottery);
  291. $slot = new LotterySlotModel($lottery->$slotid);
  292. if ($slot->locked) { # 卡槽尚未解锁
  293. return Resp::err(ErrCode::err_lottery_slot_locked);
  294. }
  295. if ($slot->opents > now() || $slot->herotype) { # 卡槽使用中
  296. return Resp::err(ErrCode::err_lottery_slot_not_emputy);
  297. }
  298. $number = 1; # 抽奖次数, 固定为1
  299. $err = self::AddLotteryTimes($lottery, $type, $left_cishu, $number); # 记录抽奖次数
  300. if ($err) {
  301. return Resp::err($err);
  302. }
  303. $err = self::ConsumeLotteryCost($user, $type, $number); # 扣除抽奖花费(通知书)
  304. if ($err) {
  305. return Resp::err($err);
  306. }
  307. $err = self::Dice(GameConfig::choujiang_getItem($type), # 投骰子
  308. $number, $rewardstr);
  309. if ($err) {
  310. return Resp::err($err);
  311. }
  312. if (strlen($rewardstr) <= 0) { # 抽奖结果为空
  313. return Resp::err(ErrCode::err_innerfault);
  314. }
  315. # 依据抽奖次数找到星级数据库
  316. $xingji = self::FindStarSource($lottery->todayLotterys->$type);
  317. if (!$xingji) {
  318. return Resp::err(ErrCode::err_const_no, "抽奖-星级");
  319. }
  320. self::DiceOutStar($xingji, $star); # 计算星级
  321. $val = explode(",", $rewardstr);
  322. $itemId = $val[0];
  323. # 发放英雄(异化为英雄放入卡槽...
  324. // $he = HeroProc::AddHeroWithStar($req, $itemId, $star);
  325. $slot->herostar = $star;
  326. $slot->herotype = $itemId;
  327. $slot->opents = now() + self::DiceOutCoolingTiem(GameConfig::choujiang_searchtime_getItem($star));
  328. $lottery->$slotid = $slot;
  329. $user->privateState->lottery = $lottery; # 回写数据
  330. $req->userInfo->game = $user; # 回写user数据
  331. UserProc::updateUserInfo($req); # 回存数据
  332. return Resp::ok(array('err' => 0, # # 返回值
  333. 'store' => $user->store,
  334. 'slot' => $slot, # # 卡槽信息
  335. 'left_cishu' => $left_cishu));
  336. }
  337. // </editor-fold>
  338. //
  339. // <editor-fold defaultstate="collapsed" desc=" -==- 辅助函数 -==- ">
  340. /**
  341. * 增加抽奖次数
  342. * @param LotterySlotModel $user
  343. * @param type $type
  344. * @param int $left_cishu out
  345. * @param type $number
  346. * @return int errno
  347. */
  348. static function AddLotteryTimes(&$lottery, $type, &$left_cishu, $number = 1) {
  349. $todaylotterys = $lottery->todayLotterys;
  350. $totalLottreys = $lottery->totalLotterys;
  351. $baodiLotterys = $lottery->baodiLotterys;
  352. if (!$todaylotterys) {
  353. $todaylotterys = JsonUtil::decode(sprintf('{"%s":0}', $type));
  354. }
  355. if (!$totalLottreys) {
  356. $totalLottreys = JsonUtil::decode(sprintf('{"%s":0}', $type));
  357. }
  358. if (!$baodiLotterys) {
  359. $baodiLotterys = JsonUtil::decode(sprintf('{"%s":0}', $type));
  360. }
  361. if (!isset($todaylotterys->$type)) {
  362. $todaylotterys->$type = 0;
  363. }
  364. if (!isset($totalLottreys->$type)) {
  365. $totalLottreys->$type = 0;
  366. }
  367. if (!isset($baodiLotterys->$type)) {
  368. $baodiLotterys->$type = 0;
  369. }
  370. $todaylotterys->$type += $number;
  371. $totalLottreys->$type += $number;
  372. $baodiLotterys->$type += $number;
  373. $settings = GameConfig::choujiang_settings_getItem($type);
  374. if (null == $settings) {
  375. return ErrCode::err_const_no;
  376. }
  377. $limit = $settings->dailyLimit;
  378. if ($limit > 0 && $todaylotterys->$type > $limit) {
  379. return ErrCode::err_lottery_daily_limit; # 达到每次次数限制, (@广电总局)
  380. }
  381. $left_cishu = $limit - $todaylotterys->$type;
  382. if ($left_cishu < 0) {
  383. $left_cishu = 0;
  384. }
  385. $lottery->todayLotterys = $todaylotterys;
  386. $lottery->totalLotterys = $totalLottreys;
  387. $lottery->baodiLotterys = $baodiLotterys;
  388. return ErrCode::ok;
  389. }
  390. /**
  391. * 扣除抽奖花费
  392. * @param UserGameModel $user
  393. * @param type $type
  394. * @param type $number
  395. * @return int errno
  396. */
  397. static function ConsumeLotteryCost($user, $type, $number = 1) {
  398. $settings = GameConfig::choujiang_settings_getItem($type);
  399. if (null == $settings) {
  400. return ErrCode::err_const_no;
  401. }
  402. $amt = $settings->price;
  403. if ($number > 1) { # n连抽
  404. $amt *= $number;
  405. }
  406. switch ($type) { // 扣除花费
  407. case META_GOLD_ITEMID: # 金币抽奖
  408. if (!UserGameModel::Consume_Gold($user, $amt)) {
  409. return ErrCode::notenough_gold_msg;
  410. }
  411. break;
  412. case META_CASH_ITEMID: # 钻石抽奖
  413. if (!UserGameModel::Consume_Cash($user, $amt)) {
  414. return ErrCode::notenough_cash_msg; # 钻石余额不足
  415. }
  416. break;
  417. case META_FriendShipPoit_ItemId: # 友情点
  418. if (!UserGameModel::Consume_FriendShipPoint($user, $amt)) {
  419. return ErrCode::notenough_friendshippoint;
  420. }
  421. break;
  422. default: # 从包裹中消耗道具
  423. $err = StoreProc::removeItemFromStore($user->store, $type, $amt);
  424. if ($err) {
  425. return $err;
  426. }
  427. break;
  428. }
  429. return ErrCode::ok;
  430. }
  431. /**
  432. * 过滤奖池
  433. * @param Req $req
  434. * @param array $arr 奖池
  435. */
  436. static function FilterPrizepool($req, $arr) {
  437. # 等级限制
  438. $ret = array_filter($arr, function ($value) use($req) {
  439. return($req->userInfo->game->level >= $value->unlock_level);
  440. });
  441. return $ret;
  442. }
  443. /**
  444. * 投骰子
  445. * @param assoc_array $arr 抽奖物品概率
  446. * @param int $number 连抽次数
  447. * @return string itemid,num;itemid,num;...
  448. */
  449. static function Dice($arr, $number, &$rewardstr) {
  450. $max = 0; # 计算物品权重总和
  451. array_walk($arr, function ($value) use(&$max) {
  452. $max += $value->probability;
  453. });
  454. if (!$max) { # 配置数据有问题
  455. return ErrCode::err_const_no;
  456. }
  457. $reward = ArrayInit();
  458. for ($i = 0; $i < $number; $i++) {
  459. $rnd = CommUtil::random(1, $max); # 投骰子
  460. $start = 0;
  461. $rew = null;
  462. foreach ($arr as $item) { # 循环判断落入那个物品上
  463. if ($start < $rnd && $rnd <= $start + $item->probability) { # 落入区间
  464. $rew = $item; # 记录物品
  465. break;
  466. }
  467. $start += $item->probability; # 继续判断是否落入下一物品的区间
  468. } # foreach end
  469. if (!$rew) {
  470. return ErrCode::lottery_noselecteditem;
  471. }
  472. $reward[] = implode(',', array($rew->rewardItemId, $rew->number));
  473. } # for end
  474. $rewardstr = implode(';', $reward);
  475. return ErrCode::ok;
  476. }
  477. /**
  478. * 找到计算星级的数据源
  479. * @param type $cishu
  480. * @return array of sm_choujiang_xingji
  481. */
  482. public static function FindStarSource($cishu) {
  483. foreach (GameConfig::choujiang_xingji() as $ci => $arr) {
  484. if ($cishu <= $ci) { # 找到对应的阶段了
  485. return $arr;
  486. }
  487. }
  488. return JsonUtil::decode('[{"star":1,"probability":100}]'); # 默认值, 爽吧
  489. }
  490. /**
  491. * @deprecated since version now
  492. * 投骰子获得一个卡牌的星级
  493. * @param type $arr
  494. * @param type $ret
  495. * @return type
  496. */
  497. public static function DiceOutStar($arr, &$ret) {
  498. $max = 0;
  499. array_walk($arr, function ($value) use(&$max) {
  500. $max += $value->probability; # 计算物品权重总和
  501. });
  502. if (!$max) { # 配置数据有问题
  503. return ErrCode::err_const_no;
  504. }
  505. $rnd = CommUtil::random(1, $max); # 投骰子
  506. $start = 0; # 起点
  507. foreach ($arr as $item) { # 循环判断落入那个物品上
  508. if ($start < $rnd && $rnd <= $start + $item->probability) { # 落入区间
  509. $ret = $item->star; # 设置结果
  510. return ErrCode::ok;
  511. }
  512. $start += $item->probability; # 继续判断是否落入下一物品的区间
  513. } # foreach end
  514. return ErrCode::lottery_noselecteditem; # 没有找到对应的结果
  515. }
  516. /**
  517. * 依据星级投骰子获得一个到校时间
  518. * @param type $arr
  519. * @return type
  520. */
  521. public static function DiceOutCoolingTiem($arr) {
  522. $max = 0;
  523. array_walk($arr, function ($value) use(&$max) {
  524. $max += $value->probability; # 计算物品权重总和
  525. });
  526. $rnd = CommUtil::random(1, $max); # 投骰子
  527. $start = 0; # 起点
  528. foreach ($arr as $item) { # 循环判断落入那个物品上
  529. if ($start < $rnd && $rnd <= $start + $item->probability) { # 落入区间
  530. return $item->time; # 设置结果, 返回值
  531. }
  532. $start += $item->probability; # 继续判断是否落入下一物品的区间
  533. } # foreach end
  534. return 780; # 没有找到对应的结果, 给一个默认值13分钟
  535. }
  536. /**
  537. * 清理每天的抽奖次数记录
  538. * @param Req $req
  539. */
  540. public static function ClearLotteryRecord($req) {
  541. $req->userInfo->game->privateState->lottery->todayLotterys = ObjectInit();
  542. }
  543. // </editor-fold>
  544. //
  545. }