mirror of
https://github.com/asphyxia-core/plugins.git
synced 2026-04-26 02:02:29 -05:00
commit
716473277a
9
bst@asphyxia/README.md
Normal file
9
bst@asphyxia/README.md
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
# BeatStream
|
||||||
|
|
||||||
|
Plugin Version: **v1.0.1**
|
||||||
|
|
||||||
|
Supported Versions:
|
||||||
|
|
||||||
|
- BeatStream アニムトライヴ
|
||||||
|
- Back end ✔
|
||||||
|
- Web UI ✔
|
||||||
247
bst@asphyxia/handlers/bst2/common.ts
Normal file
247
bst@asphyxia/handlers/bst2/common.ts
Normal file
|
|
@ -0,0 +1,247 @@
|
||||||
|
import { Bst2EventParamsMap, getKEventControl } from "../../models/bst2/event_params"
|
||||||
|
import { Bst2AccountMap, Bst2BiscoMap, Bst2CourseMap, Bst2MusicRecordMap, Bst2PlayerMap, Bst2SurveyMap, Bst2TipsMap, Bst2UnlockingInfoMap, IBst2Account, IBst2Base, IBst2Bisco, IBst2Course, IBst2CrysisLog, IBst2Customization, IBst2Hacker, IBst2MusicRecord, IBst2Player, IBst2Survey, IBst2Tips, IBst2UnlockingInfo } from "../../models/bst2/profile"
|
||||||
|
import { Bst2CourseLogMap, Bst2StageLogMap, IBst2StageLog } from "../../models/bst2/stagelog"
|
||||||
|
import { bacK, BigIntProxy, boolme, fromMap, mapK, s16me, s32me, s8me, strme, toBigInt } from "../../utility/mapping"
|
||||||
|
import { isToday } from "../../utility/utility_functions"
|
||||||
|
import { DBM } from "../utility/db_manager"
|
||||||
|
import { readPlayerPostProcess, writePlayerPreProcess } from "./processing"
|
||||||
|
|
||||||
|
export namespace Bst2HandlersCommon {
|
||||||
|
export const Common: EPR = async (_0, _1, send) => await send.object({ event_ctrl: { data: getKEventControl() } })
|
||||||
|
|
||||||
|
export const BootPcb: EPR = async (_0, _1, send) => await send.object({ sinfo: { nm: K.ITEM("str", "Asphyxia"), cl_enbl: K.ITEM("bool", 1), cl_h: K.ITEM("u8", 0), cl_m: K.ITEM("u8", 0) } })
|
||||||
|
|
||||||
|
export const StartPlayer: EPR = async (_, data, send) => {
|
||||||
|
let params = fromMap(Bst2EventParamsMap)
|
||||||
|
let rid = $(data).str("rid")
|
||||||
|
let account = DB.FindOne<IBst2Account>(rid, { collection: "bst.bst2.player.account" })
|
||||||
|
if (account == null) params.playerId = -1
|
||||||
|
params.startTime = BigInt(Date.now())
|
||||||
|
send.object(mapK(params, Bst2EventParamsMap))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const PlayerSucceeded: EPR = async (_, data, send) => {
|
||||||
|
let rid = $(data).str("rid")
|
||||||
|
let account: IBst2Account = await DB.FindOne<IBst2Account>(rid, { collection: "bst.bst2.player.account" })
|
||||||
|
let result
|
||||||
|
if (account == null) {
|
||||||
|
result = {
|
||||||
|
play: false,
|
||||||
|
data: { name: "" },
|
||||||
|
record: {},
|
||||||
|
hacker: {},
|
||||||
|
phantom: {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let base: IBst2Base = await DB.FindOne<IBst2Base>(rid, { collection: "bst.bst2.player.base" })
|
||||||
|
let records: IBst2MusicRecord[] = await DB.Find<IBst2MusicRecord>({ collection: "bst.bst2.playData.musicRecord#userId", userId: account.userId })
|
||||||
|
result = {
|
||||||
|
play: true,
|
||||||
|
data: { name: base.name },
|
||||||
|
record: {},
|
||||||
|
hacker: {},
|
||||||
|
phantom: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
send.object(mapK(result, {
|
||||||
|
play: boolme(),
|
||||||
|
data: { name: strme() },
|
||||||
|
record: {},
|
||||||
|
hacker: {},
|
||||||
|
phantom: {}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ReadPlayer: EPR = async (_, data, send) => {
|
||||||
|
let refid = $(data).str("rid")
|
||||||
|
let account = await DB.FindOne<IBst2Account>(refid, { collection: "bst.bst2.player.account" })
|
||||||
|
if (account == null) return await send.deny()
|
||||||
|
|
||||||
|
let base = await DB.FindOne<IBst2Base>(refid, { collection: "bst.bst2.player.base" })
|
||||||
|
let survey = await DB.FindOne<IBst2Survey>(refid, { collection: "bst.bst2.player.survey" }) || fromMap(Bst2SurveyMap)
|
||||||
|
let unlocking = await DB.Find<IBst2UnlockingInfo>(refid, { collection: "bst.bst2.player.unlockingInfo" })
|
||||||
|
let customize = await DB.FindOne<IBst2Customization>(refid, { collection: "bst.bst2.player.customization" })
|
||||||
|
let tips = await DB.FindOne<IBst2Tips>(refid, { collection: "bst.bst2.player.tips" }) || fromMap(Bst2TipsMap)
|
||||||
|
let hacker = await DB.Find<IBst2Hacker>(refid, { collection: "bst.bst2.player.hacker" })
|
||||||
|
let crysis = await DB.Find<IBst2CrysisLog>(refid, { collection: "bst.bst2.player.event.crysis" })
|
||||||
|
let bisco = await DB.FindOne<IBst2Bisco>(refid, { collection: "bst.bst2.player.bisco" }) || fromMap(Bst2BiscoMap)
|
||||||
|
let records = await DB.Find<IBst2MusicRecord>({ collection: "bst.bst2.playData.musicRecord#userId", userId: account.userId })
|
||||||
|
let courses = await DB.Find<IBst2Course>({ collection: "bst.bst2.playData.course#userId", userId: account.userId })
|
||||||
|
|
||||||
|
account.previousStartTime = account.standardTime
|
||||||
|
account.standardTime = BigInt(Date.now())
|
||||||
|
account.ea = true
|
||||||
|
account.intrvld = 0
|
||||||
|
account.playCount++
|
||||||
|
account.playCountToday++
|
||||||
|
let eventPlayLog: { crysis?: IBst2CrysisLog[] } = {}
|
||||||
|
if (crysis.length != 0) eventPlayLog.crysis = crysis
|
||||||
|
|
||||||
|
let player: IBst2Player = {
|
||||||
|
pdata: {
|
||||||
|
account: account,
|
||||||
|
base: base,
|
||||||
|
survey: survey,
|
||||||
|
opened: {},
|
||||||
|
item: (unlocking.length == 0) ? {} : { info: unlocking },
|
||||||
|
customize: customize,
|
||||||
|
tips: tips,
|
||||||
|
hacker: (hacker.length == 0) ? {} : { info: hacker },
|
||||||
|
playLog: eventPlayLog,
|
||||||
|
bisco: { pinfo: bisco },
|
||||||
|
record: (records.length == 0) ? {} : { rec: records },
|
||||||
|
course: (courses.length == 0) ? {} : { record: courses }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
send.object(readPlayerPostProcess(mapK(player, Bst2PlayerMap)))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const WritePlayer: EPR = async (_, data, send) => {
|
||||||
|
let player = bacK(writePlayerPreProcess(data), Bst2PlayerMap).data
|
||||||
|
let refid = player.pdata.account.refid
|
||||||
|
let userId = player.pdata.account.userId
|
||||||
|
let now = BigIntProxy(BigInt(Date.now()))
|
||||||
|
|
||||||
|
let opm = new DBM.DBOperationManager()
|
||||||
|
|
||||||
|
let oldAccount = await DB.FindOne<IBst2Account>(refid, { collection: "bst.bst2.player.account" })
|
||||||
|
if (!oldAccount) {
|
||||||
|
do {
|
||||||
|
userId = Math.round(Math.random() * 99999999)
|
||||||
|
} while ((await DB.Find<IBst2Account>(null, { collection: "bst.bst2.player.account", userId: userId })).length > 0)
|
||||||
|
oldAccount = fromMap(Bst2AccountMap)
|
||||||
|
oldAccount.userId = userId
|
||||||
|
} else {
|
||||||
|
oldAccount.playCount++
|
||||||
|
if (!isToday(toBigInt(oldAccount.standardTime))) {
|
||||||
|
oldAccount.dayCount++
|
||||||
|
oldAccount.playCountToday = 1
|
||||||
|
} else oldAccount.playCountToday++
|
||||||
|
}
|
||||||
|
oldAccount.standardTime = BigIntProxy(BigInt(Date.now()))
|
||||||
|
opm.upsert<IBst2Account>(refid, { collection: "bst.bst2.player.account" }, oldAccount)
|
||||||
|
if (player.pdata.base) opm.upsert<IBst2Base>(refid, { collection: "bst.bst2.player.base" }, player.pdata.base)
|
||||||
|
if (player.pdata.item?.info?.length > 0) for (let u of player.pdata.item.info) opm.upsert<IBst2UnlockingInfo>(refid, { collection: "bst.bst2.player.unlockingInfo", type: u.type, id: u.id }, u)
|
||||||
|
if (player.pdata.customize) opm.upsert<IBst2Customization>(refid, { collection: "bst.bst2.player.customization" }, player.pdata.customize)
|
||||||
|
if (player.pdata.tips) opm.upsert<IBst2Base>(refid, { collection: "bst.bst2.player.base" }, player.pdata.base)
|
||||||
|
if (player.pdata.hacker?.info?.length > 0) for (let h of player.pdata.hacker.info) {
|
||||||
|
h.updateTime = now
|
||||||
|
opm.upsert<IBst2Hacker>(refid, { collection: "bst.bst2.player.hacker", id: h.id }, h)
|
||||||
|
}
|
||||||
|
if (player.pdata.playLog?.crysis?.length > 0) for (let c of player.pdata.playLog.crysis) opm.upsert<IBst2CrysisLog>(refid, { collection: "bst.bst2.player.event.crysis", id: c.id, stageId: c.stageId }, c)
|
||||||
|
|
||||||
|
await DBM.operate(opm)
|
||||||
|
send.object({ uid: K.ITEM("s32", oldAccount.userId) })
|
||||||
|
}
|
||||||
|
|
||||||
|
export const WriteStageLog: EPR = async (_, data, send) => {
|
||||||
|
await updateRecordFromStageLog(bacK(data, Bst2StageLogMap).data, false)
|
||||||
|
send.success()
|
||||||
|
}
|
||||||
|
|
||||||
|
export const WriteCourseStageLog: EPR = async (_, data, send) => {
|
||||||
|
await updateRecordFromStageLog(bacK(data, Bst2StageLogMap).data, true)
|
||||||
|
send.success()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateRecordFromStageLog(stageLog: IBst2StageLog, isCourseStage: boolean) {
|
||||||
|
let query: Query<IBst2MusicRecord> = { collection: "bst.bst2.playData.musicRecord#userId", userId: stageLog.userId, musicId: stageLog.musicId, chart: stageLog.chart }
|
||||||
|
let oldRecord = await DB.FindOne<IBst2MusicRecord>(query)
|
||||||
|
|
||||||
|
let time = Date.now()
|
||||||
|
stageLog.time = time
|
||||||
|
stageLog.isCourseStage = isCourseStage
|
||||||
|
|
||||||
|
if (oldRecord == null) {
|
||||||
|
oldRecord = fromMap(Bst2MusicRecordMap)
|
||||||
|
oldRecord.musicId = stageLog.musicId
|
||||||
|
oldRecord.chart = stageLog.chart
|
||||||
|
oldRecord.clearCount = (stageLog.medal >= 3) ? 1 : 0
|
||||||
|
oldRecord.score = stageLog.score
|
||||||
|
oldRecord.grade = stageLog.grade
|
||||||
|
oldRecord.gaugeTimes10 = stageLog.gaugeTimes10
|
||||||
|
oldRecord.playCount = 1
|
||||||
|
oldRecord.medal = stageLog.medal
|
||||||
|
oldRecord.combo = stageLog.combo
|
||||||
|
oldRecord.lastPlayTime = time
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.userId = stageLog.userId
|
||||||
|
} else {
|
||||||
|
if (stageLog.medal >= 3) oldRecord.clearCount++
|
||||||
|
if (oldRecord.score < stageLog.score) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.score = stageLog.score
|
||||||
|
}
|
||||||
|
if (oldRecord.grade < stageLog.grade) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.grade = stageLog.grade
|
||||||
|
}
|
||||||
|
if (oldRecord.gaugeTimes10 < stageLog.gaugeTimes10) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.gaugeTimes10 = stageLog.gaugeTimes10
|
||||||
|
}
|
||||||
|
if (oldRecord.medal < stageLog.medal) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.medal = stageLog.medal
|
||||||
|
}
|
||||||
|
if (oldRecord.combo < stageLog.combo) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.combo = stageLog.combo
|
||||||
|
}
|
||||||
|
oldRecord.lastPlayTime = time
|
||||||
|
oldRecord.playCount++
|
||||||
|
}
|
||||||
|
DBM.upsert(null, query, oldRecord)
|
||||||
|
DBM.insert(null, stageLog)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const WriteCourseLog: EPR = async (_, data, send) => {
|
||||||
|
let courseLog = bacK(data, Bst2CourseLogMap).data
|
||||||
|
let query: Query<IBst2Course> = { collection: "bst.bst2.playData.course#userId", userId: courseLog.userId, courseId: courseLog.courseId }
|
||||||
|
let oldRecord = await DB.FindOne<IBst2Course>(query)
|
||||||
|
|
||||||
|
let time = Date.now()
|
||||||
|
courseLog.time = time
|
||||||
|
|
||||||
|
if (oldRecord == null) {
|
||||||
|
oldRecord = fromMap(Bst2CourseMap)
|
||||||
|
oldRecord.courseId = courseLog.courseId
|
||||||
|
oldRecord.score = courseLog.score
|
||||||
|
oldRecord.grade = courseLog.grade
|
||||||
|
oldRecord.gauge = courseLog.gauge
|
||||||
|
oldRecord.playCount = 1
|
||||||
|
oldRecord.medal = courseLog.medal
|
||||||
|
oldRecord.combo = courseLog.combo
|
||||||
|
oldRecord.lastPlayTime = time
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.userId = courseLog.userId
|
||||||
|
} else {
|
||||||
|
if (oldRecord.score < courseLog.score) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.score = courseLog.score
|
||||||
|
}
|
||||||
|
if (oldRecord.grade < courseLog.grade) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.grade = courseLog.grade
|
||||||
|
}
|
||||||
|
if (oldRecord.gauge < courseLog.gauge) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.gauge = courseLog.gauge
|
||||||
|
}
|
||||||
|
if (oldRecord.medal < courseLog.medal) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.medal = courseLog.medal
|
||||||
|
}
|
||||||
|
if (oldRecord.combo < courseLog.combo) {
|
||||||
|
oldRecord.updateTime = time
|
||||||
|
oldRecord.combo = courseLog.combo
|
||||||
|
}
|
||||||
|
oldRecord.lastPlayTime = time
|
||||||
|
oldRecord.playCount++
|
||||||
|
}
|
||||||
|
DBM.upsert(null, query, oldRecord)
|
||||||
|
DBM.insert(null, courseLog)
|
||||||
|
|
||||||
|
send.success()
|
||||||
|
}
|
||||||
|
}
|
||||||
12
bst@asphyxia/handlers/bst2/processing.ts
Normal file
12
bst@asphyxia/handlers/bst2/processing.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { IBst2Player } from "../../models/bst2/profile"
|
||||||
|
import { KITEM2 } from "../../utility/mapping"
|
||||||
|
import { toFullWidth, toHalfWidth } from "../../utility/utility_functions"
|
||||||
|
|
||||||
|
export function readPlayerPostProcess(player: KITEM2<IBst2Player>): KITEM2<IBst2Player> {
|
||||||
|
if (player.pdata.base?.name != null) player.pdata.base.name["@content"] = toFullWidth(player.pdata.base.name["@content"])
|
||||||
|
return player
|
||||||
|
}
|
||||||
|
export function writePlayerPreProcess(player: KITEM2<IBst2Player>): KITEM2<IBst2Player> {
|
||||||
|
if (player.pdata.base?.name != null) player.pdata.base.name["@content"] = toHalfWidth(player.pdata.base.name["@content"])
|
||||||
|
return player
|
||||||
|
}
|
||||||
39
bst@asphyxia/handlers/bst2/webui.ts
Normal file
39
bst@asphyxia/handlers/bst2/webui.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import { IBst2Base, IBst2Customization } from "../../models/bst2/profile"
|
||||||
|
import { WebUIMessageType } from "../../models/utility/webui_message"
|
||||||
|
import { DBM } from "../utility/db_manager"
|
||||||
|
import { UtilityHandlersWebUI } from "../utility/webui"
|
||||||
|
|
||||||
|
export namespace Bst2HandlersWebUI {
|
||||||
|
export const UpdateSettings = async (data: {
|
||||||
|
refid: string
|
||||||
|
name: string
|
||||||
|
rippleNote: number
|
||||||
|
sfxNormalNote: number
|
||||||
|
sfxRippleNote: number
|
||||||
|
sfxSlashNote: number
|
||||||
|
sfxStreamNote: number
|
||||||
|
backgroundBrightness: number
|
||||||
|
judgeText: number
|
||||||
|
rippleNoteGuide: number
|
||||||
|
streamNoteGuide: number
|
||||||
|
sfxFine: number
|
||||||
|
sfxStreamNoteTail: number
|
||||||
|
}) => {
|
||||||
|
try {
|
||||||
|
let customization = await DB.FindOne<IBst2Customization>(data.refid, { collection: "bst.bst2.player.customization" })
|
||||||
|
if (customization == null) throw new Error("No profile for refid=" + data.refid)
|
||||||
|
customization.custom[0] = data.rippleNote
|
||||||
|
customization.custom[2] = data.sfxNormalNote
|
||||||
|
customization.custom[3] = data.sfxRippleNote
|
||||||
|
customization.custom[4] = data.sfxSlashNote
|
||||||
|
customization.custom[5] = data.sfxStreamNote
|
||||||
|
customization.custom[6] = data.backgroundBrightness
|
||||||
|
customization.custom[7] = (data.judgeText << 0) | (data.rippleNoteGuide << 1) | (data.streamNoteGuide << 2) | (data.sfxStreamNoteTail << 3) | (data.sfxFine << 4)
|
||||||
|
customization.custom[9] = data.judgeText
|
||||||
|
DBM.update<IBst2Customization>(data.refid, { collection: "bst.bst2.player.customization" }, customization)
|
||||||
|
UtilityHandlersWebUI.pushMessage("Save BeatStream Animtribe settings succeeded!", 2, WebUIMessageType.success, data.refid)
|
||||||
|
} catch (e) {
|
||||||
|
UtilityHandlersWebUI.pushMessage("Error while save BeatStream Animtribe settings: " + e.message, 2, WebUIMessageType.error, data.refid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
bst@asphyxia/handlers/utility/batch.ts
Normal file
21
bst@asphyxia/handlers/utility/batch.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { IBatchResult } from "../../models/utility/batch"
|
||||||
|
import { IPluginVersion } from "../../models/utility/plugin_version"
|
||||||
|
import { isHigherVersion } from "../../utility/utility_functions"
|
||||||
|
import { DBM } from "./db_manager"
|
||||||
|
|
||||||
|
export namespace Batch {
|
||||||
|
let registeredBatch = <{ id: string, version: string, batch: () => Promise<any> }[]>[]
|
||||||
|
|
||||||
|
export async function execute(version: string): Promise<void> {
|
||||||
|
for (let b of registeredBatch) {
|
||||||
|
if ((await DB.Find<IBatchResult>({ collection: "bst.batchResult", batchId: b.id })).length == 0) if (!isHigherVersion(version, b.version)) {
|
||||||
|
await b.batch()
|
||||||
|
await DBM.insert<IBatchResult>(null, { collection: "bst.batchResult", batchId: b.id })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function register(id: string, version: string, batch: () => Promise<any>) {
|
||||||
|
registeredBatch.push({ id: id, version: version, batch: batch })
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
7
bst@asphyxia/handlers/utility/batch_initialize.ts
Normal file
7
bst@asphyxia/handlers/utility/batch_initialize.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { Batch } from "./batch"
|
||||||
|
import { DBM } from "./db_manager"
|
||||||
|
import { bufferToBase64, log } from "../../utility/utility_functions"
|
||||||
|
|
||||||
|
export function initializeBatch() {
|
||||||
|
/* Register batch here **/
|
||||||
|
}
|
||||||
17
bst@asphyxia/handlers/utility/common.ts
Normal file
17
bst@asphyxia/handlers/utility/common.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
export namespace UtilityHandlersCommon {
|
||||||
|
export const WriteShopInfo: EPR = async (__, ___, send) => {
|
||||||
|
let result = {
|
||||||
|
sinfo: {
|
||||||
|
lid: K.ITEM("str", "ea"),
|
||||||
|
nm: K.ITEM("str", "Asphyxia shop"),
|
||||||
|
cntry: K.ITEM("str", "Japan"),
|
||||||
|
rgn: K.ITEM("str", "1"),
|
||||||
|
prf: K.ITEM("s16", 13),
|
||||||
|
cl_enbl: K.ITEM("bool", 0),
|
||||||
|
cl_h: K.ITEM("u8", 8),
|
||||||
|
cl_m: K.ITEM("u8", 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
send.object(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
212
bst@asphyxia/handlers/utility/db_manager.ts
Normal file
212
bst@asphyxia/handlers/utility/db_manager.ts
Normal file
|
|
@ -0,0 +1,212 @@
|
||||||
|
import { ICollection } from "../../models/utility/definitions"
|
||||||
|
import { log } from "../../utility/utility_functions"
|
||||||
|
|
||||||
|
export namespace DBM {
|
||||||
|
export interface IDBCollectionName extends ICollection<"dbManager.collectionName"> {
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
export interface IDBOperation<T = any, TOperation extends "insert" | "update" | "upsert" | "remove" | "skip" = "insert" | "update" | "upsert" | "remove" | "skip"> {
|
||||||
|
refid?: string
|
||||||
|
query: TOperation extends "insert" ? null : Query<T>
|
||||||
|
operation: TOperation
|
||||||
|
doc: TOperation extends "remove" ? null : T | Doc<T>
|
||||||
|
isPublicDoc?: boolean
|
||||||
|
}
|
||||||
|
export class DBOperationManager {
|
||||||
|
public operations: IDBOperation[] = []
|
||||||
|
|
||||||
|
public push(...op: IDBOperation[]): void {
|
||||||
|
this.operations.push(...op)
|
||||||
|
}
|
||||||
|
public update<T extends ICollection<any>>(refid: string | null, query: Query<T>, data: Doc<T>, isPublicDoc: boolean = true): void {
|
||||||
|
for (let o of this.operations) if (o.doc && DBOperationManager.isMatch(o.doc, query)) o.operation = "skip"
|
||||||
|
this.operations.push({ refid: refid, query: query, operation: "update", doc: data, isPublicDoc: isPublicDoc })
|
||||||
|
}
|
||||||
|
public upsert<T extends ICollection<any>>(refid: string | null, query: Query<T>, data: Doc<T>, isPublicDoc: boolean = true): void {
|
||||||
|
for (let o of this.operations) if (o.doc && DBOperationManager.isMatch(o.doc, query)) o.operation = "skip"
|
||||||
|
this.operations.push({ refid: refid, query: query, operation: "upsert", doc: data, isPublicDoc: isPublicDoc })
|
||||||
|
}
|
||||||
|
public insert<T extends ICollection<any>>(refid: string | null, data: Doc<T>, isPublicDoc: boolean = true): void {
|
||||||
|
this.operations.push({ refid: refid, operation: "insert", query: null, doc: data, isPublicDoc: isPublicDoc })
|
||||||
|
}
|
||||||
|
public remove<T extends ICollection<any>>(refid: string | null, query: Query<T>, isPublicDoc: boolean = true): void {
|
||||||
|
for (let o of this.operations) if (o.doc && DBOperationManager.isMatch(o.doc, query)) o.operation = "skip"
|
||||||
|
this.operations.push({ refid: refid, query: query, operation: "remove", doc: null, isPublicDoc: isPublicDoc })
|
||||||
|
}
|
||||||
|
public async findOne<T extends ICollection<any>>(refid: string | null, query: Query<T>, isPublicDoc: boolean = true): Promise<T | Doc<T>> {
|
||||||
|
for (let i = this.operations.length - 1; i >= 0; i--) {
|
||||||
|
let o = this.operations[i]
|
||||||
|
if (o.doc == null) continue
|
||||||
|
if (DBOperationManager.isMatch(o.doc, query) && ((o.refid && refid) ? (o.refid == refid) : true)) return o.doc
|
||||||
|
}
|
||||||
|
return ((refid == null) && isPublicDoc) ? await DB.FindOne<T>(query) : await DB.FindOne<T>(refid, query)
|
||||||
|
}
|
||||||
|
public async find<T extends ICollection<any>>(refid: string | null, query: Query<T>, isPublicDoc: boolean = true): Promise<(T | Doc<T>)[]> {
|
||||||
|
let result: (T | Doc<T>)[] = []
|
||||||
|
for (let o of this.operations) {
|
||||||
|
if (o.doc == null) continue
|
||||||
|
if (DBOperationManager.isMatch(o.doc, query) && ((o.refid && refid) ? (o.refid == refid) : true)) result.push(o.doc)
|
||||||
|
}
|
||||||
|
return result.concat(await (((refid == null) && isPublicDoc) ? DB.Find<T>(query) : DB.Find<T>(refid, query)))
|
||||||
|
}
|
||||||
|
private static isMatch<T>(entry: T | Doc<T>, query: Query<T>): boolean {
|
||||||
|
if (entry == null) return query == null
|
||||||
|
if (query.$where && !query.$where.apply(entry)) return false
|
||||||
|
let $orResult = null
|
||||||
|
let skipKeys = ["$where", "_id"]
|
||||||
|
for (let qk in query) {
|
||||||
|
if (skipKeys.includes(qk)) continue
|
||||||
|
switch (qk) {
|
||||||
|
case "$or": {
|
||||||
|
if ($orResult == null) $orResult = false
|
||||||
|
for (let or of query.$or) if (this.isMatch(entry, or)) $orResult = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case "$and": {
|
||||||
|
for (let and of query.$and) if (!this.isMatch(entry, and)) return false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case "$not": {
|
||||||
|
if (this.isMatch(entry, query.$not)) return false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
let value = entry[qk]
|
||||||
|
let q = query[qk]
|
||||||
|
if (value == q) continue
|
||||||
|
if ((typeof q != "object") && (typeof q != "function")) return false
|
||||||
|
if ((q.$exists != null)) if ((q.$exists && (value == null)) || (!q.$exists && (value != null))) return false
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
if (q.$elemMatch && !this.isMatch(value, q.$elemMatch)) return false
|
||||||
|
if (q.$size && (value.length != q.$size)) return false
|
||||||
|
continue
|
||||||
|
} else if ((typeof value == "number") || (typeof value == "string")) {
|
||||||
|
if (q.$lt) if (value >= q.$lt) return false
|
||||||
|
if (q.$lte) if (value > q.$lte) return false
|
||||||
|
if (q.$gt) if (value <= q.$gt) return false
|
||||||
|
if (q.$gte) if (value < q.$gte) return false
|
||||||
|
if (q.$in) if (!value.toString().includes(q.$in)) return false
|
||||||
|
if (q.$nin) if (value.toString().includes(q.$nin)) return false
|
||||||
|
if (q.$ne) if (value == q.$ne) return false
|
||||||
|
if (q.$regex) if (value.toString().match(q.$regex).length == 0) return false
|
||||||
|
continue
|
||||||
|
} else if (typeof value == "object") {
|
||||||
|
if (!this.isMatch(value, q)) return false
|
||||||
|
continue
|
||||||
|
} else if (q != null) return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ($orResult == null) || $orResult
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export async function getCollectionNames(filter?: string): Promise<IDBCollectionName[]> {
|
||||||
|
let result = await DB.Find<IDBCollectionName>({ collection: "dbManager.collectionName" })
|
||||||
|
if (filter != null) {
|
||||||
|
let filters = filter.split(",")
|
||||||
|
for (let i = 0; i < filter.length; i++) filters[i] = filters[i].trim()
|
||||||
|
let i = 0
|
||||||
|
while (i < result.length) {
|
||||||
|
let removeFlag = false
|
||||||
|
for (let f of filters) if (f.startsWith("!") ? !result[i].name.includes(f) : result[i].name.includes(f)) {
|
||||||
|
result.splice(i, 1)
|
||||||
|
removeFlag = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (!removeFlag) i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkData<T extends ICollection<any>>(data: T): Promise<void> {
|
||||||
|
for (let k in data) if (k.startsWith("__")) delete data[k]
|
||||||
|
if (await DB.FindOne<IDBCollectionName>({ collection: "dbManager.collectionName", name: data.collection }) == null) {
|
||||||
|
await DB.Insert<IDBCollectionName>({ collection: "dbManager.collectionName", name: data.collection })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export async function update<T extends ICollection<any>>(refid: string | null, query: Query<T>, data: Doc<T>, isPublicDoc: boolean = true) {
|
||||||
|
checkData(data)
|
||||||
|
if (refid == null) return isPublicDoc ? await DB.Update(query, data) : await DB.Update(null, query, data)
|
||||||
|
else return await DB.Update(refid, query, data)
|
||||||
|
}
|
||||||
|
export async function upsert<T extends ICollection<any>>(refid: string | null, query: Query<T>, data: Doc<T>, isPublicDoc: boolean = true) {
|
||||||
|
checkData(data)
|
||||||
|
if (refid == null) return isPublicDoc ? await DB.Upsert(query, data) : await DB.Upsert(null, query, data)
|
||||||
|
else return await DB.Upsert(refid, query, data)
|
||||||
|
}
|
||||||
|
export async function insert<T extends ICollection<any>>(refid: string | null, data: Doc<T>, isPublicDoc: boolean = true) {
|
||||||
|
checkData(data)
|
||||||
|
if (refid == null) return isPublicDoc ? await DB.Insert(data) : await DB.Insert(null, data)
|
||||||
|
else return await DB.Insert(refid, data)
|
||||||
|
}
|
||||||
|
export async function remove<T extends ICollection<any>>(refid: string | null, query: Query<T>, isPublicDoc: boolean = true) {
|
||||||
|
if (refid == null) return isPublicDoc ? await DB.Remove(query) : await DB.Remove(null, query)
|
||||||
|
else return await DB.Remove(refid, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function operate(operations: DBOperationManager) {
|
||||||
|
let result = []
|
||||||
|
for (let o of operations.operations) {
|
||||||
|
if (o.operation == "skip") continue
|
||||||
|
if (o.doc) delete o.doc._id
|
||||||
|
try {
|
||||||
|
switch (o.operation) {
|
||||||
|
case "insert":
|
||||||
|
result.push(await insert(o.refid, o.doc, o.isPublicDoc))
|
||||||
|
break
|
||||||
|
case "update":
|
||||||
|
result.push(await update(o.refid, o.query, o.doc, o.isPublicDoc))
|
||||||
|
break
|
||||||
|
case "upsert":
|
||||||
|
result.push(await upsert(o.refid, o.query, o.doc, o.isPublicDoc))
|
||||||
|
break
|
||||||
|
case "remove":
|
||||||
|
result.push(await remove(o.refid, o.query, o.isPublicDoc))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
await log(new Date().toLocaleString() + " Error: " + (e as Error).message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeAllData(refid?: string, filter?: string) {
|
||||||
|
for (let c of await getCollectionNames(filter)) remove(refid, { collection: c.name })
|
||||||
|
|
||||||
|
if ((refid == null) && (filter == null)) remove(null, { collection: "dbManager.collectionName" })
|
||||||
|
}
|
||||||
|
export async function overall(refid: string, userId: number, filter: string, operation: "delete" | "export" | "override", data?: any) {
|
||||||
|
if (refid == null) return
|
||||||
|
try {
|
||||||
|
let collections = await DBM.getCollectionNames(filter)
|
||||||
|
let traverse = async (f: (rid: string | null, query: Query<ICollection<any>>) => Promise<any>) => {
|
||||||
|
let result = []
|
||||||
|
for (let c of collections) {
|
||||||
|
if (c.name.includes("#userId") && (userId != null)) result.concat(...await f(null, { collection: c.name, userId: userId }))
|
||||||
|
else result.concat(...await f(refid, { collection: c.name }))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
switch (operation) {
|
||||||
|
case "delete":
|
||||||
|
await traverse((rid, query) => DBM.remove(rid, query))
|
||||||
|
break
|
||||||
|
case "export":
|
||||||
|
let result = await traverse((rid, query) => DB.Find(rid, query))
|
||||||
|
return JSON.stringify(result)
|
||||||
|
case "override":
|
||||||
|
if (!Array.isArray(data)) return "The data may not be an Asphyxia CORE savedata."
|
||||||
|
await traverse((rid, query) => DBM.remove(rid, query))
|
||||||
|
for (let d of data) if ((typeof (d?.collection) == "string") && (!d.collection.includes(filter))) DB.Insert(d)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
15
bst@asphyxia/handlers/utility/initialize.ts
Normal file
15
bst@asphyxia/handlers/utility/initialize.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { initializeBatch } from "./batch_initialize"
|
||||||
|
import { IPluginVersion } from "../../models/utility/plugin_version"
|
||||||
|
import { isHigherVersion } from "../../utility/utility_functions"
|
||||||
|
import { Batch } from "./batch"
|
||||||
|
import { DBM } from "./db_manager"
|
||||||
|
import { version } from "../../utility/about"
|
||||||
|
|
||||||
|
export async function initialize() {
|
||||||
|
let oldVersion = await DB.FindOne<IPluginVersion>({ collection: "bst.pluginVersion" })
|
||||||
|
if ((oldVersion == null) || isHigherVersion(oldVersion.version, version)) {
|
||||||
|
initializeBatch()
|
||||||
|
await Batch.execute(version)
|
||||||
|
await DBM.upsert<IPluginVersion>(null, { collection: "bst.pluginVersion" }, { collection: "bst.pluginVersion", version: version })
|
||||||
|
}
|
||||||
|
}
|
||||||
12
bst@asphyxia/handlers/utility/webui.ts
Normal file
12
bst@asphyxia/handlers/utility/webui.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { IWebUIMessage, WebUIMessageType } from "../../models/utility/webui_message"
|
||||||
|
import { DBM } from "./db_manager"
|
||||||
|
|
||||||
|
export namespace UtilityHandlersWebUI {
|
||||||
|
export function pushMessage(message: string, version: number, type: WebUIMessageType, rid?: string) {
|
||||||
|
DBM.upsert<IWebUIMessage>(null, { collection: "utility.webuiMessage" }, { collection: "utility.webuiMessage", message: message, type: type, refid: rid, version: version })
|
||||||
|
}
|
||||||
|
|
||||||
|
export const removeWebUIMessage = async () => {
|
||||||
|
await DBM.remove<IWebUIMessage>(null, { collection: "utility.webuiMessage" })
|
||||||
|
}
|
||||||
|
}
|
||||||
32
bst@asphyxia/index.ts
Normal file
32
bst@asphyxia/index.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
import { UtilityHandlersCommon } from "./handlers/utility/common"
|
||||||
|
import { UtilityHandlersWebUI } from "./handlers/utility/webui"
|
||||||
|
import { initialize } from "./handlers/utility/initialize"
|
||||||
|
import { Bst2HandlersCommon } from "./handlers/bst2/common"
|
||||||
|
import { Bst2HandlersWebUI } from "./handlers/bst2/webui"
|
||||||
|
|
||||||
|
export function register() {
|
||||||
|
R.GameCode("NBT")
|
||||||
|
|
||||||
|
routeBst2()
|
||||||
|
|
||||||
|
R.WebUIEvent("removeWebUIMessage", UtilityHandlersWebUI.removeWebUIMessage)
|
||||||
|
|
||||||
|
R.Unhandled()
|
||||||
|
|
||||||
|
initialize()
|
||||||
|
}
|
||||||
|
|
||||||
|
function routeBst2() {
|
||||||
|
R.Route("info2.common", Bst2HandlersCommon.Common)
|
||||||
|
R.Route("pcb2.boot", Bst2HandlersCommon.BootPcb)
|
||||||
|
R.Route("player2.start", Bst2HandlersCommon.StartPlayer)
|
||||||
|
R.Route("player2.continue", Bst2HandlersCommon.StartPlayer)
|
||||||
|
R.Route("player2.succeed", Bst2HandlersCommon.PlayerSucceeded)
|
||||||
|
R.Route("player2.read", Bst2HandlersCommon.ReadPlayer)
|
||||||
|
R.Route("player2.write", Bst2HandlersCommon.WritePlayer)
|
||||||
|
R.Route("player2.stagedata_write", Bst2HandlersCommon.WriteStageLog)
|
||||||
|
R.Route("player2.course_stage_data_write", Bst2HandlersCommon.WriteCourseStageLog)
|
||||||
|
R.Route("player2.course_data_write", Bst2HandlersCommon.WriteCourseLog)
|
||||||
|
|
||||||
|
R.WebUIEvent("bst2UpdateSettings", Bst2HandlersWebUI.UpdateSettings)
|
||||||
|
}
|
||||||
47
bst@asphyxia/models/bst2/event_params.ts
Normal file
47
bst@asphyxia/models/bst2/event_params.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import { BigIntProxy, boolme, KITEM2, KM, s32me, u64me } from "../../utility/mapping"
|
||||||
|
|
||||||
|
export interface IFloorInfectionEventParams {
|
||||||
|
id: number
|
||||||
|
musicList: number
|
||||||
|
isCompleted: boolean
|
||||||
|
}
|
||||||
|
export const FloorInfectionEventParamsMap: KM<IFloorInfectionEventParams> = {
|
||||||
|
id: s32me("infection_id", 20),
|
||||||
|
musicList: s32me("music_list", 7),
|
||||||
|
isCompleted: boolme("is_complete", true)
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2EventParams {
|
||||||
|
playerId: number
|
||||||
|
startTime: bigint | BigIntProxy
|
||||||
|
hasRbCollaboration: boolean
|
||||||
|
hasPopnCollaboration: boolean
|
||||||
|
floorInfection: { event: IFloorInfectionEventParams }
|
||||||
|
museca: { isPlayedMuseca: boolean }
|
||||||
|
}
|
||||||
|
export const Bst2EventParamsMap: KM<IBst2EventParams> = {
|
||||||
|
playerId: s32me("plyid"),
|
||||||
|
startTime: u64me("start_time"),
|
||||||
|
hasRbCollaboration: boolme("reflec_collabo", true),
|
||||||
|
hasPopnCollaboration: boolme("pop_collabo", true),
|
||||||
|
floorInfection: { event: FloorInfectionEventParamsMap, $targetKey: "floor_infection" },
|
||||||
|
museca: { isPlayedMuseca: boolme("is_play_museca", true) },
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2EventControl {
|
||||||
|
type: number
|
||||||
|
phase: number
|
||||||
|
}
|
||||||
|
export const Bst2EventControlMap: KM<IBst2EventControl> = {
|
||||||
|
type: s32me(),
|
||||||
|
phase: s32me()
|
||||||
|
}
|
||||||
|
|
||||||
|
let kEventControl: KITEM2<IBst2EventControl>[]
|
||||||
|
export function getKEventControl(): KITEM2<IBst2EventControl>[] {
|
||||||
|
if (kEventControl == null) {
|
||||||
|
kEventControl = []
|
||||||
|
for (let i = 0; i <= 40; i++) for (let j = 0; j <= 25; j++) kEventControl.push(<any>{ type: K.ITEM("s32", i), phase: K.ITEM("s32", j) })
|
||||||
|
}
|
||||||
|
return kEventControl
|
||||||
|
}
|
||||||
263
bst@asphyxia/models/bst2/profile.ts
Normal file
263
bst@asphyxia/models/bst2/profile.ts
Normal file
|
|
@ -0,0 +1,263 @@
|
||||||
|
import { BigIntProxy, boolme, colme, ignoreme, KM, s16me, s32me, s8me, strme, u16me, u64me, u8me } from "../../utility/mapping"
|
||||||
|
import { FixedSizeArray } from "../../utility/type"
|
||||||
|
import { ICollection } from "../utility/definitions"
|
||||||
|
|
||||||
|
export interface IBst2Account extends ICollection<"bst.bst2.player.account"> {
|
||||||
|
userId: number
|
||||||
|
isTakeOver: number
|
||||||
|
playerId: number
|
||||||
|
continueCount: number
|
||||||
|
playCount: number
|
||||||
|
playCountToday: number
|
||||||
|
crd: number
|
||||||
|
brd: number
|
||||||
|
dayCount: number
|
||||||
|
refid: string
|
||||||
|
lobbyId: string
|
||||||
|
mode: number
|
||||||
|
version: number
|
||||||
|
pp: boolean
|
||||||
|
ps: boolean
|
||||||
|
pay: number
|
||||||
|
payedPlayCount: number
|
||||||
|
standardTime: bigint | BigIntProxy
|
||||||
|
intrvld?: number
|
||||||
|
previousStartTime?: bigint | BigIntProxy
|
||||||
|
ea?: boolean
|
||||||
|
}
|
||||||
|
export const Bst2AccountMap: KM<IBst2Account> = {
|
||||||
|
collection: colme<IBst2Account>("bst.bst2.player.account"),
|
||||||
|
userId: s32me("usrid"),//
|
||||||
|
isTakeOver: s32me("is_takeover"),//
|
||||||
|
playerId: s32me("plyid"),
|
||||||
|
continueCount: s32me("continue_cnt"),
|
||||||
|
playCount: s32me("tpc"),//
|
||||||
|
playCountToday: s32me("dpc"),//
|
||||||
|
crd: s32me(),//
|
||||||
|
brd: s32me(),//
|
||||||
|
dayCount: s32me("tdc"),//
|
||||||
|
refid: strme("rid"),
|
||||||
|
lobbyId: strme("lid", "Asphyxia"),
|
||||||
|
mode: u8me(null, 2),
|
||||||
|
version: s16me("ver"),//
|
||||||
|
pp: boolme(),
|
||||||
|
ps: boolme(),
|
||||||
|
pay: s16me(),
|
||||||
|
payedPlayCount: s16me("pay_pc"),
|
||||||
|
standardTime: u64me("st", BigInt(Date.now())),//
|
||||||
|
intrvld: s32me(),//
|
||||||
|
previousStartTime: u64me("pst"),//
|
||||||
|
ea: boolme()//
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2Base extends ICollection<"bst.bst2.player.base"> {
|
||||||
|
name: string
|
||||||
|
brnk: number
|
||||||
|
bcnum: number
|
||||||
|
lcnum: number
|
||||||
|
volt: number
|
||||||
|
gold: number
|
||||||
|
lastMusicId: number
|
||||||
|
lastChart: number
|
||||||
|
lastSort: number
|
||||||
|
lastTab: number
|
||||||
|
splv: number
|
||||||
|
preference: number
|
||||||
|
lcid: number
|
||||||
|
hat: number
|
||||||
|
}
|
||||||
|
export const Bst2BaseMap: KM<IBst2Base> = {
|
||||||
|
collection: colme<IBst2Base>("bst.bst2.player.base"),
|
||||||
|
name: strme(),
|
||||||
|
brnk: s8me(),
|
||||||
|
bcnum: s8me(),
|
||||||
|
lcnum: s8me(),
|
||||||
|
volt: s32me(),
|
||||||
|
gold: s32me(),
|
||||||
|
lastMusicId: s32me("lmid"),
|
||||||
|
lastChart: s8me("lgrd"),
|
||||||
|
lastSort: s8me("lsrt"),
|
||||||
|
lastTab: s8me("ltab"),
|
||||||
|
splv: s8me(),
|
||||||
|
preference: s8me("pref"),
|
||||||
|
lcid: s32me(),
|
||||||
|
hat: s32me()
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2Survey extends ICollection<"bst.bst2.player.survey"> {
|
||||||
|
motivate: number
|
||||||
|
}
|
||||||
|
export const Bst2SurveyMap: KM<IBst2Survey> = {
|
||||||
|
collection: colme<IBst2Survey>("bst.bst2.player.survey"),
|
||||||
|
motivate: s8me()
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2UnlockingInfo extends ICollection<"bst.bst2.player.unlockingInfo"> {
|
||||||
|
type: number
|
||||||
|
id: number
|
||||||
|
param: number
|
||||||
|
count: number
|
||||||
|
}
|
||||||
|
export const Bst2UnlockingInfoMap: KM<IBst2UnlockingInfo> = {
|
||||||
|
collection: colme<IBst2UnlockingInfo>("bst.bst2.player.unlockingInfo"),
|
||||||
|
type: s32me(),
|
||||||
|
id: s32me(),
|
||||||
|
param: s32me(),
|
||||||
|
count: s32me()
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2Customization extends ICollection<"bst.bst2.player.customization"> {
|
||||||
|
// [rippleNote, rippleNoteColor, sfxNormalNote, sfxRippleNote, sfxSlashNote, sfxStreamNote, backgroundBrightnessTimes2, (000{sfxFine}{sfxStreamTail}{streamNoteGuide}{rippleNoteGuide}{judgeText}, ?, ?, ?, ?, ?, ?, ?, ?)]
|
||||||
|
custom: FixedSizeArray<number, 16>
|
||||||
|
}
|
||||||
|
export const Bst2CustomizationMap: KM<IBst2Customization> = {
|
||||||
|
collection: colme<IBst2Customization>("bst.bst2.player.customization"),
|
||||||
|
custom: u16me(null, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2Tips extends ICollection<"bst.bst2.player.tips"> {
|
||||||
|
lastTips: number
|
||||||
|
}
|
||||||
|
export const Bst2TipsMap: KM<IBst2Tips> = {
|
||||||
|
collection: colme<IBst2Tips>("bst.bst2.player.tips"),
|
||||||
|
lastTips: s32me("last_tips")
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2Hacker extends ICollection<"bst.bst2.player.hacker"> {
|
||||||
|
id: number
|
||||||
|
state0: number
|
||||||
|
state1: number
|
||||||
|
state2: number
|
||||||
|
state3: number
|
||||||
|
state4: number
|
||||||
|
updateTime: bigint | BigIntProxy
|
||||||
|
}
|
||||||
|
export const Bst2HackerMap: KM<IBst2Hacker> = {
|
||||||
|
collection: colme<IBst2Hacker>("bst.bst2.player.hacker"),
|
||||||
|
id: s32me(),
|
||||||
|
state0: s8me(),
|
||||||
|
state1: s8me(),
|
||||||
|
state2: s8me(),
|
||||||
|
state3: s8me(),
|
||||||
|
state4: s8me(),
|
||||||
|
updateTime: u64me("update_time")
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2CrysisLog extends ICollection<"bst.bst2.player.event.crysis"> {
|
||||||
|
id: number
|
||||||
|
stageId: number
|
||||||
|
step: number
|
||||||
|
gauge: number
|
||||||
|
state: number
|
||||||
|
}
|
||||||
|
export const Bst2CrysisLogMap: KM<IBst2CrysisLog> = {
|
||||||
|
collection: colme<IBst2CrysisLog>("bst.bst2.player.event.crysis"),
|
||||||
|
id: s32me(),
|
||||||
|
stageId: s32me("stage_no"),
|
||||||
|
step: s8me(),
|
||||||
|
gauge: s32me("r_gauge"),
|
||||||
|
state: s8me("r_state")
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2Bisco extends ICollection<"bst.bst2.player.bisco"> {
|
||||||
|
bnum: number
|
||||||
|
jbox: number
|
||||||
|
}
|
||||||
|
export const Bst2BiscoMap: KM<IBst2Bisco> = {
|
||||||
|
collection: colme<IBst2Bisco>("bst.bst2.player.bisco"),
|
||||||
|
bnum: s32me(),
|
||||||
|
jbox: s32me(),
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2MusicRecord extends ICollection<"bst.bst2.playData.musicRecord#userId"> {
|
||||||
|
musicId: number
|
||||||
|
chart: number
|
||||||
|
playCount: number
|
||||||
|
clearCount: number
|
||||||
|
gaugeTimes10: number
|
||||||
|
score: number
|
||||||
|
grade: number
|
||||||
|
medal: number
|
||||||
|
combo: number
|
||||||
|
userId: number
|
||||||
|
updateTime: number
|
||||||
|
lastPlayTime: number
|
||||||
|
}
|
||||||
|
export const Bst2MusicRecordMap: KM<IBst2MusicRecord> = {
|
||||||
|
collection: colme<IBst2MusicRecord>("bst.bst2.playData.musicRecord#userId"),
|
||||||
|
musicId: s32me("music_id"),
|
||||||
|
chart: s32me("note_level"),
|
||||||
|
playCount: s32me("play_count"),
|
||||||
|
clearCount: s32me("clear_count"),
|
||||||
|
gaugeTimes10: s32me("best_gauge"),
|
||||||
|
score: s32me("best_score"),
|
||||||
|
grade: s32me("best_grade"),
|
||||||
|
medal: s32me("best_medal"),
|
||||||
|
combo: ignoreme(),
|
||||||
|
userId: ignoreme(),
|
||||||
|
updateTime: ignoreme(),
|
||||||
|
lastPlayTime: ignoreme()
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2Course extends ICollection<"bst.bst2.playData.course#userId"> {
|
||||||
|
courseId: number
|
||||||
|
playCount: number
|
||||||
|
isTouched: boolean
|
||||||
|
clearType: number
|
||||||
|
gauge: number
|
||||||
|
score: number
|
||||||
|
grade: number
|
||||||
|
medal: number
|
||||||
|
combo: number
|
||||||
|
userId: number
|
||||||
|
updateTime: number
|
||||||
|
lastPlayTime: number
|
||||||
|
}
|
||||||
|
export const Bst2CourseMap: KM<IBst2Course> = {
|
||||||
|
collection: colme<IBst2Course>("bst.bst2.playData.course#userId"),
|
||||||
|
courseId: s32me("course_id"),
|
||||||
|
playCount: s32me("play"),
|
||||||
|
isTouched: boolme("is_touch"),
|
||||||
|
clearType: s32me("clear"),
|
||||||
|
gauge: s32me("gauge"),
|
||||||
|
score: s32me(),
|
||||||
|
grade: s32me(),
|
||||||
|
medal: s32me(),
|
||||||
|
combo: s32me(),
|
||||||
|
userId: ignoreme(),
|
||||||
|
updateTime: ignoreme(),
|
||||||
|
lastPlayTime: ignoreme()
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2Player {
|
||||||
|
pdata: {
|
||||||
|
account: IBst2Account
|
||||||
|
base: IBst2Base
|
||||||
|
opened: {}
|
||||||
|
survey: IBst2Survey
|
||||||
|
item: { info?: IBst2UnlockingInfo[] }
|
||||||
|
customize: IBst2Customization
|
||||||
|
tips: IBst2Tips
|
||||||
|
hacker: { info?: IBst2Hacker[] }
|
||||||
|
playLog: { crysis?: IBst2CrysisLog[] }
|
||||||
|
bisco: { pinfo: IBst2Bisco }
|
||||||
|
record: { rec?: IBst2MusicRecord[] }
|
||||||
|
course: { record?: IBst2Course[] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const Bst2PlayerMap: KM<IBst2Player> = {
|
||||||
|
pdata: {
|
||||||
|
account: Bst2AccountMap,
|
||||||
|
base: Bst2BaseMap,
|
||||||
|
opened: {},
|
||||||
|
survey: Bst2SurveyMap,
|
||||||
|
item: { info: { 0: Bst2UnlockingInfoMap } },
|
||||||
|
customize: Bst2CustomizationMap,
|
||||||
|
tips: Bst2TipsMap,
|
||||||
|
hacker: { info: { 0: Bst2HackerMap } },
|
||||||
|
playLog: { crysis: { 0: Bst2CrysisLogMap }, $targetKey: "play_log" },
|
||||||
|
bisco: { pinfo: Bst2BiscoMap },
|
||||||
|
record: { rec: { 0: Bst2MusicRecordMap } },
|
||||||
|
course: { record: { 0: Bst2CourseMap } }
|
||||||
|
}
|
||||||
|
}
|
||||||
80
bst@asphyxia/models/bst2/stagelog.ts
Normal file
80
bst@asphyxia/models/bst2/stagelog.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
import { colme, ignoreme, KM, s32me, strme } from "../../utility/mapping"
|
||||||
|
import { ICollection } from "../utility/definitions"
|
||||||
|
|
||||||
|
export interface IBst2StageLog extends ICollection<"bst.bst2.playData.stageLog#userId"> {
|
||||||
|
playerId: number
|
||||||
|
continueCount: number
|
||||||
|
stageId: number
|
||||||
|
userId: number
|
||||||
|
lobbyId: string
|
||||||
|
musicId: number
|
||||||
|
chart: number
|
||||||
|
gaugeTimes10: number
|
||||||
|
score: number
|
||||||
|
combo: number
|
||||||
|
grade: number
|
||||||
|
medal: number
|
||||||
|
fantasticCount: number
|
||||||
|
greatCount: number
|
||||||
|
fineCount: number
|
||||||
|
missCount: number
|
||||||
|
isCourseStage: boolean
|
||||||
|
time: number
|
||||||
|
}
|
||||||
|
export const Bst2StageLogMap: KM<IBst2StageLog> = {
|
||||||
|
collection: colme<IBst2StageLog>("bst.bst2.playData.stageLog#userId"),
|
||||||
|
playerId: s32me("play_id"),
|
||||||
|
continueCount: s32me("continue_count"),
|
||||||
|
stageId: s32me("stage_no"),
|
||||||
|
userId: s32me("user_id"),
|
||||||
|
lobbyId: strme("location_id"),
|
||||||
|
musicId: s32me("select_music_id"),
|
||||||
|
chart: s32me("select_grade"),
|
||||||
|
gaugeTimes10: s32me("result_clear_gauge"),
|
||||||
|
score: s32me("result_score"),
|
||||||
|
combo: s32me("result_max_combo"),
|
||||||
|
grade: s32me("result_grade"),
|
||||||
|
medal: s32me("result_medal"),
|
||||||
|
fantasticCount: s32me("result_fanta"),
|
||||||
|
greatCount: s32me("result_great"),
|
||||||
|
fineCount: s32me("result_fine"),
|
||||||
|
missCount: s32me("result_miss"),
|
||||||
|
isCourseStage: ignoreme(),
|
||||||
|
time: ignoreme(),
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IBst2CourseLog extends ICollection<"bst.bst2.playData.courseLog#userId"> {
|
||||||
|
playerId: number
|
||||||
|
continueCount: number
|
||||||
|
userId: number
|
||||||
|
courseId: number
|
||||||
|
gauge: number
|
||||||
|
score: number
|
||||||
|
grade: number
|
||||||
|
medal: number
|
||||||
|
combo: number
|
||||||
|
fantasticCount: number
|
||||||
|
greatCount: number
|
||||||
|
fineCount: number
|
||||||
|
missCount: number
|
||||||
|
lobbyId: string
|
||||||
|
time: number
|
||||||
|
}
|
||||||
|
export const Bst2CourseLogMap: KM<IBst2CourseLog> = {
|
||||||
|
collection: colme<IBst2CourseLog>("bst.bst2.playData.courseLog#userId"),
|
||||||
|
playerId: s32me("play_id"),
|
||||||
|
continueCount: s32me("continue_count"),
|
||||||
|
userId: s32me("user_id"),
|
||||||
|
courseId: s32me("course_id"),
|
||||||
|
lobbyId: strme("lid"),
|
||||||
|
gauge: s32me(),
|
||||||
|
score: s32me(),
|
||||||
|
combo: s32me(),
|
||||||
|
grade: s32me(),
|
||||||
|
medal: s32me(),
|
||||||
|
fantasticCount: s32me("fanta"),
|
||||||
|
greatCount: s32me("great"),
|
||||||
|
fineCount: s32me("fine"),
|
||||||
|
missCount: s32me("miss"),
|
||||||
|
time: ignoreme()
|
||||||
|
}
|
||||||
5
bst@asphyxia/models/utility/batch.ts
Normal file
5
bst@asphyxia/models/utility/batch.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
import { ICollection } from "./definitions"
|
||||||
|
|
||||||
|
export interface IBatchResult extends ICollection<"bst.batchResult"> {
|
||||||
|
batchId: string
|
||||||
|
}
|
||||||
3
bst@asphyxia/models/utility/definitions.d.ts
vendored
Normal file
3
bst@asphyxia/models/utility/definitions.d.ts
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export interface ICollection<TCollectionName extends string> {
|
||||||
|
collection: TCollectionName
|
||||||
|
}
|
||||||
5
bst@asphyxia/models/utility/plugin_version.ts
Normal file
5
bst@asphyxia/models/utility/plugin_version.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
import { ICollection } from "./definitions"
|
||||||
|
|
||||||
|
export interface IPluginVersion<TMajor extends number = number, TMinor extends number = number, TRevision extends number = number> extends ICollection<"bst.pluginVersion"> {
|
||||||
|
version: string
|
||||||
|
}
|
||||||
14
bst@asphyxia/models/utility/webui_message.ts
Normal file
14
bst@asphyxia/models/utility/webui_message.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { ICollection } from "./definitions"
|
||||||
|
|
||||||
|
export interface IWebUIMessage extends ICollection<"utility.webuiMessage"> {
|
||||||
|
message: string
|
||||||
|
type: WebUIMessageType
|
||||||
|
refid?: string
|
||||||
|
version: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum WebUIMessageType {
|
||||||
|
info = 0,
|
||||||
|
success = 1,
|
||||||
|
error = 2
|
||||||
|
}
|
||||||
4
bst@asphyxia/utility/about.ts
Normal file
4
bst@asphyxia/utility/about.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
export type Game = "bst"
|
||||||
|
export const game: Game = "bst"
|
||||||
|
export type PluginVersion = "1.0.0"
|
||||||
|
export const version: PluginVersion = "1.0.0"
|
||||||
480
bst@asphyxia/utility/mapping.ts
Normal file
480
bst@asphyxia/utility/mapping.ts
Normal file
|
|
@ -0,0 +1,480 @@
|
||||||
|
import { ICollection } from "../models/utility/definitions"
|
||||||
|
|
||||||
|
export type KArrayType = KNumberType | KBigIntType
|
||||||
|
export type KGroupType = KNumberGroupType | KBigIntGroupType
|
||||||
|
export type KType = KArrayType | KGroupType | "str" | "bin" | "ip4" | "bool"
|
||||||
|
export type KTypeExtended = KType | null | "kignore"
|
||||||
|
export type TypeForKItem = number | string | bigint | BigIntProxy | boolean | Buffer | number[] | bigint[] | boolean[] | BufferArray | NumberGroup<number[] | bigint[]>
|
||||||
|
export type TypeForKObject<T> = T extends TypeForKItem ? never : T
|
||||||
|
export type TypeForKArray = number[] | bigint[] | BufferArray
|
||||||
|
|
||||||
|
export type KKey<T> = keyof T & (
|
||||||
|
T extends string ? Exclude<keyof T, keyof string> :
|
||||||
|
T extends Buffer ? Exclude<keyof T, keyof Buffer> :
|
||||||
|
T extends boolean ? Exclude<keyof T, keyof boolean> :
|
||||||
|
T extends number[] | bigint[] | boolean[] ? Exclude<keyof T, (keyof number[]) | (keyof bigint[]) | (keyof boolean[])> :
|
||||||
|
T extends any[] ? Exclude<keyof T, keyof any[]> | number :
|
||||||
|
T extends number ? Exclude<keyof T, keyof number> :
|
||||||
|
T extends bigint | BigIntProxy ? Exclude<keyof T, keyof bigint> :
|
||||||
|
T extends BufferArray ? Exclude<keyof T, keyof BufferArray> :
|
||||||
|
T extends NumberGroup<infer TGroup> ? Exclude<keyof T, keyof NumberGroup<TGroup>> :
|
||||||
|
keyof T)
|
||||||
|
|
||||||
|
export type KTypeConvert<T extends string | Buffer | number | bigint | boolean | number[] | bigint[] | unknown> =
|
||||||
|
T extends string ? "str" :
|
||||||
|
T extends Buffer ? "bin" :
|
||||||
|
T extends number ? KNumberType | "ip4" | "bool" :
|
||||||
|
T extends bigint | BigIntProxy ? KBigIntType :
|
||||||
|
T extends boolean | boolean[] ? "bool" :
|
||||||
|
T extends number[] ? KNumberType : // KARRAY
|
||||||
|
T extends bigint[] ? KBigIntType : // KARRAY
|
||||||
|
T extends NumberGroup<number[]> ? KNumberGroupType :
|
||||||
|
T extends NumberGroup<bigint[]> ? KBigIntGroupType :
|
||||||
|
T extends BufferArray ? "u8" | "s8" :
|
||||||
|
never
|
||||||
|
|
||||||
|
export type KArrayTypeConvert<T extends Buffer | number[] | bigint[] | unknown> =
|
||||||
|
T extends Buffer ? "s8" | "u8" :
|
||||||
|
T extends number[] ? KNumberType :
|
||||||
|
T extends bigint[] ? KBigIntType :
|
||||||
|
never
|
||||||
|
|
||||||
|
export type KTypeConvertBack<TKType extends KTypeExtended> =
|
||||||
|
TKType extends "str" ? string :
|
||||||
|
TKType extends "bin" ? { type: "Buffer"; data: number[] } :
|
||||||
|
TKType extends "s8" | "u8" ? [number] | number[] | { type: "Buffer"; data: number[] } :
|
||||||
|
TKType extends KNumberType ? [number] | number[] :
|
||||||
|
TKType extends KBigIntType ? [bigint] | bigint[] :
|
||||||
|
TKType extends KNumberGroupType ? number[] :
|
||||||
|
TKType extends KBigIntGroupType ? bigint[] :
|
||||||
|
unknown
|
||||||
|
|
||||||
|
export type NumberGroup<T extends number[] | bigint[] = number[]> = {
|
||||||
|
"@numberGroupValue": T
|
||||||
|
}
|
||||||
|
export const NumberGroup = <T extends number[] | bigint[] = number[]>(ng: T) => <NumberGroup>{ "@numberGroupValue": ng }
|
||||||
|
export function isNumberGroup(value: any): value is NumberGroup {
|
||||||
|
try {
|
||||||
|
return Array.isArray(BigInt(value["@numberGroupValue"]))
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export type BufferArray = {
|
||||||
|
"@bufferArrayValue": Buffer
|
||||||
|
}
|
||||||
|
export const BufferArray = (ba: Buffer) => <BufferArray>{ "@bufferArrayValue": ba }
|
||||||
|
export function isBufferArray(value: any): value is BufferArray {
|
||||||
|
try {
|
||||||
|
return value["@bufferArrayValue"] instanceof Buffer
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export type BigIntProxy = {
|
||||||
|
"@serializedBigInt": string
|
||||||
|
}
|
||||||
|
export const BigIntProxy = (value: bigint) => <BigIntProxy>{ "@serializedBigInt": value.toString() }
|
||||||
|
export function isBigIntProxy(value: any): value is BigIntProxy {
|
||||||
|
try {
|
||||||
|
return BigInt(value["@serializedBigInt"]).toString() == value["@serializedBigInt"]
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function toBigInt(value: bigint | BigIntProxy): bigint {
|
||||||
|
if (value == null) return null
|
||||||
|
if (value instanceof BigInt) return <bigint>value
|
||||||
|
else if (value["@serializedBigInt"] != null) return BigInt(value["@serializedBigInt"])
|
||||||
|
else return BigInt(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
export type KITEM2<T> = { [K in keyof T]?: K extends KKey<T> ? KITEM2<T[K]> : never } &
|
||||||
|
{
|
||||||
|
["@attr"]: KAttrMap2<T>
|
||||||
|
["@content"]:
|
||||||
|
T extends string | Buffer | boolean | number[] | bigint[] ? T :
|
||||||
|
T extends number | bigint ? [T] :
|
||||||
|
T extends BufferArray ? Buffer :
|
||||||
|
T extends NumberGroup<infer TGroup> ? TGroup :
|
||||||
|
T extends BigIntProxy ? [bigint] : never
|
||||||
|
}
|
||||||
|
|
||||||
|
export type KAttrMap2<T> = { [key: string]: string } & {
|
||||||
|
__type?: T extends TypeForKItem ? KTypeConvert<T> : never
|
||||||
|
__count?: T extends TypeForKArray ? number : never
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ITEM2<T>(ktype: KTypeConvert<T>, value: T, attr?: KAttrMap2<T>): KITEM2<T> {
|
||||||
|
// let result
|
||||||
|
// if (value instanceof NumberGroup && IsNumberGroupKType(ktype)) {
|
||||||
|
// result = K.ITEM(<KTypeConvert<T & NumberGroup>>ktype, value.value, attr)
|
||||||
|
// } else if (Array.isArray(value) && IsNumericKType(ktype)) {
|
||||||
|
// result = K.ARRAY(<KTypeConvert<T & number[]>>ktype, <any>value, <any>attr)
|
||||||
|
// } else if (value instanceof BufferArray && IsNumericKType(ktype)) {
|
||||||
|
// result = K.ARRAY(<KTypeConvert<T & BufferArray>>ktype, value.value, attr)
|
||||||
|
// } else if (typeof value != "object" && typeof value != "function") {
|
||||||
|
// result = K.ITEM(<any>ktype, <any>value, attr)
|
||||||
|
// } else {
|
||||||
|
// Object.assign(result, value, { ["@attr"]: attr })
|
||||||
|
// result["@attr"].__type = ktype
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return <KITEM2<T>>result
|
||||||
|
let result = <KITEM2<T>>{}
|
||||||
|
result["@attr"] = Object.assign({}, attr, (!isNumberGroupKType(ktype) && isNumericKType(ktype) && Array.isArray(value)) ? { __type: ktype, __count: (<any[]>value).length } : { __type: ktype })
|
||||||
|
|
||||||
|
if ((ktype == "bool") && (typeof value == "boolean")) {
|
||||||
|
result["@content"] = <any>(value ? [1] : [0])
|
||||||
|
} else if ((ktype == "bin") && value instanceof Buffer) {
|
||||||
|
result = <any>K.ITEM("bin", value, result["@attr"])
|
||||||
|
} else if (((ktype == "s8") || (ktype == "u8")) && isBufferArray(value)) {
|
||||||
|
result["@content"] = <any>value["@bufferArrayValue"].toJSON()
|
||||||
|
result["@attr"].__count = <any>value["@bufferArrayValue"].byteLength
|
||||||
|
} else if (isNumericKType(ktype) && !Array.isArray(value)) {
|
||||||
|
result["@content"] = <any>[value]
|
||||||
|
} else if (isNumberGroupKType(ktype) && isNumberGroup(value)) {
|
||||||
|
result["@content"] = <any>value["@numberGroupValue"]
|
||||||
|
} else if (isBigIntProxy(value)) {
|
||||||
|
result["@content"] = <any>BigInt(value["@serializedBigInt"])
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result["@content"] = <any>value
|
||||||
|
}
|
||||||
|
if (isKIntType(ktype) && Array.isArray(result["@content"])) for (let i = 0; i < result["@content"].length; i++) (<number[]>result["@content"])[i] = Math.trunc(result["@content"][i])
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export type KObjectMappingRecord<T> = { [K in KKey<T>]: T[K] extends TypeForKItem ? KObjectMappingElementInfer<T[K]> : KObjectMappingRecord<T[K]> } & KObjectMappingElementInfer<T>
|
||||||
|
export interface KObjectMappingElement<T = any, TKType extends KTypeExtended = KTypeExtended> {
|
||||||
|
$type?: TKType,
|
||||||
|
$targetKey?: string,
|
||||||
|
$convert?: (source: T) => T
|
||||||
|
$convertBack?: (target: T) => T
|
||||||
|
$fallbackValue?: TKType extends "kignore" ? T : never
|
||||||
|
$defaultValue?: T
|
||||||
|
}
|
||||||
|
type KObjectMappingElementInfer<T> = KObjectMappingElement<T, (KTypeConvert<T> extends KType ? KTypeConvert<T> : never) | never | "kignore">
|
||||||
|
|
||||||
|
export type KAttrRecord<T> = { [K in keyof T]?: T extends TypeForKItem ? KAttrMap2<T[K]> : KAttrRecord<T[K]> } & { selfAttr?: KAttrMap2<T> }
|
||||||
|
|
||||||
|
export function getCollectionMappingElement<TCollection extends ICollection<any>>(collectionName: TCollection extends ICollection<infer TName> ? TName : never): KObjectMappingElement<TCollection extends ICollection<infer TName> ? TName : unknown, "kignore"> {
|
||||||
|
return ignoreme("collection", collectionName)
|
||||||
|
}
|
||||||
|
|
||||||
|
function isKType<TType>(type: TType): boolean {
|
||||||
|
return (typeof (type) == "string") && ["s8", "u8", "s16", "u16", "s32", "u32", "time", "ip4", "float", "double", "bool", "s64", "u64", "2s8", "2u8", "2s16", "2u16", "2s32", "2u32", "2f", "2d", "3s8", "3u8", "3s16", "3u16", "3s32", "3u32", "3f", "3d", "4s8", "4u8", "4s16", "4u16", "4s32", "4u32", "4f", "4d", "2b", "3b", "4b", "vb", "2s64", "2u64", "3s64", "3u64", "4s64", "4u64", "vs8", "vu8", "vs16", "vu16", "str", "bin"].includes(type)
|
||||||
|
}
|
||||||
|
function isKIntType<TType>(type: TType): boolean {
|
||||||
|
return (typeof (type) == "string") && ["s8", "u8", "s16", "u16", "s32", "u32", "2s8", "2u8", "2s16", "2u16", "2s32", "2u32", "3s8", "3u8", "3s16", "3u16", "3s32", "3u32", "4s8", "4u8", "4s16", "4u16", "4s32", "4u32", "2b", "3b", "4b", "vb", "vs8", "vu8", "vs16", "vu16"].includes(type)
|
||||||
|
}
|
||||||
|
function isKBigIntType<TType>(type: TType): boolean {
|
||||||
|
return (typeof (type) == "string") && ["s64", "u64"].includes(type)
|
||||||
|
}
|
||||||
|
function isNumericKType<TType>(type: TType): boolean {
|
||||||
|
return (typeof (type) == "string") && ["s8", "u8", "s16", "u16", "s32", "u32", "time", "ip4", "float", "double", "bool", "s64", "u64"].includes(type)
|
||||||
|
}
|
||||||
|
function isNumberGroupKType<TType>(type: TType): boolean {
|
||||||
|
return (typeof (type) == "string") && ["2s8", "2u8", "2s16", "2u16", "2s32", "2u32", "2f", "2d", "3s8", "3u8", "3s16", "3u16", "3s32", "3u32", "3f", "3d", "4s8", "4u8", "4s16", "4u16", "4s32", "4u32", "4f", "4d", "2b", "3b", "4b", "vb", "2s64", "2u64", "3s64", "3u64", "4s64", "4u64", "vs8", "vu8", "vs16", "vu16"].includes(type)
|
||||||
|
}
|
||||||
|
function isNumericKey(k: any): boolean {
|
||||||
|
return (typeof k == "number") || (parseInt(k).toString() == k)
|
||||||
|
}
|
||||||
|
function increaseNumericKey<T>(k: T, step: number = 1): T {
|
||||||
|
return (typeof k == "number") ? <T><unknown>(k + step) : (typeof k == "string" && parseInt(k).toString() == k) ? <T><unknown>(parseInt(k) + step) : k
|
||||||
|
}
|
||||||
|
function isEmptyKObject(o: object): boolean {
|
||||||
|
return (Object.keys(o).length == 0) || ((Object.keys(o).length == 1) && (o["@attr"] != null))
|
||||||
|
}
|
||||||
|
function isKMapRecordReservedKey(key: string): boolean {
|
||||||
|
return ["$type", "$targetKey", "$convert", "$convertBack", "$fallbackValue", "$defaultValue"].includes(key)
|
||||||
|
}
|
||||||
|
function isKArray<T>(data: KITEM2<T>): boolean {
|
||||||
|
return (data["@attr"] != null) && (data["@attr"].__count != null)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function appendMappingElement<T>(map: KObjectMappingRecord<T>, element: KObjectMappingElementInfer<T>): KObjectMappingRecord<T> {
|
||||||
|
let result = <KObjectMappingRecord<T>>{}
|
||||||
|
Object.assign(result, map, element)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mapKObject<T>(data: T, kMapRecord: KObjectMappingRecord<T>, kAttrRecord: KAttrRecord<T> = <KAttrRecord<T>>{}): KITEM2<T> {
|
||||||
|
if (data == null) return <KITEM2<T>>{}
|
||||||
|
let result: KITEM2<T> = <any>(((0 in data) && data instanceof Object) ? [] : {})
|
||||||
|
if (kAttrRecord.selfAttr != null) result["@attr"] = kAttrRecord.selfAttr
|
||||||
|
|
||||||
|
if (data instanceof Object) {
|
||||||
|
for (let __k in data) {
|
||||||
|
let k: keyof T = __k
|
||||||
|
let mapK: keyof T = __k
|
||||||
|
let attrK: keyof T = __k
|
||||||
|
if (!(k in kMapRecord) && isNumericKey(k)) {
|
||||||
|
for (let i = parseInt(<string>k) - 1; i >= 0; i--) if (kMapRecord[i]) {
|
||||||
|
mapK = <keyof T>i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!(k in kAttrRecord) && isNumericKey(k)) {
|
||||||
|
for (let i = parseInt(<string>k) - 1; i >= 0; i--) if (kAttrRecord[i]) {
|
||||||
|
attrK = <keyof T>i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (mapK in kMapRecord) {
|
||||||
|
let target = <KITEM2<T>[keyof T]>{}
|
||||||
|
let targetMap = kMapRecord[<KKey<T>>mapK]
|
||||||
|
let targetKey: keyof T = (targetMap.$targetKey != null) ? <keyof T>targetMap.$targetKey : k
|
||||||
|
let targetValue = (targetMap.$convert != null) ? <KTypeConvertBack<KTypeConvert<T[keyof T]>>>targetMap.$convert(<any>data[k]) : data[k]
|
||||||
|
let targetAttr = kAttrRecord[attrK]
|
||||||
|
if (targetMap.$type) {
|
||||||
|
let tt = targetMap.$type
|
||||||
|
if (tt == "kignore") continue
|
||||||
|
target["@attr"] = <any>Object.assign({}, targetAttr, (!isNumberGroupKType(tt) && isNumericKType(tt) && Array.isArray(data[k]) && Array.isArray(targetValue)) ? { __type: tt, __count: (<any[]>targetValue).length } : { __type: tt })
|
||||||
|
|
||||||
|
if ((tt == "bool") && (typeof targetValue == "boolean")) {
|
||||||
|
target["@content"] = <any>(targetValue ? [1] : [0])
|
||||||
|
} else if ((tt == "bin") && targetValue instanceof Buffer) {
|
||||||
|
target = <any>K.ITEM("bin", targetValue, target["@attr"])
|
||||||
|
} else if (((tt == "s8") || (tt == "u8")) && isBufferArray(targetValue)) {
|
||||||
|
target["@content"] = <any>targetValue["@bufferArrayValue"]
|
||||||
|
} else if (isNumericKType(tt) && !Array.isArray(targetValue)) {
|
||||||
|
target["@content"] = <any>[targetValue]
|
||||||
|
} else if (isNumberGroupKType(tt) && isNumberGroup(targetValue)) {
|
||||||
|
target["@content"] = <any>targetValue["@numberGroupValue"]
|
||||||
|
} else if (isBufferArray(targetValue)) {
|
||||||
|
target["@content"] = <any>targetValue["@bufferArrayValue"].toJSON()
|
||||||
|
target["@attr"].__count = <any>targetValue["@bufferArrayValue"].byteLength
|
||||||
|
} else if (isBigIntProxy(targetValue)) {
|
||||||
|
target["@content"] = <any>BigInt(targetValue["@serializedBigInt"])
|
||||||
|
} else {
|
||||||
|
target["@content"] = <any>targetValue
|
||||||
|
}
|
||||||
|
if (isKIntType(tt) && Array.isArray(target["@content"])) for (let i = 0; i < target["@content"].length; i++) (<number[]>target["@content"])[i] = Math.trunc(target["@content"][i])
|
||||||
|
} else {
|
||||||
|
target = <any>mapKObject(<T[keyof T]>targetValue, <KObjectMappingRecord<T[keyof T]>><unknown>targetMap, <KAttrRecord<T[keyof T]>>targetAttr)
|
||||||
|
}
|
||||||
|
result[targetKey] = target
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else result = ITEM2<T>(<KTypeConvert<T>>kAttrRecord.selfAttr.$type, data, kAttrRecord.selfAttr)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MapBackResult<T> = {
|
||||||
|
data: T,
|
||||||
|
attr?: KAttrRecord<T>
|
||||||
|
}
|
||||||
|
export function mapBackKObject<T extends object>(data: KITEM2<T>, kMapRecord?: KObjectMappingRecord<T>): MapBackResult<T> {
|
||||||
|
if (kMapRecord == null) {
|
||||||
|
if (data["@content"] || data["@attr"]) return { data: <any>data["@content"], attr: <any>data["@attr"] }
|
||||||
|
else return { data: <T>data }
|
||||||
|
}
|
||||||
|
let result: T = <T>((Array.isArray(data) || 0 in kMapRecord) ? [] : {})
|
||||||
|
let resultAttr: KAttrRecord<T> = <any>{ selfAttr: data["@attr"] ? data["@attr"] : null }
|
||||||
|
|
||||||
|
for (let __k in kMapRecord) {
|
||||||
|
if (isKMapRecordReservedKey(__k)) continue
|
||||||
|
let k = <keyof T>__k
|
||||||
|
let preservK = <keyof T>__k
|
||||||
|
do {
|
||||||
|
let targetMap = kMapRecord[<KKey<T>>preservK]
|
||||||
|
let targetKey = <keyof T>(targetMap.$targetKey ? targetMap.$targetKey : k)
|
||||||
|
let doOnceFlag = (isNumericKey(targetKey) && (data[targetKey] == null) && !isEmptyKObject(data))
|
||||||
|
let targetValue = <KITEM2<T>[keyof T]>(doOnceFlag ? data : data[targetKey])
|
||||||
|
|
||||||
|
if (targetMap.$type == "kignore") {
|
||||||
|
result[k] = targetMap.$fallbackValue
|
||||||
|
if ((targetValue != null) && (targetValue["@attr"] != null)) resultAttr[k] = <KAttrRecord<T>[keyof T]>{ selfAttr: targetValue["@attr"] }
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetValue == null) {
|
||||||
|
if (targetMap.$convertBack != null) result[k] = targetMap.$convertBack(<any>null)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetValue["@attr"] != null) {
|
||||||
|
let targetAttr: KAttrMap2<T[keyof T]> = targetValue["@attr"]
|
||||||
|
let targetResult
|
||||||
|
|
||||||
|
if (targetAttr.__type != null) { // KITEM
|
||||||
|
targetResult = targetValue["@content"]
|
||||||
|
if (isNumberGroupKType(targetAttr.__type)) { // KITEM2<NumberGroup>
|
||||||
|
// TODO: bigint number group
|
||||||
|
targetResult = NumberGroup(targetResult)
|
||||||
|
} else if (targetAttr.__type == "bin") { // KITEM<"bin">
|
||||||
|
targetResult = targetResult
|
||||||
|
} else if ((targetAttr.__type == "s8" || targetAttr.__type == "u8") && (targetResult?.type == "Buffer") && Array.isArray(targetResult?.data)) { // KITEM2<BufferArray>
|
||||||
|
targetResult = BufferArray(Buffer.from(<number[]>targetResult.data))
|
||||||
|
} else if (targetAttr.__type == "bool") { // KITEM<"bool">
|
||||||
|
targetResult = targetResult[0] == 1 ? true : false
|
||||||
|
} else if (Array.isArray(targetResult) && (targetAttr.__count == null) && isNumericKType(targetAttr.__type)) { // KITEM<KNumberType>
|
||||||
|
targetResult = ((targetAttr.__type == "s64") || (targetAttr.__type == "u64")) ? BigIntProxy(BigInt(targetResult[0])) : targetResult[0]
|
||||||
|
}
|
||||||
|
result[k] = (targetMap.$convertBack != null) ? targetMap.$convertBack(<any>targetResult) : targetResult
|
||||||
|
} else { // KObject
|
||||||
|
targetResult = (targetMap.$convertBack != null) ? targetMap.$convertBack(<any>targetValue) : targetValue;
|
||||||
|
let partial = mapBackKObject<T[keyof T] & object>(targetResult, <any>targetMap)
|
||||||
|
result[k] = partial.data
|
||||||
|
resultAttr[k] = <any>partial.attr
|
||||||
|
}
|
||||||
|
} else { // KObject
|
||||||
|
let targetResult = (targetMap.$convertBack != null) ? targetMap.$convertBack(<any>targetValue) : targetValue;
|
||||||
|
let partial = <any>mapBackKObject<T[keyof T] & object>(<any>targetResult, <any>targetMap)
|
||||||
|
result[k] = partial.data
|
||||||
|
resultAttr[k] = <any>partial.attr
|
||||||
|
}
|
||||||
|
k = increaseNumericKey(k)
|
||||||
|
if (doOnceFlag || (isNumericKey(k) && (data[<keyof T>(targetMap.$targetKey ? targetMap.$targetKey : k)] == null))) break
|
||||||
|
} while (isNumericKey(k) && !(k in kMapRecord))
|
||||||
|
}
|
||||||
|
return { data: result, attr: resultAttr }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function s8me<T extends number | number[]>(targetKey?: string, defaultValue?: T, convert?: (source: T) => T, convertBack?: (target: T) => T): KObjectMappingElement<T, "s8"> {
|
||||||
|
return {
|
||||||
|
$type: "s8",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function u8me<T extends number | number[]>(targetKey?: string, defaultValue?: T, convert?: (source: T) => T, convertBack?: (target: T) => T): KObjectMappingElement<T, "u8"> {
|
||||||
|
return {
|
||||||
|
$type: "u8",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function s16me<T extends number | number[]>(targetKey?: string, defaultValue?: T, convert?: (source: T) => T, convertBack?: (target: T) => T): KObjectMappingElement<T, "s16"> {
|
||||||
|
return {
|
||||||
|
$type: "s16",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function u16me<T extends number | number[]>(targetKey?: string, defaultValue?: T, convert?: (source: T) => T, convertBack?: (target: T) => T): KObjectMappingElement<T, "u16"> {
|
||||||
|
return {
|
||||||
|
$type: "u16",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function s32me<T extends number | number[]>(targetKey?: string, defaultValue?: T, convert?: (source: T) => T, convertBack?: (target: T) => T): KObjectMappingElement<T, "s32"> {
|
||||||
|
return {
|
||||||
|
$type: "s32",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function u32me<T extends number | number[]>(targetKey?: string, defaultValue?: T, convert?: (source: T) => T, convertBack?: (target: T) => T): KObjectMappingElement<T, "u32"> {
|
||||||
|
return {
|
||||||
|
$type: "u32",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function s64me(targetKey?: string, defaultValue?: bigint | BigIntProxy, convert?: (source: bigint | BigIntProxy) => bigint | BigIntProxy, convertBack?: (target: bigint | BigIntProxy) => bigint | BigIntProxy): KObjectMappingElement<bigint | BigIntProxy, "s64"> {
|
||||||
|
return {
|
||||||
|
$type: "s64",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function u64me(targetKey?: string, defaultValue?: bigint | BigIntProxy, convert?: (source: bigint | BigIntProxy) => bigint | BigIntProxy, convertBack?: (target: bigint | BigIntProxy) => bigint | BigIntProxy): KObjectMappingElement<bigint | BigIntProxy, "u64"> {
|
||||||
|
return {
|
||||||
|
$type: "u64",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function boolme<T extends boolean | boolean[]>(targetKey?: string, defaultValue?: T, convert?: (source: T) => T, convertBack?: (target: T) => T): KObjectMappingElement<T, "bool"> {
|
||||||
|
return {
|
||||||
|
$type: "bool",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function strme<TName extends string>(targetKey?: string, defaultValue?: TName, convert?: (source: TName) => TName, convertBack?: (target: TName) => TName): KObjectMappingElement<TName, "str"> {
|
||||||
|
return {
|
||||||
|
$type: "str",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function binme(targetKey?: string, defaultValue?: Buffer, convert?: (source: Buffer) => Buffer, convertBack?: (target: Buffer) => Buffer): KObjectMappingElement<Buffer, "bin"> {
|
||||||
|
return {
|
||||||
|
$type: "bin",
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ignoreme<T = any>(targetKey?: string, fallbackValue?: T): KObjectMappingElement<T, "kignore"> {
|
||||||
|
return {
|
||||||
|
$type: "kignore",
|
||||||
|
$fallbackValue: fallbackValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function me<T extends object>(targetKey?: string, defaultValue?: T, convert?: (source: T) => T, convertBack?: (target: T) => T): KObjectMappingElement<T, null> {
|
||||||
|
return {
|
||||||
|
$targetKey: targetKey,
|
||||||
|
$convert: convert,
|
||||||
|
$convertBack: convertBack,
|
||||||
|
$defaultValue: defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const colme = getCollectionMappingElement
|
||||||
|
export const appendme = appendMappingElement
|
||||||
|
export const mapK = mapKObject
|
||||||
|
export const bacK = mapBackKObject
|
||||||
|
|
||||||
|
export function fromMap<T>(map: KObjectMappingRecord<T>): T {
|
||||||
|
let result = <T>{}
|
||||||
|
if (map.$type == "kignore") return map.$fallbackValue
|
||||||
|
if (map.$defaultValue != null) return map.$defaultValue
|
||||||
|
if (map.$type != null) {
|
||||||
|
if (isNumericKType(map.$type)) {
|
||||||
|
if (map.$type == "bool") return <any>false
|
||||||
|
else return <any>0
|
||||||
|
} else if (isKBigIntType(map.$type)) return <any>BigInt(0)
|
||||||
|
else if (isNumberGroupKType(map.$type)) return <any>NumberGroup([0])
|
||||||
|
else if (map.$type == "str") return <any>""
|
||||||
|
|
||||||
|
else return null
|
||||||
|
}
|
||||||
|
for (let k in map) {
|
||||||
|
if (isKMapRecordReservedKey(k)) continue
|
||||||
|
let value = fromMap(map[k])
|
||||||
|
if (value != null) result[k] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export type KM<T> = KObjectMappingRecord<T>
|
||||||
4
bst@asphyxia/utility/type.ts
Normal file
4
bst@asphyxia/utility/type.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
export type FixedSizeArray<T, TSize extends number> = [T, ...T[]] & { readonly length: TSize }
|
||||||
|
export function fillArray<T, TSize extends number>(size: TSize, fillValue: T): FixedSizeArray<T, TSize> {
|
||||||
|
return <any>Array(size).fill(fillValue)
|
||||||
|
}
|
||||||
67
bst@asphyxia/utility/utility_functions.ts
Normal file
67
bst@asphyxia/utility/utility_functions.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
export function toFullWidth(s: string): string {
|
||||||
|
let resultCharCodes: number[] = []
|
||||||
|
for (let i = 0; i < s.length; i++) {
|
||||||
|
let cc = s.charCodeAt(i)
|
||||||
|
if ((cc >= 33) && (cc <= 126)) resultCharCodes.push(cc + 65281 - 33)
|
||||||
|
else if (cc == 32) resultCharCodes.push(12288) // Full-width space
|
||||||
|
else resultCharCodes.push(cc)
|
||||||
|
}
|
||||||
|
return String.fromCharCode(...resultCharCodes)
|
||||||
|
}
|
||||||
|
export function toHalfWidth(s: string): string {
|
||||||
|
let resultCharCodes: number[] = []
|
||||||
|
for (let i = 0; i < s.length; i++) {
|
||||||
|
let cc = s.charCodeAt(i)
|
||||||
|
if ((cc >= 65281) && (cc <= 65374)) resultCharCodes.push(cc - 65281 + 33)
|
||||||
|
else if (cc == 12288) resultCharCodes.push(32) // Full-width space
|
||||||
|
else resultCharCodes.push(cc)
|
||||||
|
}
|
||||||
|
return String.fromCharCode(...resultCharCodes)
|
||||||
|
}
|
||||||
|
export function isToday(st: bigint): boolean {
|
||||||
|
let now = new Date()
|
||||||
|
let today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
||||||
|
let tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1)
|
||||||
|
return (st >= (today.valueOf())) && (st < (tomorrow.valueOf()))
|
||||||
|
}
|
||||||
|
export async function log(data: any, file?: string) {
|
||||||
|
if (file == null) file = "./log.txt"
|
||||||
|
let s = IO.Exists(file) ? await IO.ReadFile(file, "") : ""
|
||||||
|
if (typeof data == "string") s += data + "\n"
|
||||||
|
else {
|
||||||
|
let n = ""
|
||||||
|
try {
|
||||||
|
n = JSON.stringify(data)
|
||||||
|
} catch { }
|
||||||
|
s += n + "\n"
|
||||||
|
}
|
||||||
|
await IO.WriteFile(file, s)
|
||||||
|
}
|
||||||
|
export function base64ToBuffer(str: string, size?: number): Buffer {
|
||||||
|
if (size != null) {
|
||||||
|
let rem = size - Math.trunc(size / 3) * 3
|
||||||
|
str = str.replace("=", "A").replace("=", "A").padEnd(Math.trunc(size / 3) * 4 + rem + 1, "A")
|
||||||
|
if (rem == 1) str += "=="
|
||||||
|
else if (rem == 2) str += "="
|
||||||
|
let result = Buffer.alloc(size, str, "base64")
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
else return Buffer.from(str, "base64")
|
||||||
|
}
|
||||||
|
export function bufferToBase64(buffer: Buffer, isTrimZero: boolean = true): string {
|
||||||
|
if (isTrimZero) for (let i = buffer.length - 1; i >= 0; i--) if (buffer.readInt8(i) != 0) return buffer.toString("base64", 0, i + 1)
|
||||||
|
return buffer.toString("base64")
|
||||||
|
}
|
||||||
|
export function isHigherVersion(left: string, right: string): boolean {
|
||||||
|
let splitedLeft = left.split(".")
|
||||||
|
let splitedRight = right.split(".")
|
||||||
|
|
||||||
|
if (parseInt(splitedLeft[0]) < parseInt(splitedRight[0])) return true
|
||||||
|
else if (parseInt(splitedLeft[0]) == parseInt(splitedRight[0])) {
|
||||||
|
if (parseInt(splitedLeft[1]) < parseInt(splitedRight[1])) return true
|
||||||
|
else if (parseInt(splitedLeft[1]) == parseInt(splitedRight[1])) {
|
||||||
|
if (parseInt(splitedLeft[2]) < parseInt(splitedRight[2])) return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
243
bst@asphyxia/webui/css/webui_util.css
Normal file
243
bst@asphyxia/webui/css/webui_util.css
Normal file
|
|
@ -0,0 +1,243 @@
|
||||||
|
#tab-content, .tab-content {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tab-content.is-active, .tab-content.is-active {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
tr#tab-content.is-active, tr.tab-content.is-active {
|
||||||
|
display: table-row;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tabs li.disabled a {
|
||||||
|
background-color: #c0c0c0;
|
||||||
|
border-color: #c0c0c0;
|
||||||
|
color: #7f7f7f;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
#form-pagination ul.pagination-list {
|
||||||
|
margin: 0!important;
|
||||||
|
}
|
||||||
|
.pagination-link, .pagination-next, .pagination-previous {
|
||||||
|
border-color: transparent;
|
||||||
|
transition: .2s linear;
|
||||||
|
}
|
||||||
|
.pagination-next, .pagination-previous {
|
||||||
|
color: #209CEE;
|
||||||
|
}
|
||||||
|
.pagination-next:not([disabled]):hover, .pagination-previous:not([disabled]):hover {
|
||||||
|
color: #118fe4;
|
||||||
|
}
|
||||||
|
/* Set all link color to Asphyxia CORE blue */
|
||||||
|
::selection {
|
||||||
|
color: white;
|
||||||
|
background-color: #209CEE;
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
color: #209CEE;
|
||||||
|
}
|
||||||
|
.tabs.is-toggle li.is-active a {
|
||||||
|
background-color: #209CEE;
|
||||||
|
border-color: #209CEE;
|
||||||
|
}
|
||||||
|
.tabs li.is-active a {
|
||||||
|
color: #209CEE;
|
||||||
|
border-color: #209CEE;
|
||||||
|
}
|
||||||
|
.pagination-link.is-current {
|
||||||
|
background-color: #209CEE;
|
||||||
|
border-color: #209CEE;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
.select:not(.is-multiple):not(.is-loading):after {
|
||||||
|
border-color: #209CEE;
|
||||||
|
}
|
||||||
|
.select select:active, .select select:focus {
|
||||||
|
border-color: #209CEE;
|
||||||
|
}
|
||||||
|
.button.is-link {
|
||||||
|
background-color: #209CEE;
|
||||||
|
}
|
||||||
|
.button.is-link.is-active, .button.is-link:active, .button.is-link.is-hovered, .button.is-link:hover {
|
||||||
|
background-color: #118fe4;
|
||||||
|
}
|
||||||
|
.input:active, .input:focus {
|
||||||
|
border-color: #209CEE;
|
||||||
|
}
|
||||||
|
.table tr.is-selected {
|
||||||
|
background-color: #209CEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
#card-content.is-hidden {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
#card-content {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.marquee-label {
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
.marquee-label-container {
|
||||||
|
overflow-x: hidden;
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* from Bulma */
|
||||||
|
.button.is-danger.is-light {
|
||||||
|
background-color: #feecf0;
|
||||||
|
color: #cc0f35;
|
||||||
|
}
|
||||||
|
.button.is-link.is-light {
|
||||||
|
background-color: #edf8ff;
|
||||||
|
color: #209CEE;
|
||||||
|
}
|
||||||
|
.button.is-danger.is-light.is-hovered, .button.is-danger.is-light:hover {
|
||||||
|
background-color: #fde0e6;
|
||||||
|
color: #cc0f35;
|
||||||
|
}
|
||||||
|
.button.is-link.is-light.is-hovered, .button.is-link.is-light:hover {
|
||||||
|
background-color: #e0f1fc;
|
||||||
|
color: #209CEE;
|
||||||
|
}
|
||||||
|
.tag.is-link.is-light {
|
||||||
|
background-color: #edf8ff;
|
||||||
|
color: #0D7DC6;
|
||||||
|
}
|
||||||
|
.tag.is-link.is-light:hover {
|
||||||
|
background-color: #209CEE;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.tag.is-delete:hover {
|
||||||
|
background-color: #FF3860!important;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media screen and (max-width: 768px) {
|
||||||
|
.pagination {
|
||||||
|
flex-wrap: nowrap;
|
||||||
|
justify-content: left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.pagination-list {
|
||||||
|
flex-wrap: nowrap;
|
||||||
|
list-style: none!important;
|
||||||
|
margin-top: 0.25em!important;
|
||||||
|
margin-bottom: 0.25em!important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.content li + li {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.one-quarter#forwide, .one-third#forwide {
|
||||||
|
display: block;
|
||||||
|
min-width: 100px;
|
||||||
|
}
|
||||||
|
.one-quarter#fornarrow, .one-third#fornarrow {
|
||||||
|
display: none;
|
||||||
|
min-width: 50px;
|
||||||
|
}
|
||||||
|
@media only screen and (max-width: 1023px) {
|
||||||
|
.one-quarter#forwide {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.one-quarter#fornarrow {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@media only screen and (max-width: 700px) {
|
||||||
|
.one-third#forwide {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.one-third#fornarrow {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@keyframes notification-fadeout {
|
||||||
|
0% {
|
||||||
|
opacity: 1;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
80% {
|
||||||
|
opacity: 1;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
99.99% {
|
||||||
|
opacity: 0;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
100% {
|
||||||
|
opacity: 0;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.notification {
|
||||||
|
animation: notification-fadeout 8s forwards;
|
||||||
|
animation-play-state: paused;
|
||||||
|
}
|
||||||
|
.notification:hover {
|
||||||
|
animation-play-state: paused;
|
||||||
|
}
|
||||||
|
.modal {
|
||||||
|
padding-bottom: 13px;
|
||||||
|
}
|
||||||
|
@media screen and (max-width:1024px) {
|
||||||
|
.modal {
|
||||||
|
transition: padding-left .2s ease-in-out 50ms;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@media screen and (min-width:1023px) {
|
||||||
|
.modal {
|
||||||
|
padding-left: 256px;
|
||||||
|
transition: padding-left .2s ease-in-out 50ms;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.tag {
|
||||||
|
transition: linear .2s;
|
||||||
|
}
|
||||||
|
.tags .tag:not(:last-child) {
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal table tr {
|
||||||
|
border: solid #dbdbdb;
|
||||||
|
border-width: 0 0 1px;
|
||||||
|
}
|
||||||
|
.modal table tbody tr:last-child {
|
||||||
|
border-bottom-width: 0;
|
||||||
|
}
|
||||||
|
.hidden-wrapper {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.hidden-x-wrapper {
|
||||||
|
overflow-x: hidden;
|
||||||
|
}
|
||||||
|
.hidden-y-wrapper {
|
||||||
|
overflow-y: hidden;
|
||||||
|
}
|
||||||
|
.scrolling-wrapper {
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
|
.scrolling-x-wrapper {
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
.scrolling-y-wrapper {
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
|
a.pagination-previous {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
a.pagination-next {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.button.checkbox, .button.checkbox .checkmark {
|
||||||
|
transition: linear .2s;
|
||||||
|
}
|
||||||
618
bst@asphyxia/webui/js/webui_util.js
Normal file
618
bst@asphyxia/webui/js/webui_util.js
Normal file
|
|
@ -0,0 +1,618 @@
|
||||||
|
function initializePaginatedContent() {
|
||||||
|
let containers = document.querySelectorAll(".paginated-container")
|
||||||
|
|
||||||
|
for (let container of containers) {
|
||||||
|
let pageSizeInput = container.querySelector("input.page-size")
|
||||||
|
let paginations = container.querySelectorAll(".pagination")
|
||||||
|
let contents = container.querySelectorAll(".paginated-content")
|
||||||
|
let group = container.getAttribute("pagination-group")
|
||||||
|
let flags = { isFirst: true }
|
||||||
|
let refreshEllipsis = (param) => {
|
||||||
|
if (flags.isFirst) return
|
||||||
|
let maxWidth = container.offsetWidth / 2
|
||||||
|
for (let pagination of paginations) {
|
||||||
|
let buttons = pagination.querySelector("ul.pagination-list")
|
||||||
|
if (buttons.childElementCount == 0) return
|
||||||
|
let show = (index) => buttons.querySelector("li[tab-index=\"" + index + "\"]").style.display = "block"
|
||||||
|
let hide = (index) => buttons.querySelector("li[tab-index=\"" + index + "\"]").style.display = "none"
|
||||||
|
let previousButton = pagination.querySelector("a.pagination-previous")
|
||||||
|
let nextButton = pagination.querySelector("a.pagination-next")
|
||||||
|
let leftEllipsis = buttons.querySelector("li.ellipsis-left")
|
||||||
|
let rightEllipsis = buttons.querySelector("li.ellipsis-right")
|
||||||
|
let width = buttons.firstChild.offsetWidth.toString()
|
||||||
|
leftEllipsis.style.width = width + "px"
|
||||||
|
rightEllipsis.style.width = width + "px"
|
||||||
|
let count = buttons.childElementCount - 2
|
||||||
|
let maxButtonCount = Math.max((buttons.firstChild.offsetWidth == 0) ? 5 : Math.trunc(maxWidth / buttons.firstChild.offsetWidth), 5)
|
||||||
|
let current = (param instanceof HTMLElement) ? param : buttons.querySelector("li.is-active")
|
||||||
|
let index = parseInt((current == null) ? 0 : current.getAttribute("tab-index"))
|
||||||
|
if (index == 0) previousButton.setAttribute("disabled", "")
|
||||||
|
else previousButton.removeAttribute("disabled")
|
||||||
|
if (index == (count - 1)) nextButton.setAttribute("disabled", "")
|
||||||
|
else nextButton.removeAttribute("disabled")
|
||||||
|
if (count <= maxButtonCount) {
|
||||||
|
for (let i = 0; i < count; i++) buttons.querySelector("li[tab-index=\"" + i + "\"]").style.display = "block"
|
||||||
|
leftEllipsis.style.display = "none"
|
||||||
|
rightEllipsis.style.display = "none"
|
||||||
|
} else {
|
||||||
|
maxButtonCount = Math.trunc((maxButtonCount - 1) / 2) * 2 + 1
|
||||||
|
let maxSurroundingButtonCount = (maxButtonCount - 5) / 2
|
||||||
|
let maxNoEllipsisIndex = maxButtonCount - 2 - maxSurroundingButtonCount - 1
|
||||||
|
|
||||||
|
if (index <= maxNoEllipsisIndex) {
|
||||||
|
for (let i = 0; i <= (maxNoEllipsisIndex + maxSurroundingButtonCount); i++) show(i)
|
||||||
|
for (let i = (maxNoEllipsisIndex + maxSurroundingButtonCount) + 1; i < count - 1; i++) hide(i)
|
||||||
|
show(count - 1)
|
||||||
|
leftEllipsis.style.display = "none"
|
||||||
|
rightEllipsis.style.display = "block"
|
||||||
|
} else if (index >= (count - maxNoEllipsisIndex - 1)) {
|
||||||
|
for (let i = 1; i < (count - maxNoEllipsisIndex - maxSurroundingButtonCount - 1); i++) hide(i)
|
||||||
|
for (let i = (count - maxNoEllipsisIndex - maxSurroundingButtonCount - 1); i < count; i++) show(i)
|
||||||
|
show(0)
|
||||||
|
leftEllipsis.style.display = "block"
|
||||||
|
rightEllipsis.style.display = "none"
|
||||||
|
} else {
|
||||||
|
for (let i = 1; i < (index - maxSurroundingButtonCount); i++) hide(i)
|
||||||
|
for (let i = (index - maxSurroundingButtonCount); i <= (index + maxSurroundingButtonCount); i++) show(i)
|
||||||
|
for (let i = (index + maxSurroundingButtonCount) + 1; i < count - 1; i++) hide(i)
|
||||||
|
show(0)
|
||||||
|
show(count - 1)
|
||||||
|
leftEllipsis.style.display = "block"
|
||||||
|
rightEllipsis.style.display = "block"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let refresh = () => {
|
||||||
|
if ((pageSizeInput == null) || (parseInt(pageSizeInput.value) <= 0)) {
|
||||||
|
for (let pagination of paginations) pagination.style.display = "none"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let pageSize = parseInt(pageSizeInput.value)
|
||||||
|
let pageCount = Math.ceil(contents.length / pageSize)
|
||||||
|
if (!flags.isFirst && (flags.pageSize == pageSize) && (flags.pageCount == pageCount)) return
|
||||||
|
for (let pagination of paginations) {
|
||||||
|
let buttons = pagination.querySelector("ul.pagination-list")
|
||||||
|
buttons.innerHTML = ""
|
||||||
|
buttons.id = "tabs"
|
||||||
|
}
|
||||||
|
for (let i = 0; i < pageCount; i++) {
|
||||||
|
for (let j = i * pageSize; j < (i + 1) * pageSize; j++) {
|
||||||
|
if (contents[j] == null) break
|
||||||
|
contents[j].classList.add("tab-content")
|
||||||
|
contents[j].setAttribute("tab-group", group)
|
||||||
|
contents[j].setAttribute("tab-index", i)
|
||||||
|
if ((i == 0) && (flags.isFirst || (flags.pageCount != pageCount))) contents[j].classList.add("is-active")
|
||||||
|
if (j == ((i + 1) * pageSize - 1)) for (let td of contents[j].querySelectorAll("td")) td.style.borderBottom = "0"
|
||||||
|
}
|
||||||
|
if (pageCount > 1) for (let pagination of paginations) {
|
||||||
|
let buttons = pagination.querySelector("ul.pagination-list")
|
||||||
|
let a = document.createElement("a")
|
||||||
|
a.classList.add("pagination-link")
|
||||||
|
a.innerText = i + 1
|
||||||
|
let li = document.createElement("li")
|
||||||
|
li.appendChild(a)
|
||||||
|
if ((i == 0) && (flags.isFirst || (flags.pageCount != pageCount))) {
|
||||||
|
li.classList.add("is-active")
|
||||||
|
a.classList.add("is-current")
|
||||||
|
}
|
||||||
|
li.setAttribute("tab-group", group)
|
||||||
|
li.setAttribute("tab-index", i)
|
||||||
|
buttons.appendChild(li)
|
||||||
|
li.addEventListener("click", () => {
|
||||||
|
refreshEllipsis(li)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pageCount > 1) for (let pagination of paginations) {
|
||||||
|
pagination.style.display = "flex"
|
||||||
|
let buttons = pagination.querySelector("ul.pagination-list")
|
||||||
|
let leftEllipsis = document.createElement("li")
|
||||||
|
leftEllipsis.style.display = "none"
|
||||||
|
leftEllipsis.classList.add("ellipsis-left", "ignore")
|
||||||
|
leftEllipsis.innerHTML = "<span class=\"pagination-ellipsis\">…</span>"
|
||||||
|
let rightEllipsis = document.createElement("li")
|
||||||
|
rightEllipsis.style.display = "none"
|
||||||
|
rightEllipsis.classList.add("ellipsis-right", "ignore")
|
||||||
|
rightEllipsis.innerHTML = "<span class=\"pagination-ellipsis\">…</span>"
|
||||||
|
buttons.firstChild.after(leftEllipsis)
|
||||||
|
buttons.lastChild.before(rightEllipsis)
|
||||||
|
|
||||||
|
let previousButton = pagination.querySelector("a.pagination-previous")
|
||||||
|
let nextButton = pagination.querySelector("a.pagination-next")
|
||||||
|
previousButton.addEventListener("click", () => {
|
||||||
|
let current = buttons.querySelector("li.is-active")
|
||||||
|
let index = parseInt(current.getAttribute("tab-index"))
|
||||||
|
if (index <= 0) return
|
||||||
|
let prev = buttons.querySelector("li[tab-index=\"" + (index - 1) + "\"]")
|
||||||
|
prev.dispatchEvent(new Event("click"))
|
||||||
|
})
|
||||||
|
nextButton.addEventListener("click", () => {
|
||||||
|
let current = buttons.querySelector("li.is-active")
|
||||||
|
let index = parseInt(current.getAttribute("tab-index"))
|
||||||
|
if (index >= (buttons.childElementCount - 3)) return // includes left & right ellipsis
|
||||||
|
let next = buttons.querySelector("li[tab-index=\"" + (index + 1) + "\"]")
|
||||||
|
next.dispatchEvent(new Event("click"))
|
||||||
|
})
|
||||||
|
} else for (let pagination of paginations) pagination.style.display = "none"
|
||||||
|
flags.pageCount = pageCount
|
||||||
|
flags.pageSize = pageSize
|
||||||
|
flags.isFirst = false
|
||||||
|
}
|
||||||
|
refresh()
|
||||||
|
pageSizeInput.addEventListener("change", refresh)
|
||||||
|
let o = new ResizeObserver(refreshEllipsis)
|
||||||
|
o.observe(container)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeTabs() {
|
||||||
|
let tabs = document.querySelectorAll("#tabs li")
|
||||||
|
let tabContents = document.querySelectorAll("#tab-content, .tab-content")
|
||||||
|
let updateActiveTab = (tabGroup, tabIndex) => {
|
||||||
|
for (let t of tabs) if (t && (t.getAttribute("tab-group") == tabGroup)) {
|
||||||
|
if (t.getAttribute("tab-index") != tabIndex) {
|
||||||
|
t.classList.remove("is-active")
|
||||||
|
for (let a of t.querySelectorAll("a")) a.classList.remove("is-current")
|
||||||
|
} else {
|
||||||
|
t.classList.add("is-active")
|
||||||
|
for (let a of t.querySelectorAll("a")) a.classList.add("is-current")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let updateActiveContent = (tabGroup, tabIndex) => {
|
||||||
|
for (let item of tabContents) {
|
||||||
|
let group = item.getAttribute("tab-group")
|
||||||
|
let index = item.getAttribute("tab-index")
|
||||||
|
if (item && (group == tabGroup)) item.classList.remove("is-active")
|
||||||
|
if ((index == tabIndex) && (group == tabGroup)) item.classList.add("is-active")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let t of tabs) {
|
||||||
|
if (!t.classList.contains("disabled") && !t.classList.contains("ignore")) t.addEventListener("click", () => {
|
||||||
|
let group = t.getAttribute("tab-group")
|
||||||
|
let index = t.getAttribute("tab-index")
|
||||||
|
updateActiveTab(group, index)
|
||||||
|
updateActiveContent(group, index)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeToggles() {
|
||||||
|
let toggles = document.querySelectorAll(".card-header .card-toggle")
|
||||||
|
let contents = document.querySelectorAll(".card-content")
|
||||||
|
|
||||||
|
for (let t of toggles) {
|
||||||
|
let card = t.getAttribute("card")
|
||||||
|
if (card == null) continue
|
||||||
|
let cc = []
|
||||||
|
for (let c of contents) if (c.getAttribute("card") == card) cc.push(c)
|
||||||
|
t.style.transition = "0.2s linear"
|
||||||
|
t.addEventListener("click", (e) => {
|
||||||
|
if (e.currentTarget.style.transform == "rotate(180deg)") {
|
||||||
|
e.currentTarget.style.transform = ""
|
||||||
|
for (let c of cc) c.classList.remove("is-hidden")
|
||||||
|
} else {
|
||||||
|
e.currentTarget.style.transform = "rotate(180deg)"
|
||||||
|
for (let c of cc) c.classList.add("is-hidden")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeModals() {
|
||||||
|
let modaltriggers = $(".modal-trigger")
|
||||||
|
for (let t of modaltriggers) {
|
||||||
|
let m = t.querySelector(".modal")
|
||||||
|
let c = m.querySelectorAll("#close")
|
||||||
|
t.addEventListener("click", (e) => { m.style.display = "flex" })
|
||||||
|
for (let v of c) v.addEventListener("click", (e) => {
|
||||||
|
m.style.display = "none"
|
||||||
|
e.stopPropagation()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeFormSelects() {
|
||||||
|
let formSelects = document.querySelectorAll("#form-select")
|
||||||
|
for (let s of formSelects) {
|
||||||
|
let input = s.querySelector("input#form-select-input")
|
||||||
|
let select = s.querySelector("select#form-select-select")
|
||||||
|
let options = select.querySelectorAll("option")
|
||||||
|
for (let i = 0; i < options.length; i++) {
|
||||||
|
let o = options[i]
|
||||||
|
let value = (o.getAttribute("value") == null) ? i : o.getAttribute("value")
|
||||||
|
let enabled = (o.getAttribute("disabled") == null) ? true : false
|
||||||
|
if (value == input.value) select.selectedIndex = i
|
||||||
|
if (!enabled) o.style.display = "none"
|
||||||
|
}
|
||||||
|
select.addEventListener("change", () => {
|
||||||
|
for (let i = 0; i < options.length; i++) {
|
||||||
|
let o = options[i]
|
||||||
|
if (o.selected) {
|
||||||
|
input.value = (o.getAttribute("value") == null) ? i : o.getAttribute("value")
|
||||||
|
input.dispatchEvent(new Event("change"))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeFormPaginations() {
|
||||||
|
let formPags = document.querySelectorAll("#form-pagination")
|
||||||
|
for (let p of formPags) {
|
||||||
|
let input = p.querySelector("input#form-pagination-input")
|
||||||
|
let options = p.querySelectorAll("ul.pagination-list li a.pagination-link")
|
||||||
|
for (let i = 0; i < options.length; i++) {
|
||||||
|
let o = options[i]
|
||||||
|
let value = (o.getAttribute("value") == null) ? i : o.getAttribute("value")
|
||||||
|
if (value == input.value) {
|
||||||
|
if (!o.classList.contains("is-current")) o.classList.add("is-current")
|
||||||
|
} else o.classList.remove("is-current")
|
||||||
|
o.addEventListener("click", () => {
|
||||||
|
for (let i = 0; i < options.length; i++) options[i].classList.remove("is-current")
|
||||||
|
if (!o.classList.contains("is-current")) o.classList.add("is-current")
|
||||||
|
input.value = (o.getAttribute("value") == null) ? i : o.getAttribute("value")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeFormValidation() {
|
||||||
|
let forms = document.querySelectorAll("form#validatable")
|
||||||
|
for (let f of forms) {
|
||||||
|
let validatableFields = f.querySelectorAll(".field#validatable")
|
||||||
|
let validatableButtons = f.querySelectorAll("button#validatable")
|
||||||
|
|
||||||
|
let getParams = (input) => {
|
||||||
|
return {
|
||||||
|
minLength: input.getAttribute("min-length"),
|
||||||
|
maxLength: input.getAttribute("max-length"),
|
||||||
|
recommendedLength: input.getAttribute("recommended-length"),
|
||||||
|
minPattern: input.getAttribute("min-pattern"),
|
||||||
|
recommendedPattern: input.getAttribute("recommended-pattern"),
|
||||||
|
isNumeric: (input.getAttribute("numeric") != null) ? true : false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let isValid = (value, params) => {
|
||||||
|
let t = value.trim()
|
||||||
|
if (params.minLength != null) if (t.length < parseInt(params.minLength)) return false
|
||||||
|
if (params.maxLength != null) if (t.length > parseInt(params.maxLength)) return false
|
||||||
|
if (params.minPattern != null) if (!(new RegExp(params.minPattern).test(t))) return false
|
||||||
|
if (params.isNumeric == true) if (parseInt(t).toString() != t) return false
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
let isFormValid = () => {
|
||||||
|
for (let field of validatableFields) for (let i of field.querySelectorAll("input#validatable")) if (!isValid(i.value, getParams(i))) return false
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let field of validatableFields) {
|
||||||
|
let inputs = field.querySelectorAll("input#validatable")
|
||||||
|
let tips = field.querySelectorAll(".help")
|
||||||
|
for (let i of inputs) i.addEventListener("change", () => {
|
||||||
|
let params = getParams(i)
|
||||||
|
// inputs
|
||||||
|
if (isValid(i.value, params)) {
|
||||||
|
i.classList.remove("is-danger")
|
||||||
|
for (let t of tips) t.classList.remove("is-danger")
|
||||||
|
} else if (!i.classList.contains("is-danger")) {
|
||||||
|
i.classList.add("is-danger")
|
||||||
|
for (let t of tips) t.classList.add("is-danger")
|
||||||
|
}
|
||||||
|
// buttons
|
||||||
|
if (isFormValid()) {
|
||||||
|
for (let b of validatableButtons) b.removeAttribute("disabled")
|
||||||
|
} else {
|
||||||
|
for (let b of validatableButtons) if (b.getAttribute("disabled") == null) b.setAttribute("disabled", "")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeFormCollections() {
|
||||||
|
let collections = document.querySelectorAll("#form-collection")
|
||||||
|
for (let c of collections) {
|
||||||
|
let maxLength = parseInt(c.getAttribute("max-length"))
|
||||||
|
let fallbackValue = JSON.parse(c.getAttribute("fallback"))
|
||||||
|
let input = c.querySelector("#form-collection-input")
|
||||||
|
let tags = c.querySelectorAll("#form-collection-tag")
|
||||||
|
let modButton = c.querySelector("#form-collection-modify")
|
||||||
|
let modTable = c.querySelector("table#multi-select")
|
||||||
|
let modInput = modTable.querySelector("input#multi-select-input")
|
||||||
|
let modTitle = modTable.querySelector("input#multi-select-title")
|
||||||
|
let deleteButtonClickEventListener = (tag) => () => {
|
||||||
|
let tvalue = JSON.parse(tag.getAttribute("value"))
|
||||||
|
let value = JSON.parse(input.value)
|
||||||
|
value.splice(value.indexOf(tvalue), 1)
|
||||||
|
if (fallbackValue != null) value.push(fallbackValue)
|
||||||
|
input.value = JSON.stringify(value)
|
||||||
|
modInput.value = input.value
|
||||||
|
modInput.dispatchEvent(new Event("change"))
|
||||||
|
tag.remove()
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let t of tags) {
|
||||||
|
let d = t.querySelector(".delete, .is-delete")
|
||||||
|
d.addEventListener("click", deleteButtonClickEventListener(t))
|
||||||
|
}
|
||||||
|
modInput.value = input.value
|
||||||
|
modInput.setAttribute("max-length", maxLength)
|
||||||
|
modInput.setAttribute("fallback", JSON.stringify(fallbackValue))
|
||||||
|
modInput.addEventListener("change", () => {
|
||||||
|
let fallbackValue = JSON.parse(c.getAttribute("fallback"))
|
||||||
|
let oldValue = JSON.parse(input.value)
|
||||||
|
let newValue = JSON.parse(modInput.value)
|
||||||
|
let tags = c.querySelectorAll("#form-collection-tag")
|
||||||
|
for (let o of oldValue) if (!newValue.includes(o) && (o != fallbackValue)) {
|
||||||
|
for (let t of tags) if (JSON.parse(t.getAttribute("value")) == o) t.remove()
|
||||||
|
}
|
||||||
|
for (let n = 0; n < newValue.length; n++) if (!oldValue.includes(newValue[n]) && (newValue[n] != fallbackValue)) {
|
||||||
|
let tag = document.createElement("div")
|
||||||
|
tag.classList.add("control")
|
||||||
|
tag.id = "form-collection-tag"
|
||||||
|
tag.setAttribute("value", newValue[n])
|
||||||
|
tag.innerHTML = "<span class=\"tags has-addons\"><span class=\"tag is-link is-light\" id=\"form-collection-tag-title\">" + JSON.parse(modTitle.value)[n] + "</span><a class=\"tag is-delete\" /></span>"
|
||||||
|
tag.querySelector("a.is-delete").addEventListener("click", deleteButtonClickEventListener(tag))
|
||||||
|
modButton.before(tag)
|
||||||
|
}
|
||||||
|
input.value = modInput.value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeMultiSelectTables() {
|
||||||
|
let tables = document.querySelectorAll("table#multi-select")
|
||||||
|
for (let table of tables) {
|
||||||
|
let valueInput = table.querySelector("input#multi-select-input")
|
||||||
|
let titleInput = table.querySelector("input#multi-select-title")
|
||||||
|
let trimValues = (values, fallback) => {
|
||||||
|
while (values.includes(fallback)) values.splice(values.indexOf(fallback), 1)
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
let fillValues = (values, fallback) => {
|
||||||
|
let maxLength = (valueInput.getAttribute("max-length") == null) ? -1 : parseInt(valueInput.getAttribute("max-length"))
|
||||||
|
while (values.length < maxLength) values.push(fallback)
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
let lines = table.querySelectorAll("tbody tr")
|
||||||
|
let refresh = () => {
|
||||||
|
let fallbackValue = JSON.parse(valueInput.getAttribute("fallback"))
|
||||||
|
let value = trimValues(JSON.parse(valueInput.value), fallbackValue)
|
||||||
|
let title = []
|
||||||
|
for (let l of lines) {
|
||||||
|
let lvalue = JSON.parse(l.getAttribute("multi-select-value"))
|
||||||
|
if (value.includes(lvalue)) {
|
||||||
|
if (!l.classList.contains("is-selected")) l.classList.add("is-selected")
|
||||||
|
title[value.indexOf(lvalue)] = l.getAttribute("multi-select-title")
|
||||||
|
l.style.fontWeight = "bold"
|
||||||
|
} else {
|
||||||
|
l.classList.remove("is-selected")
|
||||||
|
l.style.fontWeight = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
titleInput.value = JSON.stringify(title)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let l of lines) {
|
||||||
|
l.onclick = () => {
|
||||||
|
let fallbackValue = JSON.parse(valueInput.getAttribute("fallback"))
|
||||||
|
let maxLength = (valueInput.getAttribute("max-length") == null) ? -1 : parseInt(valueInput.getAttribute("max-length"))
|
||||||
|
let value = trimValues(JSON.parse(valueInput.value), fallbackValue)
|
||||||
|
let lvalue = JSON.parse(l.getAttribute("multi-select-value"))
|
||||||
|
if (value.includes(lvalue)) value.splice(value.indexOf(lvalue), 1)
|
||||||
|
else if (maxLength >= 0) {
|
||||||
|
if (value.length < maxLength) value.push(lvalue)
|
||||||
|
else alert("Cannot add more items, items are up to " + maxLength + ".")
|
||||||
|
} else value.push(lvalue)
|
||||||
|
valueInput.value = JSON.stringify(fillValues(value, fallbackValue))
|
||||||
|
refresh()
|
||||||
|
valueInput.dispatchEvent(new Event("change"))
|
||||||
|
}
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
valueInput.addEventListener("change", refresh)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeFormNumerics() {
|
||||||
|
let numerics = document.querySelectorAll("#form-numeric")
|
||||||
|
for (let n of numerics) {
|
||||||
|
let add = n.querySelector("#form-numeric-add")
|
||||||
|
let sub = n.querySelector("#form-numeric-sub")
|
||||||
|
let inputs = n.querySelectorAll("#form-numeric-input")
|
||||||
|
add.addEventListener("click", (e) => {
|
||||||
|
for (let i of inputs) {
|
||||||
|
let maxValue = parseFloat(i.getAttribute("max-value"))
|
||||||
|
let step = parseFloat(i.getAttribute("step"))
|
||||||
|
|
||||||
|
let digitCount = (i.getAttribute("digit-count") == null) ? -1 : parseInt(i.getAttribute("digit-count"))
|
||||||
|
let value = (parseFloat(i.value) * 10 + step * 10) / 10
|
||||||
|
if (value * Math.sign(step) <= maxValue * Math.sign(step)) i.value = (digitCount >= 0) ? value.toFixed(digitCount) : value
|
||||||
|
}
|
||||||
|
e.stopPropagation()
|
||||||
|
})
|
||||||
|
sub.addEventListener("click", (e) => {
|
||||||
|
for (let i of inputs) {
|
||||||
|
let minValue = parseFloat(i.getAttribute("min-value"))
|
||||||
|
let step = parseFloat(i.getAttribute("step"))
|
||||||
|
let digitCount = (i.getAttribute("digit-count") == null) ? -1 : parseInt(i.getAttribute("digit-count"))
|
||||||
|
let value = (parseFloat(i.value) * 10 - step * 10) / 10
|
||||||
|
if (value * Math.sign(step) >= minValue * Math.sign(step)) i.value = (digitCount >= 0) ? value.toFixed(digitCount) : value
|
||||||
|
}
|
||||||
|
e.stopPropagation()
|
||||||
|
})
|
||||||
|
for (let i of inputs) {
|
||||||
|
let digitCount = (i.getAttribute("digit-count") == null) ? -1 : parseInt(i.getAttribute("digit-count"))
|
||||||
|
let value = parseFloat(i.value)
|
||||||
|
i.value = (digitCount >= 0) ? value.toFixed(digitCount) : value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeUploader() {
|
||||||
|
let uploaders = document.querySelectorAll("div#uploader")
|
||||||
|
for (let uploader of uploaders) {
|
||||||
|
let input = uploader.querySelector("input#uploader-input")
|
||||||
|
let text = uploader.querySelector("input#uploader-text")
|
||||||
|
let placeholder = uploader.querySelector("#uploader-placeholder")
|
||||||
|
let remove = uploader.querySelector("#uploader-delete")
|
||||||
|
let reader = new FileReader()
|
||||||
|
input.addEventListener("change", () => {
|
||||||
|
if (input.files.length > 0) {
|
||||||
|
remove.style.display = "block"
|
||||||
|
placeholder.innerText = input.files[0].name
|
||||||
|
reader.readAsText(input.files[0])
|
||||||
|
reader.onload = () => text.value = reader.result
|
||||||
|
} else {
|
||||||
|
placeholder.innerText = ""
|
||||||
|
remove.style.display = "none"
|
||||||
|
text.value = null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
remove.addEventListener("click", (e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
input.value = null
|
||||||
|
input.dispatchEvent(new Event("change"))
|
||||||
|
})
|
||||||
|
|
||||||
|
remove.style.display = "none"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkImg() {
|
||||||
|
let imgs = document.querySelectorAll("#exist-or-not")
|
||||||
|
for (let img of imgs) {
|
||||||
|
let general = img.querySelector("img#general")
|
||||||
|
let specified = img.querySelector("img#specified")
|
||||||
|
|
||||||
|
if (specified.width == 0) specified.style.display = "none"
|
||||||
|
else general.style.display = "none"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeMarqueeLabels() {
|
||||||
|
let marqueeContainers = document.querySelectorAll(".marquee-label-container")
|
||||||
|
for (let c of marqueeContainers) {
|
||||||
|
let marquees = c.querySelectorAll(".marquee-label")
|
||||||
|
for (let marquee of marquees) {
|
||||||
|
if (marquee.closest(".marquee-label-container") != c) continue
|
||||||
|
let refresh = () => {
|
||||||
|
let lpad = parseInt(window.getComputedStyle(c, null).getPropertyValue("padding-left"))
|
||||||
|
if (lpad == NaN) lpad = 0
|
||||||
|
let rpad = parseInt(window.getComputedStyle(c, null).getPropertyValue("padding-right"))
|
||||||
|
if (rpad == NaN) rpad = 20
|
||||||
|
let hpad = lpad + rpad
|
||||||
|
let speed = marquee.getAttribute("speed")
|
||||||
|
if (speed == null) speed = 1
|
||||||
|
let stopingTime = 0.5
|
||||||
|
let duration = (20 * (marquee.offsetWidth - c.offsetWidth + hpad)) / speed + 2 * stopingTime
|
||||||
|
if ((marquee.offsetWidth > 0) && (marquee.offsetWidth > c.offsetWidth - hpad)) {
|
||||||
|
marquee.animate([
|
||||||
|
{ transform: "translateX(0)", offset: 0 },
|
||||||
|
{ transform: "translateX(0)", easing: "cubic-bezier(0.67, 0, 0.33, 1)", offset: stopingTime / duration },
|
||||||
|
{ transform: "translateX(" + (c.offsetWidth - marquee.offsetWidth - hpad) + "px)", easing: "cubic-bezier(0.67, 0, 0.33, 1)", offset: 1 - stopingTime / duration },
|
||||||
|
{ transform: "translateX(" + (c.offsetWidth - marquee.offsetWidth - hpad) + "px)", offset: 1 }
|
||||||
|
], { duration: (20 * (marquee.offsetWidth - c.offsetWidth) + 1000) / speed, direction: "alternate-reverse", iterations: Infinity })
|
||||||
|
} else marquee.style.animation = "none"
|
||||||
|
}
|
||||||
|
let o = new ResizeObserver(refresh)
|
||||||
|
o.observe(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeNotificatioAnimation() {
|
||||||
|
let notifications = document.querySelectorAll(".notification.temporary")
|
||||||
|
for (let n of notifications) {
|
||||||
|
let remove = n.querySelector(".delete")
|
||||||
|
let startSubmitter = n.querySelector("form.start")
|
||||||
|
let startPath = startSubmitter.getAttribute("action")
|
||||||
|
let startRequest = new XMLHttpRequest()
|
||||||
|
startRequest.open("POST", startPath, true)
|
||||||
|
startRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
let endSubmitter = n.querySelector("form.end")
|
||||||
|
let endPath = startSubmitter.getAttribute("action")
|
||||||
|
let endRequest = new XMLHttpRequest()
|
||||||
|
endRequest.open("POST", endPath, true)
|
||||||
|
endRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
if (startSubmitter != null) startRequest.send()
|
||||||
|
let end = () => {
|
||||||
|
n.style.display = "none"
|
||||||
|
if (endSubmitter != null) endRequest.send()
|
||||||
|
}
|
||||||
|
|
||||||
|
n.style.animationPlayState = "running"
|
||||||
|
remove.addEventListener("click", end)
|
||||||
|
n.addEventListener("animationend", end)
|
||||||
|
n.addEventListener("webkitAnimationEnd", end)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initializeCheckBoxes() {
|
||||||
|
let checks = document.querySelectorAll(".checkbox")
|
||||||
|
for (let c of checks) {
|
||||||
|
let input = c.querySelector("input[type=checkbox]")
|
||||||
|
let mark = c.querySelector(".checkmark")
|
||||||
|
let refresh = (value) => {
|
||||||
|
value = input.getAttribute("checked")
|
||||||
|
if (value == null) {
|
||||||
|
input.removeAttribute("checked")
|
||||||
|
mark.style.opacity = 0
|
||||||
|
if (!c.classList.contains("is-light")) c.classList.add("is-light")
|
||||||
|
} else {
|
||||||
|
input.setAttribute("checked", "checked")
|
||||||
|
mark.style.opacity = 100
|
||||||
|
c.classList.remove("is-light")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.addEventListener("click", () => {
|
||||||
|
let value = input.getAttribute("checked")
|
||||||
|
if (value == null) input.setAttribute("checked", "checked")
|
||||||
|
else input.removeAttribute("checked")
|
||||||
|
refresh()
|
||||||
|
})
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeLoadingModal() {
|
||||||
|
let loading = document.querySelector(".loading")
|
||||||
|
setTimeout(() => (loading == null) ? null : loading.remove(), 505)
|
||||||
|
try {
|
||||||
|
let a = loading.animate([
|
||||||
|
{ offset: 0, opacity: 1 },
|
||||||
|
{ offset: 0.25, opacity: 0 },
|
||||||
|
{ offset: 1, opacity: 0 }
|
||||||
|
], { duration: 2000 })
|
||||||
|
a.onfinish = loading.remove
|
||||||
|
a.play()
|
||||||
|
} catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
$(document).ready(() => {
|
||||||
|
initializeNotificatioAnimation()
|
||||||
|
initializePaginatedContent()
|
||||||
|
initializeTabs()
|
||||||
|
initializeToggles()
|
||||||
|
initializeModals()
|
||||||
|
initializeFormSelects()
|
||||||
|
initializeFormNumerics()
|
||||||
|
initializeFormPaginations()
|
||||||
|
initializeFormValidation()
|
||||||
|
initializeFormCollections()
|
||||||
|
initializeMultiSelectTables()
|
||||||
|
initializeUploader()
|
||||||
|
checkImg()
|
||||||
|
initializeMarqueeLabels()
|
||||||
|
initializeCheckBoxes()
|
||||||
|
|
||||||
|
removeLoadingModal()
|
||||||
|
})
|
||||||
|
|
||||||
750
bst@asphyxia/webui/profile_detail.pug
Normal file
750
bst@asphyxia/webui/profile_detail.pug
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user