Add group & individual archive function

This commit is contained in:
MrKBear 2022-04-22 20:48:53 +08:00
parent d11fd2d328
commit 276c8c63a1
3 changed files with 105 additions and 7 deletions

View File

@ -1,10 +1,10 @@
import { Individual } from "@Model/Individual"; import { Individual } from "@Model/Individual";
import { CtrlObject } from "@Model/CtrlObject"; import { CtrlObject, IArchiveCtrlObject } from "@Model/CtrlObject";
import type { Behavior, IAnyBehavior } from "@Model/Behavior"; import type { Behavior, IAnyBehavior } from "@Model/Behavior";
import { Label } from "@Model/Label"; import { Label } from "@Model/Label";
import { Range } from "@Model/Range"; import { Range } from "@Model/Range";
import { Model, ObjectID } from "@Model/Model"; import { Model, ObjectID } from "@Model/Model";
import { getDefaultValue, IObjectParamArchiveType } from "@Model/Parameter"; import { getDefaultValue, IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject } from "@Model/Parameter";
enum GenMod { enum GenMod {
Point = "p", Point = "p",
@ -12,7 +12,6 @@ enum GenMod {
} }
interface IArchiveGroup { interface IArchiveGroup {
individuals: IObjectParamArchiveType[];
genMethod: Group["genMethod"]; genMethod: Group["genMethod"];
genPoint: Group["genPoint"]; genPoint: Group["genPoint"];
genRange: IObjectParamArchiveType | undefined; genRange: IObjectParamArchiveType | undefined;
@ -419,6 +418,34 @@ class Group extends CtrlObject<IArchiveGroup> {
return dataBuffer; return dataBuffer;
} }
public override toArchive(): IArchiveCtrlObject & IArchiveGroup {
return {
...super.toArchive(),
genMethod: this.genMethod,
genPoint: this.genPoint.concat([]),
genRange: object2ArchiveObject(this.genRange) as IObjectParamArchiveType,
genCount: this.genCount,
genErrorMessage: this.genErrorMessage,
genErrorMessageShowCount: this.genErrorMessageShowCount,
killCount: this.killCount,
behaviors: object2ArchiveObject(this.behaviors) as IObjectParamArchiveType[]
};
}
public override fromArchive(archive: IArchiveCtrlObject & IArchiveGroup, paster: IArchiveParseFn): void {
super.fromArchive(archive, paster);
this.genMethod = archive.genMethod,
this.genPoint = archive.genPoint.concat([]),
this.genRange = archive.genRange ? paster(archive.genRange) as any : undefined,
this.genCount = archive.genCount,
this.genErrorMessage = archive.genErrorMessage,
this.genErrorMessageShowCount = archive.genErrorMessageShowCount,
this.killCount = archive.killCount,
this.behaviors = archive.behaviors.map((item) => {
return item ? paster(item) as any : undefined;
});
}
public constructor(model: Model) { public constructor(model: Model) {
super(model); super(model);

View File

@ -1,5 +1,15 @@
import type { Group } from "@Model/Group"; import type { Group } from "@Model/Group";
import { ObjectID } from "@Model/Model"; import { IAnyObject, ObjectID } from "@Model/Model";
import { IArchiveParseFn, object2ArchiveObject, isArchiveObjectType } from "@Model/Parameter";
interface IArchiveIndividual {
id: string;
position: number[];
velocity: number[];
acceleration: number[];
force: number[];
metaData: IAnyObject;
}
/** /**
* *
@ -170,6 +180,67 @@ class Individual {
this.metaData.set(key, value); this.metaData.set(key, value);
return value; return value;
} }
public toArchive(): IArchiveIndividual {
const metaDataArchive = {} as IAnyObject;
this.metaData.forEach((value, key) => {
// 处理内置对象
let ltObject = object2ArchiveObject(value);
if (ltObject) {
metaDataArchive[key] = ltObject;
}
// 处理数组
else if (Array.isArray(value)) {
metaDataArchive[key] = value.concat([]);
}
// 处理值
else {
metaDataArchive[key] = value;
}
});
return {
id: this.id,
position: this.position.concat([]),
velocity: this.velocity.concat([]),
acceleration: this.acceleration.concat([]),
force: this.force.concat([]),
metaData: metaDataArchive
};
}
public fromArchive(archive: IArchiveIndividual, paster: IArchiveParseFn): void {
const metaData = new Map() as Map<ObjectID, any>;
for (const key in archive.metaData) {
const value = archive.metaData[key];
// 处理内置对象
if (value instanceof Object && isArchiveObjectType(value)) {
metaData.set(key, paster(value));
}
else if (Array.isArray(value)) {
metaData.set(key, value.concat([]));
}
else {
metaData.set(key, value);
}
}
this.id = this.id,
this.position = this.position.concat([]),
this.velocity = this.velocity.concat([]),
this.acceleration = this.acceleration.concat([]),
this.force = this.force.concat([]),
this.metaData = metaData;
}
} }
export { Individual }; export { Individual };

View File

@ -1,7 +1,7 @@
import { Group } from "@Model/Group"; import { Group } from "@Model/Group";
import { Range } from "@Model/Range"; import { Range } from "@Model/Range";
import { Label } from "@Model/Label"; import { Label } from "@Model/Label";
import { Behavior } from "@Model/Behavior"; import { Behavior, IAnyBehavior } from "@Model/Behavior";
import { Individual } from "@Model/Individual"; import { Individual } from "@Model/Individual";
type IObjectParamArchiveType = { type IObjectParamArchiveType = {
@ -225,7 +225,7 @@ function getDefaultValue<P extends IParameter> (option: IParameterOption<P>): IP
return defaultObj; return defaultObj;
} }
type IRealObjectType = Range | Group | Label | Behavior; type IRealObjectType = Range | Group | Label | IAnyBehavior;
type IArchiveParseFn = (archive: IObjectParamArchiveType) => IRealObjectType | undefined; type IArchiveParseFn = (archive: IObjectParamArchiveType) => IRealObjectType | undefined;
function object2ArchiveObject(object: IRealObjectType | IRealObjectType[] | any, testArray: boolean = true): function object2ArchiveObject(object: IRealObjectType | IRealObjectType[] | any, testArray: boolean = true):
@ -358,5 +358,5 @@ export {
IParamType, IParamValue, isObjectType, isVectorType, getDefaultValue, IParamType, IParamValue, isObjectType, isVectorType, getDefaultValue,
IParameterOptionItem, IParameter, IParameterOption, IParameterValue, IParameterOptionItem, IParameter, IParameterOption, IParameterValue,
object2ArchiveObject, parameter2ArchiveObject, archiveObject2Parameter, object2ArchiveObject, parameter2ArchiveObject, archiveObject2Parameter,
IArchiveParseFn, IObjectParamArchiveType IArchiveParseFn, IObjectParamArchiveType, isArchiveObjectType
} }