Skip to content

Commit

Permalink
Merge pull request #386 from Naviary3/anonymous-badger
Browse files Browse the repository at this point in the history
  • Loading branch information
Naviary2 authored Jan 4, 2025
2 parents 44bc278 + c1f69c0 commit 9c730a3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/client/scripts/esm/game/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ function start() {

onlinegame.askServerIfWeAreInGame();

localstorage.eraseExpiredItems();

gameLoop(); // Update & draw the scene repeatedly
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@

'use strict';

/**
* This script handles reading, saving, and deleting expired
* browser local storage data for us!
* Without it, things we save NEVER expire or are deleted.
* (unless the user clears their browser cache)
*/


/** An entry in local storage */
interface Entry {
/** The actual value of the entry */
value: any,
/** The timestamp the entry will become stale, at which point it should be deleted. */
expires: number
}

/** For debugging. This prints to the console all save and delete operations. */
const printSavesAndDeletes = false;

const defaultExpiryTimeMillis = 1000 * 60 * 60 * 24; // 24 hours
// const defaultExpiryTimeMillis = 1000 * 20; // 20 seconds

function saveItem(key, value, expiryMillis = defaultExpiryTimeMillis) {
// Do this on load every time
eraseExpiredItems();

/**
* Saves an item in browser local storage
* @param key - The key-name to give this entry.
* @param value - What to save
* @param [expiryMillis] How long until this entry should be auto-deleted for being stale
*/
function saveItem(key: string, value: any, expiryMillis: number = defaultExpiryTimeMillis) {
if (printSavesAndDeletes) console.log(`Saving key to local storage: ${key}`);
const timeExpires = Date.now() + expiryMillis;
const save = { value, expires: timeExpires };
const save: Entry = { value, expires: timeExpires };
const stringifiedSave = JSON.stringify(save);
localStorage.setItem(key, stringifiedSave);
}

function loadItem(key) {
const stringifiedSave = localStorage.getItem(key); // "{ value, expiry }"
/**
* Loads an item from browser local storage
* @param key - The name/key of the item in storage
* @returns The entry
*/
function loadItem(key: string): any {
const stringifiedSave: string | null = localStorage.getItem(key); // "{ value, expiry }"
if (stringifiedSave === null) return;
let save;
let save: Entry | any;
try {
save = JSON.parse(stringifiedSave); // { value, expires }
} catch (e) { // Value wasn't in json format, just delete it. They have to be in json because we always store the 'expiry' property.
Expand All @@ -39,12 +61,16 @@ function loadItem(key) {
return save.value;
}

function deleteItem(key) {
/**
* Deletes an item from browser local storage
* @param key The name/key of the item in storage
*/
function deleteItem(key: string) {
if (printSavesAndDeletes) console.log(`Deleting local storage item with key '${key}!'`);
localStorage.removeItem(key);
}

function hasItemExpired(save) {
function hasItemExpired(save: Entry | any) {
if (save.expires === undefined) {
console.log(`Local storage item was in an old format. Deleting it! Value: ${JSON.stringify(save)}}`);
return true;
Expand Down

0 comments on commit 9c730a3

Please sign in to comment.