Skip to content

Commit

Permalink
PPF Bot support
Browse files Browse the repository at this point in the history
  • Loading branch information
ByPikod committed Dec 10, 2023
1 parent 05ff604 commit 5c95b0d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 50 deletions.
26 changes: 13 additions & 13 deletions bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
Channel,
Subscriptions,
Message,
OnlineData,
import {
Channel,
Subscriptions,
Message,
OnlineData,
CPNBot
} from "../src";

Expand All @@ -16,32 +16,32 @@ const bot = new CPNBot()
* Log when channels loaded
*/
bot.on("channelsLoaded", (channels: Channel[]) => {

const chatNames = channels.map(
(channel: Channel) => channel.getName()
);

info(`Subscribed channels: ${chatNames.join(", ")}`)

const channel = bot.getChannelByName("en")
if(!channel) return;
if (!channel) return;

/*
bot.sendAnnouncement("Test announcement", channel, true).then((data: any) => {
info(`Announcement sent, total receivers: ${data[0]}`)
});
*/

})

/**
* Log chat messages
*/
bot.on("chatMessage", (message: Message) => {

info(
`[${message.getAuthor().getFlag()}] ` +
`${message.getAuthor().getName()} > ` +
`${message.getAuthor().getName()} > ` +
`${message.getChatRoom().getName()}: ` +
`${message.getContent()}`
)
Expand Down Expand Up @@ -70,7 +70,7 @@ bot.on("heartbeat", () => {
bot.on("open", () => {

info("Bot connected!")

// Try fetch flag feature
bot.fetchFlag(1).then((data) => {
info(`Flag: ${data[0]}, ID: ${data[1]}`)
Expand All @@ -84,7 +84,7 @@ bot.on("open", () => {
})

bot.connect(
config.url,
config.url,
config.apiKey,
Subscriptions.CHAT + Subscriptions.ONLINE
)
37 changes: 18 additions & 19 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ export class Bot extends EventEmitter {
_ws: WebSocket | undefined
_timeout: number = 5000

private subscriptions: Subscriptions = 0
private channels = new Map<number, Channel>()
private users: Array<User> = []
private online: OnlineData = { total: 0 }

private botName: string
private botChatId: number
private botCountry: string
private autoReconnect: boolean
private url?: string
private apiKey?: string

private timeLastPing: number = 0
private timeLastSent: number = 0
private heartbeatTimer?: NodeJS.Timer
protected subscriptions: Subscriptions = 0
protected channels = new Map<number, Channel>()
protected users: Array<User> = []
protected online: OnlineData = { total: 0 }

protected botName: string
protected botChatId: number
protected botCountry: string
protected autoReconnect: boolean
protected url?: string
protected apiKey?: string

protected timeLastPing: number = 0
protected timeLastSent: number = 0
protected heartbeatTimer?: NodeJS.Timer

constructor(
data: BotConstructor = {
Expand Down Expand Up @@ -158,7 +158,7 @@ export class Bot extends EventEmitter {
//*****************************//

/**
* Pping the server in every 5 seconds.
* Ping the server every 5 seconds.
* @returns Returns false if connection is terminated.
*/
protected heartbeat(): boolean {
Expand Down Expand Up @@ -293,11 +293,10 @@ export class Bot extends EventEmitter {
_createUser(
authorId: number,
authorName: string,
authorFlag: string,
authorBadges: string
authorFlag: string
): User {

const user = new User(authorId, authorName, authorFlag, authorBadges, this)
const user = new User(authorId, authorName, authorFlag, this)
this.users[user.getId()] = user

return user
Expand Down
38 changes: 27 additions & 11 deletions src/cpnbot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Bot } from "./bot"
import { Channel } from "./channel"
import { User } from "./user"
import { CPNUser } from "./cpnuser"
import { promiseWithTimeout } from "./utils"

/**
Expand All @@ -13,14 +14,14 @@ export class CPNBot extends Bot {
* CPN special packet handler.
*/
protected handleTextPacket(type: string, data: any): void {


switch(type) {

switch (type) {
case "announceRes":
this.emit("announceRes", data)
return
}

super.handleTextPacket(type, data)

}
Expand All @@ -32,22 +33,22 @@ export class CPNBot extends Bot {
* @param popup Message will pop up if true.
*/
sendAnnouncement(
message: string,
message: string,
receiver: Channel | User | number,
popup: boolean
): Promise<number> {
return promiseWithTimeout(this._timeout, new Promise((res, rej) => {

const toUser = !(receiver instanceof Channel)

// Turn reciever data to id.
let receiverId: number
if(toUser) {
if(receiver instanceof User)
if (toUser) {
if (receiver instanceof User)
receiverId = receiver.getId()
else
receiverId = receiver
}else
} else
receiverId = receiver.getId()

// Send the packet
Expand All @@ -56,19 +57,34 @@ export class CPNBot extends Bot {
["announce", message, receiverId, popup, toUser]
)
)

// Wait for response
const respondListener = (data: any) => {
if(data[1] !== undefined) {
if (data[1] !== undefined) {
rej(data[1]);
return;
}
res(data)
}

this.once("announceRes", respondListener)

}))
}

/**
* Create a channel room to listen it.
* @param name
*/
_createCPNUser(
authorId: number,
authorName: string,
authorFlag: string,
authorBadges: string
): CPNUser {
const user = new CPNUser(authorId, authorName, authorFlag, authorBadges, this)
this.users[user.getId()] = user as User
return user
}

}
19 changes: 19 additions & 0 deletions src/cpnuser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Bot } from "./bot"
import { User } from "./user"

export class CPNUser extends User {
private badges: string

constructor(id: number, name: string, flag: string, badges: string, bot: Bot) {
super(id, name, flag, bot)
this.badges = badges
}

/**
* Returns the badges of the user
* @returns string
*/
getBadges(): string {
return this.badges;
}
}
28 changes: 24 additions & 4 deletions src/packets/message.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
import { BotFailedToFetchChannel } from "../exceptions"
import { Bot } from "../bot"
import { Message } from "../message"
import { CPNBot } from "../cpnbot"

/**
* We need to check if the bot is a CPNBot or not in the runtime
* because typescript somehow doesn't allow us to check if the bot is a CPNBot
* @param bot
* @returns boolean
*/
function isCpnBot(bot: Bot) {
return (bot as CPNBot).sendAnnouncement !== undefined;
}

/**
* Turns the data pack into a Message object and reutrns it
* @param data
* @returns Message
*/
export function recievedMessage(bot: Bot, data: any): Message {
export function recievedMessage(bot: Bot | CPNBot, data: any): Message {

const authorName = data[0]
const authorId = data[1]
const messageContent = data[2]
const authorFlag = data[3]
const authorBadges = data[4]
const channelId = data[5]

let authorBadges, channelId;
if (isCpnBot(bot)) {
authorBadges = data[4]
channelId = data[5];
} else {
channelId = data[4];
}

/* Get user */
let user = bot.getUserById(authorId)
if (user === undefined) {
user = bot._createUser(authorId, authorName, authorFlag, authorBadges);
if (isCpnBot(bot))
user = (bot as CPNBot)._createCPNUser(authorId, authorName, authorFlag, authorBadges);
else
user = bot._createUser(authorId, authorName, authorFlag);
}

/* Get channel */
Expand Down
4 changes: 1 addition & 3 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ export class User {
private id: number
private name: string
private flag: string
private badges: string
private bot: Bot
private messageHistory = new MessageHistory();

constructor(id: number, name: string, flag: string, badges: string, bot: Bot) {
constructor(id: number, name: string, flag: string, bot: Bot) {
this.id = id
this.name = name
this.flag = flag
this.badges = badges
this.bot = bot
}

Expand Down

0 comments on commit 5c95b0d

Please sign in to comment.