assets-getter.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { room } from "./room.js";
  2. /**
  3. * This class is responsible for loading assets from the server.
  4. */
  5. export class assets_getter {
  6. #app_url;
  7. /**
  8. * This create new assets getter.
  9. *
  10. * @param {string} app_url - This is base app url
  11. */
  12. constructor(app_url) {
  13. this.#app_url = app_url;
  14. }
  15. /**
  16. * This fetch file from the config directory.
  17. *
  18. * @async
  19. * @param {string} file - Name of the file to load
  20. * @returns {string, object} - Content of the file, or object if json
  21. * @throws {Error} - When file not exists on the server
  22. * @throws {TypeError} - When JSON file is bad formated
  23. */
  24. async load(file) {
  25. const url = this.#get_url(file);
  26. const result = await fetch(url);
  27. if (!result.ok) {
  28. let info = `Can not load file: "${url}", `;
  29. info += `status: "${result.status}".`;
  30. throw new Error(info);
  31. }
  32. if (this.#is_json(file)) {
  33. try {
  34. return await result.json();
  35. } catch (error) {
  36. let info = `JSON file "${file}" is bad formated. `;
  37. info += `More: "${error.message}".`;
  38. throw new TypeError(info);
  39. }
  40. }
  41. return await result.text();
  42. }
  43. /**
  44. * This return url for texture, from texture name or room.
  45. *
  46. * @param {string, room} target - Name or room of the texture
  47. * @returns {string} Link to the texture
  48. */
  49. texture(target) {
  50. if (target instanceof room) {
  51. target = target.texture;
  52. }
  53. return this.#get_url(`textures/${target}`);
  54. }
  55. /**
  56. * @async
  57. * @returns {object} - Return enviroment json as object
  58. */
  59. get enviroment() {
  60. return this.load("enviroment.json");
  61. }
  62. /**
  63. * @async
  64. * @returns {object} - Return items json as object
  65. */
  66. get items() {
  67. return this.load("items.json");
  68. }
  69. /**
  70. * @async
  71. * @returns {object} - Return rooms json as object
  72. */
  73. get rooms() {
  74. return this.load("rooms.json");
  75. }
  76. /**
  77. * @returns {string} - Base config directory on server
  78. */
  79. get #base_url() {
  80. return "/static/config";
  81. }
  82. /**
  83. * This function generate full URL to the file on server.
  84. *
  85. * @param {string} file - Name of the file in config location
  86. * @returns {string} - Full path to the file on server
  87. */
  88. #get_url(file) {
  89. return `${this.#app_url}/${this.#base_url}/${file}`;
  90. }
  91. /**
  92. * This function check that file is json.
  93. *
  94. * @param {string} file - Name of the file to load
  95. * @returns {bool} - True if file is json, os false when not
  96. */
  97. #is_json(file) {
  98. return file.substring(file.length - 5) === ".json";
  99. }
  100. }