Move import save functionality into SaveObject

Adds a lot of metadata to be able to easily compare the new save to the
current player.

- Adds lastSaved & identifier to the PlayerObject
- Includes a hash method used to generate an identifier
This commit is contained in:
Martin Fournier
2022-01-18 11:53:30 -05:00
parent 588c3e42d7
commit 974344545d
4 changed files with 198 additions and 87 deletions
+28 -1
View File
@@ -97,4 +97,31 @@ function generateRandomString(n: number): string {
return str;
}
export { convertTimeMsToTimeElapsedString, longestCommonStart, containsAllStrings, formatNumber, generateRandomString };
/**
* Hashes the input string. This is a fast hash, so NOT good for cryptography.
* This has been ripped off here: https://stackoverflow.com/a/52171480
* @param str The string that is to be hashed
* @param seed A seed to randomize the result
* @returns An hexadecimal string representation of the hashed input
*/
function cyrb53(str: string, seed = 0): string {
let h1 = 0xdeadbeef ^ seed;
let h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(16);
}
export {
convertTimeMsToTimeElapsedString,
longestCommonStart,
containsAllStrings,
formatNumber,
generateRandomString,
cyrb53,
};