From 7ac954f77a08c6e05651cb2c5fe81777ff84d40f Mon Sep 17 00:00:00 2001 From: goldbuick Date: Sat, 18 May 2024 15:19:09 -0400 Subject: [PATCH] working on console cli invokes --- zss/device/vm.ts | 8 ++++++-- zss/memory/index.ts | 28 ++++++++++++++++++++++++---- zss/os.ts | 14 +++++--------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/zss/device/vm.ts b/zss/device/vm.ts index e3390e37..d30621e1 100644 --- a/zss/device/vm.ts +++ b/zss/device/vm.ts @@ -26,6 +26,9 @@ memorysetdefaultplayer(playerid) // manages chips const os = createos() +// remember last tick for cli invokes +let lasttick = 0 + // tracking active player ids const SECOND_TIMEOUT = 32 const tracking: Record = {} @@ -67,7 +70,8 @@ const vm = createdevice('vm', ['tick', 'second'], (message) => { break // from clock case 'tick': - memorytick(os, message.data) + lasttick = message.data ?? 0 + memorytick(os, lasttick) break // iterate over logged in players to check activity case 'second': @@ -84,7 +88,7 @@ const vm = createdevice('vm', ['tick', 'second'], (message) => { break // user input from built-in console case 'cli': - memorycli(os, message) + memorycli(os, lasttick, message.player ?? '', message.data ?? '') break // running software messages default: diff --git a/zss/memory/index.ts b/zss/memory/index.ts index 5239679f..44e66b7d 100644 --- a/zss/memory/index.ts +++ b/zss/memory/index.ts @@ -13,6 +13,7 @@ import { createtiles, } from 'zss/gadget/data/types' import { average, unique } from 'zss/mapping/array' +import { createguid } from 'zss/mapping/guid' import { clamp } from 'zss/mapping/number' import { MAYBE, MAYBE_STRING, ispresent, isstring } from 'zss/mapping/types' import { OS } from 'zss/os' @@ -261,14 +262,33 @@ export function memorytick(os: OS, timestamp: number) { context.inputcurrent = undefined // run chip code - os.tick(item.id, timestamp, item.code, item.type) + os.tick(item.id, item.type, timestamp, item.code) } }) } -export function memorycli(os: OS, message: MESSAGE) { - // - console.info('invoke =>', message) +export function memorycli( + os: OS, + timestamp: number, + player: string, + cli: string, +) { + // player id + unique id fo run + const id = `${player}_func` + + // create / update context + const context = memoryreadchip(id) + context.book = undefined + context.board = undefined + context.inputcurrent = undefined + + console.info('running', timestamp, id, cli) + + // run chip code + os.tick(id, CODE_PAGE_TYPE.FUNC, timestamp, cli) + + // halt code + os.halt(id) } function memoryconverttogadgetlayers( diff --git a/zss/os.ts b/zss/os.ts index bc98f03c..7c0257d3 100644 --- a/zss/os.ts +++ b/zss/os.ts @@ -12,11 +12,10 @@ export type OS = { halt: (id: string) => boolean tick: ( id: string, + type: CODE_PAGE_TYPE, timestamp: number, code: string, - type: CODE_PAGE_TYPE, ) => boolean - cli: MESSAGE_FUNC message: MESSAGE_FUNC } @@ -50,15 +49,16 @@ export function createos() { } return !!chip }, - tick(id, timestamp, code, type) { + tick(id, type, timestamp, code) { let chip = chips[id] if (!ispresent(chips[id])) { const result = build(code) + + // bail on errors if (result.errors?.length) { - // todo, need an error message + // todo, need an error message that makes sense api_error('os', 'build', JSON.stringify(result.errors), '') - chips[id] = null return false } @@ -71,10 +71,6 @@ export function createos() { return !!chip?.tick(timestamp) }, - cli(incoming) { - console.info(incoming) - // todo, make this rad !! - }, message(incoming) { const { target, path } = parsetarget(incoming.target) const targetchip: CHIP | null | undefined = chips[target]