ZeroGwafa / SerenTracker

4 stars 6 forks source link

Possibility of making this work with multiple alts? #1

Open jordan30001 opened 3 years ago

jordan30001 commented 3 years ago

Would it be possible to make this work for alts?

it seems that currently the last instance to update/close is the one that saves to disk.

could it possibly link to the display name in chat and save to a different datastore per display name?

one possible solution is to store "locks" in the datastore and if a "serenData#counter" is locked then increment the counter until an unlocked store is found. locks are released on exit.

let serenDataName;
//look for next available client number
//this may be a race condition in instances where the
//user
let serenIncrementer = 1;
while(true) {
    let isLocked = localStorage.getItem("serenData" + serenIncrementer + "lock") === "true";
    if (!isLocked) {
        localStorage.setItem("serenData" + serenIncrementer + "lock", "true");
        serenDataName = "serenData" + serenIncrementer;
        break;
    } else {
        serenIncrementer++;
    }
}
//before exiting remove the aquired "lock"
//this does not account for crashes
//possibly add a "reset account counter" button?
window.onbeforeunload = function (e) {
    e = e || window.event;
    localStorage.setItem(serenDataName + "lock", "false")
    if (e) {
        e.returnValue = message;
    }
    return message;
};