Skip to content

Commit

Permalink
Add Prettier Config (#14)
Browse files Browse the repository at this point in the history
* prettier config

* Apply JavaScript formatting

---------

Co-authored-by: ChunkLightTuna <[email protected]>
  • Loading branch information
ChunkLightTuna and ChunkLightTuna authored Aug 22, 2024
1 parent 216dcb7 commit 1f67a8b
Show file tree
Hide file tree
Showing 13 changed files with 1,148 additions and 342 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.github
templates
16 changes: 16 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "none",
"bracketSpacing": false,
"semi": false,
"arrowParens": "avoid",
"overrides": [
{
"files": "*.json",
"options": {
"tabWidth": 2
}
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ _The following must be done by the owner of the Discord server you'd like to int

Players should now be able to access characters they own in Foundry from Discord.
If a PC is not showing up, ensure that player ownership has been assigned and that the character has a class, race and
background.
background.
2 changes: 1 addition & 1 deletion languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@
"oronder.Combat-Channel-Label": "Channel where Combat Tracking Messages will be posted.",
"oronder.Combat-Health-Estimate": "Combat Health Estimate",
"oronder.Combat-Health-Estimate-Label": "Select whose health will be described(i.e. Healthy) rather than shown (HP 8/9)."
}
}
8 changes: 2 additions & 6 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@
}
]
},
"esmodules": [
"dist/module.js"
],
"styles": [
"styles/module.css"
],
"esmodules": ["dist/module.js"],
"styles": ["styles/module.css"],
"languages": [
{
"lang": "en",
Expand Down
138 changes: 87 additions & 51 deletions src/combat.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {autoResizeApplicationExisting, Logger} from "./util.mjs";
import {combat_hooks, socket} from "./module.mjs";
import {COMBAT_ENABLED, COMBAT_HEALTH_ESTIMATE, COMBAT_HEALTH_ESTIMATE_TYPE, MODULE_ID} from "./constants.mjs";
import {actor_to_discord_ids} from "./sync.mjs";
import {autoResizeApplicationExisting, Logger} from './util.mjs'
import {combat_hooks, socket} from './module.mjs'
import {
COMBAT_ENABLED,
COMBAT_HEALTH_ESTIMATE,
COMBAT_HEALTH_ESTIMATE_TYPE,
MODULE_ID
} from './constants.mjs'
import {actor_to_discord_ids} from './sync.mjs'

const on_combat_start = async (combat, updateData) => {
const roundRender = parse_combat_round({...combat, ...updateData})
Expand All @@ -15,35 +20,38 @@ const on_combat_turn = async (combat, updateData, updateOptions) => {
}
const on_combat_round = async (combat, updateData, updateOptions) => {
if (updateOptions.direction < 1) return
const roundRender = parse_combat_round({...combat, ...updateData}, updateOptions)
const roundRender = parse_combat_round(
{...combat, ...updateData},
updateOptions
)
const turnRender = parse_turn(combat, updateData)
socket.emit('combat', roundRender + turnRender)
}

export function set_combat_hooks() {
Logger.info("Setting Combat Hooks.")
Logger.info('Setting Combat Hooks.')

Logger.info(combat_hooks)
const turn_off_hook = (key) => {
const turn_off_hook = key => {
if (combat_hooks[key]) {
Hooks.off(key, combat_hooks[key])
combat_hooks[key] = undefined
}
}

// Turn off hooks
["combatStart", "combatTurn", "combatRound"].forEach(turn_off_hook)
;['combatStart', 'combatTurn', 'combatRound'].forEach(turn_off_hook)

// Turn them back on
if (game.settings.get(MODULE_ID, COMBAT_ENABLED)) {
combat_hooks.combatStart = Hooks.on("combatStart", on_combat_start)
combat_hooks.combatTurn = Hooks.on("combatTurn", on_combat_turn)
combat_hooks.combatRound = Hooks.on("combatRound", on_combat_round)
combat_hooks.combatStart = Hooks.on('combatStart', on_combat_start)
combat_hooks.combatTurn = Hooks.on('combatTurn', on_combat_turn)
combat_hooks.combatRound = Hooks.on('combatRound', on_combat_round)
}
}

function get_effects_in_markdown(actor, token) {
let a = (token.document.actorLink) ? actor : token.actor
let a = token.document.actorLink ? actor : token.actor

let addedEffects = new Map()
let markdown = ''
Expand All @@ -65,7 +73,8 @@ function parse_turn(combat, updateData) {
const turn = c.turns[c.turn]
const actor = Object.assign(
game.actors.find(a => a.id === turn.actorId),
combat.combatants.find(cb => cb.tokenId === turn.tokenId))
combat.combatants.find(cb => cb.tokenId === turn.tokenId)
)

if (actor.hidden) return ''

Expand All @@ -75,17 +84,20 @@ function parse_turn(combat, updateData) {

let output = ''
if (discordId.length)
output += `${game.i18n.localize("oronder.Its-Your-Turn")} <@${discordId[0]}>\n`
output += `${game.i18n.localize('oronder.Its-Your-Turn')} <@${discordId[0]}>\n`
output += '```md\n'
output += `# ${game.i18n.localize("oronder.Initiative")} ${actor.initiative} ${game.i18n.localize("oronder.Round")} ${c.round}\n`
output += `# ${game.i18n.localize('oronder.Initiative')} ${actor.initiative} ${game.i18n.localize('oronder.Round')} ${c.round}\n`

if (turn.defeated) {
output += `${actor.name} <Defeated>\n`
} else if (token.document.hidden) {
output += `${actor.name} <Hidden>\n`
} else {
const hp = get_health(
{...actor.system.attributes.hp, ...token.document.delta?.system?.attributes?.hp},
{
...actor.system.attributes.hp,
...token.document.delta?.system?.attributes?.hp
},
healthSetting,
actor.type
)
Expand All @@ -98,7 +110,7 @@ function parse_turn(combat, updateData) {

function parse_combat_round(combat) {
// Get actors and token for each combatant by turn order
const parsed = combat.turns.map((c) => {
const parsed = combat.turns.map(c => {
return {
...c,
ix: c._id,
Expand All @@ -108,17 +120,20 @@ function parse_combat_round(combat) {
})
const healthSetting = game.settings.get(MODULE_ID, COMBAT_HEALTH_ESTIMATE)

let output = "```md\n"
output += `${game.i18n.localize("oronder.Current-Round")}: ${combat.round}\n`
output += "==================\n"
let output = '```md\n'
output += `${game.i18n.localize('oronder.Current-Round')}: ${combat.round}\n`
output += '==================\n'

// Parse each combatant
output += parsed.reduce((acc, c) => {
// Hidden from Initiative
if (c.hidden) return acc

const rawHp = {...c.actor.system.attributes.hp, ...c.token.document.delta?.system?.attributes?.hp}
const init = `${c.initiative || "XX"}`.padStart(3)
const rawHp = {
...c.actor.system.attributes.hp,
...c.token.document.delta?.system?.attributes?.hp
}
const init = `${c.initiative || 'XX'}`.padStart(3)

// Combatant is marked as defeated in initative
if (c.defeated) {
Expand All @@ -130,68 +145,88 @@ function parse_combat_round(combat) {
return acc + line
} else {
const hp = get_health(rawHp, healthSetting, c.actor.type)
const ac = c.actor.type === 'character' ? ` (AC ${c.actor.system.attributes.ac.value})` : ''
const ac =
c.actor.type === 'character'
? ` (AC ${c.actor.system.attributes.ac.value})`
: ''
return `${acc}${init}: ${c.name} <${hp}>${ac}\n${get_effects_in_markdown(c.actor, c.token)}`
}
}, '')
output += "```\n"
output += '```\n'
return output
}

function get_health(hp, combatHealthSetting, actorType) {
const format_health = (hpObj) => {
const format_health = hpObj => {
return `${hpObj.value}/${hpObj.max}${hpObj.temp ? `(${hpObj.temp})` : ''}`
}

const get_health_estimate = (hp) => {
const pct = Math.round(hp.effectiveMax ? (hp.value / hp.effectiveMax) * 100 : 0, 0, 100);
const get_health_estimate = hp => {
const pct = Math.round(
hp.effectiveMax ? (hp.value / hp.effectiveMax) * 100 : 0,
0,
100
)
switch (true) {
case pct > 99:
return game.i18n.localize("oronder.Unharmed");
return game.i18n.localize('oronder.Unharmed')
case pct > 75:
return game.i18n.localize("oronder.Healthy");
return game.i18n.localize('oronder.Healthy')
case pct > 50:
return game.i18n.localize("oronder.Injured");
return game.i18n.localize('oronder.Injured')
case pct > 25:
return game.i18n.localize("oronder.Bloodied");
return game.i18n.localize('oronder.Bloodied')
case pct > 10:
return game.i18n.localize("oronder.Severe");
return game.i18n.localize('oronder.Severe')
case pct > 0:
return game.i18n.localize("oronder.Critical");
return game.i18n.localize('oronder.Critical')
default:
return game.i18n.localize("oronder.Dead");
return game.i18n.localize('oronder.Dead')
}
}

switch (combatHealthSetting) {
case COMBAT_HEALTH_ESTIMATE_TYPE.Monsters:
return (actorType === "character")
return actorType === 'character'
? format_health(hp)
: get_health_estimate(hp)
case COMBAT_HEALTH_ESTIMATE_TYPE.All:
return get_health_estimate(hp)
case COMBAT_HEALTH_ESTIMATE_TYPE.None:
return format_health(hp)
default:
console.error(`Combat Health Setting(${combatHealthSetting}) is not supported.`)
console.error(
`Combat Health Setting(${combatHealthSetting}) is not supported.`
)
}
}

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

Hooks.on('renderCombatTrackerConfig', async (application, $html, _) => {
$('<div/>', {class: "form-group"})
$('<div/>', {class: 'form-group'})
.append(
$("<label/>", {
text: game.i18n.localize("oronder.Publish-Combat-Tracker-To-Discord"),
$('<label/>', {
text: game.i18n.localize(
'oronder.Publish-Combat-Tracker-To-Discord'
),
for: 'oronder_combat_tracker_toggle'
}),
$("<input/>", {
$('<input/>', {
type: 'checkbox',
id: 'oronder_combat_tracker_toggle',
checked: game.settings.get(MODULE_ID, COMBAT_ENABLED)
Expand All @@ -203,12 +238,11 @@ export function register_combat_settings_toggle() {
})
}


export function handle_incoming_rolls() {
socket.on('roll', async (data, callback) => {
const actor = game.actors.find(a => a.id === data.actor_id)
if (actor === undefined) {
Logger.error(game.i18n.localize("oronder.Actor-Not-Found"))
Logger.error(game.i18n.localize('oronder.Actor-Not-Found'))
return
}

Expand Down Expand Up @@ -237,7 +271,7 @@ export function handle_incoming_rolls() {
const item = actor.items.find(i => i.id === data.item_id)

if (item === undefined) {
Logger.error(game.i18n.localize("oronder.Item-Not-Found"))
Logger.error(game.i18n.localize('oronder.Item-Not-Found'))
return
}

Expand All @@ -247,8 +281,10 @@ export function handle_incoming_rolls() {
disadvantage: data.disadvantage || false
})


const spell_level = item.type === 'spell' ? Math.max(data.spell_level, item.system.level) : null
const spell_level =
item.type === 'spell'
? Math.max(data.spell_level, item.system.level)
: null
const versatile = item.type === 'weapon' ? data.versatile : null

const dmg = await item.rollDamage({
Expand All @@ -267,4 +303,4 @@ export function handle_incoming_rolls() {
break
}
})
}
}
Loading

0 comments on commit 1f67a8b

Please sign in to comment.