-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (122 loc) · 5.94 KB
/
Copy pathscript.js
File metadata and controls
139 lines (122 loc) · 5.94 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const chatLog = document.getElementById('chat-log');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
// DOM要素の存在チェック
if (!chatLog || !userInput || !sendButton) {
console.error('必要なDOM要素が見つかりません。HTMLを確認してください。');
}
// 女性キャラクターの好感度 (0:最悪 ~ 9:最高)
let girlAffection = 5; // 初期値は中立
const MAX_AFFECTION = 9;
const MIN_AFFECTION = 0;
// 会話回数
let conversationCount = 0;
const MAX_CONVERSATION = 10;
function getCurrentTime() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
return `${hours}:${minutes}`;
}
function addMessage(message, sender) {
const time = getCurrentTime();
let messageHtml = `
<div class="message ${sender}-message">
<div class="message-content-wrapper">
<div class="message-bubble">
<span>${message}</span>
</div>
<div class="message-meta message-info">
<span class="timestamp">${time}</span>
${sender === 'user' ? '<span class="read-status">未読</span>' : ''}
</div>
</div>
</div>
`;
if (chatLog) {
chatLog.insertAdjacentHTML('beforeend', messageHtml);
console.log('Generated HTML:', messageHtml); // デバッグログを追加
chatLog.scrollTop = chatLog.scrollHeight;
}
}
function handleUserInput() {
const userMessage = userInput.value;
if (userMessage.trim() !== '') {
addMessage(userMessage, 'user');
userInput.value = '';
conversationCount++;
// 入力と送信ボタンを一時的に無効化
if (userInput) userInput.disabled = true;
if (sendButton) sendButton.disabled = true;
// バックエンドAPIにメッセージを送信
fetch('http://localhost:3000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: userMessage }),
})
.then(response => response.json())
.then(data => {
const girlMessage = data.reply; // 変数名を変更
addMessage(girlMessage, 'girl'); // ここを'girl'に変更
// 直前のユーザーメッセージに既読マークを付ける
const lastUserMessageInfo = chatLog.querySelector('.user-message .message-meta .read-status');
if (lastUserMessageInfo) {
lastUserMessageInfo.innerText = '既読';
}
// LLMからの返答内容に基づいて好感度を変化させる
const lowerCaseGirlMessage = girlMessage.toLowerCase();
// 好感度を上げるキーワード(例: 肯定的な返答、絵文字など)
if (lowerCaseGirlMessage.includes('ありがとう') || lowerCaseGirlMessage.includes('嬉しい') || lowerCaseGirlMessage.includes('😊') || lowerCaseGirlMessage.includes('💕') || lowerCaseGirlMessage.includes('そうなんだ') || lowerCaseGirlMessage.includes('すごい')) {
girlAffection = Math.min(MAX_AFFECTION, girlAffection + 1);
}
// 好感度を下げるキーワード(例: 否定的な返答、冷たい言葉など)
else if (lowerCaseGirlMessage.includes('ごめん') || lowerCaseGirlMessage.includes('無理') || lowerCaseGirlMessage.includes('😒') || lowerCaseGirlMessage.includes('😑') || lowerCaseGirlMessage.includes('えー') || lowerCaseGirlMessage.includes('は?')) {
girlAffection = Math.max(MIN_AFFECTION, girlAffection - 1);
}
// 入力と送信ボタンを再度有効化
if (userInput) userInput.disabled = false;
if (sendButton) sendButton.disabled = false;
userInput.focus(); // 入力フィールドにフォーカスを戻す
// 会話回数チェックとゲーム終了判定
if (conversationCount >= MAX_CONVERSATION) {
setTimeout(() => {
let gameResult = '';
if (girlAffection >= 7) {
gameResult = 'ゲームクリア!彼女はあなたに夢中だ!';
} else if (girlAffection <= 2) {
gameResult = 'ゲームオーバー…彼女はあなたに冷めきっている。';
} else {
gameResult = 'ゲーム終了。彼女はまだあなたに心を許していない。';
}
alert(gameResult + '\nページをリロードして再挑戦!');
// ゲーム終了後は入力を無効化
if (userInput) userInput.disabled = true;
if (sendButton) sendButton.disabled = true;
}, 1000);
}
})
.catch(error => {
console.error('Error fetching girl response:', error);
addMessage('ごめんなさい、電波が悪いのかな?もう一度送ってみて。', 'girl'); // エラーメッセージも変更
// エラー時も入力と送信ボタンを有効化
if (userInput) userInput.disabled = false;
if (sendButton) sendButton.disabled = false;
});
}
}
document.addEventListener('DOMContentLoaded', () => {
addMessage('はじめまして!', 'girl'); // 初期メッセージを変更
});
if (sendButton) {
sendButton.addEventListener('click', handleUserInput);
}
if (userInput) {
userInput.addEventListener('keydown', (event) => {
// isComposingプロパティをチェックして、IME変換中でない場合のみ送信
if (event.key === 'Enter' && !event.isComposing) {
handleUserInput();
}
});
}