Files
caliverse_server/GameServer/Global/NoticeChat/NoticeChatManager.cs
2025-05-01 07:20:41 +09:00

247 lines
10 KiB
C#

using ServerCommon;
using System.Collections.Concurrent;
using ServerCore; using ServerBase;
namespace GameServer
{
public class NoticeChatManager : EntityBase, IWithLogActor
{
ConcurrentDictionary<Int32 ,NoticeChat> m_notice_chats = new();
public NoticeChatManager()
: base(EntityType.NoticeChatManager)
{
}
public async Task<Result> LoadDB()
{
var result = new Result();
var server_logic = GameServerApp.getServerLogic();
var dynamo_db_client = server_logic.getDynamoDbClient();
ArgumentNullException.ThrowIfNull(server_logic, $"server_logic is null !!!");
ArgumentNullException.ThrowIfNull(dynamo_db_client, $"dynamo_db_client is null !!!");
var doc = new NoticeChatDoc();
var query_config = dynamo_db_client.makeQueryConfigForReadByPKOnly(doc.getPK());
(result, var read_doc_list) = await dynamo_db_client.simpleQueryDocTypesWithQueryOperationConfig<NoticeChatDoc>(query_config);
if (result.isFail())
{
return result;
}
foreach (var read_doc in read_doc_list)
{
(result, var notice_chat) = await NoticeChat.createMailFromDoc(this, read_doc);
if (result.isFail() || notice_chat == null)
{
continue;
}
var notice_chat_attribute = notice_chat.getEntityAttribute<NoticeChatAttribute>();
if (notice_chat_attribute == null)
{
continue;
}
// 지정한 시간으로부터 repeat count만큼만 공지사항을 보낸다.
while (true)
{
if (notice_chat_attribute.RepeatCount == 0)
{
break;
}
if (notice_chat_attribute.NextNoticeTime > DateTime.UtcNow)
{
break;
}
notice_chat_attribute.RepeatCount -= 1;
notice_chat_attribute.NextNoticeTime = notice_chat_attribute.NextNoticeTime.AddMinutes(notice_chat_attribute.RepeatMinuteTime);
}
if (notice_chat_attribute.RepeatCount == 0)
{
continue;
}
m_notice_chats.TryAdd(notice_chat_attribute.ChatId, notice_chat);
}
return result;
}
public Task<Result> UpdateChat()
{
var result = new Result();
var err_msg = string.Empty;
var server_logic = GameServerApp.getServerLogic();
var player_manager = server_logic.getPlayerManager();
ArgumentNullException.ThrowIfNull(server_logic, $"server_logic is null !!!");
ArgumentNullException.ThrowIfNull(player_manager, $"player_manager is null !!!");
List<NoticeChat> copy_notice_chats = CopyData();
foreach (var notice_chat in copy_notice_chats)
{
var notice_chat_attribute = notice_chat.getEntityAttribute<NoticeChatAttribute>();
if(notice_chat_attribute == null)
{
continue;
}
if (notice_chat_attribute.NextNoticeTime <= DateTime.UtcNow)
{
if (notice_chat_attribute.MessageType != 10 && notice_chat_attribute.MessageType != 11)
{
err_msg = $"NoticeChatInfo messageType is invalid. - GMTOOL Data. messageType : {notice_chat_attribute.MessageType}";
Log.getLogger().error(err_msg);
m_notice_chats.TryRemove(notice_chat_attribute.ChatId, out var removed_chat);
continue;
}
if (notice_chat_attribute.RepeatCount <= 0)
{
m_notice_chats.TryRemove(notice_chat_attribute.ChatId, out var removed_chat);
continue;
}
if(EnumHelper.isDefined<ChatType>(notice_chat_attribute.MessageType) == false)
{
err_msg = $"is Not Defined EChatType !!! EChatType : {notice_chat_attribute.MessageType} - {toBasicString()}";
Log.getLogger().error(err_msg);
continue;
}
Dictionary<LanguageType, (string Sender, ChatType MessageType, string ChatMessage)> languageChatInfos = new();
foreach (var detail in notice_chat_attribute.DetailList)
{
languageChatInfos.Add(detail.Languagetype, (notice_chat_attribute.Sender, (ChatType)notice_chat_attribute.MessageType, detail.ChatMessage));
}
var receivers = player_manager.getUsers();
foreach (var receiver in receivers)
{
var player = receiver.Value;
ArgumentNullException.ThrowIfNull(player, $"player is null !!! - {player.toBasicString()}");
var user_create_or_load_action = player.getEntityAction<UserCreateOrLoadAction>();
ArgumentNullException.ThrowIfNull(user_create_or_load_action, $"user_create_or_load_action is null !!! - {player.toBasicString()}");
if (false == user_create_or_load_action.isCompletedLoadUser())
{
continue;
}
var account_attribute = player.getEntityAttribute<AccountAttribute>();
ArgumentNullException.ThrowIfNull(account_attribute, $"account_attribute is null !!! - {player.toBasicString()}");
if (languageChatInfos.TryGetValue(account_attribute.LanguageType, out var chat_info) == false)
{
continue;
}
ChatNotifyHelper.send_S2C_NTF_CHAT(receiver.Value, chat_info.MessageType, chat_info.Sender, receiver.Value.getUserNickname(), PlayerStateType.None, chat_info.ChatMessage);
}
notice_chat_attribute.RepeatCount -= 1;
notice_chat_attribute.NextNoticeTime = notice_chat_attribute.NextNoticeTime.AddMinutes(notice_chat_attribute.RepeatMinuteTime);
}
}
return Task.FromResult(result);
}
private List<NoticeChat> CopyData()
{
return new List<NoticeChat>(m_notice_chats.Values);
}
public async Task<Result> CheatFuncSaveNoticeChat(int messageType, string KoMessage, string EnMessage)
{
var result = new Result();
var err_msg = string.Empty;
var server_logic = GameServerApp.getServerLogic();
var dynamo_db_client = server_logic.getDynamoDbClient();
ArgumentNullException.ThrowIfNull(server_logic, $"server_logic is null !!!");
ArgumentNullException.ThrowIfNull(dynamo_db_client, $"dynamo_db_client is null !!!");
var fn_cheat_notice_chat_write = async delegate ()
{
var result = new Result();
int new_chat_id = 0;
var notice_chat_list = CopyData();
foreach (var noticechat in notice_chat_list)
{
var notice_chat_attrib = noticechat.getEntityAttribute<NoticeChatAttribute>();
NullReferenceCheckHelper.throwIfNull(notice_chat_attrib, () => $"notice_chat_attrib is null !!!");
int chat_id = notice_chat_attrib.ChatId;
if (new_chat_id < chat_id)
{
new_chat_id = chat_id;
}
}
(result, var notice_chat) = await NoticeChat.createTestNoticeChat(this, new_chat_id, messageType, KoMessage, EnMessage);
if (result.isFail() || notice_chat == null)
{
return result;
}
var batch = new QueryBatchEx<QueryRunnerWithDocument>(this, LogActionType.TestWriteNoticeChat
, server_logic.getDynamoDbClient());
{
batch.addQuery(new DBQWriteToAttributeAllWithTransactionRunner());
}
result = await QueryHelper.sendQueryAndBusinessLog(batch);
if (result.isFail())
{
return result;
}
m_notice_chats.TryAdd(new_chat_id, notice_chat);
return result;
};
result = await this.runTransactionRunnerSafely(TransactionIdType.PrivateContents, "CheatNoticeChatWrite", fn_cheat_notice_chat_write);
if (result.isFail())
{
err_msg = $"Failed to runTransactionRunnerSafely()!!! : {result.toBasicString()} - {this.toBasicString()}";
Log.getLogger().error(err_msg);
}
return result;
}
public override string toBasicString()
{
return $"{this.getTypeName()}";
}
public override string toSummaryString()
{
return $"{this.getTypeName()}";
}
public ILogActor toLogActor()
{
var server_logic = ServerLogicApp.getServerLogicApp();
var log_info = new NoticeChatActorLog();
if (server_logic == null)
return log_info;
log_info.initLogInfo(
// 서버 정보
server_logic.getServerConfig().getRegionId()
, server_logic.getServerConfig().getWorldId()
, server_logic.getServerType().toServerType()
);
return log_info;
}
}
}