From 4f4c38328892029f77b1305a9059272c5e71b06c Mon Sep 17 00:00:00 2001 From: memetrollsXD Date: Tue, 2 Aug 2022 15:14:10 +0200 Subject: [PATCH] Refactor lineup in DB Unsure how this will work in an already existing DB --- src/db/Avatar.ts | 32 +++++++++++++-- src/db/Player.ts | 44 +++++++++++++++++---- src/server/packets/GetAllLineupDataCsReq.ts | 21 ++++++---- src/server/packets/GetCurBattleInfoCsReq.ts | 2 +- src/server/packets/GetCurLineupDataCsReq.ts | 10 +++-- src/server/packets/JoinLineupCsReq.ts | 13 ++++-- src/server/packets/PlayerLoginCsReq.ts | 29 ++++++-------- src/server/packets/SetLineupNameCsReq.ts | 4 +- 8 files changed, 109 insertions(+), 46 deletions(-) diff --git a/src/db/Avatar.ts b/src/db/Avatar.ts index d86ae0e..3696a68 100644 --- a/src/db/Avatar.ts +++ b/src/db/Avatar.ts @@ -1,10 +1,12 @@ -import { Avatar as AvatarI } from '../data/proto/StarRail'; +import { Avatar as AvatarI, AvatarType, LineupAvatar } from '../data/proto/StarRail'; +import Logger from '../util/Logger'; import Database from './Database'; - +import Player, { LineupI } from './Player'; +const c = new Logger("Avatar"); type UID = number | string; export default class Avatar { - private constructor(public ownerUid: UID, public data: AvatarI) { + private constructor(public ownerUid: UID, public data: AvatarI, public lineup: LineupAvatar) { } @@ -22,6 +24,13 @@ export default class Avatar { promotion: 1, rank: 1, skilltreeList: [], + }, { + avatarType: AvatarType.AVATAR_FORMAL_TYPE, + hp: 10000, + id: baseAvatarId, + satiety: 100, + slot: -1, + sp: 10000 }); db.set("avatars", avatar); return avatar; @@ -34,6 +43,23 @@ export default class Avatar { return await db.getAll("avatars", query) as unknown as Avatar[]; } + public static async fromLineup(uid: UID, lineup: LineupI): Promise { + try { + const avatarList: Array = []; + + for (let i = 0; i < lineup.avatarList.length; i++) { + const avatarId = lineup.avatarList[i]; + const avatar = await Avatar.fromUID(uid, avatarId); + avatarList.push(avatar[0]); + } + + return await Promise.all(avatarList); + } catch (e) { + c.error(e as Error); + return []; + } + } + public static async remove(ownerUid: UID, baseAvatarId: number): Promise { const db = Database.getInstance(); await db.delete("avatars", { ownerUid, baseAvatarId }); diff --git a/src/db/Player.ts b/src/db/Player.ts index 6cc068e..c08b2d4 100644 --- a/src/db/Player.ts +++ b/src/db/Player.ts @@ -1,9 +1,20 @@ -import { LineupInfo, Vector } from "../data/proto/StarRail"; +import { ExtraLineupType, LineupInfo, Vector } from "../data/proto/StarRail"; import Logger from "../util/Logger"; import Account from "./Account"; +import Avatar from "./Avatar"; import Database from "./Database"; const c = new Logger("Player"); +export interface LineupI { + avatarList: number[]; + isVirtual: boolean; + planeId: number; + mp: number; + leaderSlot: number; + index: number; + extraLineupType: ExtraLineupType; + name: string; +} interface PlayerI { _id: number; name: string; @@ -21,7 +32,9 @@ interface PlayerI { } lineup: { curIndex: number; - lineups: LineupInfo[]; + lineups: { + [key: number]: LineupI; + }; } posData: { floorID: number; @@ -31,8 +44,9 @@ interface PlayerI { } export default class Player { + public readonly uid: number; private constructor(public db: PlayerI) { - + this.uid = db._id; } public static async fromUID(uid: number | string): Promise { @@ -51,12 +65,26 @@ export default class Player { return new Player(plr); } - public getCurLineup() { - return this.db.lineup.lineups[this.db.lineup.curIndex]; + public async getLineup(lineupIndex?: number): Promise { + const curIndex = this.db.lineup.curIndex; + const lineup = this.db.lineup.lineups[lineupIndex || curIndex]; + const avatars = await Avatar.fromLineup(this.uid, lineup); + let slot = 0; + avatars.forEach(avatar => { + avatar.lineup.slot = slot++; + }); + return { + ...lineup, + avatarList: avatars.map(x => x.lineup) + } } - public setCurLineup(lineup: LineupInfo, curIndex: number = this.db.lineup.curIndex) { - this.db.lineup.lineups[curIndex] = lineup; + public setLineup(lineup: LineupInfo, index?: number, curIndex: number = this.db.lineup.curIndex) { + this.db.lineup.lineups[index || curIndex] = { + ...lineup, + avatarList: lineup.avatarList.map(x => x.id) + }; + this.db.lineup.curIndex = curIndex; } @@ -82,6 +110,6 @@ export default class Player { public async save() { const db = Database.getInstance(); - await db.update("players", { _id: this.db._id } , this.db); + await db.update("players", { _id: this.db._id }, this.db); } } \ No newline at end of file diff --git a/src/server/packets/GetAllLineupDataCsReq.ts b/src/server/packets/GetAllLineupDataCsReq.ts index 6ce664d..e3d9f1c 100644 --- a/src/server/packets/GetAllLineupDataCsReq.ts +++ b/src/server/packets/GetAllLineupDataCsReq.ts @@ -1,17 +1,22 @@ -import { GetAllLineupDataScRsp } from "../../data/proto/StarRail"; +import { AvatarType, GetAllLineupDataScRsp, LineupInfo } from "../../data/proto/StarRail"; +import Avatar from "../../db/Avatar"; import Packet from "../kcp/Packet"; import Session from "../kcp/Session"; export default async function handle(session: Session, packet: Packet) { let lineup = session.player.db.lineup; - if (!lineup.curIndex) { - lineup.curIndex = 0; - session.player.db.lineup.curIndex = 0; - session.player.save(); + + const lineupList: Array = []; + for (const l of Object.values(session.player.db.lineup.lineups)) { + const lineup = await session.player.getLineup(l.index); + lineupList.push(lineup); } - session.send("GetAllLineupDataScRsp", { + + const dataObj = { retcode: 0, curIndex: lineup.curIndex, - lineupList: lineup.lineups, - } as GetAllLineupDataScRsp); + lineupList + } as GetAllLineupDataScRsp; + + session.send("GetAllLineupDataScRsp", dataObj); } \ No newline at end of file diff --git a/src/server/packets/GetCurBattleInfoCsReq.ts b/src/server/packets/GetCurBattleInfoCsReq.ts index 36c11cf..5fb9716 100644 --- a/src/server/packets/GetCurBattleInfoCsReq.ts +++ b/src/server/packets/GetCurBattleInfoCsReq.ts @@ -3,7 +3,7 @@ import Packet from "../kcp/Packet"; import Session from "../kcp/Session"; export default async function handle(session: Session, packet: Packet) { - const lineup = session.player.getCurLineup(); + const lineup = await session.player.getLineup(); session.send("GetCurBattleInfoScRsp", { retcode: 0, diff --git a/src/server/packets/GetCurLineupDataCsReq.ts b/src/server/packets/GetCurLineupDataCsReq.ts index 3857127..b6c5ec0 100644 --- a/src/server/packets/GetCurLineupDataCsReq.ts +++ b/src/server/packets/GetCurLineupDataCsReq.ts @@ -1,11 +1,15 @@ -import { AvatarType, GetCurLineupDataCsReq, GetCurLineupDataScRsp } from "../../data/proto/StarRail"; +import { GetCurLineupDataScRsp } from "../../data/proto/StarRail"; +import Avatar from "../../db/Avatar"; import Packet from "../kcp/Packet"; import Session from "../kcp/Session"; export default async function handle(session: Session, packet: Packet) { - const lineup = session.player.getCurLineup(); + let lineup = await session.player.getLineup(); + session.send("GetCurLineupDataScRsp", { retcode: 0, - lineup + lineup: { + ...lineup, + } } as GetCurLineupDataScRsp); } \ No newline at end of file diff --git a/src/server/packets/JoinLineupCsReq.ts b/src/server/packets/JoinLineupCsReq.ts index d0f382c..ea3740f 100644 --- a/src/server/packets/JoinLineupCsReq.ts +++ b/src/server/packets/JoinLineupCsReq.ts @@ -8,10 +8,15 @@ export default async function handle(session: Session, packet: Packet) { const body = packet.body as JoinLineupCsReq; session.send("JoinLineupScRsp", { retcode: 0 }); - let lineup = session.player.getCurLineup(); + let lineup = await session.player.getLineup(); const slot = body.slot || 0; - const avatar = await Avatar.fromUID(session.player.db._id, body.baseAvatarId); - if (avatar.length === 0) return session.c.warn(`Avatar ${body.baseAvatarId} not found`); + const avatarList = []; + for (const avatarId in lineup) { + const avatar = await Avatar.fromUID(session.player.db._id, Number(avatarId)); + if (avatar.length === 0) return session.c.warn(`Avatar ${body.baseAvatarId} not found`); + if (avatar) avatarList.push(avatar[0]); + } + lineup.avatarList[slot] = { avatarType: AvatarType.AVATAR_FORMAL_TYPE, hp: 10000, @@ -20,7 +25,7 @@ export default async function handle(session: Session, packet: Packet) { slot, sp: 10000 }; - session.player.setCurLineup(lineup); + session.player.setLineup(lineup); session.player.save(); session.send("SyncLineupNotify", { diff --git a/src/server/packets/PlayerLoginCsReq.ts b/src/server/packets/PlayerLoginCsReq.ts index 4dbd8e7..a7c0c65 100644 --- a/src/server/packets/PlayerLoginCsReq.ts +++ b/src/server/packets/PlayerLoginCsReq.ts @@ -43,23 +43,18 @@ export default async function handle(session: Session, packet: Packet) { Avatar.create(plr.db._id); plr.db.lineup = { curIndex: 0, - lineups: [{ - avatarList: [{ - avatarType: AvatarType.AVATAR_FORMAL_TYPE, - hp: 10000, - sp: 10000, - satiety: 100, - slot: 0, - id: 1001 - }], - planeId: 10001, - isVirtual: false, - name: "Default Party", - index: 0, - leaderSlot: 0, - mp: 100, - extraLineupType: ExtraLineupType.LINEUP_NONE - }] + lineups: { + 0: { + avatarList: [1001], + extraLineupType: ExtraLineupType.LINEUP_NONE, + index: 0, + isVirtual: false, + leaderSlot: 0, + mp: 100, // ?? Not sure what this is + name: "Default Lineup", + planeId: 10001 + } + } } plr.save(); } diff --git a/src/server/packets/SetLineupNameCsReq.ts b/src/server/packets/SetLineupNameCsReq.ts index 5ad8966..a25bc71 100644 --- a/src/server/packets/SetLineupNameCsReq.ts +++ b/src/server/packets/SetLineupNameCsReq.ts @@ -5,9 +5,9 @@ import Session from "../kcp/Session"; export default async function handle(session: Session, packet: Packet) { const body = packet.body as SetLineupNameCsReq; - let curLineup = session.player.getCurLineup(); + let curLineup = await session.player.getLineup(); curLineup.name = body.name; - session.player.setCurLineup(curLineup); + session.player.setLineup(curLineup); session.player.save(); session.send("SetLineupNameScRsp", {