test out the tables

This commit is contained in:
2019-11-11 01:40:29 -07:00
parent acba54cfa5
commit aa49e24304
2 changed files with 89 additions and 1 deletions

View File

@ -1,7 +1,62 @@
import { d } from './dice'; import { d } from './dice';
import { tableData } from './tableData';
class TableService { class TableService {
private randomTables: RandomTable[] = [];
private tableByName: {[tableName: string]: RandomTable} = {};
private tablesByTags: {[tagName: string]: RandomTable[]} = {};
private tags: Set<string> = new Set();
constructor() {
console.time('TableServiceStartup');
for (let i in tableData) {
let table = tableData[i];
let randomTable: RandomTable = new RandomTable(table);
this.randomTables.push(randomTable);
this.tableByName[table.name] = randomTable;
if (table.tags) {
for (let j in table.tags) {
let tag: string = table.tags[j];
if (!this.tablesByTags[tag]) {
this.tablesByTags[tag] = [];
}
this.tags.add(tag);
this.tablesByTags[tag].push(randomTable);
}
}
}
console.timeEnd('TableServiceStartup');
}
public getTablesWithTag(tag: string) {
return this.tablesByTags[tag];
}
public getTable(name: string) {
return this.tableByName[name];
}
public getTags() {
return this.tags;
}
public searchForTable(search: string) {
let answer = this.randomTables.filter((table: RandomTable) => {
return table.name.toLowerCase().indexOf(search.toLowerCase()) !== -1 || table.getTags().join(' ').toLowerCase().indexOf(search.toLowerCase()) !== -1;
}).map((table: RandomTable) => {
let score = search.length / table.name.length;
return {
table: table,
score: score
};
}).sort((a: {table: RandomTable, score: number}, b: {table: RandomTable, score: number}) => {
return a.score - b.score;
}).map((tableScore: {table: RandomTable, score: number}) => {
return tableScore.table;
});
return answer;
}
} }
/* /*
@ -23,13 +78,15 @@ class TableService {
class RandomTable { class RandomTable {
public name: string; public name: string;
private dice: number[]; public dice: number[];
private indexedResults: TableResult[] = []; private indexedResults: TableResult[] = [];
private config: TableConfig;
constructor(tableConfig: TableConfig) { constructor(tableConfig: TableConfig) {
this.name = tableConfig.name; this.name = tableConfig.name;
this.dice = tableConfig.dice; this.dice = tableConfig.dice;
this.constructIndex(tableConfig.possibleResults); this.constructIndex(tableConfig.possibleResults);
this.config = tableConfig;
} }
public roll(input?: number) { public roll(input?: number) {
@ -44,6 +101,18 @@ class RandomTable {
return this.indexedResults[input - 1]; return this.indexedResults[input - 1];
} }
public getPossibleResults() {
return this.config.possibleResults;
}
public getTags() {
return this.config.tags || [];
}
public getNotes() {
return this.config.notes;
}
private getRoll(): number { private getRoll(): number {
let answer: number = 0; let answer: number = 0;
for (let i in this.dice) { for (let i in this.dice) {

View File

@ -27,6 +27,13 @@
</div> </div>
<div>
<input v-model="tableSearch" />
<div v-for="table, i in searchedTables" :key="i" @click="curTable = table">
{{table.name}}
</div>
</div>
<messages></messages> <messages></messages>
</div> </div>
</template> </template>
@ -59,6 +66,9 @@ export default class TestTable extends Vue {
20, 20,
]; ];
private tableSearch: string = '';
private searchedTables: RandomTable[] = [];
private curTable: RandomTable = new RandomTable(tableData[0]); private curTable: RandomTable = new RandomTable(tableData[0]);
private tableResult: string = ''; private tableResult: string = '';
@ -83,6 +93,15 @@ export default class TestTable extends Vue {
messageService.addMessage(`${i} = ${tableResponse.value}`); messageService.addMessage(`${i} = ${tableResponse.value}`);
} }
} }
@Watch('tableSearch')
private doSearch(newValue: string, oldValue: string) {
if (newValue === '') {
this.searchedTables = [];
} else {
this.searchedTables = tableService.searchForTable(newValue);
}
}
} }
</script> </script>