Spaces:
Running
Running
// 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); | |
}); | |
} |