gameinfo.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. defined( 'IN_WEB') or die( 'Include Error!');
  3. class ModelGameinfo {
  4. public $stealCfg;
  5. public $tb_arr = [];
  6. public $steal_allow =["mid","levelId","money","createtime","stealedNum","stealProtectionTime","levelMinMoney"];
  7. public $this_allow = [];//当前模式规则
  8. public $this_tb='crazygod_api_user.gh_gameinfo_steal';//当前模式表
  9. public $tb_str = 'tb_steal'; //当前表标志
  10. public function __construct(){
  11. $this->stealCfg = oo::cfg('stealattack')['steal'];
  12. $this->tb_arr =[
  13. "tb_steal"=>'crazygod_api_user.gh_gameinfo_steal',
  14. "tb"=>otable::gh_gameserver(1),
  15. ];
  16. $this->this_allow =$this->steal_allow;
  17. }
  18. /**
  19. * Notes:获取满足条件的steal表信息
  20. * User: wsc
  21. * Time: 2020/4/20 15:36
  22. * @param string $field
  23. * @param bool $w
  24. * @param string $order
  25. * @param string $limit
  26. * @return array
  27. */
  28. public function getDataFromDB($field="*",$w=false,$order="",$limit=""){
  29. $data = [];
  30. $where = " 1=1 ";
  31. if($w&&is_array($w)){
  32. $where .= " AND ";
  33. $where .= implode(" AND ",array_values($w));
  34. }
  35. $tb = strstr($field,'rNum')?$this->this_tb.',(SELECT @rowNum:=0) b':$this->this_tb;
  36. $sql = "SELECT {$field} FROM {$tb} WHERE {$where} {$order} {$limit}";
  37. $data = oo::commonOprDb('ghgames')->getAll($sql,1);
  38. return $data;
  39. }
  40. /**
  41. * Notes:获取用户GameInfoSteal
  42. * User: wsc
  43. * Time: 2020/4/11 10:13
  44. * @param $uid
  45. * @param bool $db
  46. * @return array
  47. */
  48. public function getGameInfoSteal($uid,$db=false){
  49. $cache = [];
  50. if(!$uid){
  51. return $cache;
  52. }
  53. $key = okeys::UserGameInfoSteal($this->tb_str.'-'.$uid);
  54. if(!$db){
  55. $cache = oo::commonOprRedis('Userinfo')->hGetAll($key);
  56. }
  57. if(empty($cache['mid'])){
  58. $sql = "SELECT * FROM {$this->this_tb} WHERE mid={$uid} limit 1";
  59. $info = oo::commonOprDb('ghgames')->getOne($sql,1);
  60. oo::commonOprRedis('Userinfo')->delete($key);
  61. if(!empty($info)&&is_array($info)){
  62. $cache = $info;
  63. $ts = timeFuns::today()[1];
  64. oo::commonOprRedis('Userinfo')->hMset($key,$cache);
  65. oo::commonOprRedis('Userinfo')->expireAt($key,oo::redisRandomExpire($ts));
  66. }
  67. }
  68. return $cache;
  69. }
  70. /**
  71. * Notes:更新GameInfoSteal
  72. * User: wsc
  73. * Time: 2020/4/11 10:21
  74. * @param $uid
  75. * @param $arr
  76. * @return bool
  77. */
  78. public function upDateGameInfoSteal($uid,$arr,$limit=false){
  79. $res = false;
  80. $info = $this->getGameInfoSteal($uid);
  81. $gameInfo = $arr;
  82. if(empty($info)||!is_array($info)){
  83. $gameInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  84. $minLevelMoney = oo::commonOprModel('level')->getLevelConf($gameInfo['levelId'])['minMoney'];
  85. if(empty(array_intersect(array_keys($arr),$this->this_allow))){
  86. return false;
  87. }
  88. foreach($arr as $k=>$v){
  89. $gameInfo[$k] = $v;
  90. }
  91. $gameInfo['mid'] =$uid;
  92. $gameInfo['createtime'] =time();
  93. $gameInfo['levelMinMoney'] = $minLevelMoney;
  94. }
  95. $allow_kv = $this->allowField($gameInfo);
  96. if(!$uid||empty($allow_kv)||!is_array($allow_kv)){
  97. return $res;
  98. }
  99. if(!empty($info)&&is_array($info)){
  100. $valArr = [];
  101. foreach($allow_kv as $k=>$v){
  102. $valArr[] = $k.'='.$v;
  103. }
  104. $valStr = trim(implode(',', $valArr));
  105. $sql = "UPDATE {$this->this_tb} SET {$valStr} WHERE mid = {$uid} limit 1";
  106. $res = oo::commonOprDb('ghgames')->query($sql);
  107. if($res){
  108. $this->getGameInfoSteal($uid,true);
  109. }
  110. }else{
  111. if($gameInfo['money']<$minLevelMoney||$gameInfo['levelId']<$this->stealCfg['minLv']){
  112. return true;
  113. }
  114. if(funs::concurrentLock($uid,'insertSteal')){
  115. $res = $this->insertGameInfoSteal($allow_kv);
  116. }
  117. }
  118. return $res;
  119. }
  120. /**
  121. * Notes:写入GameInfoSteal
  122. * User: wsc
  123. * Time: 2020/4/11 10:36
  124. * @param $arr
  125. * @return bool
  126. */
  127. public function insertGameInfoSteal($arr){
  128. $allow_kv = [];
  129. $res = false;
  130. $allow_kv = $this->allowField($arr);
  131. if(!isset($allow_kv['mid'])||empty($allow_kv)){
  132. return $res;
  133. }
  134. $uid = intval($arr['mid']);
  135. $info = $this->getGameInfoSteal($uid);
  136. if(empty($info)){
  137. $keys = implode(",",array_keys($allow_kv));
  138. $values = implode(",",array_values($allow_kv));
  139. $sql = "INSERT INTO {$this->this_tb}({$keys}) values ({$values})";
  140. $res = oo::commonOprDb('ghgames')->query($sql);
  141. }
  142. return $res;
  143. }
  144. /**
  145. * Notes:删除GameInfoSteal
  146. * User: wsc
  147. * Time: 2020/4/11 17:34
  148. * @param $uids
  149. * @return bool
  150. */
  151. public function deleteGameInfoSteal($uids){
  152. $res = false;
  153. if(empty($uids)){
  154. return $res;
  155. }
  156. if(is_array($uids)){
  157. $uidStr = implode(",",array_values($uids));
  158. $w = " mid IN ({$uidStr})";
  159. }else{
  160. $w = " mid={$uids}";
  161. }
  162. if(!empty($w)){
  163. $sql = "DELETE FROM {$this->this_tb} WHERE {$w}";
  164. $res = oo::commonOprDb('ghgames')->query($sql);
  165. if($res&&!empty($uids)&&is_array($uids)){//更新缓存
  166. foreach ($uids as $v){
  167. $this->getGameInfoSteal($v,true);
  168. }
  169. }
  170. }
  171. return $res;
  172. }
  173. /**
  174. * Notes:过滤非法字段
  175. * User: wsc
  176. * Time: 2020/4/11 17:34
  177. * @param $arr
  178. * @return array
  179. */
  180. public function allowField($arr){
  181. $allow_kv = [];
  182. foreach ($arr as $k=>$v){
  183. if(!in_array($k,$this->this_allow)){//非法字段过滤
  184. continue;
  185. }
  186. $allow_kv[$k] = $v;
  187. }
  188. return $allow_kv;
  189. }
  190. /**
  191. * Notes:不满足条件
  192. * User: wsc
  193. * Time: 2020/4/14 14:59
  194. * @return array
  195. */
  196. public function noSatisfyStealDte(){
  197. $ts = time();
  198. $where ="(money < levelMinMoney OR levelId < {$this->stealCfg['minLv']}) AND stealProtectionTime<{$ts}";
  199. $sql = "SELECT mid FROM {$this->this_tb} WHERE {$where}";
  200. $data = oo::commonOprDb('ghgames')->getAll($sql,1);
  201. return $data;
  202. }
  203. public function updateStatus($w,$arr){
  204. $res =false;
  205. $uList = $this->getDataFromDB('mid',$w);
  206. $uids = !empty($uList)?array_column($uList,'mid'):[];//不满足条件的
  207. if(!empty($uids)){
  208. //更新db
  209. $allow_kv = $this->allowField($arr);
  210. foreach($allow_kv as $k=>$v){
  211. $valArr[] = $k.'='.$v;
  212. }
  213. $valStr = trim(implode(',', $valArr));
  214. $uidStr = implode(",",array_values($uids));
  215. $w = " mid IN ({$uidStr})";
  216. $sql = "UPDATE {$this->this_tb} SET {$valStr} WHERE {$w}";
  217. $res = oo::commonOprDb('ghgames')->query($sql);
  218. //更新缓存
  219. if($res&&!empty($uids)&&is_array($uids)){
  220. foreach ($uids as $v){
  221. $this->getGameInfoSteal($v,true);
  222. }
  223. }
  224. }
  225. return $res;
  226. }
  227. /**
  228. * Notes:定时操作
  229. * User: wsc
  230. * Time: 2020/4/11 15:28
  231. */
  232. public function cornTabUpTable(){
  233. //清除表里不满足条件的用户
  234. $list = $this->noSatisfyStealDte();
  235. $uidList = !empty($list)?array_column($list,'mid'):[];//不满足条件的
  236. $res['del']=$this->deleteGameInfoSteal($uidList);
  237. //更新用户状态
  238. $w = ["stealedNum!=0"];
  239. $arr=["stealedNum"=>0];
  240. $res['upd']= $this->updateStatus($w,$arr);
  241. return $res;
  242. }
  243. }