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; 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" : "") + "://" String record = "http" + (HTTP_ENCRYPTION.useInRouting ? "s" : "") + "://"
+ lr(HTTP_INFO.accessAddress, HTTP_INFO.bindAddress) + ":" + lr(HTTP_INFO.accessAddress, HTTP_INFO.bindAddress) + ":"
+ lr(HTTP_INFO.accessPort, HTTP_INFO.bindPort) + lr(HTTP_INFO.accessPort, HTTP_INFO.bindPort)
@ -176,9 +174,7 @@ public class GachaBanner {
.setGachaTimesLimit(Integer.MAX_VALUE) .setGachaTimesLimit(Integer.MAX_VALUE)
.setGachaSortId(this.getSortId()); .setGachaSortId(this.getSortId());
if(hasEpitomized() && !sessionKey.isEmpty()) { if(hasEpitomized()) {
Account account = DatabaseHelper.getAccountBySessionKey(sessionKey);
Player player = Grasscutter.getGameServer().getPlayerByAccountId(account.getId());
PlayerGachaBannerInfo gachaInfo = player.getGachaInfo().getBannerInfo(this); PlayerGachaBannerInfo gachaInfo = player.getGachaInfo().getBannerInfo(this);
info.setWishItemId(gachaInfo.getWishItemId()) info.setWishItemId(gachaInfo.getWishItemId())

View File

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

View File

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

View File

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