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

Incorrect passive emoji fix and cleaning #318

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
225 changes: 115 additions & 110 deletions src/commands/commandList/battle/PassiveInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,115 +5,120 @@
* For more information, see README.md and LICENSE
*/

const ranks = [0.2, 0.2, 0.2, 0.2, 0.14, 0.05, 0.01];
const ranks = [0.2, 0.4, 0.6, 0.8, 0.94, 0.99, 1];

module.exports = class PassiveInterface {
constructor(qualities, noCreate) {
this.init();
if (noCreate) return;

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

let avgQuality = qualities.reduce((a, b) => a + b, 0) / qualities.length;
let emoji = this.getEmoji(avgQuality);
let stats = this.toStats(qualities);

/* Construct desc */
let desc = this.statDesc;
for (var i = 0; i < stats.length; i++) {
desc = desc.replace('?', stats[i]);
}
/* Check if it has enough emojis */
if (this.emojis.length != 7) throw new Error(`[${args.id}] does not have 7 emojis`);

this.avgQuality = avgQuality;
this.qualities = qualities;
this.emoji = emoji;
this.stats = stats;
this.desc = desc;
this.sqlStat = qualities.join(',');
}

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

getEmoji(quality) {
/* If there are multiple quality, get avg */
if (typeof quality == 'string') {
quality = parseInt(quality.split(','));
quality = quality.reduce((a, b) => a + b, 0) / quality.length;
}

quality /= 100;

/* Get correct rank */
var count = 0;
for (var i = 0; i < ranks.length; i++) {
count += ranks[i];
if (quality <= count) return this.emojis[i];
}
return this.emojis[0];
}

toStats(qualities) {
if (qualities.length != this.qualityList.length)
throw new Error('Array size does not match in toStats. Passive id:' + this.id);
var stats = [];
for (var i = 0; i < qualities.length; i++) {
let quality = qualities[i];
if (quality > 100) quality = 100;
if (quality < 0) quality = 0;
let min = this.qualityList[i][0];
let max = this.qualityList[i][1];

/* rounds to 2 decimal places */
stats.push(Math.round((min + (max - min) * (quality / 100)) * 100) / 100);
}
return stats;
}

alterStats(stats) {}

static get getID() {
return new this(null, true).id;
}
static get disabled() {
return new this(null, true).disabled;
}

/* Before a turn executes */
preTurn(animal, ally, enemy, action) {}
/* After a turn executes */
postTurn(animal, ally, enemy, action) {}

/* If the passive owner is attacking*/
attack(animal, attackee, damage, type, last) {}
/* If the passive owner is attacked */
attacked(animal, attacker, damage, type, last) {}
/* If the passive owner is healing */
heal(animal, healer, amount, tag) {}
/* If the passive owner is healed */
healed(animal, healer, amount, tag) {}
/* If the passive owner is replenishing */
replenish(animal, healer, amount, tag) {}
/* If the passive owner is replenished */
replenished(animal, healer, amount, tag) {}
/* If the passive owner is attacking (after bonus damage) */
postAttack(animal, attackee, damage, type, last) {}
/* If the passive owner is attacked (after bonus damage) */
postAttacked(animal, attacker, damage, type, last) {}
/* If the passive owner is healing(after bonus heal) */
postHeal(animal, healer, amount, tag) {}
/* If the passive owner is healed (after bonus heal) */
postHealed(animal, healer, amount, tag) {}
/* If the passive owner is replenishing (after bonus heal) */
postReplenish(animal, healer, amount, tag) {}
/* If the passive owner is replenished (after bonus heal) */
postReplenished(animal, healer, amount, tag) {}

/* If the passive owner is allowed to attack */
canAttack(me, ally, enemy, action, result) {}
constructor(qualities, noCreate) {
this.init();
if (noCreate) { return; }

if (!qualities) { qualities = this.randomQualities(); }

const avgQuality = qualities.reduce((a, b) => a + b, 0) / qualities.length;
const emoji = this.getEmoji(avgQuality);
const stats = this.toStats(qualities);

/* Construct desc */
let desc = this.statDesc;
for (let i = 0; i < stats.length; i++) {
desc = desc.replace('?', stats[i]);
}
/* Check if it has enough emojis */
if (this.emojis.length !== 7) { throw new Error(`[${args.id}] does not have 7 emojis`); }

this.avgQuality = avgQuality;
this.qualities = qualities;
this.emoji = emoji;
this.stats = stats;
this.desc = desc;
this.sqlStat = qualities.join(',');
}

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

getEmoji(quality) {
/* If there are multiple quality, get avg */
if (typeof quality === 'string') {
quality = quality.split(',');
quality = quality.reduce((a, b) => parseInt(a) + parseInt(b), 0) / quality.length;
}

quality /= 100;

/* Get correct rank */
for (let rankIndex = 0; rankIndex < ranks.length; rankIndex++) {
const rankQuality = ranks[rankIndex];

if (quality <= rankQuality) {
return this.emojis[rankIndex];
}
}
return this.emojis[0];
}

toStats(qualities) {
if (qualities.length !== this.qualityList.length) {
throw new Error(
`Array size does not match in toStats. Passive id:${this.id}`
);
}
const stats = [];
for (let i = 0; i < qualities.length; i++) {
let quality = qualities[i];
if (quality > 100) { quality = 100; }
if (quality < 0) { quality = 0; }
const min = this.qualityList[i][0];
const max = this.qualityList[i][1];

/* rounds to 2 decimal places */
stats.push(Math.round((min + (max - min) * (quality / 100)) * 100) / 100);
}
return stats;
}

alterStats(stats) { }

static get getID() {
return new this(null, true).id;
}
static get disabled() {
return new this(null, true).disabled;
}

/* Before a turn executes */
preTurn(animal, ally, enemy, action) { }
/* After a turn executes */
postTurn(animal, ally, enemy, action) { }

/* If the passive owner is attacking*/
attack(animal, attackee, damage, type, last) { }
/* If the passive owner is attacked */
attacked(animal, attacker, damage, type, last) { }
/* If the passive owner is healing */
heal(animal, healer, amount, tag) { }
/* If the passive owner is healed */
healed(animal, healer, amount, tag) { }
/* If the passive owner is replenishing */
replenish(animal, healer, amount, tag) { }
/* If the passive owner is replenished */
replenished(animal, healer, amount, tag) { }
/* If the passive owner is attacking (after bonus damage) */
postAttack(animal, attackee, damage, type, last) { }
/* If the passive owner is attacked (after bonus damage) */
postAttacked(animal, attacker, damage, type, last) { }
/* If the passive owner is healing(after bonus heal) */
postHeal(animal, healer, amount, tag) { }
/* If the passive owner is healed (after bonus heal) */
postHealed(animal, healer, amount, tag) { }
/* If the passive owner is replenishing (after bonus heal) */
postReplenish(animal, healer, amount, tag) { }
/* If the passive owner is replenished (after bonus heal) */
postReplenished(animal, healer, amount, tag) { }

/* If the passive owner is allowed to attack */
canAttack(me, ally, enemy, action, result) { }
};