Skip to content

Commit 92bfc37

Browse files
committed
refactorings
1 parent ac7e915 commit 92bfc37

File tree

55 files changed

+533
-749
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+533
-749
lines changed

exampleVault/Advanced Example.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
BaseSpeed: 50
2+
BaseSpeed: 40
33
SpeedMultiplier: 1
44
AdditionalBonus: 0
55
Encumbered: false
@@ -13,4 +13,16 @@ varMins: 60
1313

1414
# Travel Speed Calculator
1515

16-
TODO
16+
| DnD5e Travel Calculator |
17+
| ------------------------------------------------------------------------------------------------------------------------------------------- |
18+
| **Select Base Speed:** `INPUT[inlineSelect(option(30, Walking), option(50, Camel), option(40, Donkey), option(40, Mule), option(40, Draft Horse), option(40, Elephant), option(40, Mastiff), option(40, Moorbounder), option(40, Pony), option(40, Rhinoceros), option(60, Riding Horse), option(40, Saber-Toothed Tiger), option(60, Warhorse), option(20, Griffon [walking]), option(80, Griffon [flying]), option(40, Hippogriff [walking]), option(60, Hippogriff [flying]), option(60, Pegasus [walking]), option(90, Pegasus [flying]), option(20, Peryton [walking]), option(60, Peryton [flyingg]), option(50, Unicorn), option(60, Peryton [flying]), option(50, Unicorn), option(50, Broom of Flying), option(30, Broom of Flying [over 200 lbs]), option(80, Carpet of Flying [3ft x 5ft]), option(60, Carpet of Flying [4ft x 6ft]), option(40, Carpet of Flying [5ft x 7ft]), option(30, Carpet of Flying [6ft x 9ft]), option(300, Wind Walk), option(50, Cauldron of Flying), option(30, Cart pulled by Horses), option(30, Cart pulled by PCs), option(20, PHB Galley), option(5, PHB Keelboat), option(15, PHB Longship), option(10, PHB Rowboat), option(10, PHB Sailing Ship), option(15, PHB Warship), option(45, Aquisions Inc Battle Balloon), option(15, Aquisions Inc Mechanical Beholder), option(200, Ebberon Lyrandar Airship), option(100, Ebberon Lyrandar Galleon), option(300, Ebberon Orien Lightning Rail), showcase):BaseSpeed]` |
19+
| **Select Travel Speed:** `INPUT[inlineSelect(option(1, Normal Pace), option(0.666667, Slow Pace), option(1.333333, Fast Pace), showcase):SpeedMultiplier]` |
20+
| Additional Bonus Speed: `INPUT[number:AdditionalBonus]` |
21+
| Encumbered: `INPUT[toggle:Encumbered]` (`VIEW[{Encumbered} ? -10 : 0]`) Horseshoes of Speed: `INPUT[toggle:HorseshoesofSpeed]` (`VIEW[{HorseshoesofSpeed} ? 30 : 0]`) |
22+
| **Travel Hours Per Day:** `INPUT[number:HoursPerDay]` |
23+
| Exhaustion Level: `INPUT[inlineSelect(option(0, 0 No exhaustion), option(1, 1 Disadvantage on ability checks), option(2, 2 Speed halved), option(3, 3 Disadvantage on attack rolls and saving throws), option(4, 4 Hit point maximum halved), option(5, 5 Speed reduced to 0), option(6, 6 Death)):ExhaustionLevel]` |
24+
| New Base Speed: `VIEW[{BaseSpeed} / ({ExhaustionLevel} > 1 ? 2 : 1) + ({Encumbered} ? -10 : 0) + ({HorseshoesofSpeed} ? 30 : 0) + {AdditionalBonus}]` |
25+
| Miles To Travel: `INPUT[number:TravelDistance]` |
26+
| **Days Travel 🕓:** `VIEW[round(({TravelDistance} * ({varMins}/(({BaseSpeed} / ({ExhaustionLevel} > 1 ? 2 : 1) + ({Encumbered} ? -10 : 0) + ({HorseshoesofSpeed} ? 30 : 0) + {AdditionalBonus}) / 10) / {SpeedMultiplier})) / 60 / {HoursPerDay}, 1)]` |
27+
28+
Check out this calculator on [Josh's Publish](https://obsidianttrpgtutorials.com/Obsidian+TTRPG+Tutorials/Plugin+Tutorials/Travel+Calculators/DnD+5e+Travel+Calc/DnD+5e+Travel+Calc).

src/api/API.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { InputFieldMDRC, RenderChildType } from '../renderChildren/InputFieldMDRC';
2-
import { InputFieldDeclaration } from '../parsers/InputFieldDeclarationParser';
32
import { JsViewFieldDeclaration, ViewFieldDeclaration, ViewFieldDeclarationParser } from '../parsers/ViewFieldDeclarationParser';
43
import { BindTargetParser } from '../parsers/BindTargetParser';
54
import { ViewFieldMDRC } from '../renderChildren/ViewFieldMDRC';
@@ -8,9 +7,9 @@ import MetaBindPlugin from '../main';
87
import { NewInputFieldDeclarationParser } from '../parsers/newInputFieldParser/InputFieldParser';
98
import { Component, MarkdownPostProcessorContext } from 'obsidian';
109
import { InputFieldAPI } from './InputFieldAPI';
11-
import { UnvalidatedInputFieldDeclaration } from '../parsers/newInputFieldParser/InputFieldDeclarationValidator';
1210
import { IAPI } from './IAPI';
1311
import { ExcludedMDRC } from '../renderChildren/ExcludedMDRC';
12+
import { InputFieldDeclaration, UnvalidatedInputFieldDeclaration } from '../parsers/newInputFieldParser/InputFieldDeclaration';
1413

1514
export class API implements IAPI {
1615
public plugin: MetaBindPlugin;

src/api/InputFieldAPI.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { InputFieldArgumentType, InputFieldType } from '../parsers/InputFieldDeclarationParser';
21
import { ErrorCollection } from '../utils/errors/ErrorCollection';
32

3+
import { IAPI } from './IAPI';
44
import {
55
UnvalidatedBindTargetDeclaration,
66
UnvalidatedInputFieldArgument,
77
UnvalidatedInputFieldDeclaration,
8-
} from '../parsers/newInputFieldParser/InputFieldDeclarationValidator';
9-
import { IAPI } from './IAPI';
8+
} from '../parsers/newInputFieldParser/InputFieldDeclaration';
9+
import { InputFieldArgumentType, InputFieldType } from '../inputFields/InputFieldConfigs';
1010

1111
export class InputFieldAPI {
1212
private readonly api: IAPI;
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { InputFieldArgumentType, InputFieldType } from '../parsers/InputFieldDeclarationParser';
21
import { ErrorLevel, MetaBindArgumentError } from '../utils/errors/MetaBindErrors';
3-
import { ParsingResultNode } from '../parsers/newInputFieldParser/InputFieldDeclarationValidator';
2+
import { ParsingResultNode } from '../parsers/newInputFieldParser/InputFieldParser';
3+
import { InputFieldArgumentConfig, InputFieldType } from '../inputFields/InputFieldConfigs';
44

55
export abstract class AbstractInputFieldArgument {
6-
identifier: InputFieldArgumentType = InputFieldArgumentType.INVALID;
7-
allowedInputFields: InputFieldType[] = [];
86
value: any;
9-
valueLengthMin: number = 0;
10-
valueLengthMax: number = 0;
11-
allowMultiple: boolean = false;
7+
8+
abstract getConfig(): InputFieldArgumentConfig;
129

1310
parseValue(value: ParsingResultNode[]): void {
14-
this.validateValueLength(value, this.valueLengthMin, this.valueLengthMax);
11+
this.validateValueLength(value, this.getConfig().valueLengthMin, this.getConfig().valueLengthMax);
1512
this._parseValue(value);
1613
}
1714

@@ -21,29 +18,29 @@ export abstract class AbstractInputFieldArgument {
2118
if (value.length < min) {
2219
throw new MetaBindArgumentError(
2320
ErrorLevel.WARNING,
24-
`Failed to parse argument value for argument '${this.identifier}'.`,
21+
`Failed to parse argument value for argument '${this.getConfig().type}'.`,
2522
`Expected length of argument value to be between ${min} and ${max}. Received ${value.length}.`
2623
);
2724
}
2825

2926
if (value.length > max) {
3027
throw new MetaBindArgumentError(
3128
ErrorLevel.WARNING,
32-
`Failed to parse argument value for argument '${this.identifier}'.`,
29+
`Failed to parse argument value for argument '${this.getConfig().type}'.`,
3330
`Expected length of argument value to be between ${min} and ${max}. Received ${value.length}.`
3431
);
3532
}
3633
}
3734

3835
isAllowed(inputFieldType: InputFieldType): boolean {
39-
if (this.allowedInputFields.length === 0) {
36+
if (this.getConfig().allowedInputFieldTypes.length === 0) {
4037
return true;
4138
}
4239

43-
return this.allowedInputFields.contains(inputFieldType);
40+
return this.getConfig().allowedInputFieldTypes.contains(inputFieldType);
4441
}
4542

4643
getAllowedInputFieldsAsString(): string {
47-
return this.allowedInputFields.length === 0 ? 'all' : this.allowedInputFields.join(', ');
44+
return this.getConfig().allowedInputFieldTypes.length === 0 ? 'all' : this.getConfig().allowedInputFieldTypes.join(', ');
4845
}
4946
}

src/inputFieldArguments/InputFieldArgumentContainer.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AbstractInputFieldArgument } from './AbstractInputFieldArgument';
2-
import { InputFieldArgumentType } from '../parsers/InputFieldDeclarationParser';
32
import { ErrorLevel, MetaBindParsingError } from '../utils/errors/MetaBindErrors';
3+
import { InputFieldArgumentType } from '../inputFields/InputFieldConfigs';
44

55
export class InputFieldArgumentContainer {
66
arguments: AbstractInputFieldArgument[] = [];
@@ -16,12 +16,14 @@ export class InputFieldArgumentContainer {
1616
}
1717

1818
for (const argument of this.arguments) {
19-
map[argument.identifier] += 1;
20-
if (map[argument.identifier] > 1 && !argument.allowMultiple) {
19+
const argumentConfig = argument.getConfig();
20+
21+
map[argumentConfig.type] += 1;
22+
if (map[argumentConfig.type] > 1 && !argumentConfig.allowMultiple) {
2123
throw new MetaBindParsingError(
2224
ErrorLevel.CRITICAL,
2325
'failed to validate argument container',
24-
`argument '${argument.identifier}' does not allow duplicates`
26+
`argument '${argumentConfig.type}' does not allow duplicates`
2527
);
2628
}
2729
}
@@ -35,8 +37,9 @@ export class InputFieldArgumentContainer {
3537
*/
3638
mergeByOverride(other: InputFieldArgumentContainer): InputFieldArgumentContainer {
3739
for (const argument of other.arguments) {
38-
if (!argument.allowMultiple) {
39-
this.arguments = this.arguments.filter(x => x.identifier !== argument.identifier);
40+
const argumentConfig = argument.getConfig();
41+
if (!argumentConfig.allowMultiple) {
42+
this.arguments = this.arguments.filter(x => x.getConfig().type !== argumentConfig.type);
4043
}
4144
this.arguments.push(argument);
4245
}
@@ -55,8 +58,9 @@ export class InputFieldArgumentContainer {
5558
*/
5659
mergeByThrow(other: InputFieldArgumentContainer): InputFieldArgumentContainer {
5760
for (const argument of other.arguments) {
58-
if (!argument.allowMultiple) {
59-
if (this.arguments.filter(x => x.identifier === argument.identifier).length > 0) {
61+
const argumentConfig = argument.getConfig();
62+
if (!argumentConfig.allowMultiple) {
63+
if (this.arguments.filter(x => x.getConfig().type === argumentConfig.type).length > 0) {
6064
throw new MetaBindParsingError(
6165
ErrorLevel.ERROR,
6266
'failed to merge argument container',
@@ -74,7 +78,7 @@ export class InputFieldArgumentContainer {
7478
}
7579

7680
getAll(name: InputFieldArgumentType): AbstractInputFieldArgument[] {
77-
return this.arguments.filter(x => x.identifier === name);
81+
return this.arguments.filter(x => x.getConfig().type === name);
7882
}
7983

8084
get(name: InputFieldArgumentType): AbstractInputFieldArgument | undefined {

src/inputFieldArguments/InputFieldArgumentFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { InputFieldArgumentType } from '../parsers/InputFieldDeclarationParser';
21
import { ClassInputFieldArgument } from './arguments/ClassInputFieldArgument';
32
import { AddLabelsInputFieldArgument } from './arguments/AddLabelsInputFieldArgument';
43
import { MinValueInputFieldArgument } from './arguments/MinValueInputFieldArgument';
@@ -13,6 +12,7 @@ import { OffValueInputFieldArgument } from './arguments/OffValueInputFieldArgume
1312
import { OnValueInputFieldArgument } from './arguments/OnValueInputFieldArgument';
1413
import { DefaultValueInputFieldArgument } from './arguments/DefaultValueInputFieldArgument';
1514
import { PlaceholderInputFieldArgument } from './arguments/PlaceholderInputFieldArgument';
15+
import { InputFieldArgumentType } from '../inputFields/InputFieldConfigs';
1616

1717
export class InputFieldArgumentFactory {
1818
static createInputFieldArgument(argumentIdentifier: string): AbstractInputFieldArgument {
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { AbstractInputFieldArgument } from '../AbstractInputFieldArgument';
2-
import { InputFieldArgumentType, InputFieldType } from '../../parsers/InputFieldDeclarationParser';
3-
import { ParsingResultNode } from '../../parsers/newInputFieldParser/InputFieldDeclarationValidator';
2+
import { ParsingResultNode } from '../../parsers/newInputFieldParser/InputFieldParser';
3+
import { InputFieldArgumentConfig, InputFieldArgumentConfigs, InputFieldArgumentType, InputFieldType } from '../../inputFields/InputFieldConfigs';
44

55
export class AddLabelsInputFieldArgument extends AbstractInputFieldArgument {
6-
identifier: InputFieldArgumentType = InputFieldArgumentType.ADD_LABELS;
7-
allowedInputFields: InputFieldType[] = [InputFieldType.SLIDER, InputFieldType.PROGRESS_BAR];
86
value: boolean = true;
9-
valueLengthMin: number = 0;
10-
valueLengthMax: number = 1;
11-
allowMultiple: boolean = false;
127

138
override _parseValue(value: ParsingResultNode[]): void {
149
this.value = value[0] === undefined || value[0]?.value.toLowerCase() === 'true';
1510
}
11+
12+
public getConfig(): InputFieldArgumentConfig {
13+
return InputFieldArgumentConfigs.addLabels;
14+
}
1615
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { AbstractInputFieldArgument } from '../AbstractInputFieldArgument';
2-
import { InputFieldArgumentType, InputFieldType } from '../../parsers/InputFieldDeclarationParser';
3-
import { ParsingResultNode } from '../../parsers/newInputFieldParser/InputFieldDeclarationValidator';
2+
3+
import { ParsingResultNode } from '../../parsers/newInputFieldParser/InputFieldParser';
4+
import { InputFieldArgumentConfig, InputFieldArgumentConfigs, InputFieldArgumentType, InputFieldType } from '../../inputFields/InputFieldConfigs';
45

56
export class ClassInputFieldArgument extends AbstractInputFieldArgument {
6-
identifier: InputFieldArgumentType = InputFieldArgumentType.CLASS;
7-
allowedInputFields: InputFieldType[] = [];
87
value: string[] = [];
9-
valueLengthMin: number = 1;
10-
valueLengthMax: number = 1;
11-
allowMultiple: boolean = true;
128

139
_parseValue(value: ParsingResultNode[]): void {
1410
this.value = value[0].value.split(' ');
1511
}
12+
13+
public getConfig(): InputFieldArgumentConfig {
14+
return InputFieldArgumentConfigs.class;
15+
}
1616
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { AbstractInputFieldArgument } from '../AbstractInputFieldArgument';
2-
import { InputFieldArgumentType, InputFieldType } from '../../parsers/InputFieldDeclarationParser';
32
import { MBExtendedLiteral, parseLiteral } from '../../utils/Utils';
4-
import { ParsingResultNode } from '../../parsers/newInputFieldParser/InputFieldDeclarationValidator';
3+
4+
import { ParsingResultNode } from '../../parsers/newInputFieldParser/InputFieldParser';
5+
import { InputFieldArgumentConfig, InputFieldArgumentConfigs, InputFieldArgumentType, InputFieldType } from '../../inputFields/InputFieldConfigs';
56

67
export class DefaultValueInputFieldArgument extends AbstractInputFieldArgument {
7-
identifier: InputFieldArgumentType = InputFieldArgumentType.DEFAULT_VALUE;
8-
allowedInputFields: InputFieldType[] = [];
98
value: MBExtendedLiteral = '';
10-
valueLengthMin: number = 1;
11-
valueLengthMax: number = 1;
12-
allowMultiple: boolean = false;
139

1410
_parseValue(value: ParsingResultNode[]): void {
1511
this.value = parseLiteral(value[0].value);
1612
}
13+
14+
public getConfig(): InputFieldArgumentConfig {
15+
return InputFieldArgumentConfigs.defaultValue;
16+
}
1717
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { AbstractInputFieldArgument } from '../AbstractInputFieldArgument';
2-
import { InputFieldArgumentType, InputFieldType } from '../../parsers/InputFieldDeclarationParser';
32
import { ErrorLevel, MetaBindParsingError } from '../../utils/errors/MetaBindErrors';
4-
import { ParsingResultNode } from '../../parsers/newInputFieldParser/InputFieldDeclarationValidator';
3+
4+
import { ParsingResultNode } from '../../parsers/newInputFieldParser/InputFieldParser';
5+
import { InputFieldArgumentConfig, InputFieldArgumentConfigs, InputFieldArgumentType, InputFieldType } from '../../inputFields/InputFieldConfigs';
56

67
export class MaxValueInputFieldArgument extends AbstractInputFieldArgument {
7-
identifier: InputFieldArgumentType = InputFieldArgumentType.MAX_VALUE;
8-
allowedInputFields: InputFieldType[] = [InputFieldType.SLIDER, InputFieldType.PROGRESS_BAR];
98
value: number = 100;
10-
valueLengthMin: number = 1;
11-
valueLengthMax: number = 1;
12-
allowMultiple: boolean = false;
139

1410
_parseValue(value: ParsingResultNode[]): void {
1511
this.value = Number.parseInt(value[0].value);
@@ -21,4 +17,8 @@ export class MaxValueInputFieldArgument extends AbstractInputFieldArgument {
2117
);
2218
}
2319
}
20+
21+
public getConfig(): InputFieldArgumentConfig {
22+
return InputFieldArgumentConfigs.maxValue;
23+
}
2424
}

0 commit comments

Comments
 (0)