test out the tables
This commit is contained in:
@ -1,7 +1,62 @@
|
||||
import { d } from './dice';
|
||||
import { tableData } from './tableData';
|
||||
|
||||
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 {
|
||||
public name: string;
|
||||
private dice: number[];
|
||||
public dice: number[];
|
||||
private indexedResults: TableResult[] = [];
|
||||
private config: TableConfig;
|
||||
|
||||
constructor(tableConfig: TableConfig) {
|
||||
this.name = tableConfig.name;
|
||||
this.dice = tableConfig.dice;
|
||||
this.constructIndex(tableConfig.possibleResults);
|
||||
this.config = tableConfig;
|
||||
}
|
||||
|
||||
public roll(input?: number) {
|
||||
@ -44,6 +101,18 @@ class RandomTable {
|
||||
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 {
|
||||
let answer: number = 0;
|
||||
for (let i in this.dice) {
|
||||
|
@ -27,6 +27,13 @@
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<input v-model="tableSearch" />
|
||||
<div v-for="table, i in searchedTables" :key="i" @click="curTable = table">
|
||||
{{table.name}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<messages></messages>
|
||||
</div>
|
||||
</template>
|
||||
@ -59,6 +66,9 @@ export default class TestTable extends Vue {
|
||||
20,
|
||||
];
|
||||
|
||||
private tableSearch: string = '';
|
||||
private searchedTables: RandomTable[] = [];
|
||||
|
||||
private curTable: RandomTable = new RandomTable(tableData[0]);
|
||||
private tableResult: string = '';
|
||||
|
||||
@ -83,6 +93,15 @@ export default class TestTable extends Vue {
|
||||
messageService.addMessage(`${i} = ${tableResponse.value}`);
|
||||
}
|
||||
}
|
||||
|
||||
@Watch('tableSearch')
|
||||
private doSearch(newValue: string, oldValue: string) {
|
||||
if (newValue === '') {
|
||||
this.searchedTables = [];
|
||||
} else {
|
||||
this.searchedTables = tableService.searchForTable(newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
Reference in New Issue
Block a user