KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
UChatHistorySystem.cpp
이 파일의 문서화 페이지로 가기
1// Copyright (c) 2025 Doppleddiggong. All rights reserved. Unauthorized copying, modification, or distribution of this file, via any medium is strictly prohibited. Proprietary and confidential.
2
4#include "ULingoGameHelper.h"
5#include "UConfigLibrary.h"
6#include "FChatHistoryItem.h"
7#include "GameLogging.h"
8#include "Algo/Reverse.h"
9
10void UChatHistorySystem::SaveChatHistory(const FString& Question, const FString& Answer)
11{
12 const int32 UserId = ULingoGameHelper::GetUserId(GetWorld());
13 if (UserId <= 0)
14 {
15 PRINTLOG(TEXT("[ChatHistory] Invalid UserId: %d"), UserId);
16 return;
17 }
18
19 // 1. 현재 Count 가져오기 (O(1))
20 const int32 CurrentCount = UConfigLibrary::GetUserInt(UserId, TEXT("ChatHistoryCount"), 0);
21
22 // 2. 새 항목 생성
23 FChatHistoryItem NewItem;
24 NewItem.Index = CurrentCount;
25 NewItem.Question = Question;
26 NewItem.Answer = Answer;
28
29 // 3. JSON으로 직렬화
30 const FString JsonString = NewItem.ToJson();
31
32 // 4. 개별 키로 저장 (새 항목만!)
33 const FString ItemKey = FString::Printf(TEXT("ChatHistory_%d"), CurrentCount);
34 UConfigLibrary::SetUserString(UserId, ItemKey, JsonString, false);
35
36 // 5. Count 증가 (한 번에 저장)
37 UConfigLibrary::SetUserInt(UserId, TEXT("ChatHistoryCount"), CurrentCount + 1);
38}
39
40int32 UChatHistorySystem::LoadAllChatHistory(TArray<FChatHistoryItem>& OutHistoryList)
41{
42 OutHistoryList.Empty();
43
44 const int32 UserId = ULingoGameHelper::GetUserId(GetWorld());
45 if (UserId <= 0)
46 {
47 return 0;
48 }
49
50 // 1. Count 가져오기
51 const int32 Count = UConfigLibrary::GetUserInt(UserId, TEXT("ChatHistoryCount"), 0);
52 if (Count == 0)
53 {
54 return 0;
55 }
56
57 // 2. 각 항목을 개별적으로 로드
58 for (int32 i = 0; i < Count; ++i)
59 {
60 const FString ItemKey = FString::Printf(TEXT("ChatHistory_%d"), i);
61 const FString JsonString = UConfigLibrary::GetUserString(UserId, ItemKey, TEXT(""));
62
63 if (!JsonString.IsEmpty())
64 {
66 if (FChatHistoryItem::FromJson(JsonString, Item))
67 {
68 OutHistoryList.Add(Item);
69 }
70 }
71 }
72
73 // 3. 최신순으로 정렬 (역순)
74 Algo::Reverse(OutHistoryList);
75
76 return OutHistoryList.Num();
77}
78
79void UChatHistorySystem::ClearChatHistory()
80{
81 const int32 UserId = ULingoGameHelper::GetUserId(GetWorld());
82 if (UserId <= 0)
83 {
84 return;
85 }
86
87 // 1. Count 가져오기
88 const int32 Count = UConfigLibrary::GetUserInt(UserId, TEXT("ChatHistoryCount"), 0);
89
90 // 2. 모든 항목 키 삭제
91 for (int32 i = 0; i < Count; ++i)
92 {
93 const FString ItemKey = FString::Printf(TEXT("ChatHistory_%d"), i);
94 UConfigLibrary::DeleteUserKey(UserId, ItemKey, false); // 마지막에 한 번만 저장
95 }
96
97 // 3. Count 삭제 (한 번에 저장)
98 UConfigLibrary::DeleteUserKey(UserId, TEXT("ChatHistoryCount"));
99}
100
101int32 UChatHistorySystem::GetHistoryCount() const
102{
103 const int32 UserId = ULingoGameHelper::GetUserId(GetWorld());
104 if (UserId <= 0)
105 {
106 return 0;
107 }
108
109 // Count를 직접 반환 (O(1))
110 return UConfigLibrary::GetUserInt(UserId, TEXT("ChatHistoryCount"), 0);
111}
112
113int32 UChatHistorySystem::GetNextIndex() const
114{
115 // GetHistoryCount()가 다음 Index를 반환
116 return GetHistoryCount();
117}
YiSan 전반에서 사용하는 공용 인터페이스를 선언합니다.
#define PRINTLOG(fmt,...)
Definition GameLogging.h:30
Chat 대화 기록을 GConfig를 이용하여 관리하는 컴포넌트입니다.
GConfig 래퍼 설정 관리 시스템
static FString GetUserString(int32 UserId, const FString &Key, const FString &DefaultValue=TEXT(""))
유저별 문자열 설정 읽기
static void SetUserString(int32 UserId, const FString &Key, const FString &Value, bool bAutoSave=true)
유저별 문자열 설정 저장
static int32 GetUserInt(int32 UserId, const FString &Key, int32 DefaultValue=0)
유저별 정수 설정 읽기
static void DeleteUserKey(int32 UserId, const FString &Key, bool bAutoSave=true)
유저별 키 삭제
static void SetUserInt(int32 UserId, const FString &Key, int32 Value, bool bAutoSave=true)
유저별 정수 설정 저장
static int32 GetUserId(const UObject *WorldContextObject)
채팅 히스토리 한 항목을 보관하는 구조체입니다.
int32 Index
히스토리 인덱스입니다.
static bool FromJson(const FString &JsonString, FChatHistoryItem &OutItem)
JSON 문자열을 구조체로 변환합니다.
FString Timestamp
저장 시간 문자열입니다.
FString ToJson() const
구조체를 JSON 문자열로 변환합니다.
static FString CurrentTimestamp()
현재 로컬 타임스탬프를 문자열로 반환합니다.
FString Question
질문 텍스트입니다.
FString Answer
답변 텍스트입니다.