import { Action } from './action.js'; import { Actor } from './actor.js'; class Worker { stake = 0; available = true; assignedRequestId = null; constructor(reputationPublicKey) { this.reputationPublicKey = reputationPublicKey; } } /** * Purpose: Enable staking reputation to enter the pool of workers */ export class Availability extends Actor { workers = new Map(); constructor(bench, name, scene) { super(name, scene); this.bench = bench; this.actions = { assignWork: new Action('assign work', scene), }; } register(reputationPublicKey, stake) { // ? Is a particular stake amount required? const worker = this.workers.get(reputationPublicKey) ?? new Worker(reputationPublicKey); if (!worker.available) { throw new Error('Worker is already registered and busy. Cannot increase stake.'); } worker.stake += stake; // ? Interact with Bench contract to encumber reputation? this.workers.set(reputationPublicKey, worker); } get availableWorkers() { return Array.from(this.workers.values()).filter(({ available }) => !!available); } async assignWork(requestId) { // Get random worker const index = Math.floor(Math.random() * this.availableWorkers.length); const worker = this.availableWorkers[index]; worker.available = false; worker.assignedRequestId = requestId; // TODO: Notify assignee } async getAssignedWork(reputationPublicKey) { const worker = this.workers.get(reputationPublicKey); return worker.assignedRequestId; } }