ayush2917 commited on
Commit
f9ebc4d
·
verified ·
1 Parent(s): 3528b58

Update templates/adventure.html

Browse files
Files changed (1) hide show
  1. templates/adventure.html +110 -59
templates/adventure.html CHANGED
@@ -1,68 +1,119 @@
1
- {% extends 'base.html' %}
 
2
  {% block content %}
 
 
3
  <h1>Explore Vrindavan with Krishna!</h1>
4
- <div id="adventure-container">
5
- <div id="adventure-scene"></div>
6
- <form id="adventure-form">
7
- <div id="adventure-choices"></div>
8
- <input type="hidden" id="current-scene" name="current_scene">
9
- <button type="submit" id="adventure-submit" style="display: none;">Continue</button>
10
- </form>
11
- <button id="restart-adventure" style="display: none;">Restart Adventure</button>
12
  </div>
13
- <script>
14
- document.addEventListener('DOMContentLoaded', () => {
15
- const adventureScene = document.getElementById('adventure-scene');
16
- const adventureChoices = document.getElementById('adventure-choices');
17
- const adventureForm = document.getElementById('adventure-form');
18
- const currentSceneInput = document.getElementById('current-scene');
19
- const adventureSubmit = document.getElementById('adventure-submit');
20
- const restartAdventure = document.getElementById('restart-adventure');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- // Load the initial scene
23
- loadScene('start');
 
 
 
 
 
 
 
 
 
 
24
 
25
- async function loadScene(scene) {
26
- try {
27
- const response = await fetch('/adventure', {
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
- if (data.choices.length > 0) {
39
- adventureChoices.innerHTML = data.choices.map(choice => `
40
- <label>
41
- <input type="radio" name="choice" value="${choice}" required>
42
- ${choice}
43
- </label>
44
- `).join('<br>');
45
- adventureSubmit.style.display = 'block';
46
- restartAdventure.style.display = 'none';
47
- } else {
48
- adventureChoices.innerHTML = '';
49
- adventureSubmit.style.display = 'none';
50
- restartAdventure.style.display = 'block';
51
- }
52
- } catch (error) {
53
- console.error('Error loading adventure scene:', error);
54
- adventureScene.innerHTML = '<p>Error loading adventure—try again!</p>';
55
- }
56
- }
57
 
58
- adventureForm.addEventListener('submit', (e) => {
59
- e.preventDefault();
60
- loadScene();
61
- });
 
 
 
 
 
 
62
 
63
- restartAdventure.addEventListener('click', () => {
64
- loadScene('start');
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 %}