Role.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * 游戏角色控制器
  4. * Created by PhpStorm.
  5. * User: edison
  6. * Date: 2018/3/21
  7. * Time: 下午5:16
  8. */
  9. namespace app\api\controller\v1;
  10. use app\api\controller\Api;
  11. use app\common\model\RoleInfo;
  12. use app\common\model\Setting;
  13. use think\Cache;
  14. class Role extends Api
  15. {
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $settingModel = new Setting;
  20. $isCollect = (int)$settingModel::getSetting('GAME_ROLE_COLLECT');
  21. if($isCollect==0){
  22. $this->jsonResult('',0,'已经关闭游戏角色收集');
  23. }
  24. }
  25. /**
  26. * 角色信息收集接口(暂未使用))
  27. * PS: 接口改版过,需要客户端把角色管理相关接口统一使用这个;
  28. */
  29. public function index(){
  30. $input = $this->input;
  31. $role_action = $this->input('role_action');
  32. $partyname = $this->input('partyname', "");
  33. $imeil = trim($this->input('imeil'));
  34. // 角色事件:create=创角, enter=进入服务器, up_level=角色升级
  35. if(!in_array($role_action, ['create', 'enter', 'up_level'])){
  36. $this->jsonResult('', -110, '事件类型有误!');
  37. }
  38. $channelId = model('Subaccount')->where(['game_id' => $input['gameid'], 'member_id' => $input['userid']])->value('channel_id'); // 子账户的
  39. if ($channelId == 0) {
  40. $channelId = model('common/Members')->where('id', $input['userid'])->value('channel_id'); // 主账户的
  41. }
  42. $serverid = repairRoleServerid($input['gameid'], $input['gameid']);
  43. $cahce_key = 'role_action:'.$role_action .':'. md5($input['userid'] . $input['gameid'] . $serverid . $input['roleid'] . $channelId);
  44. if($role_action == 'up_level'){
  45. if (Cache::get($cahce_key)) {
  46. $this->jsonResult('', 200, '更新角色成功#cache!');
  47. } else {
  48. Cache::set($cahce_key, '1', 3600);
  49. }
  50. }
  51. $update_time = $create_time = NOW_TIMESTAMP;
  52. $roleInfoModel = new roleInfo();
  53. $result = $roleInfoModel->insert([
  54. 'role_action' => $input['role_action'],
  55. 'userid' => $input['userid'],
  56. 'channel_id' => $channelId,
  57. 'servername' => $input['servername'],
  58. 'gameid' => $input['gameid'],
  59. 'serverid' => $serverid,
  60. 'roleid' => $input['roleid'],
  61. 'rolename' => $input['rolename'],
  62. 'viplevel' => $input['viplevel'],
  63. 'rolelevel' => $input['rolelevel'],
  64. 'rolebalance' => $input['rolebalance'],
  65. 'partyname' => $partyname,
  66. 'ip' => request()->ip(),
  67. 'imeil' => $imeil,
  68. 'create_time' => $create_time,
  69. 'update_time' => $update_time
  70. ]);
  71. if($result){
  72. if($this->saveGameServer($input['userid'], $input['gameid'], $imeil,'role')){
  73. $this->jsonResult('',200,'操作成功!!');
  74. } else {
  75. $this->jsonResult('',-111,'添加操作失败!');
  76. }
  77. } else {
  78. $this->jsonResult('',200,'操作成功!!!');
  79. }
  80. }
  81. /**
  82. * 新增角色信息
  83. */
  84. public function add()
  85. {
  86. $userid = $this->input('userid');
  87. $servername = $this->input('servername');
  88. $gameid = $this->input('gameid');
  89. $serverid = $this->input('serverid');
  90. $roleid = $this->input('roleid');
  91. $rolename = $this->input('rolename');
  92. $viplevel = $this->input('viplevel');
  93. $rolelevel = $this->input('rolelevel');
  94. $rolebalance = $this->input('rolebalance');
  95. $partyname = $this->input('partyname', "");
  96. $update_time = $create_time = NOW_TIMESTAMP;
  97. $imeil = trim($this->input('imeil')); //手机imeil码
  98. //游戏ID不能为空
  99. if (empty($gameid)) {
  100. $this->jsonResult('', 0, '游戏ID不能为空');
  101. }
  102. if (empty($serverid)) {
  103. $this->jsonResult('', 0, '游戏区服ID不能为空');
  104. } elseif (empty($userid) || empty($servername) || empty($roleid) || empty($rolename)) {
  105. $this->jsonResult('', 0, '角色参数不全');
  106. }
  107. // $channelId = getChannelId($userid); // 登陆渠道的
  108. $channelId = model('Subaccount')->where(['game_id' => $gameid, 'member_id' => $userid])->value('channel_id'); // 子账户的
  109. if ($channelId == 0) {
  110. $channelId = model('common/Members')->where('id', $userid)->value('channel_id'); // 主账户的
  111. // setChannelId($userid,$channelId);
  112. }
  113. $serverid = repairRoleServerid($gameid, $serverid);
  114. // $cahce_key = md5($userid . $servername . $gameid . $serverid . $roleid . $rolename . $viplevel . $rolelevel . $rolebalance . $partyname . $channelId);
  115. $cahce_key = 'Role_add:'.md5($gameid.$userid.$serverid.$roleid);
  116. if (Cache::get($cahce_key)) {
  117. $this->jsonResult('', 1, '添加角色成功#cache!!');
  118. } else {
  119. Cache::set($cahce_key, '1', 60);
  120. }
  121. // 角色事件:create=创角, enter=进入服务器, up_level=角色升级, offline=角色下线
  122. $role_action = 'create';
  123. $roleInfoModel = new roleInfo();
  124. $info = $roleInfoModel->where(['gameid' => $gameid, 'userid' => $userid, 'roleid' => $roleid, 'serverid' => $serverid])->find();
  125. if($info){
  126. // $this->jsonResult('',1,'添加角色成功!');
  127. $role_action = 'enter';
  128. }
  129. $result = $roleInfoModel->create([
  130. 'userid' => $userid,
  131. 'channel_id' => $channelId,
  132. 'servername' => $servername,
  133. 'gameid' => $gameid,
  134. 'serverid' => $serverid,
  135. 'roleid' => $roleid,
  136. 'rolename' => $rolename,
  137. 'viplevel' => $viplevel,
  138. 'rolelevel' => $rolelevel,
  139. 'rolebalance' => $rolebalance,
  140. 'partyname' => $partyname,
  141. 'ip' => request()->ip(),
  142. 'imeil' => $imeil,
  143. 'create_time' => $create_time,
  144. 'update_time' => $update_time,
  145. 'role_action' => $role_action,
  146. ]);
  147. if($result){
  148. if($this->saveGameServer($userid,$gameid,$imeil,'role')){
  149. $this->jsonResult('',1,'添加角色成功!');
  150. }else{
  151. $this->jsonResult('',0,'添加角色失败,更新区服失败!');
  152. }
  153. }else{
  154. $this->jsonResult('',0,'添加角色失败!');
  155. }
  156. }
  157. /**
  158. * 更新角色信息(cp那边roleid等有可能为空,所以改成直接插入数据库)
  159. */
  160. public function update()
  161. {
  162. $userid = $this->input('userid');
  163. $roleid = $this->input('roleid');
  164. $gameid = $this->input('gameid');
  165. $serverid = $this->input('serverid');
  166. $servername = $this->input('servername');
  167. $rolename = $this->input('rolename', '');
  168. $viplevel = $this->input('viplevel', '');
  169. $rolelevel = $this->input('rolelevel', '');
  170. $rolebalance = $this->input('rolebalance', '');
  171. $partyname = $this->input('partyname', '');
  172. $imeil = trim($this->input('imeil')); //手机imeil码
  173. //游戏ID不能为空
  174. if (empty($gameid)) {
  175. $this->jsonResult('',0,'游戏ID不能为空');
  176. }
  177. if (empty($serverid)) {
  178. $this->jsonResult('',0,'游戏区服ID不能为空');
  179. }
  180. elseif(empty($userid) || empty($servername) || empty($roleid) || empty($rolename)){
  181. $this->jsonResult('', 0, '角色参数不全');
  182. }
  183. // $channelId = getChannelId($userid); // 登陆渠道的
  184. $channelId = model('Subaccount')->where(['game_id' => $gameid, 'member_id' => $userid])->value('channel_id'); // 子账户的
  185. if ($channelId == 0) {
  186. $channelId = model('common/Members')->where('id', $userid)->value('channel_id'); // 主账户的
  187. // setChannelId($userid,$channelId);
  188. }
  189. $serverid = repairRoleServerid($gameid, $serverid);
  190. // $cahce_key = md5($userid . $servername . $gameid . $serverid . $roleid . $rolename . $viplevel . $rolelevel . $rolebalance . $partyname . $channelId);
  191. $cahce_key = md5($gameid.$userid.$serverid.$roleid);
  192. if (Cache::get(':Role_update:' . $cahce_key)) {
  193. $this->jsonResult('', 1, '更新角色信息成功!!');
  194. } else {
  195. Cache::set(':Role_update:' . $cahce_key, '1', 60);
  196. }
  197. $roleInfoModel = new roleInfo();
  198. $result = $roleInfoModel->insert([
  199. 'userid' => $userid,
  200. 'channel_id' =>$channelId,
  201. 'servername' => $servername,
  202. 'gameid' => $gameid,
  203. 'serverid' => $serverid,
  204. 'roleid' => $roleid,
  205. 'rolename' => $rolename,
  206. 'viplevel' => $viplevel,
  207. 'rolelevel' => $rolelevel,
  208. 'rolebalance' => $rolebalance,
  209. 'partyname' => $partyname,
  210. 'ip' => request()->ip(),
  211. 'imeil' => $imeil,
  212. 'create_time' => NOW_TIMESTAMP,
  213. 'update_time' => NOW_TIMESTAMP,
  214. 'role_action' => 'up_level',
  215. ]);
  216. if($result){
  217. if($this->saveGameServer($userid,$gameid,$imeil,'role')){
  218. $this->jsonResult('',1,'更新角色信息成功!');
  219. }else{
  220. $this->jsonResult('',0,'更新角色信息失败,更新区服失败!');
  221. }
  222. }else{
  223. $this->jsonResult('',0,'更新角色信息失败!');
  224. }
  225. }
  226. }