Skip to content

Commit

Permalink
feat: 检查今日是否登岛检票过
Browse files Browse the repository at this point in the history
  • Loading branch information
enpitsuLin committed Aug 12, 2024
1 parent e73bc30 commit 68b1cf3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"dotenv": "^16.3.1",
"ofetch": "^1.3.4",
"proxy-agent": "^6.4.0",
"tsx": "^4.6.0"
"tsx": "^4.6.0",
"ufo": "^1.5.4"
},
"devDependencies": {
"@antfu/eslint-config": "^2.1.1",
Expand Down
10 changes: 4 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions src/api/skland.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createFetch } from 'ofetch'
import { ProxyAgent } from 'proxy-agent'
import type { AttendanceResponse, BindingResponse, CredResponse, GetAttendanceResponse, SklandBoard } from '../types'
import { command_header, onSignatureRequest } from '../utils'
import { SKLAND_BOARD_IDS } from '../constant'

const fetch = createFetch({
defaults: {
Expand Down Expand Up @@ -52,6 +54,19 @@ export async function getBinding(cred: string, token: string) {
return data.data
}

export async function getScoreIsCheckIn(cred: string, token: string) {
const data = await fetch<{ code: number, message: string, data: { list: { gameId: number, checked: 1 | 0 }[] } }>(
'/api/v1/score/ischeckin',
{
headers: Object.assign({ token, cred }, command_header),
query: {
gameIds: SKLAND_BOARD_IDS
}
},
)
return data
}

/**
* 登岛检票
* @param cred 鹰角网络通行证账号的登录凭证
Expand All @@ -75,18 +90,17 @@ export async function checkIn(cred: string, token: string, id: SklandBoard) {
* @param token 森空岛用户的 token
*/
export async function attendance(cred: string, token: string, body: { uid: string, gameId: string }) {

const record = await fetch<GetAttendanceResponse>(
'/api/v1/game/attendance',
{
headers: Object.assign({ token, cred }, command_header),
query: body
query: body,
},
)

const todayAttended = record.data.records.find((i) => {
const today = new Date().setHours(0, 0, 0, 0);
return new Date(Number(i.ts) * 1000).setHours(0, 0, 0, 0) === today;
const today = new Date().setHours(0, 0, 0, 0)
return new Date(Number(i.ts) * 1000).setHours(0, 0, 0, 0) === today
})
if (todayAttended) {
// 今天已经签到过了
Expand All @@ -98,7 +112,7 @@ export async function attendance(cred: string, token: string, body: { uid: strin
{
method: 'POST',
headers: Object.assign({ token, cred }, command_header),
body
body,
},
)
return data
Expand Down
42 changes: 27 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import process from 'node:process'
import { setTimeout } from 'node:timers/promises'
import { attendance, auth, checkIn, getBinding, signIn } from './api'
import { attendance, auth, checkIn, getBinding, getScoreIsCheckIn, signIn } from './api'
import { bark, serverChan } from './notifications'
import { getPrivacyName } from './utils'
import { SKLAND_BOARD_IDS, SKLAND_BOARD_NAME_MAPPING } from './constant'
Expand Down Expand Up @@ -81,23 +81,35 @@ export async function doAttendanceForAccount(token: string, options: Options) {
// 多个角色之间的延时
await setTimeout(3000)
}))
combineMessage(`成功签到${successAttendance}个角色`)
if (successAttendance !== 0)
combineMessage(`成功签到${successAttendance}个角色`)

addMessage(`# 森空岛每日签到 \n\n> ${new Intl.DateTimeFormat('zh-CN', { dateStyle: 'full', timeStyle: 'short', timeZone: 'Asia/Shanghai' }).format(new Date())}`)
addMessage('## 森空岛各版面每日检票')
await Promise.all(SKLAND_BOARD_IDS.map(async (id) => {
const data = await checkIn(cred, signToken, id)
const name = SKLAND_BOARD_NAME_MAPPING[id]
if (data.message === 'OK' && data.code === 0) {
combineMessage(`版面【${name}】登岛检票成功`)
}
else {
// 登岛检票 最后不会以错误结束进程
combineMessage(`版面【${name}】登岛检票失败, 错误信息: ${data.message}`)
}
// 多个登岛检票之间的延时
await setTimeout(3000)
}))
const isCheckIn = await getScoreIsCheckIn(cred, signToken)

await Promise.all(
SKLAND_BOARD_IDS
.map(async (id) => {
// 过滤已经签到过的
const name = SKLAND_BOARD_NAME_MAPPING[id]
if (isCheckIn.data.list.find(i => i.gameId === id)?.checked !== 1) {
const data = await checkIn(cred, signToken, id)

if (data.message === 'OK' && data.code === 0) {
combineMessage(`版面【${name}】登岛检票成功`)
}
else {
// 登岛检票 最后不会以错误结束进程
combineMessage(`版面【${name}】登岛检票失败, 错误信息: ${data.message}`)
}
// 多个登岛检票之间的延时
await setTimeout(3000)
} else {
combineMessage(`版面【${name}】今天已经登岛检票过了`)
}
})
)

await excutePushMessage()
}
11 changes: 6 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { createHash, createHmac } from 'node:crypto'
import type { FetchContext } from 'ofetch'
import { stringifyQuery } from 'ufo'

export const command_header = {
'User-Agent': 'Skland/1.21.0 (com.hypergryph.skland; build:102100065; Android 34; ) Okhttp/4.11.0',
'User-Agent': 'Skland/1.21.0 (com.hypergryph.skland; build:102100065; iOS 17.6.0; ) Alamofire/5.7.1',
'Accept-Encoding': 'gzip',
'Connection': 'close',
'Content-Type': 'application/json',
}

export const sign_header = {
platform: '1',
platform: '2',
timestamp: '',
dId: '',
vName: '1.21.0',
Expand Down Expand Up @@ -43,15 +44,15 @@ export function onSignatureRequest(ctx: FetchContext) {
if (!token)
throw new Error('token 不存在')

const searchParams = new URLSearchParams(ctx.options.query)
const query = ctx.options.query ? stringifyQuery(ctx.options.query) : ''
const timestamp = (Date.now() - 2 * MILLISECOND_PER_SECOND).toString().slice(0, -3)
const signatureHeaders = {
platform: '1',
timestamp,
dId: '',
vName: '1.21.0',
}
const str = `${pathname}${searchParams.toString()}${ctx.options.body ? JSON.stringify(ctx.options.body) : ''}${timestamp}${JSON.stringify(signatureHeaders)}`
const str = `${pathname}${query}${ctx.options.body ? JSON.stringify(ctx.options.body) : ''}${timestamp}${JSON.stringify(signatureHeaders)}`

const hmacSha256ed = createHmac('sha256', token)
.update(str, 'utf-8')
Expand All @@ -66,6 +67,6 @@ export function onSignatureRequest(ctx: FetchContext) {
})
headers.append('sign', sign)
headers.delete('token')

ctx.options.headers = headers
}

0 comments on commit 68b1cf3

Please sign in to comment.