Skip to content

Commit

Permalink
Updated level_selection and game and uploaded code
Browse files Browse the repository at this point in the history
  • Loading branch information
SLimbu30 committed Dec 6, 2023
1 parent 804ed12 commit e6239ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 84 deletions.
38 changes: 12 additions & 26 deletions game/static/game/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,11 @@ ocargo.Game.prototype.setup = function () {
}
}

document.addEventListener("DOMContentLoaded", function() {
var dataElement = document.getElementById('data');
var currentEpisode = dataElement ? dataElement.getAttribute('data-episode') : null;

if (currentEpisode) {
localStorage.setItem('currentEpisode', currentEpisode);
}

});

// Function being called when there is a change in game level.
ocargo.Game.prototype.onLevelChange = function() {
var currentLevelId = LEVEL_ID;
var currentEpisode = this.determineEpisodeFromLevel(currentLevelId);
localStorage.setItem('currentEpisode', currentEpisode);
}

ocargo.Game.prototype.determineEpisodeFromLevel = function(levelId) {
if(levelId <= 12) return "1";
else if(levelId <= 18) return "2";
else if(levelId <= 28) return "3";
else if(levelId <= 32) return "4";
else if(levelId <= 43) return "5";
else if(levelId <= 50) return "6";
else if(levelId <= 60) return "7";
else if(levelId <= 67) return "8";
else if(levelId <= 79) return "9";
else if(levelId <= 91) return "10";
else if(levelId <= 109) return "11";
const currentLevelId = LEVEL_ID;
localStorage.setItem('currentEpisode', EPISODE);
}

restoreCmsLogin()
Expand Down Expand Up @@ -156,6 +133,15 @@ ocargo.Game.prototype.determineEpisodeFromLevel = function(levelId) {
)
}

// Script used to save and check for episode upon loading of the webpage
document.addEventListener("DOMContentLoaded", function() {
const dataElement = document.getElementById('data');
const currentEpisode = dataElement ? dataElement.getAttribute('data-episode') : null;
if (currentEpisode) {
localStorage.setItem('currentEpisode', currentEpisode);
}
});

ocargo.Game.prototype.clearWorkspaceNameInputInSaveTab = function () {
$('#workspaceNameInput').val('')
}
Expand Down
42 changes: 42 additions & 0 deletions game/static/game/js/level_selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,45 @@ function setupCoins() {
}
}
}

document.addEventListener("DOMContentLoaded", function() {
const storedEpisode = localStorage.getItem('currentEpisode');
const allCollapsibles = document.querySelectorAll('.collapse');

// Function to close all except the given ID
function closeAllExcept(openId) {
allCollapsibles.forEach(function(collapsible) {
if (collapsible.id !== openId) {
collapsible.classList.remove('in');
const toggle = document.querySelector(`[data-target="#${collapsible.id}"]`);
toggle.classList.add('collapsed');
toggle.setAttribute('aria-expanded', 'false');
}
});
}

// Open the accordion for the stored episode
const accordionToOpen = storedEpisode ? document.querySelector(`#collapse-${storedEpisode}`) : null;
if (accordionToOpen) {
accordionToOpen.classList.add('in');
const toggle = document.querySelector(`[data-target="#${accordionToOpen.id}"]`);
toggle.classList.remove('collapsed');
toggle.setAttribute('aria-expanded', 'true');
} else {
closeAllExcept(); // Close all of no epissodes are stored
}

// Set up event listeners for accordion toggles
const accordionToggles = document.querySelectorAll('.episode-title');
accordionToggles.forEach(function(toggle) {
toggle.addEventListener('click', function() {
const collapseId = toggle.getAttribute('data-target').replace('#', '');
const isOpened = toggle.getAttribute('aria-expanded') === 'false'; // Notice the change here
localStorage.setItem('currentEpisode', isOpened ? collapseId.split('-')[1] : null);
if (isOpened) {
closeAllExcept(collapseId);
}
});
});

});
13 changes: 0 additions & 13 deletions game/templates/game/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,6 @@
var PYTHON_WORKSPACE = {% if python_workspace == None %}null{% else %}"{{python_workspace|linebreaksbr}}"{% endif %};
</script>

<script>
document.addEventListener("DOMContentLoaded", function(){
var dataElement = document.getElementById('data');
var currentEpisode = dataElement ? dataElement.getAttribute('data-episode') : null;

if (currentEpisode) {
localStorage.setItem('currentEpisode', currentEpisode);
}
});

</script>


<script defer type="text/javascript" src="{% static 'game/js/utils.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/skulpt/skulpt.min.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/skulpt/skulpt-stdlib.js' %}"></script>
Expand Down
45 changes: 0 additions & 45 deletions game/templates/game/level_selection.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,51 +41,6 @@
{% endfor %}
</script>

<script>
document.addEventListener("DOMContentLoaded", function() {
var storedEpisode = localStorage.getItem('currentEpisode');
var allCollapsibles = document.querySelectorAll('.collapse');

// Function to close all except the given ID
function closeAllExcept(openId) {
allCollapsibles.forEach(function(collapsible) {
if (collapsible.id !== openId) {
collapsible.classList.remove('in');
var toggle = document.querySelector(`[data-target="#${collapsible.id}"]`);
toggle.classList.add('collapsed');
toggle.setAttribute('aria-expanded', 'false');
}
});
}

// Open the accordion for the stored episode
var accordionToOpen = storedEpisode ? document.querySelector(`#collapse-${storedEpisode}`) : null;
if (accordionToOpen) {
accordionToOpen.classList.add('in');
var toggle = document.querySelector(`[data-target="#${accordionToOpen.id}"]`);
toggle.classList.remove('collapsed');
toggle.setAttribute('aria-expanded', 'true');
} else {
closeAllExcept(); // Close all if no stored episode
}

// Set up event listeners for accordion toggles
var accordionToggles = document.querySelectorAll('.episode-title');
accordionToggles.forEach(function(toggle) {
toggle.addEventListener('click', function() {
var collapseId = toggle.getAttribute('data-target').replace('#', '');
var isOpened = toggle.getAttribute('aria-expanded') === 'false'; // Notice the change here
localStorage.setItem('currentEpisode', isOpened ? collapseId.split('-')[1] : null);
if (isOpened) {
closeAllExcept(collapseId);
}
});
});

});

</script>

<script src="{% static "game/js/level_selection.js" %}"></script>
{% endblock %}

Expand Down

0 comments on commit e6239ea

Please sign in to comment.