Update templates/adventure.html
Browse files- templates/adventure.html +110 -59
templates/adventure.html
CHANGED
@@ -1,68 +1,119 @@
|
|
1 |
-
{% extends
|
|
|
2 |
{% block content %}
|
|
|
|
|
3 |
<h1>Explore Vrindavan with Krishna!</h1>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
<button id="restart-adventure" style="display: none;">Restart Adventure</button>
|
12 |
</div>
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
method: scene === 'start' ? 'GET' : 'POST',
|
29 |
-
headers: {
|
30 |
-
'Content-Type': 'application/x-www-form-urlencoded',
|
31 |
-
},
|
32 |
-
body: scene !== 'start' ? `current_scene=${encodeURIComponent(currentSceneInput.value)}&choice=${encodeURIComponent(document.querySelector('input[name="choice"]:checked')?.value || '')}` : null
|
33 |
-
});
|
34 |
-
const data = await response.json();
|
35 |
-
adventureScene.innerHTML = `<p>${data.scene}</p>`;
|
36 |
-
currentSceneInput.value = data.current_scene || '';
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
console.error('Error loading adventure scene:', error);
|
54 |
-
adventureScene.innerHTML = '<p>Error loading adventure—try again!</p>';
|
55 |
-
}
|
56 |
-
}
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
});
|
67 |
-
</script>
|
68 |
{% endblock %}
|
|
|
1 |
+
{% extends "base.html" %}
|
2 |
+
|
3 |
{% block content %}
|
4 |
+
<div class="container">
|
5 |
+
<div class="adventure-container">
|
6 |
<h1>Explore Vrindavan with Krishna!</h1>
|
7 |
+
|
8 |
+
<div id="adventure-scene" class="adventure-scene">
|
9 |
+
<p>Error loading adventure—try again</p>
|
10 |
+
</div>
|
11 |
+
|
12 |
+
<div id="adventure-choices" class="adventure-choices">
|
13 |
+
<!-- Choices will be populated dynamically -->
|
|
|
14 |
</div>
|
15 |
+
</div>
|
16 |
+
</div>
|
17 |
+
{% endblock %}
|
18 |
+
|
19 |
+
{% block scripts %}
|
20 |
+
<script>
|
21 |
+
async function loadAdventure() {
|
22 |
+
try {
|
23 |
+
const response = await fetch('/adventure', {
|
24 |
+
method: 'POST',
|
25 |
+
headers: {
|
26 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
27 |
+
},
|
28 |
+
body: new URLSearchParams({
|
29 |
+
'current_scene': document.getElementById('current-scene') ? document.getElementById('current-scene').value : 'start',
|
30 |
+
'choice': ''
|
31 |
+
})
|
32 |
+
});
|
33 |
+
|
34 |
+
const data = await response.json();
|
35 |
+
if (data.scene) {
|
36 |
+
document.getElementById('adventure-scene').innerHTML = `<p>${data.scene}</p>`;
|
37 |
+
|
38 |
+
const choicesDiv = document.getElementById('adventure-choices');
|
39 |
+
choicesDiv.innerHTML = '';
|
40 |
+
if (data.choices && data.choices.length > 0) {
|
41 |
+
data.choices.forEach(choice => {
|
42 |
+
const button = document.createElement('button');
|
43 |
+
button.textContent = choice;
|
44 |
+
button.onclick = () => selectChoice(choice, data.current_scene);
|
45 |
+
choicesDiv.appendChild(button);
|
46 |
+
});
|
47 |
+
} else {
|
48 |
+
const restartButton = document.createElement('button');
|
49 |
+
restartButton.textContent = 'Restart Adventure';
|
50 |
+
restartButton.onclick = () => loadAdventure();
|
51 |
+
choicesDiv.appendChild(restartButton);
|
52 |
+
}
|
53 |
+
|
54 |
+
// Update hidden input for current scene
|
55 |
+
let currentSceneInput = document.getElementById('current-scene');
|
56 |
+
if (!currentSceneInput) {
|
57 |
+
currentSceneInput = document.createElement('input');
|
58 |
+
currentSceneInput.type = 'hidden';
|
59 |
+
currentSceneInput.id = 'current-scene';
|
60 |
+
document.getElementById('adventure-scene').appendChild(currentSceneInput);
|
61 |
+
}
|
62 |
+
currentSceneInput.value = data.current_scene || 'start';
|
63 |
+
} else {
|
64 |
+
throw new Error('No scene data returned');
|
65 |
+
}
|
66 |
+
} catch (error) {
|
67 |
+
console.error('Error loading adventure:', error);
|
68 |
+
document.getElementById('adventure-scene').innerHTML = '<p class="adventure-error">Error loading adventure—try again</p>';
|
69 |
+
}
|
70 |
+
}
|
71 |
|
72 |
+
async function selectChoice(choice, currentScene) {
|
73 |
+
try {
|
74 |
+
const response = await fetch('/adventure', {
|
75 |
+
method: 'POST',
|
76 |
+
headers: {
|
77 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
78 |
+
},
|
79 |
+
body: new URLSearchParams({
|
80 |
+
'current_scene': currentScene,
|
81 |
+
'choice': choice
|
82 |
+
})
|
83 |
+
});
|
84 |
|
85 |
+
const data = await response.json();
|
86 |
+
if (data.scene) {
|
87 |
+
document.getElementById('adventure-scene').innerHTML = `<p>${data.scene}</p>`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
const choicesDiv = document.getElementById('adventure-choices');
|
90 |
+
choicesDiv.innerHTML = '';
|
91 |
+
if (data.choices && data.choices.length > 0) {
|
92 |
+
data.choices.forEach(choice => {
|
93 |
+
const button = document.createElement('button');
|
94 |
+
button.textContent = choice;
|
95 |
+
button.onclick = () => selectChoice(choice, data.current_scene);
|
96 |
+
choicesDiv.appendChild(button);
|
97 |
+
});
|
98 |
+
} else {
|
99 |
+
const restartButton = document.createElement('button');
|
100 |
+
restartButton.textContent = 'Restart Adventure';
|
101 |
+
restartButton.onclick = () => loadAdventure();
|
102 |
+
choicesDiv.appendChild(restartButton);
|
103 |
+
}
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
// Update hidden input for current scene
|
106 |
+
document.getElementById('current-scene').value = data.current_scene || 'start';
|
107 |
+
} else {
|
108 |
+
throw new Error('No scene data returned');
|
109 |
+
}
|
110 |
+
} catch (error) {
|
111 |
+
console.error('Error selecting choice:', error);
|
112 |
+
document.getElementById('adventure-scene').innerHTML = '<p class="adventure-error">Error loading adventure—try again</p>';
|
113 |
+
}
|
114 |
+
}
|
115 |
|
116 |
+
// Load the initial adventure scene when the page loads
|
117 |
+
document.addEventListener('DOMContentLoaded', loadAdventure);
|
118 |
+
</script>
|
|
|
|
|
119 |
{% endblock %}
|