Skip to content

Commit bad4ad3

Browse files
committed
FIX: Messages were triggering at the same time
Was calling the onMessageComplete function twice, fixed that
1 parent 9333f53 commit bad4ad3

File tree

1 file changed

+38
-70
lines changed

1 file changed

+38
-70
lines changed

ai_animation/src/domElements/chatWindows.ts

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,7 @@ function fiterAndSortChatMessagesForPhase(phase: GamePhase): Message[] {
157157
relevantMessages.sort((a, b) => a.time_sent - b.time_sent);
158158
return relevantMessages
159159
}
160-
/**
161-
* updates the gameState.phaseChatMessages array to the messages for this phase.
162-
*
163-
*/
164-
export function initChatMessagesForPhase() {
165-
gameState.phaseChatMessages = []
166-
gameState.phaseChatMessages = fiterAndSortChatMessagesForPhase(gameState.gameData.phases[gameState.phaseIndex])
167-
}
168160

169-
function playChatMessage(messageIndex) {
170-
addMessageToChat(gameState.currentPhase)
171-
172-
}
173161
// Modified to accumulate messages instead of resetting and only animate for new messages
174162
/**
175163
* Updates chat windows with messages for the current phase
@@ -180,7 +168,6 @@ export function updateChatWindows(stepMessages = false, callback?: () => void) {
180168
// Exit early if no messages
181169
if (!gameState.currentPhase.messages || !gameState.currentPhase.messages.length) {
182170
console.log("No messages to display for this phase");
183-
gameState.messagesPlaying = false;
184171
return;
185172
}
186173

@@ -201,73 +188,54 @@ export function updateChatWindows(stepMessages = false, callback?: () => void) {
201188
console.log(`Found ${relevantMessages.length} messages for player ${gameState.currentPower} in phase ${gameState.currentPhase.name}`);
202189
}
203190

204-
if (!stepMessages) {
205-
// Normal mode: show all messages at once
206-
relevantMessages.forEach(msg => {
207-
const isNew = addMessageToChat(msg);
208-
if (isNew) {
209-
// Increment message counter and play sound on every third message
210-
messageCounter++;
211-
animateHeadNod(msg, (messageCounter % config.soundEffectFrequency === 0));
212-
}
213-
});
214-
} else {
215-
// Stepwise mode: show one message at a time, animating word-by-word
216-
let index = 0;
217-
218-
// Store the start time for debugging
219-
const messageStartTime = Date.now();
220-
221-
// Function to process the next message
222-
const showNext = () => {
223-
// If we're not playing or user has manually advanced, stop message animation
224-
if (!gameState.isPlaying && !config.isDebugMode) {
225-
console.log("Playback stopped, halting message animations");
226-
return;
227-
}
191+
// Stepwise mode: show one message at a time, animating word-by-word
192+
let index = 0;
228193

229-
// All messages have been displayed
230-
if (index >= relevantMessages.length) {
231-
if (config.isDebugMode) {
232-
console.log(`All messages displayed in ${Date.now() - messageStartTime}ms`);
233-
}
234-
console.log("Messages complete, triggering next phase");
235-
if (callback) callback();
236-
return;
237-
}
194+
// Store the start time for debugging
195+
const messageStartTime = Date.now();
238196

239-
// Get the next message
240-
const msg = relevantMessages[index];
197+
// Function to process the next message
198+
const showNext = () => {
199+
// If we're not playing or user has manually advanced, stop message animation
200+
if (!gameState.isPlaying && !config.isDebugMode) {
201+
console.log("Playback stopped, halting message animations");
202+
return;
203+
}
241204

242-
// Only log in debug mode to reduce console noise
205+
// All messages have been displayed
206+
if (index >= relevantMessages.length) {
243207
if (config.isDebugMode) {
244-
console.log(`Displaying message ${index + 1}/${relevantMessages.length}: ${msg.sender} to ${msg.recipient}`);
208+
console.log(`All messages displayed in ${Date.now() - messageStartTime}ms`);
245209
}
210+
console.log("Messages complete, triggering next phase");
211+
if (callback) callback();
212+
return;
213+
}
246214

247-
// Function to call after message animation completes
248-
const onMessageComplete = () => {
249-
index++; // Only increment after animation completes
250-
251-
// Schedule next message with proper delay
252-
gameState.eventQueue.scheduleDelay(config.messageBetweenDelay, showNext, `show-next-message-${index}-${Date.now()}`);
253-
};
215+
// Get the next message
216+
const msg = relevantMessages[index];
254217

255-
// Add the message with word animation
256-
const isNew = addMessageToChat(msg, true, onMessageComplete);
218+
// Only log in debug mode to reduce console noise
219+
if (config.isDebugMode) {
220+
console.log(`Displaying message ${index + 1}/${relevantMessages.length}: ${msg.sender} to ${msg.recipient}`);
221+
}
257222

258-
// Handle non-new messages
259-
if (!isNew) {
260-
onMessageComplete(); // Skip animation for already seen messages
261-
} else {
262-
// Animate head and play sound for new messages (not just when not in debug mode)
263-
messageCounter++;
264-
animateHeadNod(msg, (messageCounter % config.soundEffectFrequency === 0));
265-
}
223+
// Function to call after message animation completes
224+
const onMessageComplete = () => {
225+
index++; // Only increment after animation completes
226+
showNext()
266227
};
267228

268-
// Start the message sequence with initial delay
269-
gameState.eventQueue.scheduleDelay(50, showNext, `start-message-sequence-${Date.now()}`);
270-
}
229+
// Add the message with word animation
230+
addMessageToChat(msg, true, onMessageComplete);
231+
232+
// Animate head and play sound for new messages (not just when not in debug mode)
233+
messageCounter++;
234+
animateHeadNod(msg, (messageCounter % config.soundEffectFrequency === 0));
235+
};
236+
237+
// Start the message sequence with initial delay
238+
gameState.eventQueue.scheduleDelay(50, showNext, `start-message-sequence-${Date.now()}`);
271239
}
272240

273241
// Modified to support word-by-word animation and callback

0 commit comments

Comments
 (0)