Skip to content

Commit

Permalink
add ability to toggle pushing combat log to discord from combat log s…
Browse files Browse the repository at this point in the history
…ettings panel
  • Loading branch information
ChunkLightTuna committed Apr 15, 2024
1 parent 569671c commit 83ab51b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
10 changes: 10 additions & 0 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
"verified": "3.1.2"
}
}
],
"requires": [
{
"id": "lib-wrapper",
"type": "module",
"compatibility": {
"minimum": "1.0.0.0",
"verified": "1.12.13.0"
}
}
]
},
"esmodules": [
Expand Down
32 changes: 31 additions & 1 deletion src/combat.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {item_roll, Logger} from "./util.mjs";
import {autoResizeApplicationExisting, item_roll, Logger} from "./util.mjs";
import {combat_hooks, socket} from "./module.mjs";
import {COMBAT_ENABLED, COMBAT_HEALTH_ESTIMATE, COMBAT_HEALTH_ESTIMATE_TYPE, ID_MAP, MODULE_ID} from "./constants.mjs";
import {actor_to_discord_ids} from "./sync.mjs";
Expand Down Expand Up @@ -181,6 +181,36 @@ function get_health(hp, combatHealthSetting, actorType) {
}
}

export function register_combat_settings_toggle() {
libWrapper.register('oronder', 'CombatTrackerConfig.prototype._updateObject', async function (wrapped, ...args) {
Logger.info(this)
Logger.info(this.form.elements.oronder_combat_tracker_toggle.checked)
Logger.info(game.settings.get(MODULE_ID, COMBAT_ENABLED))
await game.settings.set(MODULE_ID, COMBAT_ENABLED, this.form.elements.oronder_combat_tracker_toggle.checked)
set_combat_hooks()
Logger.info(game.settings.get(MODULE_ID, COMBAT_ENABLED))
return wrapped(...args)
}, 'WRAPPER')

Hooks.on('renderCombatTrackerConfig', async (application, $html, data) => {
const $label = $("<label/>", {
text: 'Publish Combat Tracker to Discord',
for: 'oronder_combat_tracker_toggle'
})
const $checkbox = $("<input/>", {
type: 'checkbox',
id: 'oronder_combat_tracker_toggle',
checked: game.settings.get(MODULE_ID, COMBAT_ENABLED)
})

$('<div/>', {class: "form-group"})
.append($label, $checkbox)
.insertBefore($html.find('form button').last())

autoResizeApplicationExisting(application)
})
}


export function handle_incoming_rolls() {
socket.on('roll', async data => {
Expand Down
6 changes: 5 additions & 1 deletion src/module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {registerSettings} from "./settings.mjs"
import {AUTH, MODULE_ID, ORONDER_WS_URL} from "./constants.mjs"
import {del_actor, sync_actor} from "./sync.mjs"
import {monks_token_bar_hooks} from "./monks_token_bar_hooks.mjs";
import {handle_incoming_rolls, set_combat_hooks} from "./combat.mjs";
import {handle_incoming_rolls, register_combat_settings_toggle, set_combat_hooks} from "./combat.mjs";

export let socket
export let session_id
Expand Down Expand Up @@ -32,8 +32,12 @@ function set_session(session) {

Hooks.once("ready", async () => {
if (game.user.isGM) {
if (!game.modules.get('lib-wrapper')?.active) {
ui.notifications.error("Oronder requires the 'libWrapper' module. Please install and activate it.")
}
await registerSettings()
open_socket_with_oronder()
register_combat_settings_toggle()
}

game.socket.on(SOCKET_NAME, data => {
Expand Down
7 changes: 4 additions & 3 deletions src/settings-form-application.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "./constants.mjs"
import {full_sync, sync_actor} from "./sync.mjs"
import {open_socket_with_oronder} from "./module.mjs"
import { set_combat_hooks } from "./combat.mjs"
import {set_combat_hooks} from "./combat.mjs"

export class OronderSettingsFormApplication extends FormApplication {
constructor(object = {}, options = {}) {
Expand All @@ -29,6 +29,7 @@ export class OronderSettingsFormApplication extends FormApplication {
id_map: id_map,
combat_health_estimate: game.settings.get(MODULE_ID, COMBAT_HEALTH_ESTIMATE),
combat_health_estimate_type: COMBAT_HEALTH_ESTIMATE_TYPE,
combat_tracking_enabled: game.settings.get(MODULE_ID, COMBAT_ENABLED),
players: game.users.filter(user => user.role < 3).map(user => ({
foundry_name: user.name,
foundry_id: user.id,
Expand Down Expand Up @@ -115,7 +116,7 @@ export class OronderSettingsFormApplication extends FormApplication {
this.object.guild.combat_channel_id = Array.from(this.form.elements.combat_channel).find(c => c.selected)?.value
this.object.guild.roll_discord_to_foundry = this.form.elements.roll_discord_to_foundry.checked
this.object.combat_health_estimate = parseInt(this.form.elements.combat_health_estimate.value)
this.object.guild.combat_tracking_enabled = this.form.elements.combat_tracking_enabled.checked
this.object.combat_tracking_enabled = this.form.elements.combat_tracking_enabled.checked
}
this.object.show_advanced = this.form.elements.show_advanced.checked

Expand Down Expand Up @@ -196,7 +197,7 @@ export class OronderSettingsFormApplication extends FormApplication {

this.bind()

await game.settings.set(MODULE_ID, COMBAT_ENABLED, this.object.guild.combat_tracking_enabled)
await game.settings.set(MODULE_ID, COMBAT_ENABLED, this.object.combat_tracking_enabled)
await game.settings.set(MODULE_ID, COMBAT_HEALTH_ESTIMATE, this.object.combat_health_estimate)

const updated_id_map = await game.settings.set(MODULE_ID, ID_MAP,
Expand Down
15 changes: 15 additions & 0 deletions src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,18 @@ export function item_roll(item) {

return new Roll(formula, item.getRollData())
}

export function autoResizeApplicationExisting(app) {
const centerPrev = app.position.top + app.position.height / 2;

const pos = app.setPosition({
width: app.position.width, height: "auto",
});

const center = pos.top + pos.height / 2;
app.setPosition({
width: app.position.width,
height: app.position.height,
top: app.position.top + (centerPrev - center),
});
}
6 changes: 3 additions & 3 deletions templates/settings-form-application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@
title="Oronder will post changes in the combat tracker to the Combat Channel configured below.">
Publish Combat Tracker to Discord</label>
<input type="checkbox" id='combat_tracking_enabled' class='control' data-action='checkbox'
style='float:right' {{checked guild.combat_tracking_enabled}}>
style='float:right' {{checked combat_tracking_enabled}}>
</div>
<div class="form-group">
<label for="combat_channel" title="Channel where Combat Tracking Messages will be posted.">
Combat Channel</label>
<select id="combat_channel" {{#unless guild.combat_tracking_enabled}}disabled{{/unless}}>
<select id="combat_channel" {{#unless combat_tracking_enabled}}disabled{{/unless}}>
<option value="">❌ DISABLED</option>
{{#each guild.text_channels}}
<option value="{{id}}" {{#if (eq id ../guild.combat_channel_id)}}selected{{/if}}>
Expand All @@ -219,7 +219,7 @@
<label for="combat_health_estimate"
title="Select whose health will be described(i.e. Healthy) rather than shown (HP 8/9).">
Combat Health Estimate</label>
<select id="combat_health_estimate" {{#unless guild.combat_tracking_enabled}}disabled{{/unless}}>
<select id="combat_health_estimate" {{#unless combat_tracking_enabled}}disabled{{/unless}}>
{{#each combat_health_estimate_type}}
<option value={{this}} {{#if (eq this ../combat_health_estimate)}}selected{{/if}}>
{{@key}}</option>
Expand Down

0 comments on commit 83ab51b

Please sign in to comment.