fix melee and ranged weapons with same name not both appearing

This commit is contained in:
NilsUeter
2023-10-05 11:44:50 +02:00
parent 6556b7d56b
commit af9a990a3f
4 changed files with 60 additions and 45 deletions
+3 -19
View File
@@ -67,32 +67,18 @@ const Unit = ({ unit, index, catalog, onePerPage, forceRules }) => {
const [image, setImage] = useState(null); const [image, setImage] = useState(null);
let { let {
name, name,
weapons, meleeWeapons,
rangedWeapons,
abilities, abilities,
keywords, keywords,
factions, factions,
rules, rules,
modelStats, modelStats,
modelList, modelList,
spells,
cost, cost,
woundTracker,
} = unit; } = unit;
const meleeWeapons = weapons const weapons = [...meleeWeapons, ...rangedWeapons];
.filter(
(weapon) =>
(weapon.range === "Melee" || weapon.type === "Melee") &&
weapon.range !== ""
)
.sort((a, b) => a.selectionName.localeCompare(b.selectionName));
const rangedWeapons = weapons
.filter(
(weapon) =>
weapon.range !== "Melee" && weapon.range !== "-" && weapon.range !== ""
)
.sort((a, b) => a.selectionName.localeCompare(b.selectionName));
const weaponDescriptions = weapons const weaponDescriptions = weapons
.filter( .filter(
@@ -288,8 +274,6 @@ const Unit = ({ unit, index, catalog, onePerPage, forceRules }) => {
className="button print-display-none" className="button print-display-none"
style={{ style={{
position: "absolute", position: "absolute",
top: "0",
right: 0,
border: "1px solid #999", border: "1px solid #999",
top: 1, top: 1,
right: 1, right: 1,
+6 -6
View File
@@ -54,12 +54,7 @@ export const Weapons = ({ title, weapons, modelStats, forceRules }) => {
}; };
export const hasDifferentProfiles = (selectionName, name) => { export const hasDifferentProfiles = (selectionName, name) => {
return ( return selectionName && selectionName.toLowerCase() !== name.toLowerCase();
selectionName &&
selectionName.toLowerCase() !== name.toLowerCase() &&
!name.includes("(Shooting)") &&
!name.includes("(Melee)")
);
}; };
const Weapon = ({ weapon, previousWeapon, nextWeapon, isMelee, className }) => { const Weapon = ({ weapon, previousWeapon, nextWeapon, isMelee, className }) => {
@@ -176,6 +171,11 @@ const getWeaponClassNames = (weapons, index) => {
let differentColor = false; let differentColor = false;
for (let i = 1; i <= index; i++) { for (let i = 1; i <= index; i++) {
let { selectionName } = weapons[i]; let { selectionName } = weapons[i];
if (!selectionName && !weapons[i - 1].selectionName) {
if (weapons[i].name !== weapons[i - 1].name) {
differentColor = !differentColor;
}
}
if (selectionName !== weapons[i - 1].selectionName) { if (selectionName !== weapons[i - 1].selectionName) {
differentColor = !differentColor; differentColor = !differentColor;
} }
-2
View File
@@ -301,8 +301,6 @@ const Unit = ({ unit, index, catalog, onePerPage, forceRules }) => {
className="button print-display-none" className="button print-display-none"
style={{ style={{
position: "absolute", position: "absolute",
top: "0",
right: 0,
border: "1px solid #999", border: "1px solid #999",
top: 1, top: 1,
right: 1, right: 1,
+51 -18
View File
@@ -140,7 +140,8 @@ export class Model extends BaseNotes {
leadership = 7; leadership = 7;
save = ""; save = "";
weapons = []; rangedWeapons = [];
meleeWeapons = [];
upgrades = []; upgrades = [];
// TODO model upgrades (i.e. tau support systems) // TODO model upgrades (i.e. tau support systems)
psyker = null; psyker = null;
@@ -153,11 +154,17 @@ export class Model extends BaseNotes {
if ( if (
this.name === model.name && this.name === model.name &&
this.count === model.count && this.count === model.count &&
this.weapons.length === model.weapons.length && this.rangedWeapons.length === model.rangedWeapons.length &&
this.meleeWeapons.length === model.meleeWeapons.length &&
this.upgrades.length === model.upgrades.length this.upgrades.length === model.upgrades.length
) { ) {
for (let wi = 0; wi < this.weapons.length; wi++) { for (let wi = 0; wi < this.rangedWeapons.length; wi++) {
if (!this.weapons[wi].equal(model.weapons[wi])) { if (!this.rangedWeapons[wi].equal(model.rangedWeapons[wi])) {
return false;
}
}
for (let wi = 0; wi < this.meleeWeapons.length; wi++) {
if (!this.meleeWeapons[wi].equal(model.meleeWeapons[wi])) {
return false; return false;
} }
} }
@@ -178,7 +185,11 @@ export class Model extends BaseNotes {
nameAndGear() { nameAndGear() {
let name = this.name; let name = this.name;
if (this.weapons.length > 0 || this.upgrades.length > 0) { if (
this.meleeWeapons.length > 0 ||
this.rangedWeapons.length > 0 ||
this.upgrades.length > 0
) {
const gear = this.getDedupedWeaponsAndUpgrades(); const gear = this.getDedupedWeaponsAndUpgrades();
name += ` (${gear.map((u) => u.toString()).join(", ")})`; name += ` (${gear.map((u) => u.toString()).join(", ")})`;
} }
@@ -187,7 +198,11 @@ export class Model extends BaseNotes {
getDedupedWeaponsAndUpgrades() { getDedupedWeaponsAndUpgrades() {
const deduped = []; const deduped = [];
for (const upgrade of [...this.weapons, ...this.upgrades]) { for (const upgrade of [
...this.rangedWeapons,
...this.meleeWeapons,
...this.upgrades,
]) {
if ( if (
!deduped.some( !deduped.some(
(e) => upgrade.getSelectionName() === e.getSelectionName() (e) => upgrade.getSelectionName() === e.getSelectionName()
@@ -200,10 +215,12 @@ export class Model extends BaseNotes {
} }
normalize() { normalize() {
this.weapons.sort(CompareWeapon); this.rangedWeapons.sort(CompareWeapon);
this.meleeWeapons.sort(CompareWeapon);
this.upgrades.sort(CompareObj); this.upgrades.sort(CompareObj);
this.normalizeUpgrades(this.weapons); this.normalizeUpgrades(this.rangedWeapons);
this.normalizeUpgrades(this.meleeWeapons);
this.normalizeUpgrades(this.upgrades); this.normalizeUpgrades(this.upgrades);
} }
@@ -237,7 +254,8 @@ export class Unit extends BaseNotes {
models = []; models = [];
modelStats = []; modelStats = [];
modelList = []; modelList = [];
weapons = []; rangedWeapons = [];
meleeWeapons = [];
spells = []; spells = [];
psykers = []; psykers = [];
explosions = []; explosions = [];
@@ -336,12 +354,16 @@ export class Unit extends BaseNotes {
(model) => (model) =>
(model.count > 1 ? `${model.count}x ` : "") + model.nameAndGear() (model.count > 1 ? `${model.count}x ` : "") + model.nameAndGear()
); );
this.weapons = this.models this.rangedWeapons = this.models
.map((m) => m.weapons) .map((m) => m.rangedWeapons)
.reduce((acc, val) => acc.concat(val), [])
.sort(CompareWeapon)
.filter((weap, i, array) => weap.name !== array[i - 1]?.name);
this.meleeWeapons = this.models
.map((m) => m.meleeWeapons)
.reduce((acc, val) => acc.concat(val), []) .reduce((acc, val) => acc.concat(val), [])
.sort(CompareWeapon) .sort(CompareWeapon)
.filter((weap, i, array) => weap.name !== array[i - 1]?.name); .filter((weap, i, array) => weap.name !== array[i - 1]?.name);
this.spells.push( this.spells.push(
...this.models ...this.models
.map((m) => m.psychicPowers) .map((m) => m.psychicPowers)
@@ -811,12 +833,19 @@ function ParseUnit(root) {
const unitUpgradesModel = new Model(); const unitUpgradesModel = new Model();
unitUpgradesModel.name = "Unit Upgrades"; unitUpgradesModel.name = "Unit Upgrades";
ParseModelProfiles(unseenProfiles, unitUpgradesModel, unit); ParseModelProfiles(unseenProfiles, unitUpgradesModel, unit);
if (unitUpgradesModel.weapons.length > 0 && unit.models.length > 0) { if (unitUpgradesModel.meleeWeapons.length > 0 && unit.models.length > 0) {
// Apply weapons at the unit level to all models in the unit. // Apply weapons at the unit level to all models in the unit.
for (const model of unit.models) { for (const model of unit.models) {
model.weapons.push(...unitUpgradesModel.weapons); model.meleeWeapons.push(...unitUpgradesModel.meleeWeapons);
} }
unitUpgradesModel.weapons.length = 0; // Clear the array. unitUpgradesModel.meleeWeapons.length = 0; // Clear the array.
}
if (unitUpgradesModel.rangedWeapons.length > 0 && unit.models.length > 0) {
// Apply weapons at the unit level to all models in the unit.
for (const model of unit.models) {
model.rangedWeapons.push(...unitUpgradesModel.rangedWeapons);
}
unitUpgradesModel.rangedWeapons.length = 0; // Clear the array.
} }
if (unitUpgradesModel.psychicPowers.length > 0) { if (unitUpgradesModel.psychicPowers.length > 0) {
// Add spells to the unit's spell list. However, we'll still need // Add spells to the unit's spell list. However, we'll still need
@@ -854,7 +883,8 @@ function ParseUnit(root) {
} }
if ( if (
unitUpgradesModel.weapons.length > 0 || unitUpgradesModel.rangedWeapons.length > 0 ||
unitUpgradesModel.meleeWeapons.length > 0 ||
unitUpgradesModel.upgrades.length > 0 unitUpgradesModel.upgrades.length > 0
) { ) {
unit.models.push(unitUpgradesModel); unit.models.push(unitUpgradesModel);
@@ -946,9 +976,12 @@ function ParseModelProfiles(profiles, model, unit) {
profile.getAttribute("type") === "model" profile.getAttribute("type") === "model"
) { ) {
// Do nothing; these were already handled. // Do nothing; these were already handled.
} else if (typeName === "Ranged Weapons" || typeName === "Melee Weapons") { } else if (typeName === "Ranged Weapons") {
const weapon = ParseWeaponProfile(profile); const weapon = ParseWeaponProfile(profile);
model.weapons.push(weapon); model.rangedWeapons.push(weapon);
} else if (typeName === "Melee Weapons") {
const weapon = ParseWeaponProfile(profile);
model.meleeWeapons.push(weapon);
} else if ( } else if (
typeName.includes("Wound Track") || typeName.includes("Wound Track") ||
typeName.includes("Stat Damage") || typeName.includes("Stat Damage") ||