ActiveProc.php 16 KB

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