Skip to content

Commit 53ab155

Browse files
oobjecttbillsonnn
andauthored
#28 - StartRoomPollEvent added (#34)
* #28 - StartRoomPollEvent added * Added handler --------- Co-authored-by: Bill <[email protected]>
1 parent becf87b commit 53ab155

File tree

7 files changed

+82
-3
lines changed

7 files changed

+82
-3
lines changed

src/nitro/communication/NitroMessages.ts

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

src/nitro/communication/messages/incoming/IncomingHeader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ export class IncomingHeader
399399
public static POLL_CONTENTS = 2997;
400400
public static POLL_ERROR = 662;
401401
public static POLL_OFFER = 3785;
402+
public static POLL_START_ROOM = 5200;
402403
public static QUESTION_ANSWERED = 2589;
403404
public static QUESTION_FINISHED = 1066;
404405
public static CFH_PENDING_CALLS = 1121;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { IMessageEvent } from '../../../../../api';
2+
import { MessageEvent } from '../../../../../events';
3+
import { RoomPollDataParser } from '../../parser';
4+
5+
export class StartRoomPollEvent extends MessageEvent implements IMessageEvent
6+
{
7+
constructor(callBack: Function)
8+
{
9+
super(callBack, RoomPollDataParser);
10+
}
11+
12+
public getParser(): RoomPollDataParser
13+
{
14+
return this.parser as RoomPollDataParser;
15+
}
16+
}

src/nitro/communication/messages/incoming/poll/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './PollOfferEvent';
44
export * from './QuestionAnsweredEvent';
55
export * from './QuestionEvent';
66
export * from './QuestionFinishedEvent';
7+
export * from './StartRoomPollEvent';
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { IMessageDataWrapper, IMessageParser } from '../../../../../api';
2+
3+
export class RoomPollDataParser implements IMessageParser
4+
{
5+
private _question: string;
6+
private _choices: string[];
7+
8+
flush(): boolean
9+
{
10+
this._question = null;
11+
this._choices = [];
12+
return true;
13+
}
14+
15+
parse(wrapper: IMessageDataWrapper): boolean
16+
{
17+
this._question = wrapper.readString();
18+
this._choices = [];
19+
20+
const totalChoices = wrapper.readInt();
21+
let total = 0;
22+
23+
while(total < totalChoices)
24+
{
25+
this._choices.push(wrapper.readString());
26+
total++;
27+
}
28+
29+
return true;
30+
}
31+
32+
public get question(): string
33+
{
34+
return this._question;
35+
}
36+
37+
public get choices(): string[]
38+
{
39+
return this._choices.slice();
40+
}
41+
}

src/nitro/communication/messages/parser/poll/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './PollQuestion';
66
export * from './QuestionAnsweredParser';
77
export * from './QuestionFinishedParser';
88
export * from './QuestionParser';
9+
export * from './RoomPollDataParser';

src/nitro/session/handler/PollHandler.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IConnection, IRoomHandlerListener } from '../../../api';
2-
import { RoomSessionPollEvent } from '../../../events';
3-
import { PollContentsEvent, PollErrorEvent, PollOfferEvent } from '../../communication';
2+
import { RoomSessionPollEvent, RoomSessionVoteEvent } from '../../../events';
3+
import { PollContentsEvent, PollErrorEvent, PollOfferEvent, StartRoomPollEvent } from '../../communication';
44
import { BaseHandler } from './BaseHandler';
55

66
export class PollHandler extends BaseHandler
@@ -12,6 +12,7 @@ export class PollHandler extends BaseHandler
1212
connection.addMessageEvent(new PollContentsEvent(this.onPollContentsEvent.bind(this)));
1313
connection.addMessageEvent(new PollOfferEvent(this.onPollOfferEvent.bind(this)));
1414
connection.addMessageEvent(new PollErrorEvent(this.onPollErrorEvent.bind(this)));
15+
connection.addMessageEvent(new StartRoomPollEvent(this.onStartRoomPollEvent.bind(this)));
1516
}
1617

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

7677
this.listener.events.dispatchEvent(pollEvent);
7778
}
79+
80+
private onStartRoomPollEvent(event: StartRoomPollEvent): void
81+
{
82+
if(!this.listener) return;
83+
84+
const session = this.listener.getSession(this.roomId);
85+
86+
if(!session) return;
87+
88+
const parser = event.getParser();
89+
90+
if(!parser) return;
91+
92+
const pollEvent = new RoomSessionVoteEvent(RoomSessionVoteEvent.VOTE_QUESTION, session, parser.question, parser.choices);
93+
94+
this.listener.events.dispatchEvent(pollEvent);
95+
}
7896
}

0 commit comments

Comments
 (0)