Skip to content

Commit

Permalink
#28 - StartRoomPollEvent added (#34)
Browse files Browse the repository at this point in the history
* #28 - StartRoomPollEvent added

* Added handler

---------

Co-authored-by: Bill <[email protected]>
  • Loading branch information
oobjectt and billsonnn authored Apr 29, 2023
1 parent becf87b commit 53ab155
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/nitro/communication/NitroMessages.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ export class IncomingHeader
public static POLL_CONTENTS = 2997;
public static POLL_ERROR = 662;
public static POLL_OFFER = 3785;
public static POLL_START_ROOM = 5200;
public static QUESTION_ANSWERED = 2589;
public static QUESTION_FINISHED = 1066;
public static CFH_PENDING_CALLS = 1121;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IMessageEvent } from '../../../../../api';
import { MessageEvent } from '../../../../../events';
import { RoomPollDataParser } from '../../parser';

export class StartRoomPollEvent extends MessageEvent implements IMessageEvent
{
constructor(callBack: Function)
{
super(callBack, RoomPollDataParser);
}

public getParser(): RoomPollDataParser
{
return this.parser as RoomPollDataParser;
}
}
1 change: 1 addition & 0 deletions src/nitro/communication/messages/incoming/poll/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './PollOfferEvent';
export * from './QuestionAnsweredEvent';
export * from './QuestionEvent';
export * from './QuestionFinishedEvent';
export * from './StartRoomPollEvent';
41 changes: 41 additions & 0 deletions src/nitro/communication/messages/parser/poll/RoomPollDataParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { IMessageDataWrapper, IMessageParser } from '../../../../../api';

export class RoomPollDataParser implements IMessageParser
{
private _question: string;
private _choices: string[];

flush(): boolean
{
this._question = null;
this._choices = [];
return true;
}

parse(wrapper: IMessageDataWrapper): boolean
{
this._question = wrapper.readString();
this._choices = [];

const totalChoices = wrapper.readInt();
let total = 0;

while(total < totalChoices)
{
this._choices.push(wrapper.readString());
total++;
}

return true;
}

public get question(): string
{
return this._question;
}

public get choices(): string[]
{
return this._choices.slice();
}
}
1 change: 1 addition & 0 deletions src/nitro/communication/messages/parser/poll/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './PollQuestion';
export * from './QuestionAnsweredParser';
export * from './QuestionFinishedParser';
export * from './QuestionParser';
export * from './RoomPollDataParser';
22 changes: 20 additions & 2 deletions src/nitro/session/handler/PollHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IConnection, IRoomHandlerListener } from '../../../api';
import { RoomSessionPollEvent } from '../../../events';
import { PollContentsEvent, PollErrorEvent, PollOfferEvent } from '../../communication';
import { RoomSessionPollEvent, RoomSessionVoteEvent } from '../../../events';
import { PollContentsEvent, PollErrorEvent, PollOfferEvent, StartRoomPollEvent } from '../../communication';
import { BaseHandler } from './BaseHandler';

export class PollHandler extends BaseHandler
Expand All @@ -12,6 +12,7 @@ export class PollHandler extends BaseHandler
connection.addMessageEvent(new PollContentsEvent(this.onPollContentsEvent.bind(this)));
connection.addMessageEvent(new PollOfferEvent(this.onPollOfferEvent.bind(this)));
connection.addMessageEvent(new PollErrorEvent(this.onPollErrorEvent.bind(this)));
connection.addMessageEvent(new StartRoomPollEvent(this.onStartRoomPollEvent.bind(this)));
}

private onPollContentsEvent(event: PollContentsEvent): void
Expand Down Expand Up @@ -75,4 +76,21 @@ export class PollHandler extends BaseHandler

this.listener.events.dispatchEvent(pollEvent);
}

private onStartRoomPollEvent(event: StartRoomPollEvent): void
{
if(!this.listener) return;

const session = this.listener.getSession(this.roomId);

if(!session) return;

const parser = event.getParser();

if(!parser) return;

const pollEvent = new RoomSessionVoteEvent(RoomSessionVoteEvent.VOTE_QUESTION, session, parser.question, parser.choices);

this.listener.events.dispatchEvent(pollEvent);
}
}

0 comments on commit 53ab155

Please sign in to comment.