PlayerProgress.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. class PlayerProgress {
  2. constructor() {
  3. this.progress = {};
  4. this.progressChains = {};
  5. this.unlocks = [];
  6. }
  7. async init() {
  8. this.progressChains = await system.jsonGet(`/api/progress`, {});
  9. this.progress["starting"] = this.progressChains["starting"][0];
  10. }
  11. getProgress(chainName) {
  12. return this.progress[chainName];
  13. }
  14. getNextStep(chainName) {
  15. let currentStep = this.getProgress(chainName);
  16. let index = this.progressChains[chainName].indexOf(currentStep);
  17. if (index == this.progressChains[chainName].length - 1) {
  18. return currentStep;
  19. }
  20. let nextStep = this.progressChains[chainName][index + 1];
  21. return nextStep;
  22. }
  23. isUnlocked(key) {
  24. return this.unlocks.indexOf(key) != -1;
  25. }
  26. advanceProgress(chainName) {
  27. this.progress[chainName] = this.getNextStep(chainName);
  28. for (let index in this.progress[chainName].unlocks) {
  29. this.unlock(this.progress[chainName].unlocks[index]);
  30. }
  31. let nextStep = this.progress[chainName];
  32. return nextStep;
  33. }
  34. unlock(key) {
  35. if (!this.isUnlocked(key)) {
  36. this.unlocks.push(key);
  37. }
  38. }
  39. }