Refactor gacha banner proto creation to not do a lookup on the database

This commit is contained in:
Melledy 2022-06-16 08:18:24 -07:00
parent 2624f48a84
commit 359e83596c
4 changed files with 14 additions and 50 deletions

View File

@ -138,13 +138,11 @@ public class GachaBanner {
default -> (eventChance > -1) ? eventChance : eventChance5;
};
}
@Deprecated
public GachaInfo toProto() {
return toProto("");
}
public GachaInfo toProto(String sessionKey) {
public GachaInfo toProto(Player player) {
// TODO: use other Nonce/key insteadof session key to ensure the overall security for the player
String sessionKey = player.getAccount().getSessionKey();
String record = "http" + (HTTP_ENCRYPTION.useInRouting ? "s" : "") + "://"
+ lr(HTTP_INFO.accessAddress, HTTP_INFO.bindAddress) + ":"
+ lr(HTTP_INFO.accessPort, HTTP_INFO.bindPort)
@ -176,9 +174,7 @@ public class GachaBanner {
.setGachaTimesLimit(Integer.MAX_VALUE)
.setGachaSortId(this.getSortId());
if(hasEpitomized() && !sessionKey.isEmpty()) {
Account account = DatabaseHelper.getAccountBySessionKey(sessionKey);
Player player = Grasscutter.getGameServer().getPlayerByAccountId(account.getId());
if(hasEpitomized()) {
PlayerGachaBannerInfo gachaInfo = player.getGachaInfo().getBannerInfo(this);
info.setWishItemId(gachaInfo.getWishItemId())

View File

@ -48,8 +48,7 @@ import static emu.grasscutter.Configuration.*;
public class GachaManager {
private final GameServer server;
private final Int2ObjectMap<GachaBanner> gachaBanners;
private GetGachaInfoRsp cachedProto;
WatchService watchService;
private WatchService watchService;
private static final int starglitterId = 221;
private static final int stardustId = 222;
@ -88,9 +87,6 @@ public class GachaManager {
getGachaBanners().put(banner.getScheduleId(), banner);
}
Grasscutter.getLogger().info("Banners successfully loaded.");
this.cachedProto = createProto();
} else {
Grasscutter.getLogger().error("Unable to load banners. Banners size is 0.");
}
@ -421,18 +417,7 @@ public class GachaManager {
}
}
@Deprecated
private synchronized GetGachaInfoRsp createProto() {
GetGachaInfoRsp.Builder proto = GetGachaInfoRsp.newBuilder().setGachaRandom(12345);
for (GachaBanner banner : getGachaBanners().values()) {
proto.addGachaInfoList(banner.toProto());
}
return proto.build();
}
private synchronized GetGachaInfoRsp createProto(String sessionKey) {
private synchronized GetGachaInfoRsp createProto(Player player) {
GetGachaInfoRsp.Builder proto = GetGachaInfoRsp.newBuilder().setGachaRandom(12345);
long currentTime = System.currentTimeMillis() / 1000L;
@ -440,22 +425,14 @@ public class GachaManager {
for (GachaBanner banner : getGachaBanners().values()) {
if ((banner.getEndTime() >= currentTime && banner.getBeginTime() <= currentTime) || (banner.getBannerType() == BannerType.STANDARD))
{
proto.addGachaInfoList(banner.toProto(sessionKey));
proto.addGachaInfoList(banner.toProto(player));
}
}
return proto.build();
}
@Deprecated
public GetGachaInfoRsp toProto() {
if (this.cachedProto == null) {
this.cachedProto = createProto();
}
return this.cachedProto;
}
public GetGachaInfoRsp toProto(String sessionKey) {
return createProto(sessionKey);
public GetGachaInfoRsp toProto(Player player) {
return createProto(player);
}
}

View File

@ -14,10 +14,7 @@ public class HandlerGetGachaInfoReq extends PacketHandler {
@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
session.send(new PacketGetGachaInfoRsp(session.getServer().getGachaManager(),
// TODO: use other Nonce/key insteadof session key to ensure the overall security for the player
session.getPlayer().getAccount().getSessionKey())
);
session.send(new PacketGetGachaInfoRsp(session.getServer().getGachaManager(), session.getPlayer()));
}
}

View File

@ -1,22 +1,16 @@
package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.gacha.GachaManager;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
public class PacketGetGachaInfoRsp extends BasePacket {
@Deprecated
public PacketGetGachaInfoRsp(GachaManager manager) {
public PacketGetGachaInfoRsp(GachaManager manager, Player player) {
super(PacketOpcodes.GetGachaInfoRsp);
this.setData(manager.toProto());
}
public PacketGetGachaInfoRsp(GachaManager manager, String sessionKey) {
super(PacketOpcodes.GetGachaInfoRsp);
this.setData(manager.toProto(sessionKey));
this.setData(manager.toProto(player));
}
}