device templates exist now

This commit is contained in:
Rachel Powers
2024-04-15 21:30:05 -07:00
parent 10d5db967c
commit 4e6ac09c5e
9 changed files with 108 additions and 29 deletions

View File

@@ -2,7 +2,7 @@
mod utils;
mod types;
use ic10emu::grammar::{LogicType, SlotLogicType};
use ic10emu::{grammar::{LogicType, SlotLogicType}, DeviceTemplate};
use serde::{Deserialize, Serialize};
use types::{Registers, Stack};
@@ -55,18 +55,18 @@ impl DeviceRef {
}
#[wasm_bindgen(getter, js_name = "nameHash")]
pub fn name_hash(&self) -> Option<f64> {
pub fn name_hash(&self) -> Option<i32> {
self.device.borrow().name_hash
}
#[wasm_bindgen(getter, js_name = "prefabName")]
pub fn prefab_name(&self) -> Option<String> {
self.device.borrow().prefab_name.clone()
self.device.borrow().prefab.as_ref().map(|prefab| prefab.name.clone())
}
#[wasm_bindgen(getter, js_name = "prefabHash")]
pub fn prefab_hash(&self) -> Option<i32> {
self.device.borrow().prefab_hash
self.device.borrow().prefab.as_ref().map(|prefab| prefab.hash)
}
#[wasm_bindgen(getter, skip_typescript)]
@@ -351,6 +351,12 @@ impl VM {
Ok(self.vm.borrow_mut().add_device(network)?)
}
#[wasm_bindgen(js_name = "addDeviceFromTemplate", skip_typescript)]
pub fn add_device_from_template(&self, template: JsValue) -> Result<u32, JsError> {
let template: DeviceTemplate = serde_wasm_bindgen::from_value(template)?;
Ok(self.vm.borrow_mut().add_device_from_template(template)?)
}
#[wasm_bindgen(js_name = "getDevice")]
pub fn get_device(&self, id: u32) -> Option<DeviceRef> {
let device = self.vm.borrow().get_device(id);

View File

@@ -4,7 +4,8 @@ export interface LogicField {
field_type: FieldType;
value: number;
}
export type Fields = Map<string, LogicField>;
export type LogicFields = Map<LogicType, LogicField>;
export type SlotLogicFields = Map<SlotLogicType, LogicField>;
export type SlotType =
| "AccessCard"
@@ -46,12 +47,12 @@ export interface SlotOccupant {
readonly quantity: number;
readonly max_quantity: number;
readonly damage: number;
readonly fields: Fields;
readonly fields: SlotLogicFields;
}
export interface Slot {
readonly typ: SlotType;
readonly occupant: SlotOccupant | undefined;
readonly fields: Fields;
readonly fields: SlotLogicFields;
}
export type Reagents = Map<string, Map<number, number>>;
@@ -80,10 +81,10 @@ export type DeviceSpec = {
};
readonly connection: number | undefined;
};
export type LogicType = { readonly LogicType: string };
export type SlotLogicType = { readonly SlotLogicType: string };
export type BatchMode = { readonly BatchMode: string };
export type ReagentMode = { readonly ReagentMode: string };
export type OperandLogicType = { readonly LogicType: string };
export type OperandSlotLogicType = { readonly SlotLogicType: string };
export type OperandBatchMode = { readonly BatchMode: string };
export type OperandReagentMode = { readonly ReagentMode: string };
export type Identifier = { readonly Identifier: { name: string } };
export type NumberFloat = { readonly Float: number };
@@ -106,10 +107,10 @@ export type Operand =
| RegisterSpec
| DeviceSpec
| NumberOperand
| LogicType
| SlotLogicType
| BatchMode
| ReagentMode
| OperandLogicType
| OperandSlotLogicType
| OperandBatchMode
| OperandReagentMode
| Identifier;
export type Alias = RegisterSpec | DeviceSpec;
@@ -141,7 +142,7 @@ export interface Program {
}
export interface DeviceRef {
readonly fields: Fields;
readonly fields: LogicFields;
readonly slots: Slot[];
readonly reagents: Reagents;
readonly connections: Connection[];
@@ -149,5 +150,29 @@ export interface DeviceRef {
readonly defines?: Defines | undefined;
readonly pins?: Pins;
readonly program?: Program;
getSlotFields(slot: number): Fields;
getSlotFields(slot: number): SlotLogicFields;
}
export interface SlotOccupantTemplate {
id?: number;
fields: { [key in SlotLogicType]?: LogicField };
}
export interface SlotTemplate {
typ: SlotType;
occupant?: SlotOccupantTemplate;
}
export interface DeviceTemplate {
id?: number;
name?: string;
prefab_name?: string;
slots: SlotTemplate[];
// reagents: { [key: string]: float}
connections: Connection[];
fields: { [key in LogicType]?: LogicField };
}
export interface VM {
addDeviceFromTemplate(template: DeviceTemplate): number
}