Skip to content

Commit

Permalink
Basic logic Added
Browse files Browse the repository at this point in the history
  • Loading branch information
yukiarimo committed Feb 17, 2024
1 parent 96134c2 commit d7dd4e9
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 71 deletions.
44 changes: 23 additions & 21 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ if (isMobile) {
}
}

function createAndShowPopups() {
if (localStorage.getItem('status') == 'undefined' || localStorage.getItem('status') == null) {
function createAndShowPopups(callback) {
if (localStorage.getItem('status') === 'undefined' || localStorage.getItem('status') === null) {
// Function to create a Bootstrap modal with custom styles
function createModal(id, title, body) {
const modalHTML = `
Expand Down Expand Up @@ -79,7 +79,9 @@ function createAndShowPopups() {
// Function to remove a modal from the DOM
function removeModal(id) {
const modalElement = document.getElementById(id);
modalElement.parentNode.removeChild(modalElement);
if (modalElement && modalElement.parentNode) {
modalElement.parentNode.removeChild(modalElement);
}
}

// Create the first modal
Expand All @@ -88,35 +90,38 @@ function createAndShowPopups() {
// Show the first modal
firstModal.show();

// Create an innerHTML small form element in Bootstrap 5 with a single input field and a submit button to collect input and parse it to the initializeSystem function
const formHTML = `
<form id="inputForm" class="form-inline justify-content-center">
<div class="mb-3">
<input type="text" class="form-control" id="inputField" placeholder="Enter your name">
</div>
<button type="button" class="btn btn-primary" onclick="initializeSystem()">Submit</button>
</form>
`;

// When the first modal is closed, remove it and create the second modal
firstModal._element.addEventListener('hidden.bs.modal', function () {
removeModal('firstModal');

// Create and show the second modal
const secondModal = createModal('secondModal', 'SYSTEM', formHTML);
const secondModal = createModal('secondModal', 'SYSTEM', `
<form id="inputForm" class="form-inline justify-content-center">
<div class="mb-3">
<input type="text" class="form-control" id="inputField" placeholder="Enter your name">
</div>
<button type="button" class="btn btn-primary" onclick="initializeSystem()">Submit</button>
</form>
`);
secondModal.show();

// When the second modal is closed, remove it and stop the function
// When the second modal is closed, remove it
secondModal._element.addEventListener('hidden.bs.modal', function () {
// get the input value from the input field and store it in the local storage
localStorage.setItem('nameInit', document.getElementById('inputField').value);
removeModal('secondModal');

// Create and show the third modal
const thirdModal = createModal('thirdModal', 'SYSTEM', 'Done! You are ready to play!');
const thirdModal = createModal('thirdModal', 'SYSTEM', `Ready ${localStorage.getItem('nameInit')}! You are ready to play!`);
thirdModal.show();

// When the third modal is closed, remove it and stop the function
// When the third modal is closed, remove it and call the callback with the value
thirdModal._element.addEventListener('hidden.bs.modal', function () {
removeModal('thirdModal');
const nameInit = localStorage.getItem('nameInit');
if (callback) {
callback(nameInit);
}
});
});
});
Expand All @@ -128,7 +133,4 @@ function initializeSystem() {
const inputField = document.getElementById('inputField');
console.log(inputField.value);
secondModal.click();
}

// Call the function to execute the popup creation and handling
createAndShowPopups();
}
171 changes: 171 additions & 0 deletions assets/js/solo-leveling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
class SoloManager {
constructor() {
this.load();
}

load() {
const data = localStorage.getItem('soloData');
this.data = data ? JSON.parse(data) : {
name: "Yuki",
status: {
xp: 0,
coins: 0,
points: 0,
retries: 0,
level: 1,
demands: {
quests: 0,
xp: 0,
modes: 0,
skippers: 0,
unlockers: 0
}
},
quests: {
active: [],
completed: [],
failed: [],
available: []
},
events: {
active: [],
completed: [],
failed: [],
available: []
},
store: {
shop: [],
exchange: [],
}
};
}

update() {
this.load();

// update the name of the user
document.getElementById('name').innerText = this.data.name;
document.getElementById('name-top').innerText = this.data.name;

// update the level of the user
document.getElementById('level').innerText = this.data.status.level;

// update the xp of the user
document.getElementById('xp').innerText = this.data.status.xp;

// update the coins of the user
document.getElementById('coins').innerText = this.data.status.coins;
}

save() {
localStorage.setItem('soloData', JSON.stringify(this.data));
}

createItem(type, category, item) {
if (!this.data[type] || !this.data[type][category]) {
console.error('Invalid type or category');
return;
}
this.data[type][category].push(item);
this.save();
}

editItem(type, category, itemId, newValues) {
const itemIndex = this.data[type][category].findIndex(item => item.id === itemId);
if (itemIndex === -1) {
console.error('Item not found');
return;
}
this.data[type][category][itemIndex] = {
...this.data[type][category][itemIndex],
...newValues
};
this.save();
}

deleteItem(type, category, itemId) {
const itemIndex = this.data[type][category].findIndex(item => item.id === itemId);
if (itemIndex === -1) {
console.error('Item not found');
return;
}
this.data[type][category].splice(itemIndex, 1);
this.save();
}

renameUser(newName) {
this.data.name = newName;
this.save();
}

modifyItemProperty(type, category, itemId, property, newValue) {
const itemIndex = this.data[type][category].findIndex(item => item.id === itemId);
if (itemIndex === -1) {
console.error('Item not found');
return;
}
this.data[type][category][itemIndex][property] = newValue;
this.save();
}

changeItem(type, category, itemId, newItem) {
const itemIndex = this.data[type][category].findIndex(item => item.id === itemId);
if (itemIndex === -1) {
console.error('Item not found');
return;
}
this.data[type][category][itemIndex] = newItem;
this.save();
}

moveItem(type, fromCategory, toCategory, itemId) {
const itemIndex = this.data[type][fromCategory].findIndex(item => item.id === itemId);
if (itemIndex === -1) {
console.error('Item not found in source category');
return;
}
const item = this.data[type][fromCategory].splice(itemIndex, 1)[0];
this.data[type][toCategory].push(item);
this.save();
}

addNotification(alertText, timestamp) {
// Find the dropdown menu where the alerts will be added
const dropdownMenu = document.getElementById('notification-center');

// Create a new alert item
const alertItem = document.createElement('a');
alertItem.classList.add('dropdown-item', 'd-flex', 'align-items-center');

// Add the inner HTML structure for the alert item
alertItem.innerHTML = `
<div class="fw-bold">
<div class="text-truncate">
<span>${alertText}</span>
</div>
<p class="small text-gray-500 mb-0">${timestamp}</p>
</div>
`;

// Append the new alert item to the dropdown menu
dropdownMenu.appendChild(alertItem);
}
}

// if soloData is not found in localStorage, create a new one with default values
const soloManager = new SoloManager();

// if soloData is found in localStorage, load it, otherwise create a new one with default values
if (!localStorage.getItem('soloData')) {
// run the createAndShowPopups(); function and save returned value to as a name of the user in soloData
createAndShowPopups(function (nameInit) {
soloManager.data.name = nameInit;
soloManager.save();
});
} else {
soloManager.load();
soloManager.update();

// Add notification to the notification center with the current date and time as the timestamp and the alert text as "Welcome back, [name]!"
soloManager.addNotification(`Welcome back, ${soloManager.data.name}!`, new Date().toLocaleString());
}
Loading

0 comments on commit d7dd4e9

Please sign in to comment.