|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
use App\Exception\BusinessException;
|
|
|
|
use App\Helper\Client\Lanhuo;
|
|
|
|
use App\Model\Game;
|
|
|
|
use App\Model\PlayerRole;
|
|
|
|
use App\Model\Server;
|
|
|
|
use App\Model\ServerUnion;
|
|
|
|
use App\Model\User;
|
|
|
|
|
|
|
|
class PlayerRoleService extends Service
|
|
|
|
{
|
|
|
|
public function save($params)
|
|
|
|
{
|
|
|
|
$userId = $params['user_id'] ?? 0;
|
|
|
|
$gameId = $params['game_id'] ?? 0;
|
|
|
|
$serverId = $params['server_id'] ?? 0;
|
|
|
|
$serverName = $params['server_name'] ?? 0;
|
|
|
|
$roleName = $params['game_player_name'] ?? 0;
|
|
|
|
$roleId = $params['game_player_id'] ?? 0;
|
|
|
|
$roleLevel = $params['role_level'] ?? 0;
|
|
|
|
$time = (int)($params['time'] ?? 0);
|
|
|
|
$clientIp = $params['client_ip'] ?? '';
|
|
|
|
$sdkVersion = $params['sdk_version'] ?? 0;
|
|
|
|
|
|
|
|
$user = User::where('id', $userId)->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 ($role->game_id) {
|
|
|
|
Lanhuo::addRole([
|
|
|
|
'user_id' => 111,
|
|
|
|
'server_id' => 1,
|
|
|
|
'server_name' => '测试',
|
|
|
|
'role_id' => '111',
|
|
|
|
'role_name' => '测试',
|
|
|
|
'role_level' => 23,
|
|
|
|
'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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|