Skip to content

Commit

Permalink
fix: Encounter blocks will less greedily add the default party if pla…
Browse files Browse the repository at this point in the history
…yers are explicitly defined
  • Loading branch information
valentine195 committed Apr 15, 2024
1 parent fd045ac commit eb4a59a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
50 changes: 32 additions & 18 deletions src/encounter/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
MarkdownRenderChild,
Notice,
parseYaml
} from "obsidian";
import { MarkdownRenderChild, Notice, parseYaml } from "obsidian";
import type InitiativeTracker from "../main";
import { Creature } from "../utils/creature";

Expand Down Expand Up @@ -82,8 +78,11 @@ export class EncounterParser {
constructor(public plugin: InitiativeTracker) {}
async parse(params: EncounterParameters): Promise<ParsedParams> {
const name = params.name;
const party = params.party ?? this.plugin.data.defaultParty;
const players: string[] = this.parsePlayers(params);
const party =
params.party ?? players.length
? null
: this.plugin.data.defaultParty;
const hide = this.parseHide(params);
const rawMonsters = params.creatures ?? [];

Expand Down Expand Up @@ -119,27 +118,26 @@ export class EncounterParser {
return [];
}
parsePlayers(params: EncounterParameters) {
const partyName = params.party ?? this.plugin.data.defaultParty;
const playersToReturn: string[] = [];
const players = params.players;
if (
partyName &&
this.plugin.data.parties.find(
(p) => p.name.toLowerCase() == partyName.toLowerCase()
)
) {
const party = this.plugin.data.parties.find(
(p) => p.name.toLowerCase() == partyName.toLowerCase()
);
playersToReturn.push(...party.players);
if (params.party) {
if (
this.plugin.data.parties.find(
(p) => p.name.toLowerCase() == params.party.toLowerCase()
)
) {
const party = this.plugin.data.parties.find(
(p) => p.name.toLowerCase() == params.party.toLowerCase()
);
playersToReturn.push(...party.players);
}
}
if (players == "none" || players == false) {
playersToReturn.splice(0, playersToReturn.length);
} else if (players == true) {
playersToReturn.push(
...[...this.plugin.players.values()].map((p) => p.name)
);
} else if (!players && !params.party) {
} else if (typeof players == "string") {
playersToReturn.push(players);
} else if (Array.isArray(players)) {
Expand All @@ -153,6 +151,22 @@ export class EncounterParser {
)
);
}
if (!playersToReturn.length) {
let partyName = this.plugin.defaultParty?.name;

if (
partyName &&
this.plugin.data.parties.find(
(p) => p.name.toLowerCase() == partyName.toLowerCase()
)
) {
const party = this.plugin.data.parties.find(
(p) => p.name.toLowerCase() == partyName.toLowerCase()
);
playersToReturn.push(...party.players);
}
}

return Array.from(new Set(playersToReturn));
}
async parseRawCreatures(rawMonsters: RawCreatureArray, rollHP: boolean) {
Expand Down
4 changes: 3 additions & 1 deletion src/encounter/ui/Encounter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
export let creatures: Map<Creature, number | string> = new Map();
export let players: string[];
export let party: string = null;
export let hide: string[] = [];
export let rollHP: boolean = plugin.data.rollHP;
Expand Down Expand Up @@ -65,9 +66,10 @@
.flat();
const transformedCreatures: CreatureState[] = [];
const combinedPlayers = [
...plugin.getPlayerNamesForParty(party),
...(party ? plugin.getPlayerNamesForParty(party) : []),
...players
];
const playersForEncounter: Creature[] = [];
for (const name of new Set(combinedPlayers)) {
playersForEncounter.push(plugin.getPlayerByName(name));
Expand Down

0 comments on commit eb4a59a

Please sign in to comment.