Advertisement
Wasshup

ChestService.js

Sep 20th, 2021
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.49 KB | None | 0 0
  1. class ChestService
  2. {
  3.     chests = [];
  4.     numChests = 0;
  5.     chestRewardText = "";
  6.     foundGoldChest = false;
  7.     chestsStored = 0;
  8.     totalBasicChestsOpened = 0;
  9.     totalGoldChestsOpened = 0;
  10.     totalMoneyFromChests = 0;
  11.  
  12.     getTotalChests()
  13.     {
  14.         return this.chests.reduce(total => ++total, 0);
  15.     }
  16.  
  17.     hasUnclaimedChests()
  18.     {
  19.         return this.getTotalChests() > 0;
  20.     }
  21.  
  22.     getChestAtDepth(worldDepth)
  23.     {
  24.         return this.getChest(Math.floor(worldDepth / 10));
  25.     }
  26.  
  27.     getChest(tenthOfDepth)
  28.     {
  29.         return this.chests[tenthOfDepth];
  30.     }
  31.  
  32.     getMaxStoredChests()
  33.     {
  34.         if(chestCollectorStorageStructure.level > 0)
  35.         {
  36.             return chestCollectorStorageStructure.statValueForCurrentLevel();
  37.         }
  38.         return 0;
  39.     }
  40.  
  41.     isChestCollectorFull()
  42.     {
  43.         return this.getMaxStoredChests() <= this.chestsStored;
  44.     }
  45.  
  46.     getStoredChestsChance()
  47.     {
  48.         if(chestCollectorStorageStructure.level > 0)
  49.         {
  50.             return chestCollectorChanceStructure.statValueForCurrentLevel();
  51.         }
  52.         return 0;
  53.     }
  54.  
  55.     chestExistsAtDepth(worldDepth)
  56.     {
  57.         let tenthOfDepth = Math.floor(worldDepth / 10);
  58.  
  59.         return this.chestExists(tenthOfDepth) && worldDepth == tenthOfDepth * 10 + this.getChest(tenthOfDepth).depthInBlock;
  60.     }
  61.  
  62.     chestExists(tenthOfDepth)
  63.     {
  64.         return this.chests[tenthOfDepth] !== undefined;
  65.     }
  66.  
  67.     spawnChest(tenthOfDepth, source = Chest.natural, isGolden = false, validBlockDepths)
  68.     {
  69.         this.chests[tenthOfDepth] = source.new(source, tenthOfDepth, isGolden, validBlockDepths);
  70.         this.presentChest(tenthOfDepth);
  71.     }
  72.  
  73.     removeAllChests(source)
  74.     {
  75.         this.forEachChest((chest, index) =>
  76.         {
  77.             if(chest.source == source.name)
  78.             {
  79.                 this.removeChest(index);
  80.             }
  81.         });
  82.     }
  83.  
  84.     removeChest(tenthOfDepth)
  85.     {
  86.         delete this.chests[tenthOfDepth];
  87.     }
  88.  
  89.     forEachChest(callback)
  90.     {
  91.         this.chests.forEach(callback);
  92.     }
  93.  
  94.     userHasFoundGoldenChest()
  95.     {
  96.         return this.foundGoldChest;
  97.     }
  98.  
  99.     getChestRewardText()
  100.     {
  101.         return this.chestRewardText;
  102.     }
  103.  
  104.     spawnChestAtRandomDepth(source)
  105.     {
  106.         let openDepths = Array
  107.             .from(Array(Math.floor(depth / 10)).keys())
  108.             .filter(tenthOfDepth => !this.chestExists(tenthOfDepth));
  109.         let tenthOfDepth = chestSpawnRoller.rand(0, openDepths.length - 1);
  110.         let validBlockDepths = this.getValidBlockDepths(tenthOfDepth);
  111.  
  112.         if(validBlockDepths.length)
  113.         {
  114.             this.spawnChest(tenthOfDepth, source, this.rollForGoldenChest(), validBlockDepths);
  115.         }
  116.     }
  117.  
  118.     spawnRandomChestsAndRemoveExpired()
  119.     {
  120.         var maxTenthOfDepth = Math.floor(depth / 10);
  121.  
  122.         for(x = 3; x <= maxTenthOfDepth; x++)
  123.         {
  124.             if(this.chestExists(x))
  125.             {
  126.                 this.updateChestTTL(x);
  127.             }
  128.             else
  129.             {
  130.                 this.rollForRandomChest(x, Chest.natural);
  131.             }
  132.         }
  133.     }
  134.  
  135.     updateChestTTL(tenthOfDepth)
  136.     {
  137.         let chest = this.getChest(tenthOfDepth);
  138.  
  139.         if(chest.source == Chest.natural.name)
  140.         {
  141.             if(chest.timeToLive > 0)
  142.             {
  143.                 chest.reduceTTL();
  144.             }
  145.             else
  146.             {
  147.                 this.removeChest(tenthOfDepth);
  148.             }
  149.         }
  150.     }
  151.  
  152.     rollForRandomChest(tenthOfDepth, source)
  153.     {
  154.         let validBlockDepths = this.getValidBlockDepths(tenthOfDepth);
  155.  
  156.         if(validBlockDepths.length && this.rollForBasicChest())
  157.         {
  158.             this.spawnChest(tenthOfDepth, source, this.rollForGoldenChest(), validBlockDepths);
  159.             this.presentChest(tenthOfDepth);
  160.         }
  161.     }
  162.  
  163.     getValidBlockDepths(tenthOfDepth)
  164.     {
  165.         var depths = [];
  166.  
  167.         for(let blockDepth = 0; blockDepth < 10; blockDepth++)
  168.         {
  169.             let worldDepth = tenthOfDepth * 10 + blockDepth;
  170.             if(worldDepth <= depth && !isDepthWithoutWorkers(worldDepth) && !isBossLevel(worldDepth))
  171.             {
  172.                 depths.push(blockDepth);
  173.             }
  174.         }
  175.  
  176.         return depths;
  177.     }
  178.  
  179.     rollForBasicChest()
  180.     {
  181.         let baseRand = 3000;
  182.         let chanceAtDepth = baseRand + ((depth-300)/2);
  183.         let chestRoller = Math.floor(chestSpawnRoller.randFloat() * chanceAtDepth);
  184.         let chestThreshold = ((worldAtDepth(x * 10).workersHired * 2) + 1 + Math.floor(afk / 35)) * STAT.chestSpawnFrequencyMultiplier();
  185.  
  186.         return chestRoller < chestThreshold;
  187.     }
  188.  
  189.     rollForGoldenChest()
  190.     {
  191.         let goldenChestChance = randRoundToInteger(1580 * STAT.goldChestSpawnFrequencyMultiplier()); //1 in 158
  192.  
  193.         return chestSpawnRoller.rand(0, goldenChestChance) <= 10;
  194.     }
  195.  
  196.     presentChest(tenthOfDepth)
  197.     {
  198.         let chest = this.getChest(tenthOfDepth);
  199.         this.giveChestReward(chest.tenthOfDepth);
  200.         newNews(_("You got {0} from a Chest!", chestService.getChestRewardText()), true);
  201.  
  202.         if(chest.isGolden)
  203.         {
  204.             trackEvent_FoundChest(1);
  205.         }
  206.         else
  207.         {
  208.             trackEvent_FoundChest(0);
  209.         }
  210.     }
  211.  
  212.     giveChestReward(tenthOfDepth, saveGameOnOpen = true)
  213.     {
  214.         if(this.getChest(tenthOfDepth).isGolden)
  215.         {
  216.             this.foundGoldChest = true;
  217.             this.chestRewardText = goldChestRewards.rollForRandomReward();
  218.             this.totalGoldChestsOpened++;
  219.         }
  220.         else
  221.         {
  222.             this.chestRewardText = basicChestRewards.rollForRandomReward();
  223.             this.totalBasicChestsOpened++;
  224.         }
  225.  
  226.         this.removeChest(tenthOfDepth);
  227.  
  228.         if(saveGameOnOpen)
  229.         {
  230.             savegame();
  231.         }
  232.     }
  233.  
  234.     testChestCollector(chestsToSpawn)
  235.     {
  236.         var chestsCollected = 0;
  237.         this.chestsStored = 0;
  238.         for(var i = 0; i < chestsToSpawn; ++i)
  239.         {
  240.             this.spawnChestAtRandomDepth(Chest.natural);
  241.             if(this.isChestCollectorFull())
  242.             {
  243.                 chestsCollected += this.chestsStored;
  244.                 this.chestsStored = 0;
  245.             }
  246.         }
  247.         chestsCollected += this.chestsStored;
  248.         console.log(chestsCollected / chestsToSpawn);
  249.         this.removeAllChests(Chest.natural);
  250.         this.chestsStored = 0;
  251.     }
  252. }
  253.  
  254. const chestService = new ChestService();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement