Skip to content

Commit

Permalink
Add save/load, add Options and Info menu
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Jul 11, 2021
1 parent 9ee4bb3 commit 28fc0df
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/frameworkDataObject/data/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** This includes all headers of the Cookie Monster Framework and their relevant data */
const headers = { infoMenu: 1, optionsMenu: 1 };
export default headers;
3 changes: 3 additions & 0 deletions src/frameworkDataObject/data/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** This includes all options of the Cookie Monster Framework and their relevant data */
const settings = {};
export default settings;
7 changes: 6 additions & 1 deletion src/frameworkDataObject/load.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import loadMod from '../saveDataFunctions/loadMod';
import headers from './data/headers';
import settings from './data/settings';
import logicLoop from './logic/logicLoop';

/**
* This creates a load function to the CM object. Per Game code/comments:
* "do stuff with the string data you saved previously"
* @param {string} JSON string of save-data
*/
export default function load(str) {
window.cookieMonsterFrameworkData.framework = JSON.parse(str);
loadMod('cookieMonsterFramework', str, settings, headers, logicLoop);
}
4 changes: 4 additions & 0 deletions src/frameworkDataObject/logic/logicLoop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Main logic loop
*/
export default function logicLoop() {}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ export default function addOptionsMenuSection() {
? '-'
: '+';
buttonSpan.onclick = function () {
toggleHeader('optionsMenu');
toggleHeader('cookieMonsterFramework', 'optionsMenu');
Game.UpdateMenu();
};
titleDiv.appendChild(buttonSpan);

modOptionsDiv.appendChild(titleDiv);

l('menu').appendChild(modOptionsDiv);
const subMenuLength = l('menu').children[2].children.length - 1;
l('menu').children[2].insertBefore(modOptionsDiv, l('menu').children[2].children[subMenuLength]);
}
3 changes: 1 addition & 2 deletions src/frameworkDataObject/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
* @returns {string} The data to be saved
*/
export default function save() {
// window.CookieMonsterFrameworkData.FrameworkSave = { test: 'test' };
return JSON.stringify(window.cookieMonsterFrameworkData.framework);
return JSON.stringify(Game.mods.cookieMonsterFramework.saveData.cookieMonsterFramework);
}
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import registerMod from './initFunctions/registerMod';
import createInfoListing from './menuFunctions/createInfoListing';
import createModMenuSection from './menuFunctions/createModMenuSection';
import toggleHeader from './menuFunctions/toggleHeader';
import loadMod from './saveDataFunctions/loadMod';
import saveFramework from './saveDataFunctions/saveFramework';

export const initFunctions = {
initModFramework,
Expand All @@ -15,4 +17,7 @@ export const menuFunctions = {
};
export const optionFunctions = {};

export const saveFunctions = {};
export const saveFunctions = {
loadMod,
saveFramework,
};
4 changes: 2 additions & 2 deletions src/menuFunctions/createModMenuSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import toggleHeader from './toggleHeader';
* Returns a subsection for the mod to add listings/info to
* @param {string} modName The name of the mod (i.e., CookieMonster)
* @param {string} modDisplayName Display name of the mod (i.e., Cookie Monster)
* @param {string} menuName Name of the menu (i.e., Info, Options)
* @param {string} menuName Name of the menu (i.e., Info, Options)
* @returns {object} modSectionDiv Div of the subseciont
*/
export default function createModMenuSection(modName, modDisplayName, menuName) {
Expand Down Expand Up @@ -32,7 +32,7 @@ export default function createModMenuSection(modName, modDisplayName, menuName)
? '-'
: '+';
buttonSpan.onclick = function () {
toggleHeader(Game.mods.cookieMonsterFramework.saveData[modName].headers[menuName]);
toggleHeader(modName, menuName);
Game.UpdateMenu();
};

Expand Down
4 changes: 3 additions & 1 deletion src/menuFunctions/toggleHeader.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import saveFramework from '../saveDataFunctions/saveFramework';

/**
* This function toggles header options by incrementing them with 1 and handling changes
* It is called by the onclick event of the +/- next to headers
Expand All @@ -17,5 +19,5 @@ export default function toggleHeader(modName, headerName) {
Game.mods.cookieMonsterFramework.saveData[modName].headers[headerName] = 0;
break;
}
// SaveConfig();
saveFramework();
}
36 changes: 36 additions & 0 deletions src/saveDataFunctions/createSaveDataObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Creates the save data object to be stored
* @param {object} saveData The loaded save data
* @param {object} settingsData The data containing default values of settings
* @param {object} headersData The data containing default values of headers
*/
export default function createSaveDataObject(saveData, settingsData, headersData) {
const modSaveData = {};

const settingsObject = {};
Object.keys(settingsData).forEach((i) => {
if (typeof saveData.settings === 'undefined' || typeof saveData.settings[i] === 'undefined') {
settingsObject[i] = settingsData[i].defaultValue; // eslint-disable-line prefer-destructuring
} else {
settingsObject[i] = saveData.settings[i];
}
});
modSaveData.settings = settingsObject;

const headersObject = {};
Object.keys(headersData).forEach((i) => {
if (typeof saveData.headers === 'undefined' || typeof saveData.headers[i] === 'undefined') {
headersObject[i] = headersData[i];
} else {
headersObject[i] = saveData.headers[i];
}
});
modSaveData.headers = headersObject;

Object.keys(saveData).forEach((key) => {
if (key !== 'settings' && key !== 'headers') {
modSaveData[key] = saveData[key];
}
});
return modSaveData;
}
27 changes: 27 additions & 0 deletions src/saveDataFunctions/loadMod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import createSaveDataObject from './createSaveDataObject';
import saveFramework from './saveFramework';

/**
* Load the data from a save-file into a mod
* @param {string} modName The name of the mode to be saved
* @param {string} saveData JSON-string of the save data
* @param {object} settingsData The data containing default values of settings
* @param {object} headersData The data containing default values of headers
* @param {Function} logicLoop The logic-loop of the mod
*/
export default function loadMod(modName, saveData, settingsData, headersData, logicLoop) {
const saveDataObject = JSON.parse(saveData);
Game.mods.cookieMonsterFramework.saveData[modName] = createSaveDataObject(
saveDataObject,
settingsData,
headersData,
);
saveFramework();
logicLoop();
Object.keys(Game.mods.cookieMonsterFramework.saveData[modName].settings).forEach((i) => {
if (typeof settingsData[i].func !== 'undefined') {
settingsData[i].func();
}
});
Game.UpdateMenu();
}
22 changes: 22 additions & 0 deletions src/saveDataFunctions/saveFramework.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This function saves the settings and headers within the Framework without saving any of the other save-data
* This allows saving in between the autosave intervals
*/
export default function saveFramework() {
Object.keys(Game.mods.cookieMonsterFramework.saveData).forEach((modName) => {
const modSaveString = JSON.stringify(Game.mods.cookieMonsterFramework.saveData[modName]);

const cookieClickerSaveString = b64_to_utf8(
unescape(localStorage.getItem('CookieClickerGame')).split('!END!')[0],
);
const pattern = new RegExp(`${modName}.*(;|$)`);
const modSave = cookieClickerSaveString.match(pattern);
if (modSave !== null) {
const newSaveString = cookieClickerSaveString.replace(
modSave[0],
`${modName}:${modSaveString}`,
);
localStorage.setItem('CookieClickerGame', escape(`${utf8_to_b64(newSaveString)}!END!`));
}
});
}
9 changes: 0 additions & 9 deletions src/saveFunctions/saveFramework.js

This file was deleted.

0 comments on commit 28fc0df

Please sign in to comment.