Spaces:
Running
Running
File size: 3,609 Bytes
a3830f2 427caef a3830f2 427caef 6c5b86d a3830f2 3eb99bc 427caef 3eb99bc a3830f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
// portfolio/npc_social_network/static/js/npc_chat.js
async function sendMessage() {
const userMessage = document.getElementById("message").value;
const npc = document.getElementById("npc").value;
const responseBox = document.getElementById("chatBox");
if (userMessage.trim() == "") return;
// UIμ μ μ λ©μμ§ μΆκ°
responseBox.innerHTML += `<div class = "chat-message"><span class="user">You:</span> ${userMessage}</div>`;
document.getElementById("message").value = "";
// μλ²μ λ©μμ§ μ μ‘
const response = await fetch('/npc_social_network/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: userMessage, npc: npc })
});
const data = await response.json();
// NPC μλ΅ μΆλ ₯
responseBox.innerHTML += `<div class="chat-message"><span class="npc">${npc}:</span> ${data.npc_reply}</div>`;
// κ°μ summary νμ (μ±ν
μ°½μ μΆλ ₯)
responseBox.innerHTML += `<div class="chat-message"><span class="npc">[κ°μ μν]:</span> ${data.memory_summary}</div>`;
// κ΄κ³ μν μ
λ°μ΄νΈ (μλ¨ #relationStatusμ νμ)
document.getElementById("relationStatus").innerText = `κ΄κ³ μ μ: ${data.relationship_with_player}`;
// Personality μν μ
λ°μ΄νΈ
const personalityStatusElem = document.getElementById("personalityStatus");
personalityStatusElem.innerHTML = ""; // μ΄κΈ°ν
for (const [key, value] of Object.entries(data.personality)) {
const li = document.createElement("li");
li.textContent = `${key}: ${value.toFixed(2)}`;
personalityStatusElem.appendChild(li);
}
// μ€ν¬λ‘€ νλ¨ κ³ μ
responseBox.scrollTop = responseBox.scrollHeight;
}
// NPC μ 보 λ‘λ (NPC μ ν λ³κ²½ μ κ΄κ³ μ μ μ
λ°μ΄νΈμ©)
async function loadNPCInfo() {
const npc = document.getElementById("npc").value;
const response = await fetch(`/npc_social_network/npc_info?npc=${encodeURIComponent(npc)}`);
const data = await response.json();
// κ΄κ³ μν μ
λ°μ΄νΈ
document.getElementById("relationStatus").innerText = `κ΄κ³ μ μ: ${data.relationship_with_player}`;
// Personality μν μ
λ°μ΄νΈ
const personalityStatusElem = document.getElementById("personalityStatus");
personalityStatusElem.innerHTML = ""; // μ΄κΈ°ν
for (const [key, value] of Object.entries(data.personality)) {
const li = document.createElement("li");
li.textContent = `${key}: ${value.toFixed(2)}`;
personalityStatusElem.appendChild(li);
}
}
// λ²νΌ ν΄λ¦ μ NPC μνΈμμ© μλ μ€ν
async function triggerNPCInteractions() {
const response = await fetch('/npc_social_network/trigger_npc_interactions', {
method: 'POST'
});
const data = await response.json();
console.log(data.status); // μ½μ νμΈμ©
}
// NPC κΈ°μ΅ λ‘λ
async function loadNPCMemory() {
const npc = document.getElementById("npc").value;
const response = await fetch(`/npc_social_network/npc_memory?npc=${encodeURIComponent(npc)}`);
const data = await response.json();
const memoryListElem = document.getElementById("memoryList");
memoryListElem.innerHTML = ""; // μ΄κΈ°ν
if (data.memory_list.length === 0) {
memoryListElem.innerHTML = "<li>(κΈ°μ΅μμ)</li>";
return;
}
data.memory_list.forEach(memory => {
const li = document.createElement("li");
li.textContent = memory;
memoryListElem.appendChild(li);
});
} |