dao-governance-framework/forum-network/public/classes/availability.js

50 lines
1.3 KiB
JavaScript

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;
}
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;
// TOOD: Notify assignee
}
}