Files
caliverse_server/GameServer/Contents/Location/Action/LocationAction.cs
2025-05-01 07:20:41 +09:00

478 lines
18 KiB
C#

using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using ServerCore;
using ServerBase;
using ServerCommon;
using ServerCommon.BusinessLogDomain;
using MetaAssets;
namespace GameServer;
internal partial class LocationAction : EntityActionBase
{
LocationCacheRequest? m_location_cache_request_nullable;
private BaseLoaction? m_current_loaction;
public LocationAction(Player owner)
: base(owner)
{ }
public override async Task<Result> onInit()
{
await Task.CompletedTask;
var result = new Result();
return result;
}
public override void onClear()
{
return;
}
public async Task<Result> tryLoadEntityAttributeFromCache()
{
var result = new Result();
var err_msg = string.Empty;
var server_logic = GameServerApp.getServerLogic();
var player = getOwner() as Player;
NullReferenceCheckHelper.throwIfNull(player, () => "player is null !!!");
var location_cache_request = new LocationCacheRequest(player, server_logic.getRedisConnector());
result = await location_cache_request.fetchLocation();
if (result.isFail())
{
err_msg = $"Failed to fetchLocation() !!! : {this.getTypeName()}";
Log.getLogger().error(err_msg);
return result;
}
var location_cache = location_cache_request.getLocationCache();
NullReferenceCheckHelper.throwIfNull(location_cache, () => $"location_cache is null !!! - {player.toBasicString()}");
var location_attribute = player.getEntityAttribute<LocationAttribute>();
NullReferenceCheckHelper.throwIfNull(location_attribute, () => $"location_attribute is null !!! - {player.toBasicString()}");
location_attribute.copyEntityAttributeFromCache(location_cache);
m_location_cache_request_nullable = location_cache_request;
return result;
}
public async Task<Result> trySaveLoactionToDB()
{
var result = new Result();
var err_msg = string.Empty;
var server_logic = GameServerApp.getServerLogic();
var player = getOwner() as Player;
NullReferenceCheckHelper.throwIfNull(player, () => "player is null !!!");
var nickname_action = player.getEntityAction<NicknameAction>();
NullReferenceCheckHelper.throwIfNull(nickname_action, () => $"nickname_action is null !!! - {player.toBasicString()}");
if (!nickname_action.isCreatedNickname())
return result;
var location_attribute = player.getEntityAttribute<LocationAttribute>();
NullReferenceCheckHelper.throwIfNull(location_attribute, () => $"location_attribute is null !!! - {player.toBasicString()}");
var server_type = server_logic.getServerType().toServerType();
switch (server_type)
{
case ServerType.Channel:
{
var game_login_action = player.getEntityAction<GameLoginAction>();
NullReferenceCheckHelper.throwIfNull(game_login_action, () => $"game_login_action is null !!! - {player.toBasicString()}");
if (!game_login_action.isServerConnectSwitching())
{
location_attribute.LastestChannelServerLocation = location_attribute.CurrentChannelServerLoaction;
location_attribute.modifiedEntityAttribute();
}
}
break;
case ServerType.Indun:
break;
default:
{
err_msg = $"ServerType invalid !!! : {server_type.ToString()}";
result.setFail(ServerErrorCode.ServerTypeInvalid, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
}
(result, var location_doc) = await location_attribute.toDocBase();
if (result.isFail() || null == location_doc)
{
return result;
}
var dynamo_db_client = server_logic.getDynamoDbClient();
NullReferenceCheckHelper.throwIfNull(dynamo_db_client, () => $"dynamo_db_client is null !!! - {player.toBasicString()}");
result = await dynamo_db_client.simpleUpsertDocumentWithDocType(location_doc);
if (result.isFail())
{
err_msg = $"Failed to simpleUpsertDocumentWithDocType() !!! : {location_doc.toBasicString()} - {player.toBasicString()}";
Log.getLogger().error(err_msg);
return result;
}
return result;
}
public Result tryUpdateCurrentLocationAtLogin(bool isFirstLogin)
{
var result = new Result();
var err_msg = string.Empty;
var server_logic = GameServerApp.getServerLogic();
var player = getOwner() as Player;
NullReferenceCheckHelper.throwIfNull(player, () => "player is null !!!");
var user_create_or_load_action = player.getEntityAction<UserCreateOrLoadAction>();
NullReferenceCheckHelper.throwIfNull(user_create_or_load_action, () => $"user_create_or_load_action is null !!! - {player.toBasicString()}");
var location_attribute = player.getEntityAttribute<LocationAttribute>();
NullReferenceCheckHelper.throwIfNull(location_attribute, () => $"location_attribute is null !!! - {player.toBasicString()}");
var server_type = server_logic.getServerType().toServerType();
switch (server_type)
{
case ServerType.Channel:
{
if(user_create_or_load_action.isTestUser() == true && isFirstLogin == true)
{
var lastest_pos = new Pos();
location_attribute.LastestChannelServerLocation.toPos(lastest_pos);
location_attribute.CurrentChannelServerLoaction.ServerName = server_logic.getServerName();
location_attribute.CurrentChannelServerLoaction.WorldMetaId = (int)server_logic.getWorldId();
location_attribute.CurrentChannelServerLoaction.fromPos(lastest_pos);
setCurrentLocation(location_attribute.CurrentChannelServerLoaction);
break;
}
if (false == user_create_or_load_action.hasUserCreateTrigger())
{
Pos? start_pos;
var lastest_pos = new Pos();
location_attribute.LastestChannelServerLocation.toPos(lastest_pos);
if (isFirstLogin)
{
if (!server_logic.getMap().GetNearestStartPoint(lastest_pos, out start_pos))
{
start_pos = lastest_pos;
}
}
else
{
start_pos = lastest_pos;
}
location_attribute.CurrentChannelServerLoaction.ServerName = server_logic.getServerName();
location_attribute.CurrentChannelServerLoaction.WorldMetaId = (int)server_logic.getWorldId();
location_attribute.CurrentChannelServerLoaction.fromPos(start_pos);
setCurrentLocation(location_attribute.CurrentChannelServerLoaction);
}
else
{
if (!server_logic.getMap().getAccountStartPoint(out var start_pos))
{
err_msg = $"Failed to getAccountStartPoint : {this.getTypeName()}";
result.setFail(ServerErrorCode.InstanceMetaDataNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
location_attribute.CurrentChannelServerLoaction.ServerName = server_logic.getServerName();
location_attribute.CurrentChannelServerLoaction.WorldMetaId = (int)server_logic.getWorldId();
location_attribute.CurrentChannelServerLoaction.fromPos(start_pos);
setCurrentLocation(location_attribute.CurrentChannelServerLoaction);
}
}
break;
case ServerType.Indun:
{
location_attribute.CurrentIndunLocation = location_attribute.EnterIndunLocation.clone();
setCurrentLocation(location_attribute.CurrentIndunLocation);
}
break;
default:
{
err_msg = $"ServerType invalid !!! : {server_type.ToString()}";
result.setFail(ServerErrorCode.ServerTypeInvalid, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
}
return result;
}
public void setCurrentLocation(BaseLoaction loaction) { m_current_loaction = loaction; }
public BaseLoaction getCurrentLocation()
{
NullReferenceCheckHelper.throwIfNull(m_current_loaction, () => $"m_current_location is null !!! - {getOwner().toBasicString()}");
return m_current_loaction;
}
public Pos getCurrentPos()
{
NullReferenceCheckHelper.throwIfNull(m_current_loaction, () => $"m_current_location is null !!! - {getOwner().toBasicString()}");
Pos pos = new();
m_current_loaction.toPos(pos);
return pos;
}
public int getCurrentMapMId()
{
var mapMId = 0;
var server_logic = GameServerApp.getServerLogic();
if (server_logic.getServerType().toServerType() == ServerType.Channel)
{
var location = m_current_loaction as ChannelServerLocation;
NullReferenceCheckHelper.throwIfNull(location, () => "ChannelServerLocation is null !!");
mapMId = location.WorldMetaId;
}
else
{
var location = m_current_loaction as IndunLocation;
NullReferenceCheckHelper.throwIfNull(location, () => "IndunLocation is null !!");
mapMId = location.InstanceMetaId;
}
return mapMId;
}
public Result tryUpdateCurrentPos(Pos newPos)
{
var result = new Result();
var err_msg = string.Empty;
var server_logic = GameServerApp.getServerLogic();
var server_type = server_logic.getServerType().toServerType();
var player = getOwner() as Player;
NullReferenceCheckHelper.throwIfNull(player, () => "player is null !!!");
var location_attribute = player.getEntityAttribute<LocationAttribute>();
NullReferenceCheckHelper.throwIfNull(location_attribute, () => $"location_attribute is null !!! - {player.toBasicString()}");
switch(server_type)
{
case ServerType.Channel:
{
location_attribute.CurrentChannelServerLoaction.fromPos(newPos);
location_attribute.modifiedEntityAttribute();
}
break;
case ServerType.Indun:
{
location_attribute.CurrentIndunLocation.fromPos(newPos);
}
break;
default:
{
err_msg = $"ServerType Invalid !!! - type:{server_type} - {player.toBasicString()}";
result.setFail(ServerErrorCode.ServerTypeInvalid, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
}
return result;
}
public Result tryModifyCurrentPos(Pos newPos)
{
var result = new Result();
var err_msg = string.Empty;
if (m_current_loaction == null)
{
err_msg = $"m_current_loaction is null : {this.getTypeName()}";
result.setFail(ServerErrorCode.InstanceMetaDataNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
m_current_loaction.fromPos(newPos);
return result;
}
public async Task<Result> tryMoveToChannel(string? serverName = null, int? worldId = null, Pos? pos = null)
{
var result = new Result();
var err_msg = string.Empty;
var server_logic = GameServerApp.getServerLogic();
var player = getOwner() as Player;
NullReferenceCheckHelper.throwIfNull(player, () => "player is null !!!");
var location_attribute = player.getEntityAttribute<LocationAttribute>();
NullReferenceCheckHelper.throwIfNull(location_attribute, () => $"location_attribute is null !!! - {player.toBasicString()}");
NullReferenceCheckHelper.throwIfNull(m_current_loaction, () => "Current Location is null !!");
NullReferenceCheckHelper.throwIfNull(m_location_cache_request_nullable, () => "Current Location Cache is null !!");
if (serverName != null)
{
location_attribute.LastestChannelServerLocation.ServerName = serverName;
}
if (worldId != null)
{
location_attribute.LastestChannelServerLocation.WorldMetaId = (int)worldId;
}
if (null != pos)
{
var location = new BaseLoaction();
location.fromPos(pos);
location_attribute.LastestChannelServerLocation.SpawnPos = location.SpawnPos;
location_attribute.LastestChannelServerLocation.ForwardAngle = location.ForwardAngle;
}
location_attribute.ReJoinIndunLocation.clear();
location_attribute.ReturnIndunLocations.Clear();
location_attribute.EnterIndunLocation.clear();
location_attribute.CurrentIndunLocation.clear();
location_attribute.modifiedEntityAttribute();
var location_cache = m_location_cache_request_nullable.getLocationCache();
NullReferenceCheckHelper.throwIfNull(location_cache, () => $"location_cache is null !!! - {player.toBasicString()}");
location_cache.copyCacheFromEntityAttribute(location_attribute);
result = await m_location_cache_request_nullable.upsertLocation();
if (result.isFail())
{
err_msg = $"Failed to upsertLocation() !!! : {this.getTypeName()}";
Log.getLogger().error(err_msg);
return result;
}
return result;
}
public async Task<Result> tryMoveToIndun(InstanceRoomInfo instanceRoomInfo, Pos? enterPos = null, bool isReturn = false)
{
var result = new Result();
var err_msg = string.Empty;
var player = getOwner() as Player;
NullReferenceCheckHelper.throwIfNull(player, () => $"player is null !!!");
var server_logic = GameServerApp.getServerLogic();
var location_attribute = player.getEntityAttribute<LocationAttribute>();
NullReferenceCheckHelper.throwIfNull(location_attribute, () => $"location_attribute is null !!! - {player.toBasicString()}");
NullReferenceCheckHelper.throwIfNull(m_current_loaction, () => $"m_current_loaction is null !!! - {player.toBasicString()}");
NullReferenceCheckHelper.throwIfNull(m_location_cache_request_nullable, () => "Current Location Cache is null !!");
if (!MetaData.Instance._IndunTable.TryGetValue(instanceRoomInfo.InstanceId, out var indun_data))
{
err_msg = $"Failed to MetaData.TryGetValue() !!! : instanceMetaId:{instanceRoomInfo.InstanceId} - {player.toBasicString()}";
result.setFail(ServerErrorCode.InstanceMetaDataNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
if (server_logic.getServerType().toServerType() == ServerType.Channel)
{
location_attribute.LastestChannelServerLocation.ServerName = server_logic.getServerName();
location_attribute.LastestChannelServerLocation.WorldMetaId = (int)server_logic.getWorldId();
location_attribute.LastestChannelServerLocation.SpawnPos = m_current_loaction.SpawnPos;
location_attribute.LastestChannelServerLocation.ForwardAngle = m_current_loaction.ForwardAngle;
server_logic.getReturnManager().addReturnUser(player.getUserGuid());
}
else if (isReturn == false)
{
IndunLocation indun_location = new();
indun_location.InstanceRoomId = location_attribute.CurrentIndunLocation.InstanceRoomId;
indun_location.InstanceMetaId = location_attribute.CurrentIndunLocation.InstanceMetaId;
indun_location.SpawnPos = m_current_loaction.SpawnPos;
indun_location.ForwardAngle = m_current_loaction.ForwardAngle;
location_attribute.ReturnIndunLocations.Add(indun_location);
server_logic.getReturnManager().addReturnUser(player.getUserGuid());
}
if (enterPos != null)
{
enterPos.Z += 100;
}
location_attribute.EnterIndunLocation.InstanceRoomId = instanceRoomInfo.roomId;
location_attribute.EnterIndunLocation.InstanceMetaId = instanceRoomInfo.InstanceId;
location_attribute.EnterIndunLocation.fromPos(enterPos == null ? MapManager.Instance.GetStartPos(indun_data.RoomFile) : enterPos);
location_attribute.modifiedEntityAttribute();
var location_cache = m_location_cache_request_nullable.getLocationCache();
NullReferenceCheckHelper.throwIfNull(location_cache, () => $"location_cache is null !!! - {player.toBasicString()}");
location_cache.copyCacheFromEntityAttribute(location_attribute);
result = await m_location_cache_request_nullable.upsertLocation();
if (result.isFail())
{
err_msg = $"Failed to upsertLocation() !!! : {result.toBasicString()}";
Log.getLogger().error(err_msg);
return result;
}
return result;
}
public bool isInConcert()
{
var attribute = getOwner().getEntityAttribute<LocationAttribute>();
NullReferenceCheckHelper.throwIfNull(attribute, () => $"location attribute is null !!! - {getOwner().toBasicString()}");
if (!MetaData.Instance._IndunTable.TryGetValue(attribute.CurrentIndunLocation.InstanceMetaId, out var indun_data))
{
return false;
}
return indun_data.placeType() == EPlaceType.Concert;
}
}