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

60 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-01-02 13:14:32 -06:00
import { Action } from './action.js';
2022-12-31 16:08:42 -06:00
import { Actor } from './actor.js';
2023-01-01 21:09:02 -06:00
class Worker {
stake = 0;
available = true;
assignedRequestId = null;
constructor(reputationPublicKey) {
this.reputationPublicKey = reputationPublicKey;
}
}
2022-12-31 16:08:42 -06:00
/**
* Purpose: Enable staking reputation to enter the pool of workers
*/
2023-01-01 21:09:02 -06:00
export class Availability extends Actor {
workers = new Map();
constructor(bench, name, scene) {
super(name, scene);
this.bench = bench;
2023-01-02 13:14:32 -06:00
this.actions = {
assignWork: new Action('assign work', scene),
};
2023-01-01 21:09:02 -06:00
}
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;
2023-01-02 13:14:32 -06:00
// TODO: Notify assignee
}
async getAssignedWork(reputationPublicKey) {
const worker = this.workers.get(reputationPublicKey);
return worker.assignedRequestId;
2023-01-01 21:09:02 -06:00
}
}