ActiveProc.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace loyalsoft;
  8. /**
  9. * Description of ActiveProc
  10. *
  11. * @author c'y'zhao
  12. */
  13. class ActiveProc {
  14. /**
  15. * 逻辑分发
  16. * 所有的Proc中必须有这样一个方法
  17. * @param Req $req
  18. */
  19. public static function procMain($req) {
  20. switch ($req->cmd) {
  21. case CmdCode::cmd_active_day7_drawreward: # 6101 签到
  22. return ActiveProc::Day7_DrawReward();
  23. case CmdCode::cmd_active_drawPackageByCode: # 6102 兑换码礼包
  24. return ActiveProc::drawPackageByCode();
  25. default:
  26. Err(ErrCode::cmd_err);
  27. }
  28. }
  29. /**
  30. * 6102 兑换码礼包
  31. */
  32. static function drawPackageByCode() {
  33. list($activeId, $codestring) = req()->paras; # 取参数 活动id, 兑换码
  34. $codePlatStr = "";
  35. $packageID = 0;
  36. if (self::PublicTokenCodeCheck($codestring)) {
  37. $packageInfo = GameConfig::token_PublicGift_getItem($codestring);
  38. my_Assert($packageInfo->expirets >= now() && $packageInfo->startTs <= now(),
  39. ErrCode::active_activecode_outtime);
  40. my_Assert(!in_array($codestring, ctx()->privateState->usedTokens), ErrCode::active_hasgetted); # 已经领取过该礼包了
  41. $packageID = $codestring;
  42. $err = StoreProc::AddMultiItemInStore($packageInfo->reward); # 发放礼包
  43. my_Assert(ErrCode::ok == $err, $err);
  44. } else {
  45. $active = GameConfig::activity_getItem($activeId); # 活动数据
  46. my_Assert(null != $active, ErrCode::active_const_no_err);
  47. my_Assert($active->startts <= now() && $active->endts >= now(), ErrCode::active_time); # 校验开放时间
  48. my_Assert(preg_match("/^[a-kmnp-z2-9]{10}$/", $codestring), ErrCode::active_activecode_format); # 基础格式校验(10个特定字符)
  49. $activeCode = CipheredBase32::Decode($codestring); # 解码
  50. $codePlatStr = GameConstants::GetPlatStringByActivteCode($activeCode); # platstr
  51. my_Assert(GameConstants::AllPlatStr == $codePlatStr # # 忽略全平台礼包
  52. || req()->getPlatStr() == $codePlatStr, # # 平台字符串必须相符
  53. ErrCode::active_activecode_plat); # # 平台错误
  54. my_Assert(is_int($activeCode->number) # # 编号为int值
  55. && $activeCode->number >= 1 && $activeCode->number <= 50000, # # 检查 兑换码的编号范围0~50000
  56. ErrCode::active_activecode_format);
  57. $packageID = $activeCode->package; # 礼包id
  58. $packageInfo = GameConfig::token_gift_getItem($packageID); # 礼包常量数据
  59. my_Assert(null != $packageInfo, ErrCode::err_const_no); # 防御
  60. my_Assert($packageInfo->expirets >= now() && $packageInfo->startTs <= now(),
  61. ErrCode::active_activecode_outtime); # 激活码已经失效,或者礼包尚未开启
  62. $privateState = new Info_PrivateState(ctx()->privateState); # 快速访问
  63. my_Assert(!in_array($packageID, $privateState->usedTokens), ErrCode::active_hasgetted); # 已经领取过该礼包了
  64. my_Assert(!self::checkActiveCodeIsUsed($activeCode), ErrCode::active_activecode_used); # 检查 该激活码是否别人已经使用过了
  65. $err = StoreProc::AddMultiItemInStore($packageInfo->reward); # 发放礼包
  66. my_Assert(ErrCode::ok == $err, $err); # 防御发放礼包过程出错
  67. $ok = self::setActiveCodeUserRecord($activeCode, req()->uid); # 插入数据库
  68. my_Assert($ok, ErrCode::err_db); # 数据库操作失败- 重试
  69. }
  70. ctx()->privateState->usedTokens[] = $packageID;
  71. UserProc::updateUserInfo(); # 回存玩家数据
  72. $ret = array(# # 返回值
  73. "plat" => $codePlatStr,
  74. "packageId" => $packageID,
  75. "reward" => $packageInfo->reward,
  76. 'gold' => ctx()->base()->gold,
  77. 'cash' => ctx()->base()->cash,
  78. 'tili' => ctx()->base()->tili,
  79. 'store' => ctx()->store,
  80. 'hero' => ctx()->heros,
  81. );
  82. return Resp::ok($ret); # 返回成功信息
  83. }
  84. public static function PublicTokenCodeCheck($codestring) {
  85. $dic = GameConfig::token_PublicGift();
  86. if (StlUtil::dictHasProperty($dic, $codestring)) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. /**
  92. * 检查兑换码是否已经使用过了
  93. * @param ActiveCode $activeCode
  94. * @return boolean true : 已经用过了, false : 激活码尚未使用过.
  95. */
  96. static function checkActiveCodeIsUsed($activeCode) {
  97. $sql = sprintf("`plat`='%d' and `package`='%d' and `number`='%d'", # # where 子句
  98. $activeCode->plat, $activeCode->package, $activeCode->number);
  99. $rows = daoInst()
  100. ->select()
  101. ->from(self::tab_toke_gift) # 表名
  102. ->where($sql)
  103. ->count();
  104. return $rows > 0;
  105. }
  106. /**
  107. * 将兑换码添加到使用记录中
  108. * @param ActiveCode $actvieCode
  109. * @param string $uid
  110. * @return boolean true: 操作成功, false: 操作失败
  111. */
  112. static function setActiveCodeUserRecord($actvieCode, $uid) {
  113. $actvieCode->uid = $uid; # 添加uid
  114. $ret = daoInst()
  115. ->insert(self::tab_toke_gift) # 表名
  116. ->data($actvieCode)
  117. ->exec();
  118. return $ret > 0;
  119. }
  120. /**
  121. * 7日签到 数据更新
  122. */
  123. public static function DailyResetDay7Task() {
  124. if (count(ctx()->privateState->LoginDays) >= 7) {
  125. ctx()->privateState->LoginDays = array();
  126. ctx()->privateState->day7_drawed = array();
  127. }
  128. $index = count(ctx()->privateState->LoginDays) + 1;
  129. ctx()->privateState->LoginDays[] = $index;
  130. ctx()->privateState->day7_accumulate += 1;
  131. }
  132. /**
  133. * 7日签到
  134. * @return type
  135. */
  136. public static function Day7_DrawReward() {
  137. list($day) = req()->paras; # 参数: 领取第x天的奖励
  138. $user = ctx();
  139. my_Assert(in_array($day, $user->privateState->LoginDays), ErrCode::active_day7_expired);
  140. my_Assert(!in_array($day, $user->privateState->day7_drawed), ErrCode::active_hasgetted);
  141. $day_rwd = GameConfig::activity_day7_getItem($day); # 查询奖励数据
  142. my_Assert(null != $day_rwd, ErrCode::err_const_no); # 防御找不到配置
  143. StoreProc::AddMultiItemInStore($day_rwd->reward); # 发放奖励
  144. $user->privateState->day7_drawed[] = $day; # 添加领取记录
  145. ctx($user);
  146. UserProc::updateUserInfo(); # 回存
  147. return Resp::ok(array(
  148. 'gold' => $user->baseInfo->gold,
  149. 'cash' => $user->baseInfo->cash,
  150. 'tili' => $user->baseInfo->tili,
  151. 'store' => $user->store,
  152. ));
  153. }
  154. }