diff --git a/src/services/tableService.ts b/src/services/tableService.ts index 8e1c8eb..6a2efd7 100644 --- a/src/services/tableService.ts +++ b/src/services/tableService.ts @@ -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 = 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) { diff --git a/src/views/test-table.vue b/src/views/test-table.vue index 506dbb4..7180b5f 100644 --- a/src/views/test-table.vue +++ b/src/views/test-table.vue @@ -27,6 +27,13 @@ +
+ +
+ {{table.name}} +
+
+ @@ -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); + } + } }