Skip to content

Commit

Permalink
Minor battle fixes
Browse files Browse the repository at this point in the history
Reorganized battle event tags
Issue with heal and floating point error
Removed special gem from lb
admin command to add all weapon possibilities
  • Loading branch information
ChristopherBThai committed May 15, 2023
1 parent fd016c7 commit 84d0702
Show file tree
Hide file tree
Showing 23 changed files with 337 additions and 77 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ let clusters = 60;
lastShardID = parseInt(result['lastShardID']);
}
if (debug) {
shards = 2;
shards = 1;
firstShardID = 0;
lastShardID = 1;
clusters = 2;
lastShardID = 0;
clusters = 1;
}

console.log(
Expand Down
156 changes: 156 additions & 0 deletions src/commands/commandList/admin/addAllWeapons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* OwO Bot for Discord
* Copyright (C) 2023 Christopher Thai
* This software is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
* For more information, see README.md and LICENSE
*/

const CommandInterface = require('../../CommandInterface.js');
const WeaponInterface = require('../battle/WeaponInterface.js');
const weaponUtil = require('../battle/util/weaponUtil.js');

module.exports = new CommandInterface({
alias: ['addallweapons'],

owner: true,

execute: async function (p) {
const id = p.args[0];
let stat = p.args[1];
const uid = await p.global.getUid(id);

if (!p.global.isInt(stat)) {
return p.errorMsg(', stat not an integer');
}
stat = parseInt(stat);

if (stat < 0 || stat > 100) {
return p.errorMsg(', stat is invalid');
}

if (!uid) {
return p.errorMsg(', invalid user id');
}
let sql = `SELECT uw.uwid, uw.wid, uwp.wpid
FROM user_weapon uw
LEFT JOIN user_weapon_passive uwp ON uw.uwid = uwp.uwid
WHERE uid = ${uid} AND avg = ${stat};`;
let result = await p.query(sql);

let existingWeapons = {};
result.forEach((weapon) => {
if (!existingWeapons[weapon.uwid]) {
existingWeapons[weapon.uwid] = {
wid: weapon.wid,
wpid: [],
};
}
if (weapon.wpid) {
existingWeapons[weapon.uwid].wpid.push(weapon.wpid);
}
});
let formattedWeapons = {};
for (let i in existingWeapons) {
const weapon = existingWeapons[i];
if (!formattedWeapons[weapon.wid]) {
formattedWeapons[weapon.wid] = [];
}
formattedWeapons[weapon.wid].push(weapon.wpid.join(','));
}

const allWeapons = getAllWeapons();

addMissingWeapons.bind(this)(formattedWeapons, allWeapons, stat, uid);
},
});

function getAllWeapons() {
const weapons = {};
for (let wid in WeaponInterface.weapons) {
const weapon = new WeaponInterface.weapons[wid](null, null, true);
if (weapon.passiveCount && weapon.availablePassives.length) {
weapons[wid] = getAllPassives(weapon.passiveCount, weapon.availablePassives);
} else {
weapons[wid] = [];
}
}
return weapons;
}

function getAllPassives(count, passives) {
let result = [];
if (count <= 0) {
return result;
}
const prev = getAllPassives(count - 1, passives);
passives.forEach((passive) => {
if (prev.length) {
prev.forEach((prevPassive) => {
result.push(prevPassive + ',' + passive);
});
} else {
result.push(passive);
}
});

return result;
}

async function addMissingWeapons(existingWeapons, allWeapons, stat, uid) {
for (let allWid in allWeapons) {
let allPassives = allWeapons[allWid];
if (allPassives.length === 0) {
if (!existingWeapons[allWid]) {
await addWeapon.bind(this)(allWid, [], stat, uid);
}
} else {
let existingPassives =
existingWeapons[allWid]?.map((passives) => {
return passives
.split(',')
.sort((a, b) => parseInt(a) - parseInt(b))
.join(',');
}) || [];
for (let i in allPassives) {
let allPassive = allPassives[i]
.split(',')
.sort((a, b) => parseInt(a) - parseInt(b))
.join(',');
if (!existingPassives.includes(allPassive)) {
let passives = allPassive.split(',');
passives = passives.map((passive) => {
return parseInt(passive);
});
await addWeapon.bind(this)(allWid, passives, stat, uid);
}
}
}
}
}

async function addWeapon(wid, passives, stat, uid) {
console.log(`Adding weapon ${wid}: ${passives}`);
const weapon = new WeaponInterface.weapons[wid](null, null, null, {
passives,
statOverride: stat,
});
const sql = weaponUtil.toSql(uid, weapon);

let result = await this.query(sql.weapon);
let uwid = result.insertId;
if (!uwid) {
this.errorMsg(', Uh oh. Something went wrong! The weapon passive could not be applied');
console.error('Unable to add weapon passive to: ' + uwid);
} else if (passives.length) {
let uwidList = [];
for (let i = 0; i < passives.length; i++) uwidList.push(uwid);
await this.query(sql.passive, uwidList);
}
await delay(500);
}

function delay(ms) {
return new Promise((res) => {
setTimeout(res, ms);
});
}
11 changes: 7 additions & 4 deletions src/commands/commandList/battle/PassiveInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

const ranks = [0.2, 0.2, 0.2, 0.2, 0.14, 0.05, 0.01];
module.exports = class PassiveInterface {
constructor(qualities, noCreate) {
constructor(qualities, noCreate, opt = {}) {
this.init();
if (noCreate) return;

if (!qualities) qualities = this.randomQualities();
/* Overrides */
const statOverride = opt.statOverride;

if (!qualities) qualities = this.randomQualities(statOverride);

let avgQuality = qualities.reduce((a, b) => a + b, 0) / qualities.length;
let emoji = this.getEmoji(avgQuality);
Expand All @@ -34,10 +37,10 @@ module.exports = class PassiveInterface {
this.sqlStat = qualities.join(',');
}

randomQualities() {
randomQualities(statOverride) {
let qualities = [];
for (let i = 0; i < this.qualityList.length; i++)
qualities.push(Math.trunc(Math.random() * 101));
qualities.push(statOverride || Math.trunc(Math.random() * 101));
return qualities;
}

Expand Down
64 changes: 53 additions & 11 deletions src/commands/commandList/battle/WeaponInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/* eslint-disable no-unused-vars */

const Logs = require('./util/logUtil.js');
const Tags = require('./util/tags.js');
const requireDir = require('require-dir');
const ranks = [
[0.2, 'Common', '<:common:416520037713838081>'],
Expand All @@ -20,7 +21,7 @@ const ranks = [

module.exports = class WeaponInterface {
/* Constructor */
constructor(cpassives, qualities, noCreate) {
constructor(cpassives, qualities, noCreate, opt = {}) {
this.init();
if (this.availablePassives === 'all') {
this.availablePassives = [];
Expand All @@ -44,9 +45,13 @@ module.exports = class WeaponInterface {
/* Mana will also have a quality (always last in quality array) */
if (this.manaRange) this.qualityList.push(this.manaRange);

/* Overrides */
const statOverride = opt.statOverride;
if (opt.passives) cpassives = this.determinedPassives(opt.passives, statOverride);

/* Get random vars if not present */
if (!cpassives) cpassives = this.randomPassives();
if (!qualities) qualities = this.randomQualities();
if (!cpassives) cpassives = this.randomPassives(statOverride);
if (!qualities) qualities = this.randomQualities(statOverride);

/* Construct stats */
let stats = this.toStats(qualities);
Expand Down Expand Up @@ -113,7 +118,7 @@ module.exports = class WeaponInterface {
}

/* Grabs a random passive(s) */
randomPassives() {
randomPassives(statOverride) {
let randPassives = [];
for (let i = 0; i < this.passiveCount; i++) {
let rand = Math.floor(Math.random() * this.availablePassives.length);
Expand All @@ -123,16 +128,24 @@ module.exports = class WeaponInterface {
throw (
'Could not get passive[' + this.availablePassives[rand] + '] for weapon[' + this.id + ']'
);
randPassives.push(new passive());
randPassives.push(new passive(null, null, { statOverride }));
}
return randPassives;
}

/* Grab predetermined passive(s) */
determinedPassives(passiveList, statOverride) {
return passiveList.map((pid) => {
let passive = passives[pid];
return new passive(null, null, { statOverride });
});
}

/* Inits random qualities */
randomQualities() {
randomQualities(statOverride) {
let qualities = [];
for (let i = 0; i < this.qualityList.length; i++)
qualities.push(Math.trunc(Math.random() * 101));
qualities.push(statOverride || Math.trunc(Math.random() * 101));
return qualities;
}

Expand Down Expand Up @@ -191,6 +204,13 @@ module.exports = class WeaponInterface {

/* Uses mana */
static useMana(me, amount, from, tags) {
if (!(tags instanceof Tags)) {
tags = new Tags({
me: tags.me,
allies: tags.allies,
enemies: tags.enemies,
});
}
let logs = new Logs();

me.stats.wp[0] -= amount;
Expand Down Expand Up @@ -287,7 +307,14 @@ module.exports = class WeaponInterface {
}

/* Deals damage to an opponent */
static inflictDamage(attacker, attackee, damage, type, tags = {}) {
static inflictDamage(attacker, attackee, damage, type, tags) {
if (!(tags instanceof Tags)) {
tags = new Tags({
me: tags.me,
allies: tags.allies,
enemies: tags.enemies,
});
}
let totalDamage = 0;
if (type == WeaponInterface.PHYSICAL)
totalDamage = damage * (1 - WeaponInterface.resToPercent(attackee.stats.pr));
Expand Down Expand Up @@ -344,7 +371,14 @@ module.exports = class WeaponInterface {
}

/* heals */
static heal(me, amount, from, tags = {}) {
static heal(me, amount, from, tags) {
if (!(tags instanceof Tags)) {
tags = new Tags({
me: tags.me,
allies: tags.allies,
enemies: tags.enemies,
});
}
let max = me.stats.hp[1] + me.stats.hp[3];
/* Full health */
if (!me || me.stats.hp[0] >= max) return { amount: 0 };
Expand Down Expand Up @@ -401,7 +435,15 @@ module.exports = class WeaponInterface {
}

/* replenishes mana*/
static replenish(me, amount, from, tags = {}) {
static replenish(me, amount, from, tags) {
if (!(tags instanceof Tags)) {
tags = new Tags({
me: tags.me,
allies: tags.allies,
enemies: tags.enemies,
});
}

let max = me.stats.wp[1] + me.stats.wp[3];
/* Full mana */
if (!me || me.stats.wp[0] >= max) return { amount: 0 };
Expand Down Expand Up @@ -567,7 +609,7 @@ module.exports = class WeaponInterface {
/* Check if the animal is at max or higher health */
static isMaxHp(animal) {
let hp = animal.stats.hp;
return hp[0] >= hp[1] + hp[3];
return hp[0] + 1 >= hp[1] + hp[3];
}

/* Check if the animal is at max or higher health */
Expand Down
14 changes: 9 additions & 5 deletions src/commands/commandList/battle/buffs/flame.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ module.exports = class Flame extends BuffInterface {

// Override
bind(animal, duration, tags) {
if (tags.flame) return;
if (tags.has('flame', animal)) return;
let logs = new Logs();
for (let i in animal.buffs) {
if (animal.buffs[i].id == this.id && !animal.buffs[i].markedForDeath) {
animal.buffs[i].markedForDeath = true;
let damage = WeaponInterface.getDamage(this.from.stats.mag, this.stats[1] / 100);
damage = WeaponInterface.inflictDamage(this.from, animal, damage, WeaponInterface.MAGICAL, {
...tags,
flame: true,
});
tags.add('flame', animal);
damage = WeaponInterface.inflictDamage(
this.from,
animal,
damage,
WeaponInterface.MAGICAL,
tags
);
logs.push(
`[FLAME] Exploded and damaged ${animal.nickname} for ${damage.amount} HP`,
damage.logs
Expand Down
2 changes: 1 addition & 1 deletion src/commands/commandList/battle/passives/Energize.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = class Energize extends PassiveInterface {
'<:lwgen:621558018017853441>',
'<:fwgen:621558018424700948>',
];
this.statDesc = 'Replenish **?** ' + WeaponInterface.wpEmoji + 'WP after every turn';
this.statDesc = `Replenish **?** ${WeaponInterface.wpEmoji}WP after every turn`;
this.qualityList = [[20, 40]];
}

Expand Down
Loading

0 comments on commit 84d0702

Please sign in to comment.