formatting, minor refactor, ui engine start

materials
Valerie 2021-07-25 18:52:19 -04:00
parent a75c18d197
commit c062ce445f
6 changed files with 177 additions and 44 deletions

17
src/Memories.ts 100644
View File

@ -0,0 +1,17 @@
import { Game } from "@game";
import { Pawn } from "./Pawn.js";
export function injectTravelMemory(target: Pawn) {
return {
type: "travel",
time: {
age: target.age,
locale: Game.current.clock.toString()
},
location: Game.current.name
}
}
function injectMemory(pawn: Pawn, memory: any) {
pawn.memories.push(memory);
}

View File

@ -20,6 +20,10 @@ export type BirthMemory = ProtoMemory & {
location: string, location: string,
} }
export type AnyMemory =
TravelMemory
| BirthMemory;
export function stringify(memory: Memory): string { export function stringify(memory: Memory): string {
switch(memory.type) { switch(memory.type) {
case "birth": { case "birth": {

View File

@ -11,66 +11,60 @@ import { inspect } from 'util'
import { Pawn } from '../Pawn.js'; import { Pawn } from '../Pawn.js';
import { Game } from '../Game.js'; import { Game } from '../Game.js';
import { Player } from './Player.js'; import { Player } from './Player.js';
import { injectTravelMemory } from '../Memories.js';
const mdns = bonjour(); const mdns = bonjour();
const ID = uuid.v4(); const ID = uuid.v4();
let devices: Player[] = []; let devices: Player[] = [];
const network = { const network = {
get players() { get players() {
return devices; return devices;
} }
} }
export type GiftMessage = { export type GiftMessage = {
pawns: string[], pawns: string[],
from: string from: string
} }
export default network; export default network;
export async function ready(name: string) { export async function ready(name: string) {
const port = await getPort({port: getPort.makeRange(52300, 52399)}); const port = await getPort({port: getPort.makeRange(52300, 52399)});
mdns.publish({ mdns.publish({
type: 'dfi', type: 'dfi',
name, name,
port: port port: port
}); });
const wss = new WebSocket.Server({ port }); const wss = new WebSocket.Server({ port });
wss.on('connection', function connection(ws) { wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) { ws.on('message', function incoming(message) {
const {pawns: pawnJsons, from} = JSON.parse(message.toString()); const {pawns: pawnJsons, from} = JSON.parse(message.toString());
const pawns = []; const pawns = [];
for(const pawnJson of pawnJsons) { for(const pawnJson of pawnJsons) {
const pawn: Pawn = Pawn.fromJson(pawnJson); const pawn: Pawn = Pawn.fromJson(pawnJson);
pawn.memories.push({ pawns.push(pawn);
type: "travel", injectTravelMemory(pawn);
time: { }
age: pawn.age, Popup.show(`${(() => {
locale: Game.current.clock.toString() if(pawns.length === 0) return `A care package has arrived from ${from}.`;
}, if(pawns.length === 1) return `A traveler from ${from} named ${pawns[0].toString()} has arrived.`;
location: Game.current.name if(pawns.length > 1) return `A caravan of ${pawns.length} people from ${from} has arrived.`
}) })()}`);
pawns.push(pawn); for(const pawn of pawns) Game.current.pawns.push(pawn);
} });
Popup.show(`${(() => { });
if(pawns.length === 0) return `A care package has arrived from ${from}.`;
if(pawns.length === 1) return `A traveler from ${from} named ${pawns[0].toString()} has arrived.`;
if(pawns.length > 1) return `A caravan of ${pawns.length} people from ${from} has arrived.`
})()}`);
for(const pawn of pawns) Game.current.pawns.push(pawn);
});
});
} }
mdns.find({ mdns.find({
type: 'dfi' type: 'dfi'
}, (service) => { }, (service) => {
const p = new Player(); const p = new Player();
p.name = service.name; p.name = service.name;
p.host = service.host; p.host = service.host;
p.port = service.port; p.port = service.port;
devices.push(p); devices.push(p);
}).on("down", (service) => { }).on("down", (service) => {
// TODO remove player from MP // TODO remove player from MP
}) })

View File

@ -20,6 +20,7 @@
}, },
"include": [ "include": [
"src/**/*.ts", "src/**/*.ts",
"content/**/*.ts" "content/**/*.ts",
"ui-engine/**/*.ts"
] ]
} }

55
ui-engine/Node.ts 100644
View File

@ -0,0 +1,55 @@
export abstract class Node {
x: number;
y: number;
w: number;
h: number;
children: Node[]
constructor() {
this.children = [];
}
layout(w: number, h: number) {
this.w = w;
this.h = h;
this.onResize(w, h);
this.children.forEach(node => node.layout(w, h));
}
append(child: Node) {
this.children.push(child);
this.layout(this.w, this.h);
}
abstract render(x: number, y: number): [number, string];
abstract onResize(w: number, h: number): void
}
export class TextNode extends Node {
_content: string;
constructor(content: string) {
super();
this.content = content;
}
set content(val: string) {
this._content = val
this.layout(this.w, this.h);
}
onResize(w: number, h: number): void {
return;
}
render(x: number, y: number): [number, string] {
if(y !== this.y) return null;
if(x < this.x) return null;
if(x > this.x + this.content.length) return null;
return [0, this.content[x - this.x]];
}
}

62
ui-engine/index.ts 100644
View File

@ -0,0 +1,62 @@
import ansi from 'sisteransi';
import { Node, TextNode } from './Node.js';
const TERMINAL_HIGH_BUFFER = '\x1b[?1049h';
const TERMINAL_LOW_BUFFER = '\x1b[?1049l'
class Screen extends Node {
buffer: ArrayBuffer;
bufferView: Uint8Array;
paletteBuffer: ArrayBuffer;
paletteBufferView: Uint8Array;
constructor() {
super();
process.stdout.write(TERMINAL_HIGH_BUFFER);
process.stdout.write(ansi.cursor.hide);
this.layout(process.stdout.columns, process.stdout.rows);
}
onResize(w: number, h: number) {
this.buffer = new ArrayBuffer(w * h);
this.bufferView = new Uint8Array(this.buffer);
this.paletteBuffer = new ArrayBuffer(w * h);
this.paletteBufferView = new Uint8Array(this.paletteBuffer);
}
// paletteAt(x: number, y: number) {
// }
offset(x: number, y: number) {
return y * this.h + x
}
updateTerminal() {
process.stdout.write(ansi.cursor.to(0, 0))
for(let y = 0; y < this.h; y ++) {
for(let x = 0; x < this.w; x ++) {
}
process.stdout.write('\r\n')
}
}
render(x: number, y: number): [number, string] {
throw new Error('Method not implemented.');
}
destroy() {
process.stdout.write(TERMINAL_LOW_BUFFER);
process.stdout.write(ansi.cursor.show);
}
}
// class Palette {
// }
const screen = new Screen();
screen.append(new TextNode('Test'))