Skip to content

Commit 15b64e5

Browse files
authored
Merge pull request #65 from blacksmoke26/dev
dev
2 parents 8da9c2c + 221d4dc commit 15b64e5

File tree

9 files changed

+149
-108
lines changed

9 files changed

+149
-108
lines changed

src/data/scenario/parser/modules/events/actions/clear-trees.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
// utils
99
import {isObject} from '~/helpers/object';
10-
import {toFloat} from '~/helpers/number';
10+
import {RADIUS_MIN} from '~/utils/defaults';
1111

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

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

28-
if (!validateRadius(node?.radius)) {
29-
return null;
30-
}
31-
3229
const action = {
3330
type: ACTION_NAME,
34-
radius: toFloat(node?.radius, 2),
31+
radius: normalizeRadius(node?.radius) || RADIUS_MIN,
3532
} as ActionParams;
3633

37-
validateLocationPosition(node?.position)
38-
&& (action.position = node.position.split(',').map(Number));
34+
normalizePosition(node?.position, position => action.position = position);
3935

4036
return action;
4137
};

src/data/scenario/parser/modules/events/actions/focus-camera.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import {isString} from '~/helpers/string';
1010
import {ENTITIES} from '~/utils/entities';
1111
import {isObject} from '~/helpers/object';
1212
import {LOCATION_TYPE} from '~/utils/action';
13-
import {isNumeric, toFloat} from '~/helpers/number';
1413

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

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

31-
const hasLocation = isString(node?.location, true)
32-
&& LOCATION_TYPE.includes(node?.location);
33-
3430
const hasEntityName = isString(node?.entity_name, true)
3531
&& ENTITIES.includes(node?.entity_name);
3632

37-
if (!hasLocation || !hasEntityName) {
33+
const hasLocation = isString(node?.location, true)
34+
&& LOCATION_TYPE.includes(node?.location);
35+
36+
if (!hasEntityName || !hasLocation) {
3837
return null;
3938
}
4039

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

47-
isNumeric(node?.distance) && validateDistance(node.distance)
48-
&& (action.distance = toFloat(node.distance, 2));
49-
50-
isNumeric(node?.rotation) && validateRotation(node.rotation)
51-
&& (action.rotation = toFloat(node.rotation, 2));
46+
normalizeDistance(node?.distance, distance => action.distance = distance);
47+
normalizeRotation(node?.rotation, rotation => action.rotation = rotation);
5248

5349
return action;
5450
};

src/data/scenario/parser/modules/events/actions/hide-ui.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {BUILDABLE_CATEGORIES} from '~/utils/action';
1616
// types
1717
import type {Json} from '~/types/json.types';
1818
import type {ActionHideUi, ActionName, ActionWithType} from '~/types/action.types';
19+
import {isInList} from '~/helpers/array';
1920

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

3839
entityTypes.length && (action.entityTypes = entityTypes);
3940
}
40-
41-
(isString(node?.buildable_categories, true)
42-
&& BUILDABLE_CATEGORIES.includes(node?.buildable_categories))
43-
&& (action.buildableCategories = node.buildable_categories);
41+
42+
isInList(
43+
node?.buildable_categories, BUILDABLE_CATEGORIES as unknown as string[],
44+
value => action.buildableCategories = value,
45+
);
4446

4547
isBool(node?.hide_disabled_ui) && (action.hideDisabledUi = node.hide_disabled_ui);
4648
isBool(node?.hide_quick_panels) && (action.hideQuickPanels = node.hide_quick_panels);

src/data/scenario/parser/modules/events/actions/modify-location.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import {snakeCase} from 'change-case';
1010
// utils
1111
import {isString} from '~/helpers/string';
1212
import {isObject} from '~/helpers/object';
13-
import {toFloat, toInteger} from '~/helpers/number';
1413

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

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

31-
if (!isString(node?.modification, true)) {
32-
return null;
33-
}
30+
if (!isString(node?.modification, true)) return null;
3431

3532
const action = {
3633
type: ACTION_NAME,
3734
} as ActionParams;
3835

39-
validateLocationIndex(node?.location_index) && (action.locationIndex = toInteger(node.location_index));
40-
validateVectorPosition(node?.position) && (action.position = node.position.split(',').map((n: string) => toFloat(n, 1)));
36+
normalizeLocationIndex(node?.location_index, value => action.locationIndex = value);
37+
normalizeVectorPosition(node?.position, value => action.position = value);
38+
4139
action.modification = snakeCase(node.modification);
4240

4341
return action;

src/utils/location.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const environments: string[] = [
3232
* @static
3333
* Random Coordinate */
3434
export function randomCoordinate(): number {
35-
return +randomFloat(0, 1).toFixed(2);
35+
return +randomFloat(0, 1).toFixed(3);
3636
}
3737

3838
/**

src/utils/scenario/defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const LOCATION_LAKES_MIN: number = 1;
4444

4545
export const LOCATION_LAKES_MAX: number = 20;
4646

47-
export const LOCATION_POSITION_MIN: number = 1;
47+
export const LOCATION_POSITION_MIN: number = 0;
4848

4949
export const LOCATION_POSITION_MAX: number = MAP_SIZE_MAX * 512;
5050

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @author Junaid Atari <mj.atari@gmail.com>
3+
* @see https://github.com/blacksmoke26/dawn-of-man-generator
4+
* @since 2024-07-14
5+
* @version 2.6.0
6+
*/
7+
8+
// helpers
9+
import {Callable, isInt, isNumeric, normalizeFloat, normalizeInt} from '~/helpers/number';
10+
11+
// utils
12+
import * as Defaults from '~/utils/defaults';
13+
//import * as ScnDefaults from '~/utils/scenario/defaults';
14+
import * as CondDefaults from '~/utils/condition';
15+
16+
// validators
17+
import {isVectorPosition} from '~/utils/scenario/validator';
18+
19+
export const normalizeRadius = (value: number | any, callback?: Callable<number>): false | number => {
20+
if (!isNumeric(value))
21+
return false;
22+
23+
return normalizeFloat(+value, {
24+
min: Defaults.RADIUS_MIN, max: Defaults.RADIUS_MAX, callback, decimals: 2,
25+
});
26+
};
27+
28+
export const normalizeDistance = (value: number | any, callback?: Callable<number>): false | number => {
29+
if (!isNumeric(value))
30+
return false;
31+
32+
return normalizeFloat(+value, {
33+
min: CondDefaults.DISTANCE_MIN, max: CondDefaults.DISTANCE_MAX, callback, decimals: 2,
34+
});
35+
};
36+
37+
export const normalizeRotation = (value: number | any, callback?: Callable<number>): false | number => {
38+
if (!isNumeric(value))
39+
return false;
40+
41+
return normalizeFloat(+value, {
42+
min: Defaults.ROTATION_MIN, max: Defaults.ROTATION_MAX, callback, decimals: 2,
43+
});
44+
};
45+
46+
export const normalizeLocationIndex = (value: any, callback?: Callable<number>): false | number => {
47+
if (!isInt(value)) {
48+
return false;
49+
}
50+
51+
return normalizeInt(+value, {
52+
min: Defaults.LOCATION_INDEX_MIN, max: Defaults.LOCATION_INDEX_MAX, callback,
53+
});
54+
};
55+
56+
export const normalizeVectorPosition = (value: any, callback?: Callable<[number, number, number]>): false | [number, number, number] => {
57+
if (!isVectorPosition(value)) {
58+
return false;
59+
}
60+
61+
const position = value.split(',').map((val: string) => normalizeInt(+val, {
62+
min: Defaults.POSITION_VECTOR_MIN, max: Defaults.POSITION_VECTOR_MAX,
63+
}));
64+
65+
callback && callback?.(position);
66+
67+
return position;
68+
};

src/utils/scenario/normalizer-location.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ export const normalizeCoordinates = (position: string, callback?: Callable<numbe
5959
* Normalize position value
6060
* @returns The value, false if invalid
6161
*/
62-
export const normalizePosition = (position: string, callback?: Callable<number>): false | [number, number] => {
62+
export const normalizePosition = (position: string, callback?: Callable<[number, number]>): false | [number, number] => {
6363
if (!isLocationPosition(position)) return false;
6464

65-
return position.split(',').map((value: string) => normalizeInt(value, {
66-
min: LOCATION_POSITION_MIN, max: LOCATION_POSITION_MAX, callback,
65+
const value = position.split(',').map((value: string) => normalizeInt(value, {
66+
min: LOCATION_POSITION_MIN, max: LOCATION_POSITION_MAX,
6767
})) as [number, number];
68+
69+
callback && callback?.(value);
70+
71+
return value;
6872
};

0 commit comments

Comments
 (0)