ActiveProc.php 18 KB

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