239 lines
8.9 KiB
C#
239 lines
8.9 KiB
C#
using Google.Protobuf;
|
|
using Google.Protobuf.WellKnownTypes;
|
|
|
|
|
|
using ServerCore;
|
|
using ServerBase;
|
|
using ServerCommon;
|
|
using ServerCommon.BusinessLogDomain;
|
|
using MetaAssets;
|
|
|
|
|
|
namespace GameServer;
|
|
|
|
public class GlobalPartyDetailInstanceAction : EntityActionBase
|
|
{
|
|
private readonly PartyInstanceCacheRequest m_party_instance_cache_request;
|
|
|
|
public GlobalPartyDetailInstanceAction(GlobalPartyDetail owner) : base(owner)
|
|
{
|
|
m_party_instance_cache_request =
|
|
new PartyInstanceCacheRequest(owner.PartyGuid, GameServerApp.getServerLogic().getRedisConnector());
|
|
}
|
|
|
|
public override async Task<Result> onInit()
|
|
{
|
|
await Task.CompletedTask;
|
|
|
|
var result = new Result();
|
|
|
|
return result;
|
|
}
|
|
|
|
public override void onClear()
|
|
{
|
|
return;
|
|
}
|
|
|
|
public async Task<Result> keep()
|
|
{
|
|
return await m_party_instance_cache_request.keepPartyInstanceCache();
|
|
}
|
|
|
|
public async Task<Result> loadPartyInstance()
|
|
{
|
|
var attribute = getOwner().getEntityAttribute<PartyInstanceAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(attribute, () => $"PartyInstanceAttribute is null !! - {getOwner().toBasicString()}");
|
|
|
|
var result = await m_party_instance_cache_request.fetchPartyInstanceCache();
|
|
if (result.isFail()) return result;
|
|
|
|
result = await ServerBase.DataCopyHelper.copyEntityAttributeFromCaches(attribute,
|
|
new List<CacheBase> { m_party_instance_cache_request.getPartyInstanceCache()! });
|
|
if (result.isFail())
|
|
{
|
|
Log.getLogger().error(result.toBasicString());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> registerPartyInstance(int instanceId, string roomId, Timestamp startTime, Timestamp endTime, int joinCount, bool isNotify)
|
|
{
|
|
var result = new Result();
|
|
|
|
var party = getOwner() as GlobalPartyDetail;
|
|
NullReferenceCheckHelper.throwIfNull(party, () => "global party detail is null !!!");
|
|
|
|
var party_instance_attribute = getOwner().getEntityAttribute<PartyInstanceAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(party_instance_attribute, () => $"PartyInstanceAttribute is null !! - {party.toBasicString()}");
|
|
|
|
// 1. cache 생성
|
|
result = await m_party_instance_cache_request.createPartyInstanceCache(instanceId, roomId, startTime, endTime, joinCount);
|
|
if (result.isFail()) return result;
|
|
|
|
// 2. attribute 복사
|
|
result = await ServerBase.DataCopyHelper.copyEntityAttributeFromCaches(party_instance_attribute,
|
|
new List<CacheBase> { m_party_instance_cache_request.getPartyInstanceCache()! });
|
|
if (result.isFail())
|
|
{
|
|
Log.getLogger().error(result.toBasicString());
|
|
}
|
|
|
|
if (false == isNotify) return result;
|
|
|
|
// 8. instance 정보 알림 ( to Servers )
|
|
var server_message = new ServerMessage();
|
|
server_message.PartyInstanceInfoNoti = new();
|
|
server_message.PartyInstanceInfoNoti.PartyGuid = party.PartyGuid;
|
|
|
|
PartyHelper.BroadcastToServers(party, server_message, true);
|
|
|
|
// 9. instance 정보 알림 ( to Clients )
|
|
var client_message = new ClientToGame();
|
|
client_message.Message = new();
|
|
client_message.Message.PartyInstanceInfoNoti = new();
|
|
client_message.Message.PartyInstanceInfoNoti.InstanceId = instanceId;
|
|
client_message.Message.PartyInstanceInfoNoti.StartTime = startTime;
|
|
client_message.Message.PartyInstanceInfoNoti.EndTime = endTime;
|
|
client_message.Message.PartyInstanceInfoNoti.JoinMemberCount = joinCount;
|
|
client_message.Message.PartyInstanceInfoNoti.IsEnd = BoolType.False;
|
|
|
|
PartyHelper.BroadcastToClients(party, client_message, new List<string>());
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> deletePartyInstance()
|
|
{
|
|
// 1. cache 제거
|
|
var result = await m_party_instance_cache_request.deletePartyInstanceCache();
|
|
if (result.isFail())
|
|
{
|
|
Log.getLogger().error(result.toBasicString());
|
|
}
|
|
|
|
// 2. attribute 제거
|
|
var attribute = getOwner().getEntityAttribute<PartyInstanceAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(attribute, () => $"PartyInstanceAttribute is null !! - {getOwner().toBasicString()}");
|
|
|
|
attribute.onClear();
|
|
|
|
return result;
|
|
}
|
|
|
|
public bool isExist()
|
|
{
|
|
var attribute = getOwner().getEntityAttribute<PartyInstanceAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(attribute, () => $"PartyInstanceAttribute is null !! - {getOwner().toBasicString()}");
|
|
|
|
return attribute.InstanceId > 0;
|
|
}
|
|
|
|
public Timestamp getStartTime()
|
|
{
|
|
var attribute = getOwner().getEntityAttribute<PartyInstanceAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(attribute, () => $"PartyInstanceAttribute is null !! - {getOwner().toBasicString()}");
|
|
|
|
return attribute.StartTime;
|
|
}
|
|
|
|
public async Task<Result> finishPartyInstance(int joinCount = 0)
|
|
{
|
|
return await changePartyInstance(DateTimeHelper.MinTime.ToTimestamp(), DateTimeHelper.MinTime.ToTimestamp(), joinCount, true, true);
|
|
}
|
|
|
|
public async Task<Result> changePartyInstance(Timestamp startTime, Timestamp endTime, int joinCount, bool isFinish, bool notifyStartInstance)
|
|
{
|
|
var result = new Result();
|
|
|
|
// 1. cache 수정
|
|
var cache = m_party_instance_cache_request.getPartyInstanceCache();
|
|
NullReferenceCheckHelper.throwIfNull(cache, () => $"Party Instance Cache is null !!! - {getOwner().toBasicString()}");
|
|
|
|
cache.JoinMemberCount = joinCount;
|
|
cache.StartTime = startTime;
|
|
cache.EndTime = endTime;
|
|
|
|
if (isFinish)
|
|
{
|
|
cache.InstanceId = 0;
|
|
cache.RoomId = string.Empty;
|
|
}
|
|
|
|
result = await m_party_instance_cache_request.upsertPartyInstanceCache();
|
|
if (result.isFail()) return result;
|
|
|
|
// 2. attribute 수정
|
|
var attribute = getOwner().getEntityAttribute<PartyInstanceAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(attribute, () => $"PartyInstanceAttribute is null !! - {getOwner().toBasicString()}");
|
|
|
|
attribute.JoinMemberCount = cache.JoinMemberCount;
|
|
attribute.StartTime = startTime;
|
|
attribute.EndTime = endTime;
|
|
|
|
if (isFinish)
|
|
{
|
|
attribute.InstanceId = 0;
|
|
attribute.RoomId = string.Empty;
|
|
}
|
|
|
|
if (notifyStartInstance)
|
|
{
|
|
sendPartyInstanceInfoToMembers();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task changeJoinMemberCount(int joinCount, bool notify)
|
|
{
|
|
// 1. cache 수정
|
|
var cache = m_party_instance_cache_request.getPartyInstanceCache();
|
|
NullReferenceCheckHelper.throwIfNull(cache, () => $"Party Instance Cache is null !!! - {getOwner().toBasicString()}");
|
|
|
|
cache.JoinMemberCount = joinCount;
|
|
|
|
await m_party_instance_cache_request.upsertPartyInstanceCache();
|
|
|
|
// 2. attribute 수정
|
|
var attribute = getOwner().getEntityAttribute<PartyInstanceAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(attribute, () => $"PartyInstanceAttribute is null !! - {getOwner().toBasicString()}");
|
|
|
|
attribute.JoinMemberCount = cache.JoinMemberCount;
|
|
|
|
if (notify)
|
|
{
|
|
sendPartyInstanceInfoToMembers();
|
|
}
|
|
}
|
|
|
|
private void sendPartyInstanceInfoToMembers()
|
|
|
|
{
|
|
// 1. server message
|
|
var party = getOwner() as GlobalPartyDetail;
|
|
NullReferenceCheckHelper.throwIfNull(party, () => "Party Detail is null");
|
|
|
|
var attribute = getOwner().getEntityAttribute<PartyInstanceAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(attribute, () => $"PartyInstanceAttribute is null !! - {party.toBasicString()}");
|
|
|
|
var server_message = new ServerMessage();
|
|
server_message.PartyInstanceInfoNoti = new();
|
|
server_message.PartyInstanceInfoNoti.PartyGuid = party.PartyGuid;
|
|
|
|
PartyHelper.BroadcastToServers(party, server_message, true);
|
|
|
|
// 2. client message
|
|
var client_message = new ClientToGame();
|
|
client_message.Message = new();
|
|
client_message.Message.PartyInstanceInfoNoti = new();
|
|
client_message.Message.PartyInstanceInfoNoti.InstanceId = attribute.InstanceId;
|
|
client_message.Message.PartyInstanceInfoNoti.JoinMemberCount = attribute.JoinMemberCount;
|
|
client_message.Message.PartyInstanceInfoNoti.StartTime = attribute.StartTime;
|
|
client_message.Message.PartyInstanceInfoNoti.EndTime = attribute.EndTime;
|
|
client_message.Message.PartyInstanceInfoNoti.IsEnd = attribute.InstanceId == 0 ? BoolType.True : BoolType.False;
|
|
|
|
PartyHelper.BroadcastToClients(party, client_message, new List<string>());
|
|
}
|
|
} |