Save system: ceremonial PR #42
| @@ -4,8 +4,8 @@ import { getThralls, ItemStage, LifeStage, Thrall } from "./thralls.ts"; | ||||
| import { SaveFileV1, mustBeSaveFileV1 } from "./saveformat.ts"; | ||||
|  | ||||
| interface NewRoundConfig { | ||||
|   asSuccessor: SuccessorOption, | ||||
|   withWish: Wish | null, | ||||
|   asSuccessor: SuccessorOption; | ||||
|   withWish: Wish | null; | ||||
| } | ||||
|  | ||||
| export class PlayerProgress { | ||||
| @@ -59,20 +59,24 @@ export class PlayerProgress { | ||||
|         INT: file.stats.int, | ||||
|         CHA: file.stats.cha, | ||||
|         PSI: file.stats.psi, | ||||
|       } | ||||
|       }; | ||||
|       this.#talents = { | ||||
|         AGI: file.talents.agi, | ||||
|         INT: file.talents.int, | ||||
|         CHA: file.talents.cha, | ||||
|         PSI: file.talents.psi, | ||||
|       } | ||||
|       this.#isInPenance = file.isInPenance, | ||||
|       this.#wish = file.wishId >= 0 ? {id: file.wishId} : null; | ||||
|       }; | ||||
|       (this.#isInPenance = file.isInPenance), | ||||
|         (this.#wish = file.wishId >= 0 ? { id: file.wishId } : null); | ||||
|       this.#exp = file.exp; | ||||
|       this.#blood = file.blood; | ||||
|       this.#itemsPurloined = file.itemsPurloined; | ||||
|       this.#skillsLearned = file.skillsLearned; | ||||
|       this.#untrimmedSkillsAvailable = file.untrimmedSkillsAvailableIds.map((id) => {return {id: id}}); | ||||
|       this.#untrimmedSkillsAvailable = file.untrimmedSkillsAvailableIds.map( | ||||
|         (id) => { | ||||
|           return { id: id }; | ||||
|         }, | ||||
|       ); | ||||
|       this.#thrallsUnlocked = file.thrallsUnlocked; | ||||
|       this.#thrallDamage = {}; | ||||
|       for (let i = 0; i < file.thrallDamage.length; ++i) { | ||||
|   | ||||
							
								
								
									
										50
									
								
								src/save.ts
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								src/save.ts
									
									
									
									
									
								
							| @@ -1,4 +1,4 @@ | ||||
| import { getPlayerProgress } from "./playerprogress" | ||||
| import { getPlayerProgress } from "./playerprogress"; | ||||
| import { getStateManager } from "./statemanager"; | ||||
| import { getThralls } from "./thralls"; | ||||
| import { SaveFileV1, StatCounterV1 } from "./saveformat"; | ||||
| @@ -16,8 +16,8 @@ export function mustBeSaveFileV1(obj: unknown): SaveFileV1 { | ||||
|   if (obj === undefined || obj === null) { | ||||
|     throw new Error("nonexistent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`not an object; was ${typeof obj}`); | ||||
|   } | ||||
|  | ||||
|   if (!("version" in obj)) { | ||||
| @@ -42,7 +42,10 @@ export function mustBeSaveFileV1(obj: unknown): SaveFileV1 { | ||||
|     blood: mustGetNumber(obj, "blood"), | ||||
|     itemsPurloined: mustGetNumber(obj, "itemsPurloined"), | ||||
|     skillsLearned: mustGetNumberArray(obj, "skillsLearned"), | ||||
|         untrimmedSkillsAvailableIds: mustGetNumberArray(obj, "untrimmedSkillsAvailableIds"), | ||||
|     untrimmedSkillsAvailableIds: mustGetNumberArray( | ||||
|       obj, | ||||
|       "untrimmedSkillsAvailableIds", | ||||
|     ), | ||||
|     thrallsUnlocked: mustGetNumberArray(obj, "thrallsUnlocked"), | ||||
|     thrallDamage: mustGetNumberArray(obj, "thrallDamage"), | ||||
|     thrallsObtainedItem: mustGetNumberArray(obj, "thrallsObtainedItem"), | ||||
| @@ -54,15 +57,15 @@ function mustGetNumber(obj: object, key: string) : number { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "number") { | ||||
|   if (typeof val !== "number") { | ||||
|     throw new Error(`not a number: ${key}: ${val}`); | ||||
|   } | ||||
|   return val; | ||||
| @@ -72,15 +75,15 @@ function mustGetString(obj: object, key: string) : string { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "string") { | ||||
|   if (typeof val !== "string") { | ||||
|     throw new Error(`not a string: ${key}: ${val}`); | ||||
|   } | ||||
|   return val; | ||||
| @@ -90,15 +93,15 @@ function mustGetStatCounterV1(obj: object, key: string) : StatCounterV1 { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "object") { | ||||
|   if (typeof val !== "object") { | ||||
|     throw new Error(`not an object: ${key}: ${val}`); | ||||
|   } | ||||
|  | ||||
| @@ -122,15 +125,15 @@ function mustGetBoolean(obj: object, key: string) : boolean { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "boolean") { | ||||
|   if (typeof val !== "boolean") { | ||||
|     throw new Error(`not boolean: ${key}: ${val}`); | ||||
|   } | ||||
|   return val; | ||||
| @@ -140,20 +143,20 @@ function mustGetNumberArray(obj: object, key: string) : number[] { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "object") { | ||||
|   if (typeof val !== "object") { | ||||
|     throw new Error(`not an object: ${key}: ${val}`); | ||||
|   } | ||||
|  | ||||
|   for (const x of val) { | ||||
|         if (typeof(x) !== "number") { | ||||
|     if (typeof x !== "number") { | ||||
|       throw new Error(`contained non-number item in ${key}: ${val}`); | ||||
|     } | ||||
|   } | ||||
| @@ -210,7 +213,7 @@ function readBestSave(): SaveFileV1LoadResult { | ||||
|   const from1 = readFromSlot("FLEDGLING_SLOT_1"); | ||||
|   const from2 = readFromSlot("FLEDGLING_SLOT_2"); | ||||
|   if (from1.file && from2.file) { | ||||
|         return (from1.file.revision > from2.file.revision) ? from1 : from2; | ||||
|     return from1.file.revision > from2.file.revision ? from1 : from2; | ||||
|   } | ||||
|  | ||||
|   var errors: string[] = []; | ||||
| @@ -236,7 +239,10 @@ function readBestSave(): SaveFileV1LoadResult { | ||||
| } | ||||
|  | ||||
| export function save() { | ||||
|     const targetSlot : SaveSlot = (readBestSave().slot === "FLEDGLING_SLOT_1") ? "FLEDGLING_SLOT_2" : "FLEDGLING_SLOT_1"; | ||||
|   const targetSlot: SaveSlot = | ||||
|     readBestSave().slot === "FLEDGLING_SLOT_1" | ||||
|       ? "FLEDGLING_SLOT_2" | ||||
|       : "FLEDGLING_SLOT_1"; | ||||
|   return saveIntoSlot(targetSlot); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -35,8 +35,8 @@ export function mustBeSaveFileV1(obj: unknown): SaveFileV1 { | ||||
|   if (obj === undefined || obj === null) { | ||||
|     throw new Error("nonexistent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`not an object; was ${typeof obj}`); | ||||
|   } | ||||
|  | ||||
|   if (!("version" in obj)) { | ||||
| @@ -61,7 +61,10 @@ export function mustBeSaveFileV1(obj: unknown): SaveFileV1 { | ||||
|     blood: mustGetNumber(obj, "blood"), | ||||
|     itemsPurloined: mustGetNumber(obj, "itemsPurloined"), | ||||
|     skillsLearned: mustGetNumberArray(obj, "skillsLearned"), | ||||
|         untrimmedSkillsAvailableIds: mustGetNumberArray(obj, "untrimmedSkillsAvailableIds"), | ||||
|     untrimmedSkillsAvailableIds: mustGetNumberArray( | ||||
|       obj, | ||||
|       "untrimmedSkillsAvailableIds", | ||||
|     ), | ||||
|     thrallsUnlocked: mustGetNumberArray(obj, "thrallsUnlocked"), | ||||
|     thrallDamage: mustGetNumberArray(obj, "thrallDamage"), | ||||
|     thrallsObtainedItem: mustGetNumberArray(obj, "thrallsObtainedItem"), | ||||
| @@ -73,15 +76,15 @@ function mustGetNumber(obj: object, key: string) : number { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "number") { | ||||
|   if (typeof val !== "number") { | ||||
|     throw new Error(`not a number: ${key}: ${val}`); | ||||
|   } | ||||
|   return val; | ||||
| @@ -91,15 +94,15 @@ function mustGetString(obj: object, key: string) : string { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "string") { | ||||
|   if (typeof val !== "string") { | ||||
|     throw new Error(`not a string: ${key}: ${val}`); | ||||
|   } | ||||
|   return val; | ||||
| @@ -109,15 +112,15 @@ function mustGetStatCounterV1(obj: object, key: string) : StatCounterV1 { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "object") { | ||||
|   if (typeof val !== "object") { | ||||
|     throw new Error(`not an object: ${key}: ${val}`); | ||||
|   } | ||||
|  | ||||
| @@ -141,15 +144,15 @@ function mustGetBoolean(obj: object, key: string) : boolean { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "boolean") { | ||||
|   if (typeof val !== "boolean") { | ||||
|     throw new Error(`not boolean: ${key}: ${val}`); | ||||
|   } | ||||
|   return val; | ||||
| @@ -159,20 +162,20 @@ function mustGetNumberArray(obj: object, key: string) : number[] { | ||||
|   if (obj === null || obj === undefined) { | ||||
|     throw new Error("container absent"); | ||||
|   } | ||||
|     if (typeof(obj) !== "object") { | ||||
|         throw new Error(`container was not an object; was ${typeof(obj)}`); | ||||
|   if (typeof obj !== "object") { | ||||
|     throw new Error(`container was not an object; was ${typeof obj}`); | ||||
|   } | ||||
|   if (!(key in obj)) { | ||||
|     throw new Error(`missing number: ${key}`); | ||||
|   } | ||||
|   const dict = obj as { [key: string]: any }; | ||||
|   const val = dict[key]; | ||||
|     if (typeof(val) !== "object") { | ||||
|   if (typeof val !== "object") { | ||||
|     throw new Error(`not an object: ${key}: ${val}`); | ||||
|   } | ||||
|  | ||||
|   for (const x of val) { | ||||
|         if (typeof(x) !== "number") { | ||||
|     if (typeof x !== "number") { | ||||
|       throw new Error(`contained non-number item in ${key}: ${val}`); | ||||
|     } | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user