A repository for a FoundryVTT plugin for Kingmaker homebrew.

Adding functionality for gainLose buttons to follow thresholds

+83 -25
+83 -25
src/svelte/kingdom-desc.svelte
··· 11 11 12 12 let container: HTMLElement | null = $state(null); 13 13 14 + function getThreshold(key: string): number | null { 15 + if (key.includes("commodities")) { 16 + // calculate commodities 17 + const size = actor.getFlag("kingdom-homebrew", "size") as number; 18 + 19 + if ((size > 0) && (size < 10)) { 20 + return 4; 21 + } else if ((size > 9) && (size < 25)) { 22 + return 8; 23 + } else if ((size > 24) && (size < 50)) { 24 + return 12; 25 + } else if ((size > 49) && (size < 100)) { 26 + return 16; 27 + } else { 28 + return 20; 29 + } 30 + } else { 31 + const threshold = actor.getFlag("kingdom-homebrew", `${key}.threshold`) as number | undefined; 32 + 33 + if (threshold === undefined) { 34 + return null; 35 + } 36 + 37 + return threshold; 38 + } 39 + } 40 + 41 + function tuneValue(val: number, minimum: number, maximum: number): number { 42 + return Math.min(Math.max(val, minimum), maximum); 43 + } 44 + 14 45 const handlers: Record<string, (event: Event) => void> = { 15 46 "gainLose": (event: Event): void => { 16 47 const button = (event.target as HTMLButtonElement); ··· 25 56 26 57 const updateResource = (key: string, value: any = undefined): void => { 27 58 if (amountType === "value") { 59 + let updatedValue: number; 60 + 28 61 if (value === undefined) { 29 62 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 30 - socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, priorValue + (parseInt(amount) * (gainLose === "gain" ? 1 : -1))); 63 + updatedValue = priorValue + (parseInt(amount) * (gainLose === "gain" ? 1 : -1)); 31 64 } else { 32 - socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, value * (gainLose === "gain" ? 1 : -1)); 65 + const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 66 + updatedValue = priorValue + (value * (gainLose === "gain" ? 1 : -1)); 33 67 } 34 68 35 - foundry.documents.ChatMessage.create({ 36 - user: (game as foundry.Game).users.activeGM.id, 37 - content: createChatMessage(key) 69 + const threshold = getThreshold(key); 70 + 71 + socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, tuneValue(updatedValue, 0, threshold || 100000)).then(() => { 72 + foundry.documents.ChatMessage.create({ 73 + user: (game as foundry.Game).users.activeGM.id, 74 + content: createChatMessage(key) 75 + }); 38 76 }); 39 77 } else if (amountType === "valueMultiple") { 40 78 try { ··· 45 83 content: `<input name="kmhb-multiple" type="number" min="1" max="50" step="1" autofocus />`, 46 84 ok: { 47 85 label: "Submit", 48 - callback: (event: PointerEvent | SubmitEvent, button: HTMLButtonElement, dialog: foundry.applications.api.DialogV2): any => (button.form?.elements.namedItem("kmhb-multiple") as HTMLInputElement | null)?.valueAsNumber 86 + callback: (_event: PointerEvent | SubmitEvent, button: HTMLButtonElement, _dialog: foundry.applications.api.DialogV2): any => (button.form?.elements.namedItem("kmhb-multiple") as HTMLInputElement | null)?.valueAsNumber 49 87 } 50 88 }).then((val: number): void => { 51 89 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 52 - socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, priorValue + (parseInt(amount) * val * (gainLose === "gain" ? 1 : -1))); 90 + let updatedValue = priorValue + (parseInt(amount) * val * (gainLose === "gain" ? 1 : -1)); 91 + let threshold = getThreshold(key); 53 92 54 - foundry.documents.ChatMessage.create({ 55 - user: (game as foundry.Game).users.activeGM.id, 56 - content: createChatMessage(key, false, parseInt(amount) * val) 93 + socket?.executeAsGM("updatedKingdomValue", `${key}.current`, actor, tuneValue(updatedValue, 0, threshold || 100000)).then(() => { 94 + foundry.documents.ChatMessage.create({ 95 + user: (game as foundry.Game).users.activeGM.id, 96 + content: createChatMessage(key, false, parseInt(amount) * val) 97 + }); 57 98 }); 58 99 }); 59 100 } catch {} ··· 66 107 content: `<input name="kmhb-multiple" type="number" min="1" max="50" step="1" autofocus />`, 67 108 ok: { 68 109 label: "Submit", 69 - callback: (event: PointerEvent | SubmitEvent, button: HTMLButtonElement, dialog: foundry.applications.api.DialogV2): any => (button.form?.elements.namedItem("kmhb-multiple") as HTMLInputElement | null)?.valueAsNumber 110 + callback: (_event: PointerEvent | SubmitEvent, button: HTMLButtonElement, _dialog: foundry.applications.api.DialogV2): any => (button.form?.elements.namedItem("kmhb-multiple") as HTMLInputElement | null)?.valueAsNumber 70 111 } 71 112 }).then((val: number): void => { 72 113 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.next`) as number; 73 - socket?.executeAsGM("updateKingdomValue", `${key}.next`, actor, priorValue + (parseInt(amount) * val * (gainLose === "gain" ? 1 : -1))); 114 + let updatedValue = priorValue + (parseInt(amount) * val * (gainLose === "gain" ? 1 : -1)); 115 + let threshold = getThreshold(key); 74 116 75 - foundry.documents.ChatMessage.create({ 76 - user: (game as foundry.Game).users.activeGM.id, 77 - content: createChatMessage(key, true, parseInt(amount) * val) 117 + socket?.executeAsGM("updateKingdomValue", `${key}.next`, actor, tuneValue(updatedValue, 0, threshold || 100000)).then(() => { 118 + foundry.documents.ChatMessage.create({ 119 + user: (game as foundry.Game).users.activeGM.id, 120 + content: createChatMessage(key, true, parseInt(amount) * val) 121 + }); 78 122 }); 79 123 }); 80 124 } catch {} ··· 83 127 roll.evaluate().then((val: Roll.Evaluated<Roll<EmptyObject>>): void => { 84 128 val.toMessage().then(() => { 85 129 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 86 - socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, priorValue + (val.total * (gainLose === "gain" ? 1 : -1))); 130 + let updatedValue = priorValue + (val.total * (gainLose === "gain" ? 1 : -1)); 131 + let threshold = getThreshold(key); 87 132 88 - foundry.documents.ChatMessage.create({ 89 - user: (game as foundry.Game).users.activeGM.id, 90 - content: createChatMessage(key, false, val.total) 133 + socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, tuneValue(updatedValue, 0, threshold || 100000)).then(() => { 134 + foundry.documents.ChatMessage.create({ 135 + user: (game as foundry.Game).users.activeGM.id, 136 + content: createChatMessage(key, false, val.total) 137 + }); 91 138 }); 92 139 }); 93 140 }); 94 141 } else if (amountType === "valueNextTurn") { 95 142 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.next`) as number; 96 - socket?.executeAsGM("updateKingdomValue", `${key}.next`, actor, priorValue + (parseInt(value) * (gainLose === "gain" ? 1 : -1))); 143 + let updatedValue = priorValue + (parseInt(amount) * (gainLose === "gain" ? 1 : -1)); 144 + let threshold = getThreshold(key); 145 + 146 + socket?.executeAsGM("updateKingdomValue", `${key}.next`, actor, tuneValue(updatedValue, 0, threshold || 100000)).then(() => { 147 + foundry.documents.ChatMessage.create({ 148 + user: (game as foundry.Game).users.activeGM.id, 149 + content: createChatMessage(key, true, parseInt(amount)) 150 + }) 151 + }); 97 152 } else if (amountType === "rollNextTurn") { 98 153 const roll = new foundry.dice.Roll(amount); 99 154 roll.evaluate().then((val: Roll.Evaluated<Roll<EmptyObject>>): void => { 100 155 val.toMessage().then(() => { 101 156 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.next`) as number; 102 - socket?.executeAsGM("updateKingdomValue", `${key}.next`, actor, priorValue + (val.total * (gainLose === "gain" ? 1 : -1))); 103 - 104 - foundry.documents.ChatMessage.create({ 105 - user: (game as foundry.Game).users.activeGM.id, 106 - content: createChatMessage(key, true, val.total) 157 + let updatedValue = priorValue + (val.total * (gainLose === "gain" ? 1 : -1)); 158 + let threshold = getThreshold(key); 159 + 160 + socket?.executeAsGM("updateKingdomValue", `${key}.next`, actor, tuneValue(updatedValue, 0, threshold || 100000)).then(() => { 161 + foundry.documents.ChatMessage.create({ 162 + user: (game as foundry.Game).users.activeGM.id, 163 + content: createChatMessage(key, true, val.total) 164 + }); 107 165 }); 108 166 }); 109 167 });