| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- /**
- * 游戏角色控制器
- * Created by PhpStorm.
- * User: edison
- * Date: 2018/3/21
- * Time: 下午5:16
- */
- namespace app\api\controller\v1;
- use app\api\controller\Api;
- use app\common\model\RoleInfo;
- use app\common\model\Setting;
- use think\Cache;
- class Role extends Api
- {
- public function _initialize()
- {
- parent::_initialize();
- $settingModel = new Setting;
- $isCollect = (int)$settingModel::getSetting('GAME_ROLE_COLLECT');
- if($isCollect==0){
- $this->jsonResult('',0,'已经关闭游戏角色收集');
- }
- }
- /**
- * 角色信息收集接口(暂未使用))
- * PS: 接口改版过,需要客户端把角色管理相关接口统一使用这个;
- */
- public function index(){
- $input = $this->input;
- $role_action = $this->input('role_action');
- $partyname = $this->input('partyname', "");
- $imeil = trim($this->input('imeil'));
- // 角色事件:create=创角, enter=进入服务器, up_level=角色升级
- if(!in_array($role_action, ['create', 'enter', 'up_level'])){
- $this->jsonResult('', -110, '事件类型有误!');
- }
-
- $channelId = model('Subaccount')->where(['game_id' => $input['gameid'], 'member_id' => $input['userid']])->value('channel_id'); // 子账户的
- if ($channelId == 0) {
- $channelId = model('common/Members')->where('id', $input['userid'])->value('channel_id'); // 主账户的
- }
-
- $serverid = repairRoleServerid($input['gameid'], $input['gameid']);
- $cahce_key = 'role_action:'.$role_action .':'. md5($input['userid'] . $input['gameid'] . $serverid . $input['roleid'] . $channelId);
- if($role_action == 'up_level'){
- if (Cache::get($cahce_key)) {
- $this->jsonResult('', 200, '更新角色成功#cache!');
- } else {
- Cache::set($cahce_key, '1', 3600);
- }
- }
- $update_time = $create_time = NOW_TIMESTAMP;
- $roleInfoModel = new roleInfo();
- $result = $roleInfoModel->insert([
- 'role_action' => $input['role_action'],
- 'userid' => $input['userid'],
- 'channel_id' => $channelId,
- 'servername' => $input['servername'],
- 'gameid' => $input['gameid'],
- 'serverid' => $serverid,
- 'roleid' => $input['roleid'],
- 'rolename' => $input['rolename'],
- 'viplevel' => $input['viplevel'],
- 'rolelevel' => $input['rolelevel'],
- 'rolebalance' => $input['rolebalance'],
- 'partyname' => $partyname,
- 'ip' => request()->ip(),
- 'imeil' => $imeil,
- 'create_time' => $create_time,
- 'update_time' => $update_time
- ]);
-
- if($result){
- if($this->saveGameServer($input['userid'], $input['gameid'], $imeil,'role')){
- $this->jsonResult('',200,'操作成功!!');
- } else {
- $this->jsonResult('',-111,'添加操作失败!');
- }
- } else {
- $this->jsonResult('',200,'操作成功!!!');
- }
- }
-
- /**
- * 新增角色信息
- */
- public function add()
- {
- $userid = $this->input('userid');
- $servername = $this->input('servername');
- $gameid = $this->input('gameid');
- $serverid = $this->input('serverid');
- $roleid = $this->input('roleid');
- $rolename = $this->input('rolename');
- $viplevel = $this->input('viplevel');
- $rolelevel = $this->input('rolelevel');
- $rolebalance = $this->input('rolebalance');
- $partyname = $this->input('partyname', "");
- $update_time = $create_time = NOW_TIMESTAMP;
- $imeil = trim($this->input('imeil')); //手机imeil码
- //游戏ID不能为空
- if (empty($gameid)) {
- $this->jsonResult('', 0, '游戏ID不能为空');
- }
- if (empty($serverid)) {
- $this->jsonResult('', 0, '游戏区服ID不能为空');
- } elseif (empty($userid) || empty($servername) || empty($roleid) || empty($rolename)) {
- $this->jsonResult('', 0, '角色参数不全');
- }
- // $channelId = getChannelId($userid); // 登陆渠道的
- $channelId = model('Subaccount')->where(['game_id' => $gameid, 'member_id' => $userid])->value('channel_id'); // 子账户的
- if ($channelId == 0) {
- $channelId = model('common/Members')->where('id', $userid)->value('channel_id'); // 主账户的
- // setChannelId($userid,$channelId);
- }
- $serverid = repairRoleServerid($gameid, $serverid);
- // $cahce_key = md5($userid . $servername . $gameid . $serverid . $roleid . $rolename . $viplevel . $rolelevel . $rolebalance . $partyname . $channelId);
- $cahce_key = 'Role_add:'.md5($gameid.$userid.$serverid.$roleid);
- if (Cache::get($cahce_key)) {
- $this->jsonResult('', 1, '添加角色成功#cache!!');
- } else {
- Cache::set($cahce_key, '1', 60);
- }
- // 角色事件:create=创角, enter=进入服务器, up_level=角色升级, offline=角色下线
- $role_action = 'create';
- $roleInfoModel = new roleInfo();
- $info = $roleInfoModel->where(['gameid' => $gameid, 'userid' => $userid, 'roleid' => $roleid, 'serverid' => $serverid])->find();
- if($info){
- // $this->jsonResult('',1,'添加角色成功!');
- $role_action = 'enter';
- }
- $result = $roleInfoModel->create([
- 'userid' => $userid,
- 'channel_id' => $channelId,
- 'servername' => $servername,
- 'gameid' => $gameid,
- 'serverid' => $serverid,
- 'roleid' => $roleid,
- 'rolename' => $rolename,
- 'viplevel' => $viplevel,
- 'rolelevel' => $rolelevel,
- 'rolebalance' => $rolebalance,
- 'partyname' => $partyname,
- 'ip' => request()->ip(),
- 'imeil' => $imeil,
- 'create_time' => $create_time,
- 'update_time' => $update_time,
- 'role_action' => $role_action,
- ]);
- if($result){
- if($this->saveGameServer($userid,$gameid,$imeil,'role')){
- $this->jsonResult('',1,'添加角色成功!');
- }else{
- $this->jsonResult('',0,'添加角色失败,更新区服失败!');
- }
- }else{
- $this->jsonResult('',0,'添加角色失败!');
- }
- }
- /**
- * 更新角色信息(cp那边roleid等有可能为空,所以改成直接插入数据库)
- */
- public function update()
- {
- $userid = $this->input('userid');
- $roleid = $this->input('roleid');
- $gameid = $this->input('gameid');
- $serverid = $this->input('serverid');
- $servername = $this->input('servername');
- $rolename = $this->input('rolename', '');
- $viplevel = $this->input('viplevel', '');
- $rolelevel = $this->input('rolelevel', '');
- $rolebalance = $this->input('rolebalance', '');
- $partyname = $this->input('partyname', '');
- $imeil = trim($this->input('imeil')); //手机imeil码
- //游戏ID不能为空
- if (empty($gameid)) {
- $this->jsonResult('',0,'游戏ID不能为空');
- }
- if (empty($serverid)) {
- $this->jsonResult('',0,'游戏区服ID不能为空');
- }
- elseif(empty($userid) || empty($servername) || empty($roleid) || empty($rolename)){
- $this->jsonResult('', 0, '角色参数不全');
- }
- // $channelId = getChannelId($userid); // 登陆渠道的
- $channelId = model('Subaccount')->where(['game_id' => $gameid, 'member_id' => $userid])->value('channel_id'); // 子账户的
- if ($channelId == 0) {
- $channelId = model('common/Members')->where('id', $userid)->value('channel_id'); // 主账户的
- // setChannelId($userid,$channelId);
- }
- $serverid = repairRoleServerid($gameid, $serverid);
- // $cahce_key = md5($userid . $servername . $gameid . $serverid . $roleid . $rolename . $viplevel . $rolelevel . $rolebalance . $partyname . $channelId);
- $cahce_key = md5($gameid.$userid.$serverid.$roleid);
- if (Cache::get(':Role_update:' . $cahce_key)) {
- $this->jsonResult('', 1, '更新角色信息成功!!');
- } else {
- Cache::set(':Role_update:' . $cahce_key, '1', 60);
- }
- $roleInfoModel = new roleInfo();
- $result = $roleInfoModel->insert([
- 'userid' => $userid,
- 'channel_id' =>$channelId,
- 'servername' => $servername,
- 'gameid' => $gameid,
- 'serverid' => $serverid,
- 'roleid' => $roleid,
- 'rolename' => $rolename,
- 'viplevel' => $viplevel,
- 'rolelevel' => $rolelevel,
- 'rolebalance' => $rolebalance,
- 'partyname' => $partyname,
- 'ip' => request()->ip(),
- 'imeil' => $imeil,
- 'create_time' => NOW_TIMESTAMP,
- 'update_time' => NOW_TIMESTAMP,
- 'role_action' => 'up_level',
- ]);
- if($result){
- if($this->saveGameServer($userid,$gameid,$imeil,'role')){
- $this->jsonResult('',1,'更新角色信息成功!');
- }else{
- $this->jsonResult('',0,'更新角色信息失败,更新区服失败!');
- }
- }else{
- $this->jsonResult('',0,'更新角色信息失败!');
- }
- }
- }
|