67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import { randomUUID } from "crypto";
|
|
|
|
export interface LedgerEntry {
|
|
timestamp: number,
|
|
type: ('post' | 'citation'),
|
|
postId: string,
|
|
citationId: string | null,
|
|
change: number,
|
|
balance: number
|
|
}
|
|
|
|
export class Member {
|
|
|
|
private static localStore = new Map<string, Member>();
|
|
|
|
public static getAllMembers() {
|
|
return Array.from(Member.localStore.values());
|
|
}
|
|
|
|
public static getMember(id: string) {
|
|
return Member.localStore.get(id);
|
|
}
|
|
|
|
private _id: string;
|
|
private _reputation: number;
|
|
private _ledger: LedgerEntry[];
|
|
|
|
constructor(id?: string) {
|
|
this._id = id ? id : randomUUID();
|
|
this._reputation = 0;
|
|
this._ledger = [];
|
|
Member.localStore.set(this.id, this);
|
|
}
|
|
|
|
public get id() { return this._id; }
|
|
public get reputation() { return this._reputation; }
|
|
public get ledger() { return this._ledger; }
|
|
|
|
public postReputation(postId: string, amount: number) {
|
|
const entry: LedgerEntry = {
|
|
timestamp: Date.now(),
|
|
type: "post",
|
|
postId: postId,
|
|
citationId: null,
|
|
change: amount,
|
|
balance: this.reputation + amount
|
|
}
|
|
this._ledger.push(entry);
|
|
this._reputation = entry.balance;
|
|
return entry;
|
|
}
|
|
|
|
public citationReputation(postId: string, citationId: string, amount: number) {
|
|
const entry: LedgerEntry = {
|
|
timestamp: Date.now(),
|
|
type: "citation",
|
|
postId: postId,
|
|
citationId: citationId,
|
|
change: amount,
|
|
balance: this.reputation + amount
|
|
}
|
|
this._ledger.push(entry);
|
|
this._reputation = entry.balance;
|
|
return entry;
|
|
}
|
|
|
|
} |