ActiveProc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace loyalsoft;
  3. require_once __DIR__ . '/ActiveProc/CipheredBase32.php'; # 算法库
  4. /**
  5. * 活动模块
  6. */
  7. class ActiveProc {
  8. /**
  9. * 兑换码礼包使用记录
  10. */
  11. const tab_toke_gift = 'tab_token_gift';
  12. /**
  13. * 逻辑分发
  14. * 所有的Proc中必须有这样一个方法
  15. */
  16. public static function procMain() {
  17. switch (req()->cmd) {
  18. case CmdCode::active_day7_drawreward: # 6502 领取七日签到奖励
  19. return self::Day7_DrawReward();
  20. case CmdCode::active_getzonePublicTs: # 6503 查询开服时间戳
  21. return ActiveProc::GetZonePublicTS();
  22. case CmdCode::active_getTodayOnlineInfos: # 6504 查询当天在线时长
  23. return self::getTotayOnlineInfos();
  24. case CmdCode::Task_Tili: # 6508 体力变化
  25. return ActiveProc::RecoveryTili();
  26. case CmdCode::active_token_drawReward: # 6512 兑换码礼包
  27. return self::drawActivePackageByCode();
  28. case CmdCode::active_draw_onlinegift: # 6513 领取在线(时长)礼包
  29. return self::drawOnlineGift();
  30. case CmdCode::active_draw_reggift: # 6514 领取全服注册礼包
  31. return self::drawRegGift();
  32. case CmdCode::active_presentTili: # 6515 领取在线赠送体力
  33. return self::drawTiliGift();
  34. case CmdCode::active_get_regnum: # 6516 查询当前全服注册人数
  35. return self::getRegNumber();
  36. default:
  37. return Resp::err(ErrCode::cmd_err);
  38. }
  39. }
  40. /**
  41. * 每天重置
  42. */
  43. static function DailyReset() {
  44. self::DailyResetDay7Task();
  45. self::ClearOnlineGiftTs();
  46. self::ClearDailyTiliGift();
  47. }
  48. // <editor-fold defaultstate="collapsed" desc=" 赠送体力 ">
  49. /**
  50. * [6515] 领取 赠送体力
  51. */
  52. static function drawTiliGift() {
  53. $user = new Data_UserGame(req()->userInfo->game);
  54. $privateState = new Info_PrivateState($user->privateState);
  55. $hour = date('G');
  56. my_Assert(!in_array($hour, $privateState->dailyDrawedTiliGift), ErrCode::active_hasgetted); # 判断是否已领取
  57. $arr = explode(',', glc()->activity_presentTili_times); # 判断是否满足领取条件
  58. my_Assert(in_array($hour, $arr), ErrCode::active_illegal); # 不满足条件
  59. Data_UserGame::Add_tili(glc()->activity_presentTili_number); # 发放奖励
  60. $privateState->dailyDrawedTiliGift[] = $hour;
  61. req()->userInfo->game->privateState = $privateState;
  62. UserProc::updateUserInfo(); # 回存
  63. return Resp::ok(array(# # 返回值
  64. 'key' => 'ok',
  65. 'record' => $privateState->dailyDrawedTiliGift,
  66. 'tili' => $user->baseInfo->tili,
  67. 'time' => $user->privateState->TiliTime,
  68. ));
  69. }
  70. ////
  71. // </editor-fold>
  72. // <editor-fold defaultstate="collapsed" desc=" 全服注册礼包 ">
  73. /**
  74. * [6516] 查询当前全服注册人数
  75. */
  76. static function getRegNumber() {
  77. return Resp::ok(array(
  78. 'num' => gMem()->get(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid))
  79. ));
  80. }
  81. /**
  82. * [6514] 领取全服注册礼包
  83. */
  84. static function drawRegGift() {
  85. $user = new Data_UserGame(req()->userInfo->game);
  86. $privateState = new Info_PrivateState($user->privateState);
  87. $giftId = req()->paras[0]; # 礼包id
  88. $giftModel = GameConfig::activity_reggift_getItem($giftId); # 取礼包常亮数据
  89. if (null == $giftModel) {
  90. return Resp::err(ErrCode::err_const_no);
  91. }
  92. if (in_array($giftId, $privateState->drawedRegGift)) { # 判断是否已领取
  93. return Resp::err(ErrCode::active_hasgetted);
  94. }
  95. // 判断是否满足领取条件
  96. if (gMem()->get(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid)) < $giftModel->regNumber) {
  97. return Resp::err(ErrCode::active_illegal); # 不满足条件
  98. }
  99. $err = StoreProc::AddMultiItemInStore($giftModel->rewardstr); # 发放奖励
  100. if ($err) {
  101. return Resp::err($err); # 失败
  102. }
  103. $privateState->drawedRegGift[] = $giftId;
  104. $user->privateState = $privateState;
  105. req()->userInfo->game = $user;
  106. UserProc::updateUserInfo(); # 回存
  107. return Resp::ok(array(# # 返回值
  108. 'key' => 'ok',
  109. 'store' => $user->store
  110. ));
  111. }
  112. // </editor-fold>
  113. // <editor-fold defaultstate="collapsed" desc=" 在线礼包 ">
  114. /**
  115. * 【6504】查询当天在线时长信息
  116. */
  117. static function getTotayOnlineInfos() {
  118. return Resp::ok(array(
  119. "onlineGiftIDs" => req()->userInfo->game->privateState->onlineGiftIDs,
  120. "onlineGiftts" => req()->userInfo->game->privateState->onlineGiftts
  121. ));
  122. }
  123. /**
  124. * [6513] 领取连续在线礼包
  125. */
  126. static function drawOnlineGift() {
  127. $user = req()->userInfo->game;
  128. $privateState = new Info_PrivateState($user->privateState);
  129. $giftid = req()->paras[0]; # 提取参数: 礼包的编号
  130. $giftData = GameConfig::activity_onlinegift_getItem($giftid); # 在线礼包常量数据
  131. my_Assert(null != $giftData, ErrCode::onlinegift_constno_err); # 防御找不到配置数据
  132. my_Assert($privateState->onlineGiftts >= $giftData->onlineSec, # # 时间未到,不可以领取
  133. ErrCode::onlinegift_timenotenough);
  134. if (!CommUtil::isPropertyExists($privateState, "onlineGiftIDs") #
  135. || !is_array($privateState->onlineGiftIDs)) {
  136. $privateState->onlineGiftIDs = array(); # 初始化 这个字段
  137. }
  138. // var_dump($privateState->onlineGiftIDs);
  139. my_Assert(!in_array($giftid, $privateState->onlineGiftIDs), # # 判断礼包ID是否已经领取
  140. ErrCode::active_hasgetted);
  141. $err = StoreProc::AddMultiItemInStore($giftData->rewardstr); # 发放奖励
  142. if ($err) {
  143. return Resp::err($err, '发放奖励失败');
  144. }
  145. $privateState->onlineGiftIDs[] = $giftid; # 记录已领礼包ID
  146. $user->privateState = $privateState;
  147. req()->userInfo->game = $user;
  148. UserProc::updateUserInfo(); # 回写用户数据
  149. return Resp::ok(array(
  150. 'onlineGiftts' => $privateState->onlineGiftts,
  151. 'onlineGiftIDs' => $privateState->onlineGiftIDs,
  152. "reward" => $packageInfo->reward,
  153. 'gold' => $user->baseInfo->gold,
  154. 'cash' => $user->baseInfo->cash,
  155. 'tili' => $user->baseInfo->tili,
  156. 'store' => $user->store,
  157. 'hero' => $user->heros
  158. )); # 设置返回值
  159. }
  160. /**
  161. * 清理/重置 在线礼包时间戳
  162. */
  163. static private function ClearOnlineGiftTs() {
  164. req()->userInfo->game->privateState->onlineGiftts = 0;
  165. req()->userInfo->game->privateState->onlineGiftIDs = array();
  166. }
  167. // </editor-fold>
  168. // <editor-fold defaultstate="collapsed" desc=" 兑换码 ">
  169. /**
  170. * [6512]凭兑换码领取礼包
  171. */
  172. static function drawActivePackageByCode() {
  173. $user = new Data_UserGame(req()->userInfo->game);
  174. $privateState = new Info_PrivateState($user->privateState); # 快速访问
  175. list( $activeId, $codestring) = req()->paras; # 取参数 活动id, 兑换码
  176. $active = GameConfig::activity_getItem($activeId); # 活动数据
  177. my_Assert(null != $active, ErrCode::active_const_no_err);
  178. my_Assert($active->startts <= now() && $active->endts >= now(), ErrCode::active_time); # 校验开放时间
  179. my_Assert(preg_match("/^[a-kmnp-z2-9]{10}$/", $codestring), ErrCode::active_activecode_format); # 基础格式校验(10个特定字符)
  180. $activeCode = CipheredBase32::Decode($codestring); # 解码
  181. $codePlatStr = GameConstants::GetPlatStringByActivteCode($activeCode); # platstr
  182. my_Assert(GameConstants::AllPlatStr == $codePlatStr # # 忽略全平台礼包
  183. || req()->userInfo->getPlatStr() == $codePlatStr, # # 平台字符串必须相符
  184. ErrCode::active_activecode_plat); # # 平台错误
  185. my_Assert(is_int($activeCode->number) # # 编号为int值
  186. && $activeCode->number >= 1 && $activeCode->number <= 50000, # # 检查 兑换码的编号范围0~50000
  187. ErrCode::active_activecode_format);
  188. $packageID = $activeCode->package; # 礼包id
  189. $packageInfo = GameConfig::tokenGift_getItem($packageID); # 礼包常量数据
  190. my_Assert(null != $packageInfo, ErrCode::err_const_no); # 防御
  191. my_Assert($packageInfo->expirets >= now() && $packageInfo->startTs <= now(),
  192. ErrCode::active_activecode_outtime); # 激活码已经失效,或者礼包尚未开启
  193. my_Assert(!in_array($packageID, $privateState->usedTokens), ErrCode::active_hasgetted); # 已经领取过该礼包了
  194. my_Assert(!self::checkActiveCodeIsUsed($activeCode), ErrCode::active_activecode_used); # 检查 该激活码是否已经使用过了
  195. $err = StoreProc::AddMultiItemInStore($packageInfo->reward); # 发放礼包
  196. my_Assert(ErrCode::ok == $err, $err); # 防御发放礼包过程出错
  197. req()->userInfo->game->privateState->usedTokens[] = $packageID; # 记录领取记录
  198. $ok = self::setActiveCodeUserRecord($activeCode, req()->uid); # 插入数据库
  199. my_Assert($ok, ErrCode::err_db); # 数据库操作失败- 重试
  200. UserProc::updateUserInfo(); # 回存玩家数据
  201. $ret = array(# # 返回值
  202. "plat" => $codePlatStr,
  203. "packageId" => $activeCode->package,
  204. "reward" => $packageInfo->reward,
  205. 'gold' => $user->baseInfo->gold,
  206. 'cash' => $user->baseInfo->cash,
  207. 'tili' => $user->baseInfo->tili,
  208. 'store' => $user->store,
  209. 'hero' => $user->heros
  210. );
  211. return Resp::ok($ret); # 返回成功信息
  212. }
  213. /**
  214. * 检查兑换码是否已经使用过了
  215. * @param ActiveCode $activeCode
  216. * @return boolean true : 已经用过了, false : 激活码尚未使用过.
  217. */
  218. static function checkActiveCodeIsUsed($activeCode) {
  219. $sql = sprintf("`plat`='%d' and `package`='%d' and `number`='%d'", # # where 子句
  220. $activeCode->plat, $activeCode->package, $activeCode->number);
  221. $rows = daoInst()
  222. ->select()
  223. ->from(self::tab_toke_gift) # 表名
  224. ->where($sql)
  225. ->count();
  226. return $rows > 0;
  227. }
  228. /**
  229. * 将兑换码添加到使用记录中
  230. * @param ActiveCode $actvieCode
  231. * @param string $uid
  232. * @return boolean true: 操作成功, false: 操作失败
  233. */
  234. static function setActiveCodeUserRecord($actvieCode, $uid) {
  235. $actvieCode->uid = $uid; # 添加uid
  236. $ret = daoInst()
  237. ->insert(self::tab_toke_gift) # 表名
  238. ->data($actvieCode)
  239. ->exec();
  240. return $ret > 0;
  241. }
  242. // </editor-fold>
  243. /**
  244. * [6503] 查询当前分区开放时间戳
  245. */
  246. private static function GetZonePublicTS() {
  247. $zoneInfo = GameConfig::zonelist_getItem(req()->zoneid);
  248. return Resp::ok($zoneInfo->publicTs);
  249. }
  250. // <editor-fold defaultstate="collapsed" desc=" 体力 ">
  251. /**
  252. * [6508]
  253. */
  254. public static function RecoveryTili() {
  255. $useTili = req()->paras[0];
  256. ActiveProc::ChangeTili($useTili);
  257. UserProc::updateUserInfo();
  258. $result = array(
  259. 'time' => req()->userInfo->game->privateState->TiliTime,
  260. 'tili' => req()->userInfo->game->baseInfo->tili
  261. );
  262. $resp = Resp::ok($result);
  263. return $resp;
  264. }
  265. /**
  266. * 清理每天的赠送体力领取记录
  267. */
  268. private static function ClearDailyTiliGift() {
  269. req()->userInfo->game->privateState->dailyDrawedTiliGift = ArrayInit();
  270. }
  271. /**
  272. * 修改玩家体力
  273. * @param int $useTili $useTili>0代表增加体力, $useTili<0代表消耗体力
  274. */
  275. public static function ChangeTili($useTili) {
  276. $user = req()->userInfo->game;
  277. $moreTili = 0; # 超上限的体力值
  278. // 如果没有tilitime这个值,说明是以前老用户新登录的,为了纠错在判断里面补值.(正常用户不会进入判断内部)
  279. if (!CommUtil::isPropertyExists($user->privateState, "TiliTime")) {
  280. $user->privateState->TiliTime = time() - 120;
  281. }
  282. if ($user->baseInfo->tili > 120) { # 如果该账户体力值本身是大于上限的,获取一下超出的体力值
  283. $moreTili = $user->baseInfo->tili - 120; # 用moreTili保存一下额外的体力值
  284. }
  285. $useTime = time() - $user->privateState->TiliTime; # 距离上次修改体力值的时间
  286. if ($useTime > 300) { # 该账户超过了单位体力时间,需要重新计算体力值
  287. if ($moreTili <= 0) {
  288. $user->baseInfo->tili += (int) ( (int) ($useTime / 300));
  289. if ($user->baseInfo->tili > 120) { # 体力值上限
  290. $user->baseInfo->tili = 120;
  291. } // 这代码真烂,以后用的时候再修复吧... -gwang 20200110154858
  292. }
  293. $user->privateState->TiliTime = time();
  294. }
  295. if ($useTili < 0) { # 如果是消耗体力
  296. my_Assert(abs($useTili) < $user->baseInfo->tili, ErrCode::notenough_tili); # 体力不足
  297. $user->baseInfo->tili += $useTili; # 扣除体力
  298. } else if ($useTili > 0) { # 如果是购买增加体力
  299. $user->baseInfo->tili += $useTili;
  300. }
  301. return ErrCode::ok;
  302. }
  303. // </editor-fold>
  304. //
  305. //
  306. // <editor-fold defaultstate="collapsed" desc=" 7日签到 ">
  307. /**
  308. * 重置7日签到相关计数器
  309. */
  310. static function DailyResetDay7Task() {
  311. req()->userInfo->game->gates->Times = 0;
  312. $typeID = 6; # 7日活动
  313. $ac = GameConfig::activity_getItem($typeID);
  314. my_Assert(null != $ac, ErrCode::err_const_no);
  315. $startDay = tsDay($ac->startts);
  316. $newLoginDays = array_filter(req()->userInfo->game->privateState->LoginDays, # 剔除不符合时间的登录记录
  317. function ($tsday)use ($startDay) {
  318. return $startDay <= $tsday;
  319. }
  320. );
  321. req()->userInfo->game->privateState->LoginDays = $newLoginDays; #
  322. if (count(req()->userInfo->game->privateState->LoginDays) < 7) {
  323. req()->userInfo->game->privateState->LoginDays[] = tsDay();
  324. }
  325. }
  326. /**
  327. * [6502] 领取七日签到奖励
  328. */
  329. static function Day7_DrawReward() {
  330. // 设计7日数据结构
  331. list($day) = req()->paras; # 参数: 领取第x天的奖励
  332. $user = req()->userInfo->game;
  333. $private = new Info_PrivateState($user->privateState); # 私有数据
  334. my_Assert(count($private->LoginDays) >= $day, ErrCode::active_day7_totaldays);
  335. my_Assert(!in_array($day, $private->day7_drawed), ErrCode::active_hasgetted);
  336. $day_rwd = GameConfig::activity_day7_getItem($day); # 查询奖励数据
  337. my_Assert(null != $day_rwd, ErrCode::err_const_no); # 防御找不到配置
  338. StoreProc::AddMultiItemInStore($day_rwd->reward); # 发放奖励
  339. $private->day7_drawed[] = $day; # 添加领取记录
  340. $user->privateState = $private; # 更新数据
  341. UserProc::updateUserInfo(); # 回存
  342. return Resp::ok(array(
  343. 'gold' => $user->baseInfo->gold,
  344. 'cash' => $user->baseInfo->cash,
  345. 'tili' => $user->baseInfo->tili,
  346. 'store' => $user->store,
  347. 'hero' => $user->heros
  348. )); # 返回值
  349. }
  350. // </editor-fold>
  351. //
  352. }