Skip to content

Commit

Permalink
被封暂停抽奖
Browse files Browse the repository at this point in the history
  • Loading branch information
lzghzr committed Nov 21, 2018
1 parent b107fe6 commit 4f4d44f
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 77 deletions.
2 changes: 1 addition & 1 deletion bilive/@types/bilive.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface server {
protocol: string
}
interface config {
[index: string]: number | string | number[]
[index: string]: string | boolean | number | number[]
defaultUserID: number
serverURL: string
eventRooms: number[]
Expand Down
101 changes: 66 additions & 35 deletions bilive/plugins/raffle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@ class Raffle extends Plugin {
public description = '自动参与抽奖'
public version = '0.0.1'
public author = 'lzghzr'
// 是否开启抽奖
/**
* 是否开启抽奖
*
* @private
* @memberof Raffle
*/
private _raffle = false
/**
* 被封关闭抽奖
*
* @private
* @type {Map<string,boolean>}
* @memberof Raffle
*/
private _raffleBanList: Map<string, boolean> = new Map()
public async load({ defaultOptions, whiteList }: { defaultOptions: options, whiteList: Set<string> }) {
// 抽奖延时
defaultOptions.config['raffleDelay'] = 0
Expand All @@ -35,7 +48,14 @@ class Raffle extends Plugin {
tip: '就是每个用户多少概率漏掉1个奖啦,范围0~100',
type: 'number'
}
whiteList.add('droprate')
// 被封停止
defaultOptions.config['raffleBan'] = false
defaultOptions.info['raffleBan'] = {
description: '被封停止',
tip: '检测到被封以后停止抽奖',
type: 'boolean'
}
whiteList.add('raffleBan')
// 小电视抽奖
defaultOptions.newUserData['smallTV'] = false
defaultOptions.info['smallTV'] = {
Expand Down Expand Up @@ -70,45 +90,56 @@ class Raffle extends Plugin {
whiteList.add('beatStorm')
this.loaded = true
}
public async loop({ cstHour, options }: { cstHour: number, options: options }) {
public async start({ options }: { options: options }) {
const csttime = Date.now() + 8 * 60 * 60 * 1000
const cst = new Date(csttime)
const cstHour = cst.getUTCHours()
// 抽奖暂停
const rafflePause = <number[]>options.config['rafflePause']
this._raffle = this._isRafflePause(cstHour, rafflePause)
}
public async loop({ cstMin, cstHour, options }: { cstMin: number, cstHour: number, options: options }) {
// 每天00:00, 10:00, 20:00刷新
if (cstMin === 0 && cstHour % 10 === 0) this._raffleBanList.clear()
// 抽奖暂停
const rafflePause = <number[]>options.config['rafflePause']
this._raffle = this._isRafflePause(cstHour, rafflePause)
}
public async msg({ message, options, users }: { message: raffleMessage | lotteryMessage | beatStormMessage, options: options, users: Map<string, User> }) {
if (!this._raffle) return
users.forEach(async (user, uid) => {
if (user.captchaJPEG !== '' || !user.userData[message.cmd] || (options.config['raffleBan'] && this._raffleBanList.get(uid))) return
const droprate = <number>options.config['droprate']
if (droprate !== 0 && Math.random() < droprate / 100) tools.Log(user.nickname, '丢弃抽奖', message.id)
else {
const raffleDelay = <number>options.config['raffleDelay']
if (raffleDelay !== 0) await tools.Sleep(raffleDelay)
// @ts-ignore
if (message.time_wait !== undefined) await tools.Sleep(message.time_wait * 1000)
if (options.config['raffleBan'] && this._raffleBanList.get(uid)) return
const raffleBan = await new Lottery(message, user).Start()
if (raffleBan === 'raffleBan') this._raffleBanList.set(uid, true)
}
})
}
/**
* 是否暂停抽奖
*
* @private
* @param {number} cstHour
* @param {number[]} rafflePause
* @returns {boolean}
* @memberof Raffle
*/
private _isRafflePause(cstHour: number, rafflePause: number[]): boolean {
if (rafflePause.length > 1) {
const start = rafflePause[0]
const end = rafflePause[1]
if (start > end && (cstHour >= start || cstHour < end) || (cstHour >= start && cstHour < end)) this._raffle = false
else this._raffle = true
}
else this._raffle = true
}
public async msg({ message, options, users }: { message: raffleMessage | lotteryMessage | beatStormMessage, options: options, users: Map<string, User> }) {
if (this._raffle) {
users.forEach(async user => {
if (user.captchaJPEG === '' && user.userData[message.cmd]) {
const droprate = <number>options.config['droprate']
if (droprate !== 0 && Math.random() < droprate / 100)
tools.Log(user.nickname, '丢弃抽奖', message.id)
else {
const raffleDelay = <number>options.config['raffleDelay']
if (raffleDelay !== 0) await tools.Sleep(raffleDelay)
switch (message.cmd) {
case 'smallTV':
new Lottery(message, user).SmallTV()
break
case 'raffle':
new Lottery(message, user).Raffle()
break
case 'lottery':
new Lottery(message, user).Lottery()
break
case 'beatStorm':
new Lottery(message, user).BeatStorm()
break
}
}
}
})
if (start > end && (cstHour >= start || cstHour < end) || (cstHour >= start && cstHour < end))
return false
return true
}
return true
}
}

Expand Down
102 changes: 64 additions & 38 deletions bilive/plugins/raffle/raffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { tools, AppClient } from '../../plugin'
*/
class Raffle {
/**
* 创建一个 Raffle 实例
* @param {raffleMessage | lotteryMessage | beatStormMessage} raffleMessage
* Creates an instance of Raffle.
* @param {(raffleMessage | lotteryMessage | beatStormMessage)} raffleMessage
* @param {User} user
* @memberof Raffle
*/
constructor(raffleMessage: raffleMessage | lotteryMessage | beatStormMessage, user: User) {
Expand Down Expand Up @@ -39,14 +40,38 @@ class Raffle {
* @memberof Raffle
*/
private _url!: string
/**
* 开始抽奖
*
* @returns
* @memberof Raffle
*/
public async Start() {
let raffleBan: string | void
switch (this._raffleMessage.cmd) {
case 'smallTV':
raffleBan = await this.SmallTV()
break
case 'raffle':
raffleBan = await this.Raffle()
break
case 'lottery':
raffleBan = await this.Lottery()
break
case 'beatStorm':
raffleBan = await this.BeatStorm()
break
}
return raffleBan
}
/**
* 参与小电视抽奖
*
* @memberof Raffle
*/
public SmallTV() {
this._url = 'https://api.live.bilibili.com/gift/v4/smalltv'
this._Raffle()
return this._Raffle()
}
/**
* 参与Raffle类抽奖
Expand All @@ -55,7 +80,7 @@ class Raffle {
*/
public Raffle() {
this._url = 'https://api.live.bilibili.com/gift/v4/smalltv'
this._Raffle()
return this._Raffle()
}
/**
* 参与Lottery类抽奖
Expand All @@ -64,16 +89,16 @@ class Raffle {
*/
public Lottery() {
this._url = 'https://api.live.bilibili.com/lottery/v1/lottery'
this._Lottery()
return this._Lottery()
}
/**
* 参与节奏风暴
*
* @memberof Raffle
*/
public async BeatStorm() {
public BeatStorm() {
this._url = 'https://api.live.bilibili.com/lottery/v1/Storm'
this._BeatStorm()
return this._BeatStorm()
}
/**
* Raffle类抽奖
Expand All @@ -82,16 +107,16 @@ class Raffle {
* @memberof Raffle
*/
private async _Raffle() {
await tools.Sleep((<raffleMessage>this._raffleMessage).time_wait * 1000)
this._RaffleAward()
// 因为app抽奖并没有join步骤, 所以此项暂时留空
return this._RaffleAward()
}
/**
* 获取抽奖结果
*
* @private
* @memberof Raffle
*/
private async _RaffleAward() {
private async _RaffleAward(): Promise<'raffleBan' | void> {
const { id, roomID, title, type } = this._raffleMessage
const reward: requestOptions = {
method: 'POST',
Expand All @@ -100,28 +125,30 @@ class Raffle {
json: true,
headers: this._user.headers
}
tools.XHR<raffleAward>(reward, 'Android').then(raffleAward => {
if (raffleAward !== undefined && raffleAward.response.statusCode === 200) {
if (raffleAward.body.code === 0) {
const gift = raffleAward.body.data
if (gift.gift_num === 0) tools.Log(this._user.nickname, title, id, raffleAward.body.msg)
else {
const msg = `${this._user.nickname} ${title} ${id} 获得 ${gift.gift_num}${gift.gift_name}`
tools.Log(msg)
if (gift.gift_name.includes('小电视')) tools.sendSCMSG(msg)
}
const raffleAward = await tools.XHR<raffleAward>(reward, 'Android')
if (raffleAward !== undefined && raffleAward.response.statusCode === 200) {
if (raffleAward.body.code === 0) {
const gift = raffleAward.body.data
if (gift.gift_num === 0) tools.Log(this._user.nickname, title, id, raffleAward.body.msg)
else {
const msg = `${this._user.nickname} ${title} ${id} 获得 ${gift.gift_num}${gift.gift_name}`
tools.Log(msg)
if (gift.gift_name.includes('小电视')) tools.sendSCMSG(msg)
}
else tools.Log(this._user.nickname, title, id, raffleAward.body)
}
})
else {
tools.Log(this._user.nickname, title, id, raffleAward.body)
if (raffleAward.body.code === 400) return 'raffleBan'
}
}
}
/**
* Lottery类抽奖
*
* @private
* @memberof Raffle
*/
private async _Lottery() {
private async _Lottery(): Promise<'raffleBan' | void> {
const { id, roomID, title, type } = this._raffleMessage
const reward: requestOptions = {
method: 'POST',
Expand All @@ -130,14 +157,14 @@ class Raffle {
json: true,
headers: this._user.headers
}
tools.XHR<lotteryReward>(reward, 'Android').then(lotteryReward => {
if (lotteryReward !== undefined && lotteryReward.response.statusCode === 200) {
if (lotteryReward.body.code === 0) {
tools.Log(this._user.nickname, title, id, lotteryReward.body.data.message)
}
else tools.Log(this._user.nickname, title, id, lotteryReward.body)
const lotteryReward = await tools.XHR<lotteryReward>(reward, 'Android')
if (lotteryReward !== undefined && lotteryReward.response.statusCode === 200) {
if (lotteryReward.body.code === 0) tools.Log(this._user.nickname, title, id, lotteryReward.body.data.message)
else {
tools.Log(this._user.nickname, title, id, lotteryReward.body)
if (lotteryReward.body.code === 400) return 'raffleBan'
}
})
}
}
/**
* 节奏风暴
Expand All @@ -154,14 +181,13 @@ class Raffle {
json: true,
headers: this._user.headers
}
tools.XHR<joinStorm>(join, 'Android').then(joinStorm => {
if (joinStorm !== undefined && joinStorm.response.statusCode === 200 && joinStorm.body !== undefined) {
const content = joinStorm.body.data
if (content !== undefined && content.gift_num > 0)
tools.Log(this._user.nickname, title, id, `${content.mobile_content} 获得 ${content.gift_num}${content.gift_name}`)
else tools.Log(this._user.nickname, title, id, joinStorm.body)
}
})
const joinStorm = await tools.XHR<joinStorm>(join, 'Android')
if (joinStorm !== undefined && joinStorm.response.statusCode === 200 && joinStorm.body !== undefined) {
const content = joinStorm.body.data
if (content !== undefined && content.gift_num > 0)
tools.Log(this._user.nickname, title, id, `${content.mobile_content} 获得 ${content.gift_num}${content.gift_name}`)
else tools.Log(this._user.nickname, title, id, joinStorm.body)
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
"devDependencies": {
"@types/bootstrap": "^4.1.2",
"@types/jquery": "^3.3.22",
"@types/node": "^10.12.2",
"@types/request": "^2.48.0",
"@types/node": "^10.12.9",
"@types/request": "^2.48.1",
"@types/ws": "^5.1.2",
"npm-run-posix-or-windows": "^2.0.2",
"rimraf": "^2.6.2",
"typescript": "^3.1.6"
},
"dependencies": {
"request": "^2.88.0",
"ws": "^6.1.0"
"ws": "^6.1.2"
}
}

0 comments on commit 4f4d44f

Please sign in to comment.