Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing bug with huntbot giving incorrect xp to pets on both teams #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/commands/commandList/battle/util/battleUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const imagegenAuth = require('../../../../../../tokens/imagegen.json');
const request = require('request');
const crateUtil = require('./crateUtil.js');
const alterBattle = require('../../patreon/alterBattle.js');
const petUtil = require('./petUtil.js');

const maxAnimals = 6;
const attack = '👊🏼';
Expand Down Expand Up @@ -988,11 +989,16 @@ async function finishBattle(msg,p,battle,color,text,playerWin,enemyWin,logs,sett
/* An error occured */
if(!playerWin&&!enemyWin) return;

await teamUtil.giveXP(p,battle.player,pXP);
// update player streak
await teamUtil.updateStreak(p, battle.player, pXP.addStreak, pXP.resetStreak);

// give xp to player teams
await petUtil.giveXp(p, null, {base: pXP.xp, bonus: pXP.bonus, inactiveHalf: false});

// give xp to enemy team
await petUtil.giveXp(p, battle.enemy, {base: eXP.xp, bonus: eXP.bonus});
/* xp quest */
p.quest("xp",pXP.total);

await teamUtil.giveXP(p,battle.enemy,eXP.xp);
}


Expand Down
98 changes: 98 additions & 0 deletions src/commands/commandList/battle/util/petUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,101 @@ exports.getDisplay = function(p,animals){

return {embed};
}

exports.giveXp = async function(p,team,xp) {
let sql = '';
let xpMapping = {};
if (team) {
// battle - oppposing team

// get highest level
let highestLvl = 1;
for (let i in team.team) {
if(team.team[i].stats.lvl > highestLvl) highestLvl = team.team[i].stats.lvl;
}

//full xp with bonus xp (will be level diff only, already calculated)
let xpGain = xp.base + xp.bonus;

// calculate xp per pet on team
for (let i in team.team) {
// catch up multiplier gets applied
let lvl = team.team[i].stats.lvl;

let multiplier = lvl < highestLvl ? 2 + ((highestLvl-lvl)/10) : 1;
// hard cap at 10
if (multiplier > 10) multiplier = 10;

xpMapping[team.team[i].pid] = Math.round(multiplier * xpGain);
}

}
else {
// something done by the user (battling, hunting, huntbot), give exp to all pets on teams

// find and map all the user's pets on teams and whether team is active or not
sql = `SELECT ANIMAL.pid, ANIMAL.xp, PET_TEAM.pgid, CASE WHEN PET_TEAM_ACTIVE.PGID IS NOT NULL THEN 1 ELSE 0 END AS active FROM USER INNER JOIN PET_TEAM TEAM ON USER.UID = TEAM.UID INNER JOIN PET_TEAM_ANIMAL PET_TEAM ON TEAM.PGID = PET_TEAM.PGID INNER JOIN ANIMAL ON PET_TEAM.PID = ANIMAL.PID LEFT JOIN pet_team_active PET_TEAM_ACTIVE ON PET_TEAM.PGID = PET_TEAM_ACTIVE.PGID WHERE USER.ID = ${p.msg.author.id} ORDER BY ACTIVE DESC`;
let results = await p.query(sql);
let teams = {};
results.forEach(animal => {
if (teams[animal.pgid]) {
teams[animal.pgid].animals.push({
id: animal.pid,
lvl: animalUtil.toLvl(animal.xp).lvl
});
}
else {
teams[animal.pgid] = {
active: animal.active,
animals: [{
id: animal.pid,
lvl: animalUtil.toLvl(animal.xp).lvl
}]
};
}
});

// assign exp per team
for (let id in teams) {
let team = teams[id];

// get highest level
let highestLvl = 1;
for (let i in team.animals) {
if(team.animals[i].lvl > highestLvl) highestLvl = team.animals[i].lvl;
}

// assign xp
team.animals.forEach(animal => {
if (xpMapping[animal.id]) return; // skip, already mapped

let xpGain = xp.base;
if (xp.inactiveHalf) {
// hunting, inactive team gets half (no bonus xp)
if (!team.active) xpGain = xpGain / 2;
}
else {
// battling, active team gets bonus xp
if (team.active) xpGain = xpGain + xp.bonus;

// all teams get catch up multiplier applied
let multiplier = animal.lvl < highestLvl ? 2 + ((highestLvl-animal.lvl)/10) : 1;
// hard cap at 10
if (multiplier > 10) multiplier = 10;

xpGain = xpGain * multiplier;
}
xpMapping[animal.id] = Math.round(xpGain);
});
}
}

// build out query
let cases = '';
for (let id in xpMapping) {
cases += ` WHEN animal.pid = ${id} THEN ${xpMapping[id]}`;
}
sql = `UPDATE IGNORE animal SET xp = xp + (CASE ${cases} ELSE 0 END) WHERE pid IN (${Object.keys(xpMapping)})`;

return await p.query(sql);
}
42 changes: 3 additions & 39 deletions src/commands/commandList/battle/util/teamUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,48 +386,12 @@ exports.isDead = function(team){
return totalhp<=0;
}

/* Distributes xp to team */
exports.giveXP = async function(p,team,xp){
let isInt = p.global.isInt(xp);
let total = (isInt)?xp:xp.total;
let addStreak = (isInt)?false:xp.addStreak;
let resetStreak = (isInt)?false:xp.resetStreak;

let highestLvl = 1;
for(let i in team.team){
let lvl = team.team[i].stats.lvl;
if(lvl >highestLvl)
highestLvl = lvl;
}

let cases = '';
for(let i in team.team){
let mult = 1;
let lvl = team.team[i].stats.lvl;
if(lvl < highestLvl)
mult = 2 + ((highestLvl-lvl)/10)
if(mult>10) mult = 10;
cases += ` WHEN animal.pid = ${team.team[i].pid} THEN ${Math.round(total*mult)}`;
}

/* updates streak for team */
exports.updateStreak = async function(p, team, addStreak, resetStreak) {
let sql = '';
if (isInt) {
sql = `UPDATE IGNORE pet_team
INNER JOIN pet_team_animal ON pet_team.pgid = pet_team_animal.pgid
INNER JOIN animal ON pet_team_animal.pid = animal.pid
SET animal.xp = animal.xp + (CASE ${cases} ELSE ${Math.round(total/2)} END)
WHERE pet_team.pgid = ${team.pgid};`;
} else {
sql = `UPDATE IGNORE user
INNER JOIN pet_team ON user.uid = pet_team.uid
INNER JOIN pet_team_animal ON pet_team.pgid = pet_team_animal.pgid
INNER JOIN animal ON pet_team_animal.pid = animal.pid
SET animal.xp = animal.xp + (CASE ${cases} ELSE ${Math.round(xp.xp/2)} END)
WHERE user.id = ${p.msg.author.id};`;
}

if(addStreak) sql += `UPDATE pet_team SET highest_streak = IF(streak+1>highest_streak,streak+1,highest_streak), streak = streak + 1 WHERE pgid = ${team.pgid};`;
if(resetStreak) sql += `UPDATE pet_team SET streak = 0 WHERE pgid = ${team.pgid};`;

return await p.query(sql);
}
19 changes: 5 additions & 14 deletions src/commands/commandList/zoo/autohunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const botrank = "SELECT (COUNT(*)) AS rank, (SELECT COUNT(*) FROM autohunt) AS t
const logger = require('../../../utils/logger.js');
const animals = require('../../../../../tokens/owo-animals.json');
const parse = require('parse-duration');
const petUtil = require('../battle/util/petUtil.js');

module.exports = new CommandInterface({

Expand Down Expand Up @@ -74,22 +75,12 @@ async function claim(p,msg,con,query,bot){
if(result[0][0]&&result[0][0].patreon==1)
patreon = true;

sql = "";
//Get total exp
//Give xp to pets
let totalExp = Math.floor(autohuntutil.getLvl(query.exp,0,"exp").stat*duration);
sql += `UPDATE IGNORE user
INNER JOIN pet_team ON user.uid = pet_team.uid
INNER JOIN pet_team_animal ON pet_team.pgid = pet_team_animal.pgid
INNER JOIN animal ON pet_team_animal.pid = animal.pid
LEFT JOIN (SELECT pt2.pgid FROM user u2
INNER JOIN pet_team pt2 ON pt2.uid = u2.uid
LEFT JOIN pet_team_active pt_act ON pt2.pgid = pt_act.pgid
WHERE u2.id = ${msg.author.id}
ORDER BY pt_act.pgid DESC, pt2.pgid ASC LIMIT 1) tmp
ON tmp.pgid = pet_team.pgid
SET animal.xp = animal.xp + (CASE WHEN tmp.pgid IS NULL THEN ${Math.round(totalExp/2)} ELSE ${totalExp} END)
WHERE user.id = ${msg.author.id};`;
petUtil.giveXp(p, null, {base: totalExp, inactiveHalf: true});

sql = "";

//Get all animal
let total = {};
let digits = 1;
Expand Down
18 changes: 6 additions & 12 deletions src/commands/commandList/zoo/catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const dateUtil = require('../../../utils/dateUtil.js');
const gemUtil = require('./gemUtil.js');
const animalUtil = require('./animalUtil.js');
const alterHunt = require('./../patreon/alterHunt.js');
const petUtil = require('../battle/util/petUtil.js');
const lootboxChance = 0.05;

module.exports = new CommandInterface({
Expand Down Expand Up @@ -40,17 +41,12 @@ module.exports = new CommandInterface({
let msg=p.msg,con=p.con;

let sql = "SELECT money,IF(patreonAnimal = 1 OR (TIMESTAMPDIFF(MONTH,patreonTimer,NOW())<patreonMonths),1,0) as patreon FROM cowoncy LEFT JOIN user ON cowoncy.id = user.id LEFT JOIN patreons ON user.uid = patreons.uid WHERE cowoncy.id = "+msg.author.id+";";
sql += `SELECT name,nickname,animal.pid,MAX(tmp.pgid) AS active
sql += `SELECT name,nickname,animal.pid
FROM user u
INNER JOIN pet_team ON u.uid = pet_team.uid
INNER JOIN pet_team_animal ON pet_team.pgid = pet_team_animal.pgid
INNER JOIN animal ON pet_team_animal.pid = animal.pid
LEFT JOIN (SELECT pt2.pgid FROM user u2
INNER JOIN pet_team pt2 ON pt2.uid = u2.uid
LEFT JOIN pet_team_active pt_act ON pt2.pgid = pt_act.pgid
WHERE u2.id = ${p.msg.author.id}
ORDER BY pt_act.pgid DESC, pt2.pgid ASC LIMIT 1) tmp
ON tmp.pgid = pet_team.pgid
INNER JOIN pet_team_active ON pet_team.pgid = pet_team_active.pgid
WHERE u.id = ${p.msg.author.id}
GROUP BY animal.pid
ORDER BY pet_team_animal.pos ASC;`;
Expand Down Expand Up @@ -83,13 +79,11 @@ module.exports = new CommandInterface({
text += `\n${p.config.emoji.blank} **|** `;
petText = '';
for(let i in result[1]){
sql += `UPDATE animal SET xp = xp + ${result[1][i].active?animal.xp:Math.round(animal.xp/2)} WHERE pid = ${result[1][i].pid};`;
if (result[1][i].active) {
let pet = p.global.validAnimal(result[1][i].name);
petText += (pet.uni?pet.uni:pet.value)+" ";
}
let pet = p.global.validAnimal(result[1][i].name);
petText += (pet.uni?pet.uni:pet.value)+" ";
}
animalXp = animal.xp;
petUtil.giveXp(p, null, {base: animalXp, inactiveHalf: true});
text += `${petText}gained **${animalXp}xp**!`;
}

Expand Down