Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
viable monsters displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
brehaut committed Aug 27, 2014
1 parent 10e713b commit 41fdffd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
19 changes: 13 additions & 6 deletions src/ts/bestiary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ module manticore.bestiary {
}


function isViable(partyLevel:number, monster:data.Monster): boolean {
return !!relativeCost(relativeLevel(partyLevel, monster.level));
}

function priceMonster(partyLevel:number, m:data.Monster) {
var cost = relativeCost(relativeLevel(partyLevel, m.level));
var multiplier = scaleFactor(m.scale, partyLevel);
Expand Down Expand Up @@ -215,13 +219,12 @@ module manticore.bestiary {


// public API:
export function allocationsForParty(characters:number,
partyLevel:number,
export function allocationsForParty(party:data.IParty,
selectedMonsters:Array<data.Monster>) {

return allocateMonsters(priceParty(characters),
return allocateMonsters(priceParty(party.size),
selectedMonsters
.map((m) => priceMonster(partyLevel, m))
.map((m) => priceMonster(party.level, m))
.filter((m) => m !== null)
);
}
Expand Down Expand Up @@ -256,8 +259,12 @@ module manticore.bestiary {
return attributes;
}

public filteredBestiary(filter: data.IPredicate<data.Monster>) {
return this.monsters.filter(filter);
public filteredBestiary(party: data.IParty,
filter: data.IPredicate<data.Monster>) {
return this.monsters
.filter(m => isViable(party.level, m))
.filter(filter)
;
}

private distinctValues<T>(accessor:(m:data.Monster)=>T):T[] {
Expand Down
2 changes: 1 addition & 1 deletion src/ts/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module manticore.data {


export interface Allocator {
(partySize: number, partyLevel: number, monsters: Array<Monster>): Array<Array<Allocation>>;
(party: IParty, monsters: Array<Monster>): Array<Array<Allocation>>;
}


Expand Down
43 changes: 25 additions & 18 deletions src/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ module manticore.interface {
class NumericField implements IView {
private el: HTMLInputElement;

public onChange: Event<number>;
public onChanged: Event<number>;

constructor (val:number, max?:number) {
this.onChange = new Event<number>();
this.onChanged = new Event<number>();

this.el = <HTMLInputElement> DOM.input({
type: "number",
min: 0,
min: 1,
max: max,
value: val,
onkeydown: (e) => {
Expand All @@ -65,7 +65,7 @@ module manticore.interface {
var v = Math.max(0, Math.min(+e.target.value, max || Infinity));
e.target.value = v;

this.onChange.trigger(v);
this.onChanged.trigger(v);
}

});
Expand All @@ -80,16 +80,18 @@ module manticore.interface {
class PartyView implements IView {
private el:HTMLElement;

public onChanged: Event<data.IParty>;

constructor () {
this.onChanged = new Event<data.IParty>();
this.createElements();
}

public getPartyLevel () {
return +(<HTMLInputElement> this.el.querySelector("div.level input")).value;
}

public getPartySize () {
return +(<HTMLInputElement> this.el.querySelector("div.size input")).value;
public getPartyInfo():data.IParty {
return {
level: +(<HTMLInputElement> this.el.querySelector("div.level input")).value,
size: +(<HTMLInputElement> this.el.querySelector("div.size input")).value
};
}

public _appendTo(element:HTMLElement) {
Expand All @@ -101,6 +103,7 @@ module manticore.interface {
val:number,
max?:number) {
var input = new NumericField(val, max);
input.onChanged.register(_ => this.onChanged.trigger(this.getPartyInfo()));

var div = DOM.div(
{"class": "field " + className},
Expand Down Expand Up @@ -234,7 +237,7 @@ module manticore.interface {
}

public updateSelectedCount(count: number) {
this.selectionCountEl.innerText = _("Number of monsters selected: ") + count;
this.selectionCountEl.innerText = _("Number selected ") + count;
}

public _appendTo(element:HTMLElement) {
Expand Down Expand Up @@ -403,23 +406,27 @@ module manticore.interface {
this.bindEvents();

this._appendTo(root);

this.updateSelectionInfo();
}

private updateSelectionInfo() {
this.filtersView.updateSelectedCount(this.getSelection().length);
}

private getSelection() {
var pred = data.predicateForFilters(this.filtersView.getFilters());
return this.catalog.filteredBestiary(pred);
return this.catalog.filteredBestiary(this.partyView.getPartyInfo(), pred);
}

private bindEvents() {
this.filtersView.onFilterChanged.register(_ => {
this.filtersView.updateSelectedCount(this.getSelection().length);
});
this.filtersView.onFilterChanged.register(_ => this.updateSelectionInfo());
this.partyView.onChanged.register(_ => this.updateSelectionInfo());

this.resultsView.onRequestGenerate.register(_ => {
var selection = this.getSelection();

var alloc = this.allocator(this.partyView.getPartySize(),
this.partyView.getPartyLevel(),
var alloc = this.allocator(this.partyView.getPartyInfo(),
selection);

this.resultsView.displayResults(alloc);
Expand All @@ -430,7 +437,7 @@ module manticore.interface {
this.partyView._appendTo(this.viewContainer);
this.filtersView._appendTo(this.viewContainer);
this.resultsView._appendTo(this.viewContainer);

element.appendChild(this.viewContainer);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/ts/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module manticore.interface.strings {
"[results summary]": "Encounters that fit these criteria",

"generate encounters": "Generate encounters",
"more": "More…"
"more": "More…",
"Number selected ": "Number of viable monsters selected: "
}


Expand Down

0 comments on commit 41fdffd

Please sign in to comment.