You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ItemManager.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { TableDefinitionExporter } from "../../Types/Interfaces";
  2. import { TableDefiniton, _Rank } from "../../Types/Types";
  3. import { FrontworkAdmin } from "../../Admin/Admin";
  4. import { T1 } from "../../Types/Items";
  5. import { FrontworkComponent } from "../../Types/FrontworkComponent";
  6. import { RPC } from "rpclibrary";
  7. const fetch = require('node-fetch')
  8. const xml2js = require('xml2js');
  9. const parser = new xml2js.Parser(/* options */);
  10. export type ItemManagerFeatureIfc = any
  11. export type Item = {
  12. id?:number
  13. name:string
  14. iconname:string
  15. url:string
  16. quality:string
  17. hidden:boolean
  18. }
  19. export class ItemManager
  20. implements FrontworkComponent<any, any, ItemManagerFeatureIfc>, TableDefinitionExporter{
  21. admin:FrontworkAdmin
  22. name = "ItemManager";
  23. exportRPCs(): RPC<any, any>[]{
  24. return [{
  25. name: 'getItems',
  26. call: async () => await this.getItems()
  27. },{
  28. name: 'getItem',
  29. call: async (name:string) => await this.getItem(name)
  30. }]
  31. }
  32. exportRPCFeatures() {
  33. return []
  34. }
  35. getItems = async () :Promise<Item[]> => await this.admin.knex.select('*').from('items')
  36. getItem = async (name:string):Promise<Item> => {
  37. const res = await fetch('https://classic.wowhead.com/item='+name+'&xml');
  38. const txt = await res.text();
  39. const r = await parser.parseStringPromise(txt);
  40. try{
  41. return <Item>{
  42. name: r.wowhead.item[0].name[0],
  43. iconname: r.wowhead.item[0].icon[0]._,
  44. url: r.wowhead.item[0].link[0],
  45. quality: r.wowhead.item[0].quality[0]._,
  46. }
  47. }catch (e){
  48. console.log(name)
  49. throw e
  50. }
  51. }
  52. getTableDefinitions(): TableDefiniton[] {
  53. return [
  54. {
  55. name: 'reservations',
  56. tableBuilder: (table) => {
  57. table.integer("user_id").primary()
  58. table.foreign("user_id").references("id").inTable('users')
  59. table.integer("item_id").primary()
  60. table.foreign("item_id").references("id").inTable('items')
  61. table.integer("raid_id").primary()
  62. table.foreign("raid_id").references("id").inTable('raids')
  63. }
  64. },{
  65. name: 'items',
  66. tableBuilder: (table) => {
  67. table.increments("id").primary()
  68. table.string('name').unique().notNullable()
  69. table.string('iconname').notNullable()
  70. table.string('url').notNullable()
  71. table.string('quality').defaultTo('Epic').notNullable()
  72. table.boolean('hidden').defaultTo(false).notNullable()
  73. }
  74. }]
  75. }
  76. countItems = async() :Promise<number> => {
  77. const count = await this.admin.knex('items').count('*');
  78. return <number>count[0]['count(*)']
  79. }
  80. initialize = async() => {
  81. const allItems = [...T1]
  82. const countCache = await this.countItems()
  83. if(countCache != allItems.length){
  84. const items:Item[] = await Promise.all(allItems.map((i) => this.getItem(i)))
  85. try{
  86. await this.admin
  87. .knex('items')
  88. .insert(items)
  89. }catch(e){
  90. console.info("Skipping item insertion")
  91. }
  92. }
  93. }
  94. }