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 12 let container: HTMLElement | null = $state(null); 13 14 const handlers: Record<string, (event: Event) => void> = { 15 "gainLose": (event: Event): void => { 16 const button = (event.target as HTMLButtonElement); ··· 25 26 const updateResource = (key: string, value: any = undefined): void => { 27 if (amountType === "value") { 28 if (value === undefined) { 29 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 30 - socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, priorValue + (parseInt(amount) * (gainLose === "gain" ? 1 : -1))); 31 } else { 32 - socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, value * (gainLose === "gain" ? 1 : -1)); 33 } 34 35 - foundry.documents.ChatMessage.create({ 36 - user: (game as foundry.Game).users.activeGM.id, 37 - content: createChatMessage(key) 38 }); 39 } else if (amountType === "valueMultiple") { 40 try { ··· 45 content: `<input name="kmhb-multiple" type="number" min="1" max="50" step="1" autofocus />`, 46 ok: { 47 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 49 } 50 }).then((val: number): void => { 51 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))); 53 54 - foundry.documents.ChatMessage.create({ 55 - user: (game as foundry.Game).users.activeGM.id, 56 - content: createChatMessage(key, false, parseInt(amount) * val) 57 }); 58 }); 59 } catch {} ··· 66 content: `<input name="kmhb-multiple" type="number" min="1" max="50" step="1" autofocus />`, 67 ok: { 68 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 70 } 71 }).then((val: number): void => { 72 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))); 74 75 - foundry.documents.ChatMessage.create({ 76 - user: (game as foundry.Game).users.activeGM.id, 77 - content: createChatMessage(key, true, parseInt(amount) * val) 78 }); 79 }); 80 } catch {} ··· 83 roll.evaluate().then((val: Roll.Evaluated<Roll<EmptyObject>>): void => { 84 val.toMessage().then(() => { 85 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 86 - socket?.executeAsGM("updateKingdomValue", `${key}.current`, actor, priorValue + (val.total * (gainLose === "gain" ? 1 : -1))); 87 88 - foundry.documents.ChatMessage.create({ 89 - user: (game as foundry.Game).users.activeGM.id, 90 - content: createChatMessage(key, false, val.total) 91 }); 92 }); 93 }); 94 } else if (amountType === "valueNextTurn") { 95 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.next`) as number; 96 - socket?.executeAsGM("updateKingdomValue", `${key}.next`, actor, priorValue + (parseInt(value) * (gainLose === "gain" ? 1 : -1))); 97 } else if (amountType === "rollNextTurn") { 98 const roll = new foundry.dice.Roll(amount); 99 roll.evaluate().then((val: Roll.Evaluated<Roll<EmptyObject>>): void => { 100 val.toMessage().then(() => { 101 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) 107 }); 108 }); 109 });
··· 11 12 let container: HTMLElement | null = $state(null); 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 + 45 const handlers: Record<string, (event: Event) => void> = { 46 "gainLose": (event: Event): void => { 47 const button = (event.target as HTMLButtonElement); ··· 56 57 const updateResource = (key: string, value: any = undefined): void => { 58 if (amountType === "value") { 59 + let updatedValue: number; 60 + 61 if (value === undefined) { 62 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 63 + updatedValue = priorValue + (parseInt(amount) * (gainLose === "gain" ? 1 : -1)); 64 } else { 65 + const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 66 + updatedValue = priorValue + (value * (gainLose === "gain" ? 1 : -1)); 67 } 68 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 + }); 76 }); 77 } else if (amountType === "valueMultiple") { 78 try { ··· 83 content: `<input name="kmhb-multiple" type="number" min="1" max="50" step="1" autofocus />`, 84 ok: { 85 label: "Submit", 86 + callback: (_event: PointerEvent | SubmitEvent, button: HTMLButtonElement, _dialog: foundry.applications.api.DialogV2): any => (button.form?.elements.namedItem("kmhb-multiple") as HTMLInputElement | null)?.valueAsNumber 87 } 88 }).then((val: number): void => { 89 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 90 + let updatedValue = priorValue + (parseInt(amount) * val * (gainLose === "gain" ? 1 : -1)); 91 + let threshold = getThreshold(key); 92 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 + }); 98 }); 99 }); 100 } catch {} ··· 107 content: `<input name="kmhb-multiple" type="number" min="1" max="50" step="1" autofocus />`, 108 ok: { 109 label: "Submit", 110 + callback: (_event: PointerEvent | SubmitEvent, button: HTMLButtonElement, _dialog: foundry.applications.api.DialogV2): any => (button.form?.elements.namedItem("kmhb-multiple") as HTMLInputElement | null)?.valueAsNumber 111 } 112 }).then((val: number): void => { 113 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.next`) as number; 114 + let updatedValue = priorValue + (parseInt(amount) * val * (gainLose === "gain" ? 1 : -1)); 115 + let threshold = getThreshold(key); 116 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 + }); 122 }); 123 }); 124 } catch {} ··· 127 roll.evaluate().then((val: Roll.Evaluated<Roll<EmptyObject>>): void => { 128 val.toMessage().then(() => { 129 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.current`) as number; 130 + let updatedValue = priorValue + (val.total * (gainLose === "gain" ? 1 : -1)); 131 + let threshold = getThreshold(key); 132 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 + }); 138 }); 139 }); 140 }); 141 } else if (amountType === "valueNextTurn") { 142 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.next`) as number; 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 + }); 152 } else if (amountType === "rollNextTurn") { 153 const roll = new foundry.dice.Roll(amount); 154 roll.evaluate().then((val: Roll.Evaluated<Roll<EmptyObject>>): void => { 155 val.toMessage().then(() => { 156 const priorValue = actor.getFlag("kingdom-homebrew", `${key}.next`) as number; 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 + }); 165 }); 166 }); 167 });