first(['id', 'account', 'nickname', 'puid', 'promote_id', 'promote_account']); if ($user == null) { throw new BusinessException('用户不存在'); } $game = Game::where('id', $gameId)->first(['id', 'game_name', 'sdk_version']); if ($game == null) { throw new BusinessException('游戏不存在'); } $serverInfo = ['server_id' => $serverId, 'server_name' => $serverName]; $roleInfo = ['role_id' => $roleId, 'role_name' => $roleName, 'role_level' => $roleLevel]; $this->saveRole($game, $user, $serverInfo, $roleInfo, $time, $clientIp); $this->saveServer($serverInfo, $game, $time); $this->saveServerUnion($serverInfo, $game, $time); } private function saveRole( Game $game, User $user, array $serverInfo, array $roleInfo, int $time = 0, string $clientIp = '' ) { $role = PlayerRole::where('user_id', $user->id) ->where('game_id', $game->id) ->where('server_id', $serverInfo['server_id']) ->where('role_id', $roleInfo['role_id']) ->first(); if ($role == null) { $role = new PlayerRole(); $role->create_time = $time; $role->create_ip = $clientIp; $role->promote_id = $user->promote_id; $role->promote_account = $user->promote_account; } $role->game_id = $game->id; $role->game_name = $game->game_name; $role->server_id = $serverInfo['server_id']; $role->server_name = $serverInfo['server_name']; $role->role_id = $roleInfo['role_id']; $role->role_name = $roleInfo['role_name']; $role->role_level = $roleInfo['role_level']; $role->game_role_id = $game->id . '#' . $roleInfo['role_id'];; $role->user_id = $user->id; $role->user_account = $user->account; $role->user_nickname = $user->nickname; $role->play_time = $time; $role->sdk_version = $game->sdk_version; $role->play_ip = $clientIp; $role->pipuid = $user->puid; $role->save(); if (in_array($role->game_id, [335, 336])) { Lanhuo::addRole([ 'user_id' => $role->user_id, 'server_id' => $role->server_id, 'server_name' => $role->server_name, 'role_id' => $role->role_id, 'role_name' => $role->role_name, 'role_level' => $role->role_level, 'profession_id' => '', // 职业ID 'profession_name' => '', // 职业名称 'power' => '', // 战力 ]); } } private function saveServer(array $serverInfo, Game $game, int $time = 0) { $serverName = $serverInfo['server_name'] ?? ''; $serverId = $serverInfo['server_id'] ?? 0; $server = Server::where('game_id', $game->id)->where('server_id', $serverId)->first(); if ($server == null) { $server = new Server(); $server->game_id = $game->id; $server->game_name = $game->game_name; $server->server_name = $serverName; $server->server_num = $serverId; $server->server_id = $serverId; $server->server_version = $game->sdk_version; $server->create_time = $time; $server->start_time = $time; $server->save(); } else { if ($server->server_name != $serverName) { $server->server_name = $serverName; $server->save(); } } } private function saveServerUnion(array $serverInfo, Game $game, int $time = 0) { $serverName = $serverInfo['server_name'] ?? ''; $serverId = $serverInfo['server_id'] ?? 0; $server = Server::where('server_name', $serverName)->where('game_id', $game->id)->first(['id']); if ($server) { return; } $serverUnion = ServerUnion::where('server_name', $serverName)->where('game_id', $game->id)->first(['id']); if ($serverUnion == null) { $serverUnion = new ServerUnion(); $serverUnion->game_id = $game->id; $serverUnion->game_name = $game->game_name; $serverUnion->server_name = $serverName; $serverUnion->server_num = $serverId; $serverUnion->server_id = $serverId; $serverUnion->server_version = $game->sdk_version; $serverUnion->create_time = $time; $serverUnion->start_time = $time; $serverUnion->save(); } } }