Refactor lineup in DB
Unsure how this will work in an already existing DB
This commit is contained in:
parent
5a19a0c2e4
commit
4f4c383288
@ -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 Database from './Database';
|
||||||
|
import Player, { LineupI } from './Player';
|
||||||
|
const c = new Logger("Avatar");
|
||||||
type UID = number | string;
|
type UID = number | string;
|
||||||
|
|
||||||
export default class Avatar {
|
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,
|
promotion: 1,
|
||||||
rank: 1,
|
rank: 1,
|
||||||
skilltreeList: [],
|
skilltreeList: [],
|
||||||
|
}, {
|
||||||
|
avatarType: AvatarType.AVATAR_FORMAL_TYPE,
|
||||||
|
hp: 10000,
|
||||||
|
id: baseAvatarId,
|
||||||
|
satiety: 100,
|
||||||
|
slot: -1,
|
||||||
|
sp: 10000
|
||||||
});
|
});
|
||||||
db.set("avatars", avatar);
|
db.set("avatars", avatar);
|
||||||
return avatar;
|
return avatar;
|
||||||
@ -34,6 +43,23 @@ export default class Avatar {
|
|||||||
return await db.getAll("avatars", query) as unknown as 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> {
|
public static async remove(ownerUid: UID, baseAvatarId: number): Promise<void> {
|
||||||
const db = Database.getInstance();
|
const db = Database.getInstance();
|
||||||
await db.delete("avatars", { ownerUid, baseAvatarId });
|
await db.delete("avatars", { ownerUid, baseAvatarId });
|
||||||
|
@ -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 Logger from "../util/Logger";
|
||||||
import Account from "./Account";
|
import Account from "./Account";
|
||||||
|
import Avatar from "./Avatar";
|
||||||
import Database from "./Database";
|
import Database from "./Database";
|
||||||
const c = new Logger("Player");
|
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 {
|
interface PlayerI {
|
||||||
_id: number;
|
_id: number;
|
||||||
name: string;
|
name: string;
|
||||||
@ -21,7 +32,9 @@ interface PlayerI {
|
|||||||
}
|
}
|
||||||
lineup: {
|
lineup: {
|
||||||
curIndex: number;
|
curIndex: number;
|
||||||
lineups: LineupInfo[];
|
lineups: {
|
||||||
|
[key: number]: LineupI;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
posData: {
|
posData: {
|
||||||
floorID: number;
|
floorID: number;
|
||||||
@ -31,8 +44,9 @@ interface PlayerI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default class Player {
|
export default class Player {
|
||||||
|
public readonly uid: number;
|
||||||
private constructor(public db: PlayerI) {
|
private constructor(public db: PlayerI) {
|
||||||
|
this.uid = db._id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async fromUID(uid: number | string): Promise<Player | undefined> {
|
public static async fromUID(uid: number | string): Promise<Player | undefined> {
|
||||||
@ -51,12 +65,26 @@ export default class Player {
|
|||||||
return new Player(plr);
|
return new Player(plr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getCurLineup() {
|
public async getLineup(lineupIndex?: number): Promise<LineupInfo> {
|
||||||
return this.db.lineup.lineups[this.db.lineup.curIndex];
|
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) {
|
public setLineup(lineup: LineupInfo, index?: number, curIndex: number = this.db.lineup.curIndex) {
|
||||||
this.db.lineup.lineups[curIndex] = lineup;
|
this.db.lineup.lineups[index || curIndex] = {
|
||||||
|
...lineup,
|
||||||
|
avatarList: lineup.avatarList.map(x => x.id)
|
||||||
|
};
|
||||||
|
|
||||||
this.db.lineup.curIndex = curIndex;
|
this.db.lineup.curIndex = curIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,6 +110,6 @@ export default class Player {
|
|||||||
|
|
||||||
public async save() {
|
public async save() {
|
||||||
const db = Database.getInstance();
|
const db = Database.getInstance();
|
||||||
await db.update("players", { _id: this.db._id } , this.db);
|
await db.update("players", { _id: this.db._id }, this.db);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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 Packet from "../kcp/Packet";
|
||||||
import Session from "../kcp/Session";
|
import Session from "../kcp/Session";
|
||||||
|
|
||||||
export default async function handle(session: Session, packet: Packet) {
|
export default async function handle(session: Session, packet: Packet) {
|
||||||
let lineup = session.player.db.lineup;
|
let lineup = session.player.db.lineup;
|
||||||
if (!lineup.curIndex) {
|
|
||||||
lineup.curIndex = 0;
|
const lineupList: Array<LineupInfo> = [];
|
||||||
session.player.db.lineup.curIndex = 0;
|
for (const l of Object.values(session.player.db.lineup.lineups)) {
|
||||||
session.player.save();
|
const lineup = await session.player.getLineup(l.index);
|
||||||
|
lineupList.push(lineup);
|
||||||
}
|
}
|
||||||
session.send("GetAllLineupDataScRsp", {
|
|
||||||
|
const dataObj = {
|
||||||
retcode: 0,
|
retcode: 0,
|
||||||
curIndex: lineup.curIndex,
|
curIndex: lineup.curIndex,
|
||||||
lineupList: lineup.lineups,
|
lineupList
|
||||||
} as GetAllLineupDataScRsp);
|
} as GetAllLineupDataScRsp;
|
||||||
|
|
||||||
|
session.send("GetAllLineupDataScRsp", dataObj);
|
||||||
}
|
}
|
@ -3,7 +3,7 @@ import Packet from "../kcp/Packet";
|
|||||||
import Session from "../kcp/Session";
|
import Session from "../kcp/Session";
|
||||||
|
|
||||||
export default async function handle(session: Session, packet: Packet) {
|
export default async function handle(session: Session, packet: Packet) {
|
||||||
const lineup = session.player.getCurLineup();
|
const lineup = await session.player.getLineup();
|
||||||
|
|
||||||
session.send("GetCurBattleInfoScRsp", {
|
session.send("GetCurBattleInfoScRsp", {
|
||||||
retcode: 0,
|
retcode: 0,
|
||||||
|
@ -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 Packet from "../kcp/Packet";
|
||||||
import Session from "../kcp/Session";
|
import Session from "../kcp/Session";
|
||||||
|
|
||||||
export default async function handle(session: Session, packet: Packet) {
|
export default async function handle(session: Session, packet: Packet) {
|
||||||
const lineup = session.player.getCurLineup();
|
let lineup = await session.player.getLineup();
|
||||||
|
|
||||||
session.send("GetCurLineupDataScRsp", {
|
session.send("GetCurLineupDataScRsp", {
|
||||||
retcode: 0,
|
retcode: 0,
|
||||||
lineup
|
lineup: {
|
||||||
|
...lineup,
|
||||||
|
}
|
||||||
} as GetCurLineupDataScRsp);
|
} as GetCurLineupDataScRsp);
|
||||||
}
|
}
|
@ -8,10 +8,15 @@ export default async function handle(session: Session, packet: Packet) {
|
|||||||
const body = packet.body as JoinLineupCsReq;
|
const body = packet.body as JoinLineupCsReq;
|
||||||
session.send("JoinLineupScRsp", { retcode: 0 });
|
session.send("JoinLineupScRsp", { retcode: 0 });
|
||||||
|
|
||||||
let lineup = session.player.getCurLineup();
|
let lineup = await session.player.getLineup();
|
||||||
const slot = body.slot || 0;
|
const slot = body.slot || 0;
|
||||||
const avatar = await Avatar.fromUID(session.player.db._id, body.baseAvatarId);
|
const avatarList = [];
|
||||||
if (avatar.length === 0) return session.c.warn(`Avatar ${body.baseAvatarId} not found`);
|
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] = {
|
lineup.avatarList[slot] = {
|
||||||
avatarType: AvatarType.AVATAR_FORMAL_TYPE,
|
avatarType: AvatarType.AVATAR_FORMAL_TYPE,
|
||||||
hp: 10000,
|
hp: 10000,
|
||||||
@ -20,7 +25,7 @@ export default async function handle(session: Session, packet: Packet) {
|
|||||||
slot,
|
slot,
|
||||||
sp: 10000
|
sp: 10000
|
||||||
};
|
};
|
||||||
session.player.setCurLineup(lineup);
|
session.player.setLineup(lineup);
|
||||||
session.player.save();
|
session.player.save();
|
||||||
|
|
||||||
session.send("SyncLineupNotify", {
|
session.send("SyncLineupNotify", {
|
||||||
|
@ -43,23 +43,18 @@ export default async function handle(session: Session, packet: Packet) {
|
|||||||
Avatar.create(plr.db._id);
|
Avatar.create(plr.db._id);
|
||||||
plr.db.lineup = {
|
plr.db.lineup = {
|
||||||
curIndex: 0,
|
curIndex: 0,
|
||||||
lineups: [{
|
lineups: {
|
||||||
avatarList: [{
|
0: {
|
||||||
avatarType: AvatarType.AVATAR_FORMAL_TYPE,
|
avatarList: [1001],
|
||||||
hp: 10000,
|
extraLineupType: ExtraLineupType.LINEUP_NONE,
|
||||||
sp: 10000,
|
index: 0,
|
||||||
satiety: 100,
|
isVirtual: false,
|
||||||
slot: 0,
|
leaderSlot: 0,
|
||||||
id: 1001
|
mp: 100, // ?? Not sure what this is
|
||||||
}],
|
name: "Default Lineup",
|
||||||
planeId: 10001,
|
planeId: 10001
|
||||||
isVirtual: false,
|
}
|
||||||
name: "Default Party",
|
}
|
||||||
index: 0,
|
|
||||||
leaderSlot: 0,
|
|
||||||
mp: 100,
|
|
||||||
extraLineupType: ExtraLineupType.LINEUP_NONE
|
|
||||||
}]
|
|
||||||
}
|
}
|
||||||
plr.save();
|
plr.save();
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,9 @@ import Session from "../kcp/Session";
|
|||||||
export default async function handle(session: Session, packet: Packet) {
|
export default async function handle(session: Session, packet: Packet) {
|
||||||
const body = packet.body as SetLineupNameCsReq;
|
const body = packet.body as SetLineupNameCsReq;
|
||||||
|
|
||||||
let curLineup = session.player.getCurLineup();
|
let curLineup = await session.player.getLineup();
|
||||||
curLineup.name = body.name;
|
curLineup.name = body.name;
|
||||||
session.player.setCurLineup(curLineup);
|
session.player.setLineup(curLineup);
|
||||||
session.player.save();
|
session.player.save();
|
||||||
|
|
||||||
session.send("SetLineupNameScRsp", {
|
session.send("SetLineupNameScRsp", {
|
||||||
|
Loading…
Reference in New Issue
Block a user