Refactor lineup in DB

Unsure how this will work in an already existing DB
This commit is contained in:
memetrollsXD 2022-08-02 15:14:10 +02:00
parent 5a19a0c2e4
commit 4f4c383288
No known key found for this signature in database
GPG Key ID: 105C2F3417AC32CD
8 changed files with 109 additions and 46 deletions

View File

@ -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<Avatar[]> {
try {
const avatarList: Array<Avatar> = [];
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<void> {
const db = Database.getInstance();
await db.delete("avatars", { ownerUid, baseAvatarId });

View File

@ -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<Player | undefined> {
@ -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<LineupInfo> {
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;
}

View File

@ -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<LineupInfo> = [];
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);
}

View File

@ -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,

View File

@ -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);
}

View File

@ -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);
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", {

View File

@ -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",
lineups: {
0: {
avatarList: [1001],
extraLineupType: ExtraLineupType.LINEUP_NONE,
index: 0,
isVirtual: false,
leaderSlot: 0,
mp: 100,
extraLineupType: ExtraLineupType.LINEUP_NONE
}]
mp: 100, // ?? Not sure what this is
name: "Default Lineup",
planeId: 10001
}
}
}
plr.save();
}

View File

@ -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", {