AppServer.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace loyalsoft;
  3. include_once __DIR__ . '/../main.php'; # 导入主要工具库
  4. /**
  5. * API入口
  6. * @author gwang (mail@wanggangzero.cn)
  7. */
  8. class AppServer {
  9. /**
  10. * The Main Entrance of App
  11. * @return Resp
  12. */
  13. public function api() {
  14. $uniq = uniqid(); # 自己生成一个唯一串,作为此次处理过程的标记。
  15. $GLOBALS['deal-uid'] = $uniq; # 写入日志的时候带上,方便辨认同一个处理过程.
  16. $GLOBALS['zoneid'] = req()->zoneid; # 暂时未想到更好的方案
  17. $err = self::BeforeProc(); # 预处理
  18. my_Assert(ErrCode::ok == $err, $err); # 如果tk校验未通过
  19. $resp = self::Route(); # 分发逻辑
  20. self::AfterProc($resp); # 执行后处理逻辑
  21. return $resp;
  22. }
  23. /**
  24. * 预处理逻辑
  25. */
  26. private static function BeforeProc() {
  27. if (!GAME_ONLINE) {
  28. return ErrCode::ok; # 直接返回 0:成功
  29. }
  30. my_Assert(!config::Inst()->isBaned(req()->uid), ErrCode::err_server_updating); # 检查封号
  31. if (self::isUpdating() # 检查是否处于更新阶段,暂停对玩家请求的响应。
  32. && !config::Inst()->isTester(req()->uid)) { # 排除测试号
  33. return ErrCode::err_server_updating;
  34. }
  35. $ssd = GameConfig::service_schedule_getItem(1); # 服务计划(固定只有1条)
  36. if (now() > $ssd->startts && now() < $ssd->endts) { # 在维护期间
  37. Err(ErrCode::err_server_maintaining, $ssd->reason);
  38. }
  39. // todo: 检查clientVersion 判断是否应该强制升级
  40. if (!Index::$isDebugging && abs(now() - req()->ts) > OFFSET_MSGTIME) { # 如果服务端客户端时间戳超过规定误差,则消息非法
  41. return ErrCode::err_outtime;
  42. }
  43. return ErrCode::ok; # 所有检查通过
  44. }
  45. /**
  46. * 后处理逻辑
  47. * @param Resp $resp
  48. */
  49. private static function AfterProc(&$resp) {
  50. if (req()->userInfoChanged) { # 回写玩家数据
  51. if (!UserProc::setUserInfo(ctx())) { # 失败, 改写返回值
  52. Err(ErrCode::err_innerfault, "回写玩家数据");
  53. }
  54. }
  55. $resp->AfterProc();
  56. // self::LogCmd($resp); # 向MySQL记录操作日志
  57. self::LogCmdMongo($resp); # 向MongoDB写入操作日志
  58. CLog::flush(); # flush日志
  59. }
  60. /**
  61. * 新版: 使用pdo_mysql+dao版本
  62. * @param Resp $resp
  63. */
  64. private static function LogCmd($resp) {
  65. $tablename = 'tab_op_log' . date('Ymd'); # 今天的表名
  66. $old_tablename = 'tab_op_log' . date('Ymd', now(-86400 * 21)); # 待删除的表名 日志保留21天
  67. $sql = sprintf('create table if not exists %s like `tpl_op_log_tab`;', $tablename); # 创建今天的表
  68. $sql .= sprintf('drop table if exists %s;', $old_tablename); # 循环删除以前的表
  69. $sql .= sprintf("insert into %s (`uid`,`zoneid`,`cmd`,`days`,`param`,`ret`) values ('%s', %d, %d, %d, '%s', '%s');", #
  70. $tablename, req()->uid, req()->zoneid, req()->cmd, # # Uid, zoneid, cmd
  71. (null != ctx() ? (totalDays() - totalDays(ctx()->baseInfo->firstLogin)) : 0), # # ps.留存天数
  72. JsonUtil::encode(req()->paras), # # req->paras
  73. JsonUtil::encode($resp->result)); # # resp->result
  74. daoInst()->exec($sql); # 执行sql
  75. }
  76. /**
  77. * 新版: MongoDB版本
  78. * @param Resp $resp
  79. */
  80. private static function LogCmdMongo($resp) {
  81. $tablename = 'ylsj2019.tab_op_log' . date('Ym'); # 当月的日志表名
  82. $arr = array(
  83. 'uid' => req()->uid,
  84. 'req' => req()->storage(),
  85. 'ret' => $resp,
  86. 'ts' => TimeUtil::dtCurrent(),
  87. );
  88. gMongo()->insert($tablename, $arr);
  89. }
  90. /**
  91. * 路由方法
  92. */
  93. private static function Route() {
  94. $proc = OpeCode::getProc(req()->ope); # 映射处理模块.
  95. // DebugHelper::print_stack_trace();
  96. my_Assert($proc != "err", ErrCode::ope_err); # 未能找到对应的处理模块
  97. try {
  98. $resp = call_user_func(array($proc, 'procMain'), req()); # 调用对应的处理逻辑
  99. if (!($resp instanceof Resp)) { # 异常返回值
  100. Err(ErrCode::err_innerfault, JsonUtil::encode($resp));
  101. }
  102. return $resp; # 返回值
  103. } catch (\Exception $ex) { # 异常信息写入日志
  104. $msg = CommUtil::str2UTF8($ex->getMessage()); # 异常信息转下码
  105. Err(ErrCode::err_unknownn, "call_user_func got Exception: $msg"); # 返回给客户端
  106. }
  107. }
  108. /**
  109. * 返回是否处于更新中
  110. * @return boolean
  111. */
  112. private static function isUpdating() {
  113. $ts = now();
  114. if ($ts > glc()->updatingBeginTs && $ts < glc()->updatingEndTs) {
  115. return true;
  116. }
  117. return false;
  118. }
  119. }