-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
159 lines (139 loc) · 5.14 KB
/
Copy pathinit.js
File metadata and controls
159 lines (139 loc) · 5.14 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Game state variables
let gameActive = false;
let selectedCaseType = null;
let inGameTime = 0; // seconds
let gameIntervalId = null;
let cost = 0;
let score = 0;
let patientData = null;
let vitalSigns = null;
let caseHistory = [];
let actionInProgress = false;
// Cloudflare Worker proxy URL - keeps API key secure on server side
// Update this URL after deploying your Cloudflare Worker (see worker.js)
const workerUrl = "https://rhesus.w4yq4gvh58.workers.dev/";
const apiModel = "gpt-4o-mini";
// DOM Elements
const startGameButton = document.getElementById('startGame');
const caseButtons = document.querySelectorAll('.case-button');
const patientDataSection = document.getElementById('patient-data');
const patientDemographics = document.getElementById('patient-demographics');
const chiefComplaint = document.getElementById('chief-complaint');
const historySection = document.getElementById('history');
const vitalsDisplay = document.getElementById('vitals');
const resultsArea = document.getElementById('results-area');
const messageArea = document.getElementById('messageArea');
const chatInput = document.getElementById('chatInput');
const sendMessageButton = document.getElementById('sendMessage');
const actionButtons = document.getElementById('action-buttons');
const subActionsArea = document.getElementById('sub-actions');
const orderEntryArea = document.getElementById('order-entry');
const preGameMessage = document.getElementById('pre-game-message');
const timerDisplay = document.getElementById('timer');
const scoreDisplay = document.getElementById('score');
const costDisplay = document.getElementById('cost');
// Initialize the game
function init() {
// Set up event listeners
caseButtons.forEach(button => {
button.addEventListener('click', () => {
selectedCaseType = button.dataset.type;
caseButtons.forEach(btn => btn.classList.remove('selected'));
button.classList.add('selected');
// Update UI to show selection
startGameButton.disabled = false;
startGameButton.classList.remove('disabled');
});
});
startGameButton.addEventListener('click', () => startGame());
sendMessageButton.addEventListener('click', () => sendMessage());
chatInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
// Set up action button listeners
document.querySelectorAll('.action-button').forEach(button => {
button.addEventListener('click', () => {
if (gameActive && !actionInProgress) {
handleAction(button.dataset.action);
}
});
});
}
// Format game time (convert seconds to mm:ss)
function formatGameTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
}
// Update displays (timer, score, cost)
function updateDisplays() {
timerDisplay.textContent = `Time: ${formatGameTime(inGameTime)}`;
scoreDisplay.textContent = `Score: ${score}`;
costDisplay.textContent = `Cost: $${cost.toFixed(2)}`;
}
// Increment the cost counter
function incrementCost() {
cost += 0.01;
updateDisplays();
}
// Reset the game
function resetGame() {
// Clear any existing intervals
if (gameIntervalId) {
clearInterval(gameIntervalId);
gameIntervalId = null;
}
// Reset game state
gameActive = false;
selectedCaseType = null;
inGameTime = 0;
cost = 0;
score = 0;
patientData = null;
vitalSigns = null;
caseHistory = [];
// Update UI
updateDisplays();
// Reset UI elements
startGameButton.disabled = false;
startGameButton.classList.remove('disabled');
caseButtons.forEach(btn => btn.disabled = false);
chatInput.disabled = true;
sendMessageButton.disabled = true;
actionButtons.parentElement.classList.add('hidden');
preGameMessage.classList.remove('hidden');
patientDataSection.classList.add('hidden');
// Clear content areas
vitalsDisplay.innerHTML = '<p>Select a case type and start the game to see vital signs.</p>';
resultsArea.innerHTML = '<p>Results from your orders will appear here.</p>';
messageArea.innerHTML = '';
patientDemographics.textContent = '';
chiefComplaint.textContent = '';
historySection.textContent = '';
}
// Call the API through the secure Cloudflare Worker proxy
async function callAPI(messages) {
try {
const response = await fetch(workerUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: apiModel,
messages: messages
})
});
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('API call failed:', error);
throw error;
}
}
// Initialize the game when the page loads
document.addEventListener('DOMContentLoaded', init);