From d643bedbbe2511ee5520778d4f91c73cdc8ae16c Mon Sep 17 00:00:00 2001 From: timing1337 <56186498+timing1337@users.noreply.github.com> Date: Wed, 3 Aug 2022 12:52:47 +0700 Subject: [PATCH] gacha: implementation #2 (#27) Co-authored-by: labalityowo <56186498+labalityowo@users.noreply.github.com> --- .gitignore | 3 +- src/index.ts | 2 + src/server/packets/DoGachaCsReq.ts | 35 ++++++++++++++ src/server/packets/GetGachaInfoCsReq.ts | 25 +++++----- src/util/Banner.ts | 61 +++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 src/server/packets/DoGachaCsReq.ts create mode 100644 src/util/Banner.ts diff --git a/.gitignore b/.gitignore index b842dbd..866016f 100644 --- a/.gitignore +++ b/.gitignore @@ -109,4 +109,5 @@ dist # CrepeSR config.json -src/data/* \ No newline at end of file +src/data/* +banners.json \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e3db77e..2033a23 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,11 +6,13 @@ import Interface from "./commands/Interface"; import HttpServer from "./http/HttpServer"; import SRServer from "./server/kcp/SRServer"; +import Banners from "./util/Banner"; import Logger from "./util/Logger"; import ProtoFactory from "./util/ProtoFactory" const c = new Logger("CrepeSR"); c.log(`Starting CrepeSR...`); +Banners.init(); ProtoFactory.init(); Interface.start(); HttpServer.getInstance().start(); diff --git a/src/server/packets/DoGachaCsReq.ts b/src/server/packets/DoGachaCsReq.ts new file mode 100644 index 0000000..3dff2bf --- /dev/null +++ b/src/server/packets/DoGachaCsReq.ts @@ -0,0 +1,35 @@ +import { DoGachaCsReq, DoGachaScRsp, GachaItem, Item, ItemList } from "../../data/proto/StarRail"; +import Banners from "../../util/Banner"; +import Packet from "../kcp/Packet"; +import Session from "../kcp/Session"; + +export default async function handle(session: Session, packet: Packet) { + const gachaItemList: GachaItem[] = []; + const body = packet.body as DoGachaCsReq; + const banner = Banners.config.find(banner => banner.gachaId === body.gachaId)!; + const combined = banner.rateUpItems4.concat(banner.rateUpItems5) + //bad gachaing but whatever.... + //TODO: pity system, proper logic + for(let i = 0; i < body.gachaNum; i++){ + const result = combined[Math.floor(Math.random() * combined.length)]; + gachaItemList.push({ + gachaItem: { + itemId: result, + num: 1 + } as Item, + tokenItem: {}, + transferItemList: {}, + isNew: true //TODO: avatar checking + } as GachaItem); + } + session.send("DoGachaScRsp", { + retcode: 0, + gachaId: body.gachaId!, + gachaNum: body.gachaNum!, + newGachaRandom: body.gachaRandom!, + newbieGachaCnt: 0, + todayGachaCnt: 0, + todayTotalGachaCnt: 0, //todo find out what are THESE + gachaItemList: gachaItemList + } as DoGachaScRsp); +} \ No newline at end of file diff --git a/src/server/packets/GetGachaInfoCsReq.ts b/src/server/packets/GetGachaInfoCsReq.ts index 41fc8dd..382b094 100644 --- a/src/server/packets/GetGachaInfoCsReq.ts +++ b/src/server/packets/GetGachaInfoCsReq.ts @@ -1,21 +1,22 @@ -import { GetGachaInfoScRsp } from "../../data/proto/StarRail"; +import { GachaInfo, GetGachaInfoCsReq, GetGachaInfoScRsp } from "../../data/proto/StarRail"; import Packet from "../kcp/Packet"; import Session from "../kcp/Session"; - -const unix = () => Math.floor(Date.now() / 1000); +import Banner from './../../util/Banner'; export default async function handle(session: Session, packet: Packet) { session.send("GetGachaInfoScRsp", { - gachaRandom: 2503, + gachaRandom: 0, retcode: 0, - gachaInfoList: [{ - beginTime: unix(), - endTime: unix() * 2, - newbieGachaCnt: 10, - todayGachaCnt: 10, - gachaId: 1001, // TODO: Figure out gachaIDs - detailWebview: "https://omfgdogs.com/" - }], + gachaInfoList: Banner.config.map(banner => { + return { + beginTime: 0, + endTime: 1924992000, + gachaId: banner.gachaId, + detailWebview: banner.detailWebview, + newbieGachaCnt: 0, + todayGachaCnt: 0 + } as GachaInfo + }), todaySingleGachaMaxCnt: 10, todayTotalGachaCnt: 10, } as GetGachaInfoScRsp); diff --git a/src/util/Banner.ts b/src/util/Banner.ts new file mode 100644 index 0000000..91e6454 --- /dev/null +++ b/src/util/Banner.ts @@ -0,0 +1,61 @@ +import fs from 'fs'; +import { resolve } from 'path'; +import { VerboseLevel } from './Logger'; + +type Banner = { + gachaId: number, + detailWebview: string, + rateUpItems4: number[], + rateUpItems5: number[], + costItemId: number +} + +function r(...args: string[]) { + return fs.readFileSync(resolve(__dirname, ...args)).toString(); +} + +export default class Banners { + public static config: Banner[]; + + public static init(){ + Banners.readConfig(); + } + + private static readConfig(){ + let config: Banner[]; + const defaultConfig: Banner[] = [ + { + gachaId: 1001, + detailWebview: "", + rateUpItems4: [ + 1001, 1103 + ], + rateUpItems5: [ + 1102 + ], + costItemId: -1 //unused for now + } as Banner + ]; + + try { + config = JSON.parse(r('../../banners.json')); + + for(const [index, gachaBanner] of Object.entries(config)){ + const missing = Object.keys(defaultConfig[0]).filter(key => !gachaBanner.hasOwnProperty(key)); + if (missing.length > 0) { + console.log(`Missing ${missing.join(', ')}, using default values. Backup of your older config: ${JSON.stringify(gachaBanner, null, 2)}`); + config[parseInt(index)] = defaultConfig[0]; + } + } + Banners.updateConfig(config); + } catch { + console.error("Could not read banners file. Creating one for you..."); + Banners.updateConfig(defaultConfig); + } + } + + private static updateConfig(config: Banner[]) { + this.config = config; + fs.writeFileSync('./banners.json', JSON.stringify(config, null, 2)); + } +}