Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ChestService
- {
- chests = [];
- numChests = 0;
- chestRewardText = "";
- foundGoldChest = false;
- chestsStored = 0;
- totalBasicChestsOpened = 0;
- totalGoldChestsOpened = 0;
- totalMoneyFromChests = 0;
- getTotalChests()
- {
- return this.chests.reduce(total => ++total, 0);
- }
- hasUnclaimedChests()
- {
- return this.getTotalChests() > 0;
- }
- getChestAtDepth(worldDepth)
- {
- return this.getChest(Math.floor(worldDepth / 10));
- }
- getChest(tenthOfDepth)
- {
- return this.chests[tenthOfDepth];
- }
- getMaxStoredChests()
- {
- if(chestCollectorStorageStructure.level > 0)
- {
- return chestCollectorStorageStructure.statValueForCurrentLevel();
- }
- return 0;
- }
- isChestCollectorFull()
- {
- return this.getMaxStoredChests() <= this.chestsStored;
- }
- getStoredChestsChance()
- {
- if(chestCollectorStorageStructure.level > 0)
- {
- return chestCollectorChanceStructure.statValueForCurrentLevel();
- }
- return 0;
- }
- chestExistsAtDepth(worldDepth)
- {
- let tenthOfDepth = Math.floor(worldDepth / 10);
- return this.chestExists(tenthOfDepth) && worldDepth == tenthOfDepth * 10 + this.getChest(tenthOfDepth).depthInBlock;
- }
- chestExists(tenthOfDepth)
- {
- return this.chests[tenthOfDepth] !== undefined;
- }
- spawnChest(tenthOfDepth, source = Chest.natural, isGolden = false, validBlockDepths)
- {
- this.chests[tenthOfDepth] = source.new(source, tenthOfDepth, isGolden, validBlockDepths);
- this.presentChest(tenthOfDepth);
- }
- removeAllChests(source)
- {
- this.forEachChest((chest, index) =>
- {
- if(chest.source == source.name)
- {
- this.removeChest(index);
- }
- });
- }
- removeChest(tenthOfDepth)
- {
- delete this.chests[tenthOfDepth];
- }
- forEachChest(callback)
- {
- this.chests.forEach(callback);
- }
- userHasFoundGoldenChest()
- {
- return this.foundGoldChest;
- }
- getChestRewardText()
- {
- return this.chestRewardText;
- }
- spawnChestAtRandomDepth(source)
- {
- let openDepths = Array
- .from(Array(Math.floor(depth / 10)).keys())
- .filter(tenthOfDepth => !this.chestExists(tenthOfDepth));
- let tenthOfDepth = chestSpawnRoller.rand(0, openDepths.length - 1);
- let validBlockDepths = this.getValidBlockDepths(tenthOfDepth);
- if(validBlockDepths.length)
- {
- this.spawnChest(tenthOfDepth, source, this.rollForGoldenChest(), validBlockDepths);
- }
- }
- spawnRandomChestsAndRemoveExpired()
- {
- var maxTenthOfDepth = Math.floor(depth / 10);
- for(x = 3; x <= maxTenthOfDepth; x++)
- {
- if(this.chestExists(x))
- {
- this.updateChestTTL(x);
- }
- else
- {
- this.rollForRandomChest(x, Chest.natural);
- }
- }
- }
- updateChestTTL(tenthOfDepth)
- {
- let chest = this.getChest(tenthOfDepth);
- if(chest.source == Chest.natural.name)
- {
- if(chest.timeToLive > 0)
- {
- chest.reduceTTL();
- }
- else
- {
- this.removeChest(tenthOfDepth);
- }
- }
- }
- rollForRandomChest(tenthOfDepth, source)
- {
- let validBlockDepths = this.getValidBlockDepths(tenthOfDepth);
- if(validBlockDepths.length && this.rollForBasicChest())
- {
- this.spawnChest(tenthOfDepth, source, this.rollForGoldenChest(), validBlockDepths);
- this.presentChest(tenthOfDepth);
- }
- }
- getValidBlockDepths(tenthOfDepth)
- {
- var depths = [];
- for(let blockDepth = 0; blockDepth < 10; blockDepth++)
- {
- let worldDepth = tenthOfDepth * 10 + blockDepth;
- if(worldDepth <= depth && !isDepthWithoutWorkers(worldDepth) && !isBossLevel(worldDepth))
- {
- depths.push(blockDepth);
- }
- }
- return depths;
- }
- rollForBasicChest()
- {
- let baseRand = 3000;
- let chanceAtDepth = baseRand + ((depth-300)/2);
- let chestRoller = Math.floor(chestSpawnRoller.randFloat() * chanceAtDepth);
- let chestThreshold = ((worldAtDepth(x * 10).workersHired * 2) + 1 + Math.floor(afk / 35)) * STAT.chestSpawnFrequencyMultiplier();
- return chestRoller < chestThreshold;
- }
- rollForGoldenChest()
- {
- let goldenChestChance = randRoundToInteger(1580 * STAT.goldChestSpawnFrequencyMultiplier()); //1 in 158
- return chestSpawnRoller.rand(0, goldenChestChance) <= 10;
- }
- presentChest(tenthOfDepth)
- {
- let chest = this.getChest(tenthOfDepth);
- this.giveChestReward(chest.tenthOfDepth);
- newNews(_("You got {0} from a Chest!", chestService.getChestRewardText()), true);
- if(chest.isGolden)
- {
- trackEvent_FoundChest(1);
- }
- else
- {
- trackEvent_FoundChest(0);
- }
- }
- giveChestReward(tenthOfDepth, saveGameOnOpen = true)
- {
- if(this.getChest(tenthOfDepth).isGolden)
- {
- this.foundGoldChest = true;
- this.chestRewardText = goldChestRewards.rollForRandomReward();
- this.totalGoldChestsOpened++;
- }
- else
- {
- this.chestRewardText = basicChestRewards.rollForRandomReward();
- this.totalBasicChestsOpened++;
- }
- this.removeChest(tenthOfDepth);
- if(saveGameOnOpen)
- {
- savegame();
- }
- }
- testChestCollector(chestsToSpawn)
- {
- var chestsCollected = 0;
- this.chestsStored = 0;
- for(var i = 0; i < chestsToSpawn; ++i)
- {
- this.spawnChestAtRandomDepth(Chest.natural);
- if(this.isChestCollectorFull())
- {
- chestsCollected += this.chestsStored;
- this.chestsStored = 0;
- }
- }
- chestsCollected += this.chestsStored;
- console.log(chestsCollected / chestsToSpawn);
- this.removeAllChests(Chest.natural);
- this.chestsStored = 0;
- }
- }
- const chestService = new ChestService();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement