Skip to content

Commit

Permalink
Merge pull request #65 from blacksmoke26/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
blacksmoke26 authored Jul 20, 2024
2 parents 8da9c2c + 221d4dc commit 15b64e5
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 108 deletions.
16 changes: 6 additions & 10 deletions src/data/scenario/parser/modules/events/actions/clear-trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

// utils
import {isObject} from '~/helpers/object';
import {toFloat} from '~/helpers/number';
import {RADIUS_MIN} from '~/utils/defaults';

// validators
import {validateLocationPosition, validateRadius} from '~/utils/scenario/validator';
// normalizers
import {normalizePosition} from '~/utils/scenario/normalizer-location';
import {normalizeRadius} from '~/utils/scenario/normalizer-action';

// types
import type {Json} from '~/types/json.types';
Expand All @@ -25,17 +26,12 @@ export const jsonToRedux = (node: Json | any): Json | null => {
return null;
}

if (!validateRadius(node?.radius)) {
return null;
}

const action = {
type: ACTION_NAME,
radius: toFloat(node?.radius, 2),
radius: normalizeRadius(node?.radius) || RADIUS_MIN,
} as ActionParams;

validateLocationPosition(node?.position)
&& (action.position = node.position.split(',').map(Number));
normalizePosition(node?.position, position => action.position = position);

return action;
};
20 changes: 8 additions & 12 deletions src/data/scenario/parser/modules/events/actions/focus-camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import {isString} from '~/helpers/string';
import {ENTITIES} from '~/utils/entities';
import {isObject} from '~/helpers/object';
import {LOCATION_TYPE} from '~/utils/action';
import {isNumeric, toFloat} from '~/helpers/number';

// validators
import {validateDistance, validateRotation} from '~/utils/scenario/validator';
// normalizers
import {normalizeDistance, normalizeRotation} from '~/utils/scenario/normalizer-action';

// types
import type {Json} from '~/types/json.types';
Expand All @@ -28,13 +27,13 @@ export const jsonToRedux = (node: Json | any): Json | null => {
return null;
}

const hasLocation = isString(node?.location, true)
&& LOCATION_TYPE.includes(node?.location);

const hasEntityName = isString(node?.entity_name, true)
&& ENTITIES.includes(node?.entity_name);

if (!hasLocation || !hasEntityName) {
const hasLocation = isString(node?.location, true)
&& LOCATION_TYPE.includes(node?.location);

if (!hasEntityName || !hasLocation) {
return null;
}

Expand All @@ -44,11 +43,8 @@ export const jsonToRedux = (node: Json | any): Json | null => {
location: node.location,
} as ActionParams;

isNumeric(node?.distance) && validateDistance(node.distance)
&& (action.distance = toFloat(node.distance, 2));

isNumeric(node?.rotation) && validateRotation(node.rotation)
&& (action.rotation = toFloat(node.rotation, 2));
normalizeDistance(node?.distance, distance => action.distance = distance);
normalizeRotation(node?.rotation, rotation => action.rotation = rotation);

return action;
};
10 changes: 6 additions & 4 deletions src/data/scenario/parser/modules/events/actions/hide-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {BUILDABLE_CATEGORIES} from '~/utils/action';
// types
import type {Json} from '~/types/json.types';
import type {ActionHideUi, ActionName, ActionWithType} from '~/types/action.types';
import {isInList} from '~/helpers/array';

const ACTION_NAME: ActionName = 'HideUi';
type ActionParams = ActionWithType<'HideUi', ActionHideUi>;
Expand All @@ -37,10 +38,11 @@ export const jsonToRedux = (node: Json | any): Json | null => {

entityTypes.length && (action.entityTypes = entityTypes);
}

(isString(node?.buildable_categories, true)
&& BUILDABLE_CATEGORIES.includes(node?.buildable_categories))
&& (action.buildableCategories = node.buildable_categories);

isInList(
node?.buildable_categories, BUILDABLE_CATEGORIES as unknown as string[],
value => action.buildableCategories = value,
);

isBool(node?.hide_disabled_ui) && (action.hideDisabledUi = node.hide_disabled_ui);
isBool(node?.hide_quick_panels) && (action.hideQuickPanels = node.hide_quick_panels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import {snakeCase} from 'change-case';
// utils
import {isString} from '~/helpers/string';
import {isObject} from '~/helpers/object';
import {toFloat, toInteger} from '~/helpers/number';

// validators
import {validateLocationIndex, validateVectorPosition} from '~/utils/scenario/validator';
// normalizers
import {normalizeLocationIndex, normalizeVectorPosition} from '~/utils/scenario/normalizer-action';

// types
import type {Json} from '~/types/json.types';
Expand All @@ -28,16 +27,15 @@ export const jsonToRedux = (node: Json | any): Json | null => {
return null;
}

if (!isString(node?.modification, true)) {
return null;
}
if (!isString(node?.modification, true)) return null;

const action = {
type: ACTION_NAME,
} as ActionParams;

validateLocationIndex(node?.location_index) && (action.locationIndex = toInteger(node.location_index));
validateVectorPosition(node?.position) && (action.position = node.position.split(',').map((n: string) => toFloat(n, 1)));
normalizeLocationIndex(node?.location_index, value => action.locationIndex = value);
normalizeVectorPosition(node?.position, value => action.position = value);

action.modification = snakeCase(node.modification);

return action;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const environments: string[] = [
* @static
* Random Coordinate */
export function randomCoordinate(): number {
return +randomFloat(0, 1).toFixed(2);
return +randomFloat(0, 1).toFixed(3);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/scenario/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const LOCATION_LAKES_MIN: number = 1;

export const LOCATION_LAKES_MAX: number = 20;

export const LOCATION_POSITION_MIN: number = 1;
export const LOCATION_POSITION_MIN: number = 0;

export const LOCATION_POSITION_MAX: number = MAP_SIZE_MAX * 512;

Expand Down
68 changes: 68 additions & 0 deletions src/utils/scenario/normalizer-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @author Junaid Atari <mj.atari@gmail.com>
* @see https://github.com/blacksmoke26/dawn-of-man-generator
* @since 2024-07-14
* @version 2.6.0
*/

// helpers
import {Callable, isInt, isNumeric, normalizeFloat, normalizeInt} from '~/helpers/number';

// utils
import * as Defaults from '~/utils/defaults';
//import * as ScnDefaults from '~/utils/scenario/defaults';
import * as CondDefaults from '~/utils/condition';

// validators
import {isVectorPosition} from '~/utils/scenario/validator';

export const normalizeRadius = (value: number | any, callback?: Callable<number>): false | number => {
if (!isNumeric(value))
return false;

return normalizeFloat(+value, {
min: Defaults.RADIUS_MIN, max: Defaults.RADIUS_MAX, callback, decimals: 2,
});
};

export const normalizeDistance = (value: number | any, callback?: Callable<number>): false | number => {
if (!isNumeric(value))
return false;

return normalizeFloat(+value, {
min: CondDefaults.DISTANCE_MIN, max: CondDefaults.DISTANCE_MAX, callback, decimals: 2,
});
};

export const normalizeRotation = (value: number | any, callback?: Callable<number>): false | number => {
if (!isNumeric(value))
return false;

return normalizeFloat(+value, {
min: Defaults.ROTATION_MIN, max: Defaults.ROTATION_MAX, callback, decimals: 2,
});
};

export const normalizeLocationIndex = (value: any, callback?: Callable<number>): false | number => {
if (!isInt(value)) {
return false;
}

return normalizeInt(+value, {
min: Defaults.LOCATION_INDEX_MIN, max: Defaults.LOCATION_INDEX_MAX, callback,
});
};

export const normalizeVectorPosition = (value: any, callback?: Callable<[number, number, number]>): false | [number, number, number] => {
if (!isVectorPosition(value)) {
return false;
}

const position = value.split(',').map((val: string) => normalizeInt(+val, {
min: Defaults.POSITION_VECTOR_MIN, max: Defaults.POSITION_VECTOR_MAX,
}));

callback && callback?.(position);

return position;
};
10 changes: 7 additions & 3 deletions src/utils/scenario/normalizer-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ export const normalizeCoordinates = (position: string, callback?: Callable<numbe
* Normalize position value
* @returns The value, false if invalid
*/
export const normalizePosition = (position: string, callback?: Callable<number>): false | [number, number] => {
export const normalizePosition = (position: string, callback?: Callable<[number, number]>): false | [number, number] => {
if (!isLocationPosition(position)) return false;

return position.split(',').map((value: string) => normalizeInt(value, {
min: LOCATION_POSITION_MIN, max: LOCATION_POSITION_MAX, callback,
const value = position.split(',').map((value: string) => normalizeInt(value, {
min: LOCATION_POSITION_MIN, max: LOCATION_POSITION_MAX,
})) as [number, number];

callback && callback?.(value);

return value;
};
Loading

0 comments on commit 15b64e5

Please sign in to comment.