Skip to content

Commit

Permalink
Merge pull request #41 from padlocks/dev
Browse files Browse the repository at this point in the history
bugfix: rework generateFish(), fixes sell decrement
  • Loading branch information
padlocks authored Jun 26, 2024
2 parents 0546c11 + 76b354e commit c19747c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 67 deletions.
102 changes: 59 additions & 43 deletions src/class/Fish.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ class Fish {
};

static async sendToUser(capabilities, choices, weights, user) {
const fishArray = [];
let fishArray = [];
const numberCapability = capabilities.find(capability => !isNaN(capability));
if (numberCapability !== undefined) {
for (let i = 0; i < numberCapability; i++) {
const nextChoice = await this.generateFish(capabilities, choices, weights, user);
fishArray.push(nextChoice);
}

if (!isNaN(numberCapability)) {
fishArray = await this.generateFish(numberCapability, capabilities, choices, weights, user);
}
else {
const nextChoice = await this.generateFish(capabilities, choices, weights, user);
fishArray.push(nextChoice);
fishArray = await this.generateFish(1, capabilities, choices, weights, user);
}

const uniqueFishArray = [];
fishArray.forEach(oneFish => {
const countCapability = capabilities.find(capability => capability.toLowerCase().includes('count'));
Expand All @@ -64,48 +61,67 @@ class Fish {
return await uniqueFishArray.map(element => element.item);
};

static async generateFish(capabilities, choices, weights, user) {
static async generateFish(number, capabilities, choices, weights, user) {
// const userData = await User.findOne({ userId: user });
let draw = await Utils.getWeightedChoice(choices, weights);
draw = draw.charAt(0).toUpperCase() + draw.slice(1);
const currentBiome = await user.getCurrentBiome();
const biome = currentBiome.charAt(0).toUpperCase() + currentBiome.slice(1);

let f = await FishSchema.find({ rarity: draw, biome: biome });
if (draw === 'Lucky') {
const itemFind = await Utils.getWeightedChoice(['fish', 'item'], [80, 20]);
if (itemFind === 'item') {
const options = await Item.find({ rarity: draw });
const random = Math.floor(Math.random() * options.length);
f = [options[random]];
const choice = [];
for (let i = 0; i < number; i++) {

let draw = await Utils.getWeightedChoice(choices, weights);
draw = draw.charAt(0).toUpperCase() + draw.slice(1);
const currentBiome = await user.getCurrentBiome();
const biome = currentBiome.charAt(0).toUpperCase() + currentBiome.slice(1);

let f = await FishSchema.find({ rarity: draw, biome: biome });
if (draw === 'Lucky') {
const itemFind = await Utils.getWeightedChoice(['fish', 'item'], [80, 20]);
if (itemFind === 'item') {
const options = await Item.find({ rarity: draw });
const random = Math.floor(Math.random() * options.length);
f = [options[random]];
}
}
const filteredChoices = await Promise.all(f.map(async fishObj => {
await new Promise(resolve => setTimeout(resolve, 100));

const isMatch = capabilities.some(capability => fishObj.qualities.includes(capability));

if (isMatch) {
return fishObj;
}
else {
return null;
}
}));

// Remove null values (fish that didn't match the capabilities) from the array
const validChoices = filteredChoices.filter(choice => choice !== null);

if (validChoices.length === 0) {
return await this.generateFish(number, capabilities, choices, weights, user);
}

choice.push(validChoices[Math.floor(Math.random() * validChoices.length)]);
}
const filteredChoices = await Promise.all(f.map(async fishObj => {
await new Promise(resolve => setTimeout(resolve, 100));

const isMatch = capabilities.some(capability => fishObj.qualities.includes(capability));

if (isMatch) {
return fishObj;

// merge any duplicate fish
const uniqueChoices = [];
choice.forEach(fish => {
const existingFish = uniqueChoices.find(f => f.name === fish.name);
if (existingFish) {
existingFish.count++;
}
else {
return null;
uniqueChoices.push({ ...fish._doc });
}
}));

// Remove null values (fish that didn't match the capabilities) from the array
const validChoices = filteredChoices.filter(choice => choice !== null);

if (validChoices.length === 0) {
return await this.generateFish(capabilities, choices, weights, user);
}

const random = Math.floor(Math.random() * validChoices.length);
const choice = validChoices[random];
});

// const clonedChoice = await Utils.clone(choice, user);
const clonedChoice = await user.sendToInventory(choice, 1);

// const clonedChoice = await user.sendToInventory(choice, 1);
const clonedChoice = [];
for (const fish of uniqueChoices) {
clonedChoice.push(await user.sendToInventory(fish, fish.count));
}

// Check for locked status and update the cloned fish as necessary.
if (user) {
const fishIds = (await user.getInventory()).fish;
Expand Down
52 changes: 28 additions & 24 deletions src/class/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class User {

async incrementCommandCount() {
this.user.commands++;
return this.save();
return await this.save();
}

async getCurrentBiome() {
Expand All @@ -39,7 +39,7 @@ class User {

async setCurrentBiome(biome) {
this.user.currentBiome = biome;
return this.save();
return await this.save();
}

async getStats() {
Expand All @@ -48,7 +48,7 @@ class User {

async setStats(stats) {
this.user.stats = stats;
return this.save();
return await this.save();
}

async getMoney() {
Expand All @@ -57,7 +57,7 @@ class User {

async addMoney(amount) {
this.user.inventory.money += parseInt(amount);
return this.save();
return await this.save();
}

async getInventory() {
Expand All @@ -72,7 +72,7 @@ class User {

async removeBait(baitId) {
this.user.inventory.baits = this.user.inventory.baits.filter((b) => b.valueOf() !== baitId);
this.save();
await this.save();
}

async getItems() {
Expand All @@ -96,7 +96,7 @@ class User {
async addQuest(questId) {
const inventory = await this.getInventory();
inventory.quests.push(questId);
return this.save();
return await this.save();
}

async getGacha() {
Expand All @@ -117,7 +117,9 @@ class User {
return fish;
}

async removeFish(fishId, count = 1) {
async removeFish(fishId, count) {
if (!count || count < 1) count = 1;

// if fish count is greater than 1, decrement count
const fish = await FishData.findById(fishId);
if (fish.count >= 1) {
Expand All @@ -126,8 +128,8 @@ class User {
}

if (fish.count <= 0) {
this.user.inventory.fish = this.user.inventory.fish.filter((f) => f.valueOf() !== fishId);
this.save();
this.user.inventory.fish = this.user.inventory.fish.filter((f) => f.valueOf() !== fishId.valueOf());
await this.save();
}

return;
Expand All @@ -136,7 +138,7 @@ class User {
async removeListOfFish(fishIds) {
fishIds = fishIds.map((f) => f.valueOf());
this.user.inventory.fish = this.user.inventory.fish.filter((f) => !fishIds.includes(f.valueOf()));
this.save();
await this.save();
}

async getCodes() {
Expand All @@ -146,7 +148,7 @@ class User {
async addCode(codeId) {
const codes = await this.getCodes();
codes.push(codeId);
return this.save();
return await this.save();
}

async generateBoostedXP() {
Expand Down Expand Up @@ -188,7 +190,7 @@ class User {
const user = this.user;
if (rodId === 'none') {
user.inventory.equippedRod = null;
this.save();
await this.save();
return null;
}
else {
Expand All @@ -207,7 +209,7 @@ class User {
const rodObject = await ItemData.findById(rod.valueOf());

// Save the updated user document
this.save();
await this.save();
return rodObject;
}
}
Expand All @@ -218,7 +220,7 @@ class User {

async addXP(amount) {
this.user.xp += amount;
return this.save();
return await this.save();
}

async getLevel() {
Expand Down Expand Up @@ -278,7 +280,7 @@ class User {
}

await rod.save();
this.save();
await this.save();
return rod;
}

Expand Down Expand Up @@ -308,7 +310,7 @@ class User {
user.inventory.equippedBait = bait;
const baitObject = await ItemData.findById(bait?.valueOf()) || null;

this.save();
await this.save();
return baitObject;
}

Expand Down Expand Up @@ -340,7 +342,7 @@ class User {
// remove box from user inventory
if (box.count <= 0) {
user.inventory.gacha = user.inventory.gacha.filter((i) => i.valueOf() !== box.id);
this.save();
await this.save();
}

// check box capabilities to generate items
Expand Down Expand Up @@ -425,15 +427,16 @@ class User {
}
});

this.save();
await this.save();
}

async addCustomRodToInventory(rodId) {
(await this.getInventory()).rods.push(rodId);
return this.save();
return await this.save();
}

async sendToInventory(item, count = 1) {
async sendToInventory(item, count) {
if (!count) count = 1;
const user = this.user;
const userId = await this.getUserId();
let itemObject = await Item.findById(item);
Expand Down Expand Up @@ -470,6 +473,7 @@ class User {
break;
case 'fish':
clonedItem = await Utils.clone(itemObject, userId);
clonedItem.count = count;
user.inventory.fish.push(clonedItem);
finalItem = clonedItem;
newItemCount = clonedItem.count || 1;
Expand Down Expand Up @@ -530,7 +534,7 @@ class User {
break;
}
await finalItem.save();
this.save();
await await this.save();

return { item: finalItem, count: newItemCount };
}
Expand All @@ -552,15 +556,15 @@ class User {
await buff.save();

user.inventory.buffs = user.inventory.buffs.filter((b) => b.id !== buff.id);
return this.save();
return await this.save();
}

async updateLevel() {
const user = this.user;
const level = await this.getLevel();
const oldLevel = user.level;
user.level = level;
this.save();
await this.save();

return oldLevel < level;
}
Expand All @@ -572,7 +576,7 @@ class User {
async setLastVoted() {
const user = this.user;
user.stats.lastVoted = Date.now();
return this.save();
return await this.save();
}

async vote() {
Expand Down

0 comments on commit c19747c

Please sign in to comment.