Common reorg (#8025)
* Re-organize common folder * fix: Correct paths in tests * fix: move new content to proper folder * chore: Move audio folder to assets * Move sprites to sprites assets directory * Move css sprites to assets directory * Split out readmes for common code and sprites * Move images to assets directory * Move destinatin of shared browserified file * remove unused file * move compiled js to client-old * Fix karma tests * fix: Correct paths for sprites
This commit is contained in:
committed by
Matteo Pagliazzi
parent
d971e673af
commit
81b7eeeb71
@@ -0,0 +1,76 @@
|
||||
import _ from 'lodash';
|
||||
import splitWhitespace from '../libs/splitWhitespace';
|
||||
|
||||
/*
|
||||
Updates user stats with new stats. Handles death, leveling up, etc
|
||||
{stats} new stats
|
||||
{update} if aggregated changes, pass in userObj as update. otherwise commits will be made immediately
|
||||
*/
|
||||
|
||||
function getStatToAllocate (user) {
|
||||
let suggested;
|
||||
|
||||
let statsObj = user.stats.toObject ? user.stats.toObject() : user.stats;
|
||||
|
||||
switch (user.preferences.allocationMode) {
|
||||
case 'flat': {
|
||||
let stats = _.pick(statsObj, splitWhitespace('con str per int'));
|
||||
return _.invert(stats)[_.min(stats)];
|
||||
}
|
||||
case 'classbased': {
|
||||
let preference;
|
||||
let lvlDiv7 = statsObj.lvl / 7;
|
||||
let ideal = [lvlDiv7 * 3, lvlDiv7 * 2, lvlDiv7, lvlDiv7];
|
||||
|
||||
switch (statsObj.class) {
|
||||
case 'wizard': {
|
||||
preference = ['int', 'per', 'con', 'str'];
|
||||
break;
|
||||
}
|
||||
case 'rogue': {
|
||||
preference = ['per', 'str', 'int', 'con'];
|
||||
break;
|
||||
}
|
||||
case 'healer': {
|
||||
preference = ['con', 'int', 'str', 'per'];
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
preference = ['str', 'con', 'per', 'int'];
|
||||
}
|
||||
}
|
||||
|
||||
let diff = [
|
||||
statsObj[preference[0]] - ideal[0],
|
||||
statsObj[preference[1]] - ideal[1],
|
||||
statsObj[preference[2]] - ideal[2],
|
||||
statsObj[preference[3]] - ideal[3],
|
||||
];
|
||||
|
||||
suggested = _.findIndex(diff, (val) => {
|
||||
if (val === _.min(diff)) return true;
|
||||
});
|
||||
|
||||
return suggested !== -1 ? preference[suggested] : 'str';
|
||||
}
|
||||
case 'taskbased': {
|
||||
suggested = _.invert(statsObj.training)[_.max(statsObj.training)];
|
||||
|
||||
user.stats.training.str = 0;
|
||||
user.stats.training.int = 0;
|
||||
user.stats.training.con = 0;
|
||||
user.stats.training.per = 0;
|
||||
|
||||
return suggested || 'str';
|
||||
}
|
||||
default: {
|
||||
return 'str';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function autoAllocate (user) {
|
||||
let statToIncrease = getStatToAllocate(user);
|
||||
|
||||
return user.stats[statToIncrease]++;
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
import predictableRandom from './predictableRandom';
|
||||
|
||||
module.exports = function crit (user, stat = 'str', chance = 0.03) {
|
||||
let s = user._statsComputed[stat];
|
||||
if (predictableRandom(user) <= chance * (1 + s / 100)) {
|
||||
return 1.5 + 4 * s / (s + 200);
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
// TODO remove completely, use _.get, only used in client
|
||||
|
||||
module.exports = function dotGet (user, path) {
|
||||
return _.get(user, path);
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
/*
|
||||
This allows you to set object properties by dot-path. Eg, you can run pathSet('stats.hp',50,user) which is the same as
|
||||
user.stats.hp = 50. This is useful because in our habitrpg-shared functions we're returning changesets as {path:value},
|
||||
so that different consumers can implement setters their own way. Derby needs model.set(path, value) for example, where
|
||||
Angular sets object properties directly - in which case, this function will be used.
|
||||
*/
|
||||
|
||||
// TODO use directly _.set and remove this fn, only used in client
|
||||
|
||||
module.exports = function dotSet (user, path, val) {
|
||||
return _.set(user, path, val);
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
import content from '../content/index';
|
||||
import i18n from '../i18n';
|
||||
|
||||
module.exports = function handleTwoHanded (user, item, type = 'equipped', req = {}) {
|
||||
let currentShield = content.gear.flat[user.items.gear[type].shield];
|
||||
let currentWeapon = content.gear.flat[user.items.gear[type].weapon];
|
||||
|
||||
let message;
|
||||
|
||||
if (item.type === 'shield' && (currentWeapon ? currentWeapon.twoHanded : false)) {
|
||||
user.items.gear[type].weapon = 'weapon_base_0';
|
||||
message = i18n.t('messageTwoHandedUnequip', {
|
||||
twoHandedText: currentWeapon.text(req.language), offHandedText: item.text(req.language),
|
||||
}, req.language);
|
||||
} else if (item.twoHanded && (currentShield && user.items.gear[type].shield !== 'shield_base_0')) {
|
||||
user.items.gear[type].shield = 'shield_base_0';
|
||||
message = i18n.t('messageTwoHandedEquip', {
|
||||
twoHandedText: item.text(req.language), offHandedText: currentShield.text(req.language),
|
||||
}, req.language);
|
||||
}
|
||||
|
||||
return message;
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
import handleTwoHanded from './handleTwoHanded';
|
||||
import predictableRandom from './predictableRandom';
|
||||
import crit from './crit';
|
||||
import randomVal from './randomVal';
|
||||
import dotSet from './dotSet';
|
||||
import dotGet from './dotGet';
|
||||
import randomDrop from './randomDrop';
|
||||
import autoAllocate from './autoAllocate';
|
||||
import updateStats from './updateStats';
|
||||
import ultimateGear from './ultimateGear';
|
||||
import nullify from './nullify';
|
||||
|
||||
module.exports = {
|
||||
handleTwoHanded,
|
||||
predictableRandom,
|
||||
crit,
|
||||
randomVal,
|
||||
dotSet,
|
||||
dotGet,
|
||||
randomDrop,
|
||||
autoAllocate,
|
||||
updateStats,
|
||||
ultimateGear,
|
||||
nullify,
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
// TODO remove once v2 is retired
|
||||
|
||||
module.exports = function nullify (user) {
|
||||
user.ops = null;
|
||||
user.fns = null;
|
||||
user = null;
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
// Because the same op needs to be performed on the client and the server (critical hits, item drops, etc),
|
||||
// we need things to be "random", but technically predictable so that they don't go out-of-sync
|
||||
|
||||
module.exports = function predictableRandom (user, seed) {
|
||||
if (!seed || seed === Math.PI) {
|
||||
let stats = user.stats.toObject ? user.stats.toObject() : user.stats;
|
||||
// These items are not part of the stat object but exists on the server (see controllers/user#getUser)
|
||||
// we remove them in order to use the same user.stats both on server and on client
|
||||
stats = _.omit(stats, 'toNextLevel', 'maxHealth', 'maxMP');
|
||||
|
||||
seed = _.reduce(stats, (accumulator, val) => {
|
||||
if (_.isNumber(val)) {
|
||||
return accumulator + val;
|
||||
} else {
|
||||
return accumulator;
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
let x = Math.sin(seed++) * 10000;
|
||||
return x - Math.floor(x);
|
||||
};
|
||||
@@ -0,0 +1,113 @@
|
||||
import _ from 'lodash';
|
||||
import content from '../content/index';
|
||||
import i18n from '../i18n';
|
||||
import { daysSince } from '../cron';
|
||||
import { diminishingReturns } from '../statHelpers';
|
||||
import _predictableRandom from './predictableRandom';
|
||||
import randomVal from './randomVal';
|
||||
|
||||
// Clone a drop object maintaining its functions so that we can change it without affecting the original item
|
||||
function cloneDropItem (drop) {
|
||||
return _.cloneDeep(drop, (val) => {
|
||||
return _.isFunction(val) ? val : undefined; // undefined will be handled by lodash
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = function randomDrop (user, options, req = {}) {
|
||||
let acceptableDrops;
|
||||
let chance;
|
||||
let drop;
|
||||
let dropMultiplier;
|
||||
let rarity;
|
||||
let task;
|
||||
|
||||
let predictableRandom = options.predictableRandom || _predictableRandom;
|
||||
task = options.task;
|
||||
|
||||
chance = _.min([Math.abs(task.value - 21.27), 37.5]) / 150 + 0.02;
|
||||
chance *= task.priority * // Task priority: +50% for Medium, +100% for Hard
|
||||
(1 + (task.streak / 100 || 0)) * // Streak bonus: +1% per streak
|
||||
(1 + user._statsComputed.per / 100) * // PERception: +1% per point
|
||||
(1 + (user.contributor.level / 40 || 0)) * // Contrib levels: +2.5% per level
|
||||
(1 + (user.achievements.rebirths / 20 || 0)) * // Rebirths: +5% per achievement
|
||||
(1 + (user.achievements.streak / 200 || 0)) * // Streak achievements: +0.5% per achievement
|
||||
(user._tmp.crit || 1) * (1 + 0.5 * (_.reduce(task.checklist, (m, i) => {
|
||||
return m + (i.completed ? 1 : 0); // +50% per checklist item complete. TODO: make this into X individual drop chances instead
|
||||
}, 0) || 0));
|
||||
chance = diminishingReturns(chance, 0.75);
|
||||
|
||||
if (predictableRandom(user, user.stats.gp) < chance) {
|
||||
if (!user.party.quest.progress.collectedItems) user.party.quest.progress.collectedItems = 0;
|
||||
user.party.quest.progress.collectedItems++;
|
||||
user.markModified('party.quest.progress');
|
||||
}
|
||||
|
||||
if (user.purchased && user.purchased.plan && user.purchased.plan.customerId) {
|
||||
dropMultiplier = 2;
|
||||
} else {
|
||||
dropMultiplier = 1;
|
||||
}
|
||||
|
||||
if (daysSince(user.items.lastDrop.date, user.preferences) === 0 &&
|
||||
user.items.lastDrop.count >= dropMultiplier * (5 + Math.floor(user._statsComputed.per / 25) + (user.contributor.level || 0))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (user.flags && user.flags.dropsEnabled && predictableRandom(user, user.stats.exp) < chance) {
|
||||
rarity = predictableRandom(user, user.stats.gp);
|
||||
|
||||
if (rarity > 0.6) { // food 40% chance
|
||||
drop = cloneDropItem(randomVal(user, _.where(content.food, {
|
||||
canDrop: true,
|
||||
})));
|
||||
|
||||
if (!user.items.food[drop.key]) {
|
||||
user.items.food[drop.key] = 0;
|
||||
}
|
||||
user.items.food[drop.key] += 1;
|
||||
drop.type = 'Food';
|
||||
drop.dialog = i18n.t('messageDropFood', {
|
||||
dropArticle: drop.article,
|
||||
dropText: drop.text(req.language),
|
||||
dropNotes: drop.notes(req.language),
|
||||
}, req.language);
|
||||
} else if (rarity > 0.3) { // eggs 30% chance
|
||||
drop = cloneDropItem(randomVal(user, content.dropEggs));
|
||||
if (!user.items.eggs[drop.key]) {
|
||||
user.items.eggs[drop.key] = 0;
|
||||
}
|
||||
user.items.eggs[drop.key]++;
|
||||
drop.type = 'Egg';
|
||||
drop.dialog = i18n.t('messageDropEgg', {
|
||||
dropText: drop.text(req.language),
|
||||
dropNotes: drop.notes(req.language),
|
||||
}, req.language);
|
||||
} else { // Hatching Potion, 30% chance - break down by rarity.
|
||||
if (rarity < 0.02) { // Very Rare: 10% (of 30%)
|
||||
acceptableDrops = ['Golden'];
|
||||
} else if (rarity < 0.09) { // Rare: 20% of 30%
|
||||
acceptableDrops = ['Zombie', 'CottonCandyPink', 'CottonCandyBlue'];
|
||||
} else if (rarity < 0.18) { // uncommon: 30% of 30%
|
||||
acceptableDrops = ['Red', 'Shade', 'Skeleton'];
|
||||
} else { // common, 40% of 30%
|
||||
acceptableDrops = ['Base', 'White', 'Desert'];
|
||||
}
|
||||
drop = cloneDropItem(randomVal(user, _.pick(content.hatchingPotions, (v, k) => {
|
||||
return acceptableDrops.indexOf(k) >= 0;
|
||||
})));
|
||||
if (!user.items.hatchingPotions[drop.key]) {
|
||||
user.items.hatchingPotions[drop.key] = 0;
|
||||
}
|
||||
user.items.hatchingPotions[drop.key]++;
|
||||
drop.type = 'HatchingPotion';
|
||||
drop.dialog = i18n.t('messageDropPotion', {
|
||||
dropText: drop.text(req.language),
|
||||
dropNotes: drop.notes(req.language),
|
||||
}, req.language);
|
||||
}
|
||||
|
||||
user._tmp.drop = drop;
|
||||
user.items.lastDrop.date = Number(new Date());
|
||||
user.items.lastDrop.count++;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
import _ from 'lodash';
|
||||
import predictableRandom from './predictableRandom';
|
||||
|
||||
// Get a random property from an object
|
||||
// returns random property (the value)
|
||||
|
||||
module.exports = function randomVal (user, obj, options = {}) {
|
||||
let array = options.key ? _.keys(obj) : _.values(obj);
|
||||
let rand = predictableRandom(user, options.seed);
|
||||
array.sort();
|
||||
return array[Math.floor(rand * array.length)];
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
import _ from 'lodash';
|
||||
import content from '../content/index';
|
||||
|
||||
module.exports = function resetGear (user) {
|
||||
let gear = user.items.gear;
|
||||
|
||||
_.each(['equipped', 'costume'], function resetUserGear (type) {
|
||||
gear[type] = {};
|
||||
gear[type].armor = 'armor_base_0';
|
||||
gear[type].weapon = 'weapon_warrior_0';
|
||||
gear[type].head = 'head_base_0';
|
||||
gear[type].shield = 'shield_base_0';
|
||||
});
|
||||
|
||||
// Gear.owned is a Mongo object so the _.each function iterates over hidden properties.
|
||||
// The content.gear.flat[k] check should prevent this causing an error
|
||||
_.each(gear.owned, function resetOwnedGear (v, k) {
|
||||
if (gear.owned[k] && content.gear.flat[k] && content.gear.flat[k].value) {
|
||||
gear.owned[k] = false;
|
||||
}
|
||||
});
|
||||
|
||||
gear.owned.weapon_warrior_0 = true; // eslint-disable-line camelcase
|
||||
user.preferences.costume = false;
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
import content from '../content/index';
|
||||
import _ from 'lodash';
|
||||
|
||||
module.exports = function ultimateGear (user) {
|
||||
let owned = typeof window !== 'undefined' ? user.items.gear.owned : user.items.gear.owned.toObject();
|
||||
|
||||
content.classes.forEach((klass) => {
|
||||
if (user.achievements.ultimateGearSets[klass] !== true) {
|
||||
user.achievements.ultimateGearSets[klass] = _.reduce(['armor', 'shield', 'head', 'weapon'], (soFarGood, type) => {
|
||||
let found = _.find(content.gear.tree[type][klass], {
|
||||
last: true,
|
||||
});
|
||||
return soFarGood && (!found || owned[found.key] === true);
|
||||
}, true);
|
||||
|
||||
if (user.achievements.ultimateGearSets[klass] === true) {
|
||||
user.addNotification('ULTIMATE_GEAR_ACHIEVEMENT');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let ultimateGearSetValues;
|
||||
if (user.achievements.ultimateGearSets.toObject) {
|
||||
ultimateGearSetValues = Object.values(user.achievements.ultimateGearSets.toObject());
|
||||
} else {
|
||||
ultimateGearSetValues = Object.values(user.achievements.ultimateGearSets);
|
||||
}
|
||||
|
||||
let hasFullSet = _.includes(ultimateGearSetValues, true);
|
||||
|
||||
if (hasFullSet && user.flags.armoireEnabled !== true) {
|
||||
user.flags.armoireEnabled = true;
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
@@ -0,0 +1,101 @@
|
||||
import _ from 'lodash';
|
||||
import {
|
||||
MAX_HEALTH,
|
||||
MAX_STAT_POINTS,
|
||||
} from '../constants';
|
||||
import { toNextLevel } from '../statHelpers';
|
||||
import autoAllocate from './autoAllocate';
|
||||
|
||||
module.exports = function updateStats (user, stats, req = {}, analytics) {
|
||||
let allocatedStatPoints;
|
||||
let totalStatPoints;
|
||||
let experienceToNextLevel;
|
||||
|
||||
user.stats.hp = stats.hp > 0 ? stats.hp : 0;
|
||||
user.stats.gp = stats.gp > 0 ? stats.gp : 0;
|
||||
if (!user._tmp) user._tmp = {};
|
||||
|
||||
experienceToNextLevel = toNextLevel(user.stats.lvl);
|
||||
|
||||
if (stats.exp >= experienceToNextLevel) {
|
||||
user.stats.exp = stats.exp;
|
||||
|
||||
while (stats.exp >= experienceToNextLevel) {
|
||||
stats.exp -= experienceToNextLevel;
|
||||
user.stats.lvl++;
|
||||
|
||||
experienceToNextLevel = toNextLevel(user.stats.lvl);
|
||||
user.stats.hp = MAX_HEALTH;
|
||||
allocatedStatPoints = user.stats.str + user.stats.int + user.stats.con + user.stats.per;
|
||||
totalStatPoints = allocatedStatPoints + user.stats.points;
|
||||
|
||||
if (totalStatPoints >= MAX_STAT_POINTS) {
|
||||
continue; // eslint-disable-line no-continue
|
||||
}
|
||||
if (user.preferences.automaticAllocation) {
|
||||
autoAllocate(user);
|
||||
} else {
|
||||
user.stats.points = user.stats.lvl - allocatedStatPoints;
|
||||
totalStatPoints = user.stats.points + allocatedStatPoints;
|
||||
|
||||
if (totalStatPoints > MAX_STAT_POINTS) {
|
||||
user.stats.points = MAX_STAT_POINTS - allocatedStatPoints;
|
||||
}
|
||||
|
||||
if (user.stats.points < 0) {
|
||||
user.stats.points = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
user.stats.exp = stats.exp;
|
||||
|
||||
if (!user.flags.customizationsNotification && (user.stats.exp > 5 || user.stats.lvl > 1)) {
|
||||
user.flags.customizationsNotification = true;
|
||||
}
|
||||
if (!user.flags.itemsEnabled && (user.stats.exp > 10 || user.stats.lvl > 1)) {
|
||||
user.flags.itemsEnabled = true;
|
||||
}
|
||||
if (!user.flags.dropsEnabled && user.stats.lvl >= 3) {
|
||||
user.flags.dropsEnabled = true;
|
||||
user.addNotification('DROPS_ENABLED');
|
||||
|
||||
if (user.items.eggs.Wolf > 0) {
|
||||
user.items.eggs.Wolf++;
|
||||
} else {
|
||||
user.items.eggs.Wolf = 1;
|
||||
}
|
||||
}
|
||||
_.each({
|
||||
vice1: 30,
|
||||
atom1: 15,
|
||||
moonstone1: 60,
|
||||
goldenknight1: 40,
|
||||
}, (lvl, k) => {
|
||||
if (user.stats.lvl >= lvl && !user.flags.levelDrops[k]) {
|
||||
user.flags.levelDrops[k] = true;
|
||||
if (!user.items.quests[k])
|
||||
user.items.quests[k] = 0;
|
||||
user.items.quests[k]++;
|
||||
user.markModified('flags.levelDrops');
|
||||
if (analytics) {
|
||||
analytics.track('acquire item', {
|
||||
uuid: user._id,
|
||||
itemKey: k,
|
||||
acquireMethod: 'Level Drop',
|
||||
category: 'behavior',
|
||||
headers: req.headers,
|
||||
});
|
||||
}
|
||||
user._tmp.drop = {
|
||||
type: 'Quest',
|
||||
key: k,
|
||||
};
|
||||
}
|
||||
});
|
||||
if (!user.flags.rebirthEnabled && (user.stats.lvl >= 50 || user.achievements.beastMaster)) {
|
||||
user.addNotification('REBIRTH_ENABLED');
|
||||
user.flags.rebirthEnabled = true;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user