ActiveProc.php 19 KB

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