Skip to content

Commit

Permalink
Update comments and resolve type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinLeamy committed Aug 22, 2023
1 parent 3083e37 commit 7344996
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/server/logic/events/handlers/admin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { makeEventHandler } from "@/server/logic/events/core";
import { q } from "@/server/logic/events/query";
import { createComponentFromFieldName, componentGet, componentUpdate } from "@/server/logic/utils/components";
import {
createComponentFromFieldName,
componentGet,
componentUpdate,
} from "@/server/logic/utils/components";
import { entityGet, entityInvoke } from "@/server/logic/utils/delta";
import {
copyPlayer,
Expand Down
7 changes: 6 additions & 1 deletion src/server/logic/utils/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Component from "@/shared/ecs/gen/components";
import { snakeCaseToUpperCamalCase } from "@/shared/util/text";
import { ok } from "assert";

// Access a field of a component given the path of the field.
export function componentGet(component: any, path: string[]): any {
let current = component;
for (const key of path) {
Expand All @@ -14,6 +15,8 @@ export function componentGet(component: any, path: string[]): any {
return current;
}

// Update a field of a component given the path of the field and the new value.
// Throws if the path is invalid.
export function componentUpdate(component: any, path: string[], newValue: any) {
let current = component;
for (let i = 0; i < path.length - 1; ++i) {
Expand All @@ -23,6 +26,8 @@ export function componentUpdate(component: any, path: string[], newValue: any) {
current[path[path.length - 1]] = newValue;
}

// Creates a default component given the name of the component's field.
// e.g. "health" -> Component.Health.create()
export function createComponentFromFieldName(field: string): any {
return Component[snakeCaseToUpperCamalCase(field) as unknown as keyof Component].create();
return (Component as any)[snakeCaseToUpperCamalCase(field)].create();
}
5 changes: 4 additions & 1 deletion src/server/logic/utils/delta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Delta } from "@/shared/ecs/gen/delta";
import { snakeCaseToCamalCase, snakeCaseToUpperCamalCase } from "@/shared/util/text";
import {
snakeCaseToCamalCase,
snakeCaseToUpperCamalCase,
} from "@/shared/util/text";

type EntityField = keyof Delta;
type EntityMethod = "get" | "mutable" | "set" | "clear";
Expand Down

0 comments on commit 7344996

Please sign in to comment.