move 'vote' to Protocol

This commit is contained in:
modeco80
2024-08-21 22:36:22 -04:00
parent 1673f0abd7
commit 0010a8f300
3 changed files with 28 additions and 6 deletions

View File

@@ -359,9 +359,10 @@ export default class CollabVMServer implements IProtocolHandlers {
if (!this.authCheck(user, this.Config.auth.guestPermissions.callForReset)) return;
if (this.voteCooldown !== 0) {
user.sendMsg(cvm.guacEncode('vote', '3', this.voteCooldown.toString()));
user.protocol.sendVoteCooldown(this.voteCooldown);
return;
}
this.startVote();
this.clients.forEach((c) => c.protocol.sendChatMessage('', `${user.username} has started a vote to reset the VM.`));
}
@@ -918,7 +919,7 @@ export default class CollabVMServer implements IProtocolHandlers {
startVote() {
if (this.voteInProgress) return;
this.voteInProgress = true;
this.clients.forEach((c) => c.sendMsg(cvm.guacEncode('vote', '0')));
this.clients.forEach((c) => c.protocol.sendVoteStarted());
this.voteTime = this.Config.collabvm.voteTime;
this.voteInterval = setInterval(() => {
this.voteTime--;
@@ -933,7 +934,7 @@ export default class CollabVMServer implements IProtocolHandlers {
this.voteInProgress = false;
clearInterval(this.voteInterval);
var count = this.getVoteCounts();
this.clients.forEach((c) => c.sendMsg(cvm.guacEncode('vote', '2')));
this.clients.forEach((c) => c.protocol.sendVoteEnded());
if (result === true || (result === undefined && count.yes >= count.no)) {
this.clients.forEach((c) => c.protocol.sendChatMessage('', 'The vote to reset the VM has won.'));
this.VM.Reset();
@@ -953,9 +954,9 @@ export default class CollabVMServer implements IProtocolHandlers {
sendVoteUpdate(client?: User) {
if (!this.voteInProgress) return;
var count = this.getVoteCounts();
var msg = cvm.guacEncode('vote', '1', (this.voteTime * 1000).toString(), count.yes.toString(), count.no.toString());
if (client) client.sendMsg(msg);
else this.clients.forEach((c) => c.sendMsg(msg));
if (client) client.protocol.sendVoteStats(this.voteTime * 1000, count.yes, count.no);
else this.clients.forEach((c) => c.protocol.sendVoteStats(this.voteTime * 1000, count.yes, count.no));
}
getVoteCounts(): VoteTally {

View File

@@ -291,6 +291,22 @@ export class GuacamoleProtocol extends ProtocolBase implements IProtocol {
this.user?.sendMsg(cvm.guacEncode(...arr));
}
sendVoteStarted(): void {
this.user?.sendMsg(cvm.guacEncode('vote', '0'));
}
sendVoteStats(msLeft: number, nrYes: number, nrNo: number): void {
this.user?.sendMsg(cvm.guacEncode('vote', '1', msLeft.toString(), nrYes.toString(), nrNo.toString()));
}
sendVoteEnded(): void {
this.user?.sendMsg(cvm.guacEncode('vote', '2'));
}
sendVoteCooldown(ms: number): void {
this.user?.sendMsg(cvm.guacEncode('vote', '3', ms.toString()));
}
sendScreenResize(width: number, height: number): void {
this.user?.sendMsg(cvm.guacEncode('size', '0', width.toString(), height.toString()));
}

View File

@@ -114,6 +114,11 @@ export interface IProtocol {
sendListResponse(list: ListEntry[]): void;
sendVoteStarted(): void;
sendVoteStats(msLeft: number, nrYes: number, nrNo: number): void;
sendVoteEnded(): void;
sendVoteCooldown(ms: number): void;
sendScreenResize(width: number, height: number): void;
// Sends a rectangle update to the user.