robosats/frontend/src/models/Slot.model.ts

79 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-12-15 15:17:46 +00:00
import { sha256 } from 'js-sha256';
2023-11-21 17:36:59 +00:00
import { Robot, type Order } from '.';
2024-04-30 14:01:54 +00:00
import { roboidentitiesClient } from '../services/Roboidentities/Web';
2023-11-21 17:36:59 +00:00
class Slot {
2024-05-01 18:11:20 +00:00
constructor(
token: string,
shortAliases: string[],
robotAttributes: Record<any, any>,
onRobotUpdate: () => void,
) {
2023-11-21 17:36:59 +00:00
this.token = token;
2023-12-15 15:17:46 +00:00
this.hashId = sha256(sha256(this.token));
2024-04-28 12:34:32 +00:00
this.nickname = null;
2024-04-27 21:33:52 +00:00
roboidentitiesClient.generateRoboname(this.hashId).then((nickname) => {
this.nickname = nickname;
2024-05-01 18:11:20 +00:00
onRobotUpdate();
2024-04-27 21:33:52 +00:00
});
2024-04-28 12:34:32 +00:00
roboidentitiesClient.generateRobohash(this.hashId, 'small');
roboidentitiesClient.generateRobohash(this.hashId, 'large');
2023-12-15 15:17:46 +00:00
this.robots = shortAliases.reduce((acc: Record<string, Robot>, shortAlias: string) => {
acc[shortAlias] = new Robot(robotAttributes);
return acc;
}, {});
2023-11-21 17:36:59 +00:00
this.order = null;
2023-12-15 15:17:46 +00:00
2023-11-21 17:36:59 +00:00
this.activeShortAlias = null;
this.lastShortAlias = null;
this.copiedToken = false;
2024-05-01 18:11:20 +00:00
onRobotUpdate();
2023-11-21 17:36:59 +00:00
}
token: string | null;
2023-12-15 15:17:46 +00:00
hashId: string | null;
nickname: string | null;
2023-12-02 19:50:07 +00:00
robots: Record<string, Robot>;
2023-11-21 17:36:59 +00:00
order: Order | null;
activeShortAlias: string | null;
lastShortAlias: string | null;
copiedToken: boolean;
setCopiedToken = (copied: boolean): void => {
this.copiedToken = copied;
};
getRobot = (shortAlias?: string): Robot | null => {
2023-12-22 12:58:59 +00:00
if (shortAlias) {
2023-11-21 17:36:59 +00:00
return this.robots[shortAlias];
2023-12-22 12:58:59 +00:00
} else if (this.activeShortAlias !== null && this.robots[this.activeShortAlias]) {
2023-11-21 17:36:59 +00:00
return this.robots[this.activeShortAlias];
2023-12-22 12:58:59 +00:00
} else if (this.lastShortAlias !== null && this.robots[this.lastShortAlias]) {
2023-11-21 17:36:59 +00:00
return this.robots[this.lastShortAlias];
} else if (Object.values(this.robots).length > 0) {
return Object.values(this.robots)[0];
}
return null;
};
2024-01-23 10:37:04 +00:00
updateRobot = (shortAlias: string, attributes: Record<any, any>): Robot | null => {
2023-11-21 17:36:59 +00:00
this.robots[shortAlias].update(attributes);
if (attributes.lastOrderId) {
2023-11-21 17:36:59 +00:00
this.lastShortAlias = shortAlias;
if (this.activeShortAlias === shortAlias) {
this.activeShortAlias = null;
}
}
if (attributes.activeOrderId) {
2023-11-21 17:36:59 +00:00
this.activeShortAlias = attributes.shortAlias;
}
return this.robots[shortAlias];
};
}
export default Slot;