mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-12 20:34:36 +01:00
Implement gacha guaranteed claim
This commit is contained in:
@@ -36,6 +36,10 @@ public class GachaDef extends BaseDef {
|
||||
public int getId() {
|
||||
return Id;
|
||||
}
|
||||
|
||||
public boolean canGuarantee() {
|
||||
return this.GuaranteeTimes > 0;
|
||||
}
|
||||
|
||||
public GachaStorageDef getStorageData() {
|
||||
return GameData.getGachaStorageDataTable().get(this.getStorageId());
|
||||
|
||||
@@ -10,6 +10,7 @@ import emu.nebula.data.resources.GachaDef.GachaPackage;
|
||||
import emu.nebula.data.resources.GachaPkgDef;
|
||||
import emu.nebula.database.GameDatabaseObject;
|
||||
import emu.nebula.game.player.Player;
|
||||
import emu.nebula.proto.GachaInformation.GachaInfo;
|
||||
import emu.nebula.util.Utils;
|
||||
import lombok.Getter;
|
||||
|
||||
@@ -39,6 +40,10 @@ public class GachaBannerInfo implements GameDatabaseObject {
|
||||
this.bannerId = data.getId();
|
||||
}
|
||||
|
||||
public void setUsedGuarantee(boolean value) {
|
||||
this.usedGuarantee = value;
|
||||
}
|
||||
|
||||
public int doPull(GachaDef data) {
|
||||
// Pull chances
|
||||
int chanceA = 20; // 2%
|
||||
@@ -98,4 +103,19 @@ public class GachaBannerInfo implements GameDatabaseObject {
|
||||
// Get random id
|
||||
return pkg.next();
|
||||
}
|
||||
|
||||
// Proto
|
||||
|
||||
public GachaInfo toProto() {
|
||||
var proto = GachaInfo.newInstance()
|
||||
.setId(this.getBannerId())
|
||||
.setGachaTotalTimes(this.getTotal())
|
||||
.setTotalTimes(this.getTotal())
|
||||
.setAupMissTimes(this.getMissTimesA())
|
||||
.setAMissTimes(this.getMissTimesA())
|
||||
.setReveFirstTenReward(true)
|
||||
.setRecvGuaranteeReward(this.isUsedGuarantee());
|
||||
|
||||
return proto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package emu.nebula.game.gacha;
|
||||
import java.util.Collection;
|
||||
|
||||
import emu.nebula.Nebula;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.data.resources.GachaDef;
|
||||
import emu.nebula.game.player.Player;
|
||||
import emu.nebula.game.player.PlayerChangeInfo;
|
||||
import emu.nebula.game.player.PlayerManager;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
@@ -19,7 +21,19 @@ public class GachaManager extends PlayerManager {
|
||||
this.bannerInfos = new Int2ObjectOpenHashMap<>();
|
||||
}
|
||||
|
||||
public synchronized GachaBannerInfo getBannerInfoById(int id) {
|
||||
if (!this.loaded) {
|
||||
this.loadFromDatabase();
|
||||
}
|
||||
|
||||
return this.bannerInfos.get(id);
|
||||
}
|
||||
|
||||
public synchronized Collection<GachaBannerInfo> getBannerInfos() {
|
||||
if (!this.loaded) {
|
||||
this.loadFromDatabase();
|
||||
}
|
||||
|
||||
return this.bannerInfos.values();
|
||||
}
|
||||
|
||||
@@ -34,6 +48,39 @@ public class GachaManager extends PlayerManager {
|
||||
);
|
||||
}
|
||||
|
||||
public PlayerChangeInfo recvGuarantee(int id) {
|
||||
// Get banner info
|
||||
var banner = this.getBannerInfoById(id);
|
||||
if (banner == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get banner data
|
||||
var data = GameData.getGachaDataTable().get(id);
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if we have enough pulls for a guarantee
|
||||
if (!data.canGuarantee() || banner.getTotal() < data.getGuaranteeTimes()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Make sure we havent used our guarantee yet
|
||||
if (banner.isUsedGuarantee()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Set guarantee
|
||||
banner.setUsedGuarantee(true);
|
||||
banner.save();
|
||||
|
||||
// Give player the guaranteed item
|
||||
return getPlayer().getInventory().addItem(data.getGuaranteeTid(), data.getGuaranteeQty());
|
||||
}
|
||||
|
||||
// Database
|
||||
|
||||
private void loadFromDatabase() {
|
||||
var db = Nebula.getGameDatabase();
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package emu.nebula.server.handlers;
|
||||
|
||||
import emu.nebula.net.NetHandler;
|
||||
import emu.nebula.net.NetMsgId;
|
||||
import emu.nebula.proto.Public.UI32;
|
||||
import emu.nebula.net.HandlerId;
|
||||
import emu.nebula.net.GameSession;
|
||||
|
||||
@HandlerId(NetMsgId.gacha_guarantee_reward_receive_req)
|
||||
public class HandlerGachaGuaranteeRewardReceiveReq extends NetHandler {
|
||||
|
||||
@Override
|
||||
public byte[] handle(GameSession session, byte[] message) throws Exception {
|
||||
// Parse req
|
||||
var req = UI32.parseFrom(message);
|
||||
|
||||
// Recieve guaranteed reward
|
||||
var change = session.getPlayer().getGachaManager().recvGuarantee(req.getValue());
|
||||
|
||||
if (change == null) {
|
||||
return session.encodeMsg(NetMsgId.gacha_guarantee_reward_receive_failed_ack);
|
||||
}
|
||||
|
||||
// Encode and send
|
||||
return session.encodeMsg(NetMsgId.gacha_guarantee_reward_receive_succeed_ack, change.toProto());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package emu.nebula.server.handlers;
|
||||
|
||||
import emu.nebula.net.NetHandler;
|
||||
import emu.nebula.net.NetMsgId;
|
||||
import emu.nebula.proto.GachaInformation.GachaInfo;
|
||||
import emu.nebula.proto.GachaInformation.GachaInformationResp;
|
||||
import emu.nebula.net.HandlerId;
|
||||
import emu.nebula.net.GameSession;
|
||||
@@ -16,16 +15,7 @@ public class HandlerGachaInformationReq extends NetHandler {
|
||||
var rsp = GachaInformationResp.newInstance();
|
||||
|
||||
for (var bannerInfo : session.getPlayer().getGachaManager().getBannerInfos()) {
|
||||
var info = GachaInfo.newInstance()
|
||||
.setId(bannerInfo.getBannerId())
|
||||
.setGachaTotalTimes(bannerInfo.getTotal())
|
||||
.setTotalTimes(bannerInfo.getTotal())
|
||||
.setAupMissTimes(bannerInfo.getMissTimesA())
|
||||
.setAMissTimes(bannerInfo.getMissTimesA())
|
||||
.setReveFirstTenReward(true)
|
||||
.setRecvGuaranteeReward(bannerInfo.isUsedGuarantee());
|
||||
|
||||
rsp.addInformation(info);
|
||||
rsp.addInformation(bannerInfo.toProto());
|
||||
}
|
||||
|
||||
// Encode and send
|
||||
|
||||
Reference in New Issue
Block a user