mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
Readded context menus
This commit is contained in:
@@ -226,4 +226,61 @@ export class UnitDatabase {
|
||||
shortLabel: "",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
getRandomUnit(
|
||||
options: {
|
||||
type?: string;
|
||||
role?: string;
|
||||
ranges?: string[];
|
||||
eras?: string[];
|
||||
coalition?: string;
|
||||
}
|
||||
) {
|
||||
/* Start from all the unit blueprints in the database */
|
||||
var unitBlueprints = this.getBlueprints();
|
||||
|
||||
/* If a specific type or role is provided, use only the blueprints of that type or role */
|
||||
if (options.type && options.role) {
|
||||
console.error("Can't create random unit if both type and role are provided. Either create by type or by role.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (options.type) {
|
||||
unitBlueprints = this.getByType(options.type);
|
||||
} else if (options.role) {
|
||||
unitBlueprints = this.getByType(options.role);
|
||||
}
|
||||
|
||||
/* Keep only the units that have a range included in the requested values */
|
||||
if (options.ranges) {
|
||||
unitBlueprints = unitBlueprints.filter((unitBlueprint: UnitBlueprint) => {
|
||||
var rangeType = "";
|
||||
var range = unitBlueprint.acquisitionRange;
|
||||
if (range !== undefined) {
|
||||
if (range >= 0 && range < 10000) rangeType = "Short range";
|
||||
else if (range >= 10000 && range < 100000) rangeType = "Medium range";
|
||||
else if (range >= 100000 && range < 999999) rangeType = "Long range";
|
||||
}
|
||||
return options.ranges?.includes(rangeType);
|
||||
});
|
||||
}
|
||||
|
||||
/* Keep only the units that have an era included in the requested values */
|
||||
if (options.eras) {
|
||||
unitBlueprints = unitBlueprints.filter((unitBlueprint: UnitBlueprint) => {
|
||||
return unitBlueprint.era ? options.eras?.includes(unitBlueprint.era) : true;
|
||||
});
|
||||
}
|
||||
|
||||
/* Keep only the units that have the correct coalition, if selected */
|
||||
if (options.coalition) {
|
||||
unitBlueprints = unitBlueprints.filter((unitBlueprint: UnitBlueprint) => {
|
||||
return unitBlueprint.coalition && unitBlueprint.coalition !== "" ? options.coalition === unitBlueprint.coalition : true;
|
||||
});
|
||||
}
|
||||
|
||||
var index = Math.floor(Math.random() * unitBlueprints.length);
|
||||
return unitBlueprints[index];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user