Files
goodpuppies--petplay/FYOUNET/actors/SimpleOverlayActor.ts
2024-05-26 22:14:39 +03:00

86 lines
2.2 KiB
TypeScript

// OverlayActor.ts
import { Address } from "../actorsystem/types.ts";
import { actorManager } from "../actorsystem/actorManager.ts";
import { ActorP2P } from "../actorsystem/actorP2P.ts";
import { OVRInterface } from "../helper/helpermodules/OVRInterface.ts";
interface OverlayPayload {
addr: Address<OverlayPayload>;
data: string;
}
export interface OverlayCommand {
type: string;
payload?: CreateBasicOverlayPayload | DelOverlayPayload | MoveOverlayPayload;
}
interface CreateBasicOverlayPayload {
overlayName: string;
pathToTexture: string;
}
interface DelOverlayPayload {
// Define properties specific to deleting an overlay
overlayName: string;
}
interface MoveOverlayPayload {
// Define properties specific to moving an overlay
m34: string;
}
function format(command: OverlayCommand): string {
let result = command.type;
if (command.payload) {
Object.entries(command.payload).forEach(([_key, value]) => {
result += `;${value}`;
});
}
return`${result};`;
}
export class SimpleOverlayActor extends ActorP2P<SimpleOverlayActor> {
private name: string; //name of overlay
private ovrConnector: OVRInterface; //ovr interface
constructor(publicIp: string, name: string, executablePath: string) {
super(name, publicIp);
this.name = name;
this.ovrConnector = new OVRInterface(this.name, executablePath);
this.onStart();
}
async onStart() {
await this.ovrConnector.connect();
this.ovrConnector.subscribe(this.onOverlayMessage.bind(this));
}
async onStop() {
await this.ovrConnector.disconnect();
}
async h_sendToOverlay(_ctx: actorManager, data: string | OverlayCommand) {
if (typeof data !== "string") {
data = format(data);
}
//format(data)
await this.ovrConnector.send(data);
}
private onOverlayMessage(data: string) {
console.log(`Received overlay message: ${data}`);
// Notify other actors or handle the message accordingly
}
async receive(ctx: actorManager, msg: OverlayPayload) {
console.log(`OverlayActor received message: ${msg.data}`);
await this.h_sendToOverlay(ctx,msg.data);
}
}