Commit dc9cef8a authored by Luke H-W's avatar Luke H-W Committed by GitHub
Browse files

Refactoring (#1660)



* Refactor a couple of iterators

* Use side-effect instead of second iterator

* Make World::onTick return shouldDelete instead of success

* Replace Shop iterator with side effects

* Scene

* Clean up Expeditions

* Refactor Expeditions

* Clean up Expeditions, Player

* Limit Expeditions by AR

* Lombokify props
Co-authored-by: default avatarAnimeGitB <AnimeGitB@bigblueball.in>
parent bccf516c
...@@ -2,32 +2,20 @@ package emu.grasscutter.data.excels; ...@@ -2,32 +2,20 @@ package emu.grasscutter.data.excels;
import emu.grasscutter.data.GameResource; import emu.grasscutter.data.GameResource;
import emu.grasscutter.data.ResourceType; import emu.grasscutter.data.ResourceType;
import lombok.Getter;
@ResourceType(name = "PlayerLevelExcelConfigData.json") @ResourceType(name = "PlayerLevelExcelConfigData.json")
@Getter
public class PlayerLevelData extends GameResource { public class PlayerLevelData extends GameResource {
private int level; private int level;
private int exp; private int exp;
private int rewardId; private int rewardId;
private int expeditionLimitAdd = 0;
private int unlockWorldLevel; private int unlockWorldLevel;
private long unlockDescTextMapHash;
@Override @Override
public int getId() { public int getId() {
return this.level; return this.level;
} }
public int getLevel() {
return level;
}
public int getExp() {
return exp;
}
public int getRewardId() {
return rewardId;
}
public int getUnlockWorldLevel() {
return unlockWorldLevel;
}
} }
package emu.grasscutter.game.expedition; package emu.grasscutter.game.expedition;
import dev.morphia.annotations.Entity; import dev.morphia.annotations.Entity;
import emu.grasscutter.net.proto.AvatarExpeditionInfoOuterClass.AvatarExpeditionInfo;
import lombok.Getter;
import lombok.Setter;
@Entity @Entity
@Getter @Setter
public class ExpeditionInfo { public class ExpeditionInfo {
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public int getExpId() {
return expId;
}
public void setExpId(int expId) {
this.expId = expId;
}
public int getHourTime() {
return hourTime;
}
public void setHourTime(int hourTime) {
this.hourTime = hourTime;
}
public int getStartTime() {
return startTime;
}
public void setStartTime(int startTime) {
this.startTime = startTime;
}
private int state; private int state;
private int expId; private int expId;
private int hourTime; private int hourTime;
private int startTime; private int startTime;
public AvatarExpeditionInfo toProto() {
return AvatarExpeditionInfo.newBuilder()
.setStateValue(this.getState())
.setExpId(this.getExpId())
.setHourTime(this.getHourTime())
.setStartTime(this.getStartTime())
.build();
}
} }
package emu.grasscutter.game.expedition; package emu.grasscutter.game.expedition;
public class ExpeditionRewardData { import emu.grasscutter.game.inventory.GameItem;
private int itemId; import emu.grasscutter.utils.Utils;
private int minCount; import lombok.Getter;
private int maxCount;
public int getItemId() {
return itemId;
}
public int getMinCount() { return minCount; } public class ExpeditionRewardData {
@Getter private int itemId;
@Getter private int minCount;
@Getter private int maxCount;
public int getMaxCount() { public GameItem getReward() {
return maxCount; return new GameItem(itemId, Utils.randomRange(minCount, maxCount));
} }
} }
package emu.grasscutter.game.expedition; package emu.grasscutter.game.expedition;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import emu.grasscutter.game.inventory.GameItem;
import lombok.Getter;
public class ExpeditionRewardDataList { public class ExpeditionRewardDataList {
public int getHourTime() { @Getter private int hourTime;
return hourTime; @Getter private List<ExpeditionRewardData> expeditionRewardData;
}
public List<ExpeditionRewardData> getExpeditionRewardData() {
return expeditionRewardData;
}
private int hourTime; public List<GameItem> getRewards() {
private List<ExpeditionRewardData> expeditionRewardData; List<GameItem> rewards = new ArrayList<>();
if (expeditionRewardData != null) {
expeditionRewardData.forEach(data -> rewards.add(data.getReward()));
}
return rewards;
}
} }
...@@ -2,15 +2,9 @@ package emu.grasscutter.game.expedition; ...@@ -2,15 +2,9 @@ package emu.grasscutter.game.expedition;
import java.util.List; import java.util.List;
public class ExpeditionRewardInfo { import lombok.Getter;
public int getExpId() {
return expId;
}
public List<ExpeditionRewardDataList> getExpeditionRewardDataList() {
return expeditionRewardDataList;
}
private int expId; public class ExpeditionRewardInfo {
private List<ExpeditionRewardDataList> expeditionRewardDataList; @Getter private int expId;
@Getter private List<ExpeditionRewardDataList> expeditionRewardDataList;
} }
...@@ -4,6 +4,7 @@ import java.util.stream.Stream; ...@@ -4,6 +4,7 @@ import java.util.stream.Stream;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.Getter;
public enum PlayerProperty { public enum PlayerProperty {
PROP_NONE (0), PROP_NONE (0),
...@@ -53,8 +54,8 @@ public enum PlayerProperty { ...@@ -53,8 +54,8 @@ public enum PlayerProperty {
PROP_PLAYER_WAIT_SUB_HOME_COIN (10043); PROP_PLAYER_WAIT_SUB_HOME_COIN (10043);
private static final int inf = Integer.MAX_VALUE; // Maybe this should be something else? private static final int inf = Integer.MAX_VALUE; // Maybe this should be something else?
private final int id, min, max; @Getter private final int id, min, max;
private final boolean dynamicRange; @Getter private final boolean dynamicRange;
private static final Int2ObjectMap<PlayerProperty> map = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap<PlayerProperty> map = new Int2ObjectOpenHashMap<>();
static { static {
...@@ -84,22 +85,6 @@ public enum PlayerProperty { ...@@ -84,22 +85,6 @@ public enum PlayerProperty {
this(id, Integer.MIN_VALUE, inf, dynamicRange); this(id, Integer.MIN_VALUE, inf, dynamicRange);
} }
public int getId() {
return this.id;
}
public int getMin() {
return this.min;
}
public int getMax() {
return this.max;
}
public boolean getDynamicRange() {
return dynamicRange;
}
public static PlayerProperty getPropById(int value) { public static PlayerProperty getPropById(int value) {
return map.getOrDefault(value, null); return map.getOrDefault(value, null);
} }
......
...@@ -2,26 +2,28 @@ package emu.grasscutter.game.shop; ...@@ -2,26 +2,28 @@ package emu.grasscutter.game.shop;
import emu.grasscutter.data.common.ItemParamData; import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.excels.ShopGoodsData; import emu.grasscutter.data.excels.ShopGoodsData;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ShopInfo { public class ShopInfo {
private int goodsId = 0; @Getter @Setter private int goodsId = 0;
private ItemParamData goodsItem; @Getter @Setter private ItemParamData goodsItem;
private int scoin = 0; @Getter @Setter private int scoin = 0;
private List<ItemParamData> costItemList; @Getter @Setter private List<ItemParamData> costItemList;
private int boughtNum = 0; @Getter @Setter private int boughtNum = 0;
private int buyLimit = 0; @Getter @Setter private int buyLimit = 0;
private int beginTime = 0; @Getter @Setter private int beginTime = 0;
private int endTime = 1924992000; @Getter @Setter private int endTime = 1924992000;
private int minLevel = 0; @Getter @Setter private int minLevel = 0;
private int maxLevel = 61; @Getter @Setter private int maxLevel = 61;
private List<Integer> preGoodsIdList = new ArrayList<>(); @Getter @Setter private List<Integer> preGoodsIdList = new ArrayList<>();
private int mcoin = 0; @Getter @Setter private int mcoin = 0;
private int hcoin = 0; @Getter @Setter private int hcoin = 0;
private int disableType = 0; @Getter @Setter private int disableType = 0;
private int secondarySheetId = 0; @Getter @Setter private int secondarySheetId = 0;
private String refreshType; private String refreshType;
...@@ -41,8 +43,8 @@ public class ShopInfo { ...@@ -41,8 +43,8 @@ public class ShopInfo {
} }
} }
private transient ShopRefreshType shopRefreshType; @Setter private transient ShopRefreshType shopRefreshType;
private int shopRefreshParam; @Getter @Setter private int shopRefreshParam;
public ShopInfo(ShopGoodsData sgd) { public ShopInfo(ShopGoodsData sgd) {
this.goodsId = sgd.getGoodsId(); this.goodsId = sgd.getGoodsId();
...@@ -60,126 +62,6 @@ public class ShopInfo { ...@@ -60,126 +62,6 @@ public class ShopInfo {
this.shopRefreshParam = sgd.getRefreshParam(); this.shopRefreshParam = sgd.getRefreshParam();
} }
public int getHcoin() {
return hcoin;
}
public void setHcoin(int hcoin) {
this.hcoin = hcoin;
}
public List<Integer> getPreGoodsIdList() {
return preGoodsIdList;
}
public void setPreGoodsIdList(List<Integer> preGoodsIdList) {
this.preGoodsIdList = preGoodsIdList;
}
public int getMcoin() {
return mcoin;
}
public void setMcoin(int mcoin) {
this.mcoin = mcoin;
}
public int getDisableType() {
return disableType;
}
public void setDisableType(int disableType) {
this.disableType = disableType;
}
public int getSecondarySheetId() {
return secondarySheetId;
}
public void setSecondarySheetId(int secondarySheetId) {
this.secondarySheetId = secondarySheetId;
}
public int getGoodsId() {
return goodsId;
}
public void setGoodsId(int goodsId) {
this.goodsId = goodsId;
}
public ItemParamData getGoodsItem() {
return goodsItem;
}
public void setGoodsItem(ItemParamData goodsItem) {
this.goodsItem = goodsItem;
}
public int getScoin() {
return scoin;
}
public void setScoin(int scoin) {
this.scoin = scoin;
}
public List<ItemParamData> getCostItemList() {
return costItemList;
}
public void setCostItemList(List<ItemParamData> costItemList) {
this.costItemList = costItemList;
}
public int getBoughtNum() {
return boughtNum;
}
public void setBoughtNum(int boughtNum) {
this.boughtNum = boughtNum;
}
public int getBuyLimit() {
return buyLimit;
}
public void setBuyLimit(int buyLimit) {
this.buyLimit = buyLimit;
}
public int getBeginTime() {
return beginTime;
}
public void setBeginTime(int beginTime) {
this.beginTime = beginTime;
}
public int getEndTime() {
return endTime;
}
public void setEndTime(int endTime) {
this.endTime = endTime;
}
public int getMinLevel() {
return minLevel;
}
public void setMinLevel(int minLevel) {
this.minLevel = minLevel;
}
public int getMaxLevel() {
return maxLevel;
}
public void setMaxLevel(int maxLevel) {
this.maxLevel = maxLevel;
}
public ShopRefreshType getShopRefreshType() { public ShopRefreshType getShopRefreshType() {
if (refreshType == null) if (refreshType == null)
return ShopRefreshType.NONE; return ShopRefreshType.NONE;
...@@ -191,15 +73,16 @@ public class ShopInfo { ...@@ -191,15 +73,16 @@ public class ShopInfo {
}; };
} }
public void setShopRefreshType(ShopRefreshType shopRefreshType) { private boolean evaluateVirtualCost(ItemParamData item) {
this.shopRefreshType = shopRefreshType; return switch (item.getId()) {
} case 201 -> {this.hcoin += item.getCount(); yield true;}
case 203 -> {this.mcoin += item.getCount(); yield true;}
public int getShopRefreshParam() { default -> false;
return shopRefreshParam; };
} }
public void setShopRefreshParam(int shopRefreshParam) { public void removeVirtualCosts() {
this.shopRefreshParam = shopRefreshParam; if (this.costItemList != null)
this.costItemList.removeIf(item -> evaluateVirtualCost(item));
} }
} }
...@@ -3,7 +3,6 @@ package emu.grasscutter.game.shop; ...@@ -3,7 +3,6 @@ package emu.grasscutter.game.shop;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.excels.ShopGoodsData; import emu.grasscutter.data.excels.ShopGoodsData;
import emu.grasscutter.server.game.BaseGameSystem; import emu.grasscutter.server.game.BaseGameSystem;
import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameServer;
...@@ -14,7 +13,6 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; ...@@ -14,7 +13,6 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import static emu.grasscutter.config.Configuration.*; import static emu.grasscutter.config.Configuration.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
public class ShopSystem extends BaseGameSystem { public class ShopSystem extends BaseGameSystem {
...@@ -60,22 +58,7 @@ public class ShopSystem extends BaseGameSystem { ...@@ -60,22 +58,7 @@ public class ShopSystem extends BaseGameSystem {
List<ShopTable> banners = DataLoader.loadList("Shop.json", ShopTable.class); List<ShopTable> banners = DataLoader.loadList("Shop.json", ShopTable.class);
if (banners.size() > 0) { if (banners.size() > 0) {
for (ShopTable shopTable : banners) { for (ShopTable shopTable : banners) {
for (ShopInfo cost : shopTable.getItems()) { shopTable.getItems().forEach(ShopInfo::removeVirtualCosts);
if (cost.getCostItemList() != null) {
Iterator<ItemParamData> iterator = cost.getCostItemList().iterator();
while (iterator.hasNext()) {
ItemParamData ipd = iterator.next();
if (ipd.getId() == 201) {
cost.setHcoin(cost.getHcoin() + ipd.getCount());
iterator.remove();
}
if (ipd.getId() == 203) {
cost.setMcoin(cost.getMcoin() + ipd.getCount());
iterator.remove();
}
}
}
}
getShopData().put(shopTable.getShopId(), shopTable.getItems()); getShopData().put(shopTable.getShopId(), shopTable.getItems());
} }
Grasscutter.getLogger().debug("Shop data successfully loaded."); Grasscutter.getLogger().debug("Shop data successfully loaded.");
......
...@@ -28,6 +28,7 @@ import emu.grasscutter.utils.Position; ...@@ -28,6 +28,7 @@ import emu.grasscutter.utils.Position;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
public class Scene { public class Scene {
private final World world; private final World world;
...@@ -261,12 +262,11 @@ public class Scene { ...@@ -261,12 +262,11 @@ public class Scene {
} }
} }
private void removePlayerAvatars(Player player) { private synchronized void removePlayerAvatars(Player player) {
Iterator<EntityAvatar> it = player.getTeamManager().getActiveTeam().iterator(); var team = player.getTeamManager().getActiveTeam();
while (it.hasNext()) { // removeEntities(team, VisionType.VISION_TYPE_REMOVE); // List<SubType> isn't cool apparently :(
this.removeEntity(it.next(), VisionType.VISION_TYPE_REMOVE); team.forEach(e -> removeEntity(e, VisionType.VISION_TYPE_REMOVE));
it.remove(); team.clear();
}
} }
public void spawnPlayer(Player player) { public void spawnPlayer(Player player) {
...@@ -567,40 +567,40 @@ public class Scene { ...@@ -567,40 +567,40 @@ public class Scene {
return SceneIndexManager.queryNeighbors(getScriptManager().getBlocksIndex(), return SceneIndexManager.queryNeighbors(getScriptManager().getBlocksIndex(),
player.getPosition().toXZDoubleArray(), Grasscutter.getConfig().server.game.loadEntitiesForPlayerRange); player.getPosition().toXZDoubleArray(), Grasscutter.getConfig().server.game.loadEntitiesForPlayerRange);
} }
public void checkBlocks() {
Set<SceneBlock> visible = new HashSet<>();
for (Player player : this.getPlayers()) {
var blocks = getPlayerActiveBlocks(player);
visible.addAll(blocks);
}
Iterator<SceneBlock> it = this.getLoadedBlocks().iterator(); private boolean unloadBlockIfNotVisible(Collection<SceneBlock> visible, SceneBlock block) {
while (it.hasNext()) { if (visible.contains(block)) return false;
SceneBlock block = it.next(); this.onUnloadBlock(block);
return true;
}
if (!visible.contains(block)) { private synchronized boolean loadBlock(SceneBlock block) {
it.remove(); if (this.loadedBlocks.contains(block)) return false;
this.onLoadBlock(block, this.players);
this.loadedBlocks.add(block);
return true;
}
onUnloadBlock(block); public synchronized void checkBlocks() {
} Set<SceneBlock> visible = this.players.stream()
} .map(player -> this.getPlayerActiveBlocks(player))
.flatMap(Collection::stream)
.collect(Collectors.toSet());
for (var block : visible) { this.loadedBlocks.removeIf(block -> unloadBlockIfNotVisible(visible, block));
if (!this.getLoadedBlocks().contains(block)) { visible.stream()
this.onLoadBlock(block, this.getPlayers()); .filter(block -> !this.loadBlock(block))
this.getLoadedBlocks().add(block); .forEach(block -> {
}else {
// dynamic load the groups for players in a loaded block // dynamic load the groups for players in a loaded block
var toLoad = this.getPlayers().stream() var toLoad = this.players.stream()
.filter(p -> block.contains(p.getPosition())) .filter(p -> block.contains(p.getPosition()))
.map(p -> playerMeetGroups(p, block)) .map(p -> this.playerMeetGroups(p, block))
.flatMap(Collection::stream) .flatMap(Collection::stream)
.toList(); .toList();
onLoadGroup(toLoad); this.onLoadGroup(toLoad);
} });
}
} }
public List<SceneGroup> playerMeetGroups(Player player, SceneBlock block) { public List<SceneGroup> playerMeetGroups(Player player, SceneBlock block) {
List<SceneGroup> sceneGroups = SceneIndexManager.queryNeighbors(block.sceneGroupIndex, player.getPosition().toDoubleArray(), List<SceneGroup> sceneGroups = SceneIndexManager.queryNeighbors(block.sceneGroupIndex, player.getPosition().toDoubleArray(),
Grasscutter.getConfig().server.game.loadEntitiesForPlayerRange); Grasscutter.getConfig().server.game.loadEntitiesForPlayerRange);
......
...@@ -330,10 +330,11 @@ public class World implements Iterable<Player> { ...@@ -330,10 +330,11 @@ public class World implements Iterable<Player> {
} }
} }
public void onTick() { // Returns true if the world should be deleted
for (Scene scene : this.getScenes().values()) { public boolean onTick() {
scene.onTick(); if (this.getPlayerCount() == 0) return true;
} this.scenes.forEach((k, scene) -> scene.onTick());
return false;
} }
public void close() { public void close() {
......
...@@ -218,21 +218,10 @@ public final class GameServer extends KcpServer { ...@@ -218,21 +218,10 @@ public final class GameServer extends KcpServer {
var tickStart = Instant.now(); var tickStart = Instant.now();
// Tick worlds. // Tick worlds.
Iterator<World> it = this.getWorlds().iterator(); this.worlds.removeIf(World::onTick);
while (it.hasNext()) {
World world = it.next();
if (world.getPlayerCount() == 0) {
it.remove();
}
world.onTick();
}
// Tick players. // Tick players.
for (Player player : this.getPlayers().values()) { this.players.values().forEach(Player::onTick);
player.onTick();
}
// Tick scheduler. // Tick scheduler.
this.getScheduler().runTasks(); this.getScheduler().runTasks();
......
...@@ -10,6 +10,7 @@ import emu.grasscutter.server.packet.send.PacketAvatarExpeditionAllDataRsp; ...@@ -10,6 +10,7 @@ import emu.grasscutter.server.packet.send.PacketAvatarExpeditionAllDataRsp;
public class HandlerAvatarExpeditionAllDataReq extends PacketHandler { public class HandlerAvatarExpeditionAllDataReq 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 PacketAvatarExpeditionAllDataRsp(session.getPlayer())); var player = session.getPlayer();
session.send(new PacketAvatarExpeditionAllDataRsp(player.getExpeditionInfo(), player.getExpeditionLimit()));
} }
} }
...@@ -6,20 +6,19 @@ import emu.grasscutter.net.packet.PacketOpcodes; ...@@ -6,20 +6,19 @@ import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.AvatarExpeditionCallBackReqOuterClass.AvatarExpeditionCallBackReq; import emu.grasscutter.net.proto.AvatarExpeditionCallBackReqOuterClass.AvatarExpeditionCallBackReq;
import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketAvatarExpeditionCallBackRsp; import emu.grasscutter.server.packet.send.PacketAvatarExpeditionCallBackRsp;
import emu.grasscutter.server.packet.send.PacketAvatarExpeditionStartRsp;
import emu.grasscutter.utils.Utils;
@Opcodes(PacketOpcodes.AvatarExpeditionCallBackReq) @Opcodes(PacketOpcodes.AvatarExpeditionCallBackReq)
public class HandlerAvatarExpeditionCallBackReq extends PacketHandler { public class HandlerAvatarExpeditionCallBackReq 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 {
AvatarExpeditionCallBackReq req = AvatarExpeditionCallBackReq.parseFrom(payload); AvatarExpeditionCallBackReq req = AvatarExpeditionCallBackReq.parseFrom(payload);
var player = session.getPlayer();
for (int i = 0; i < req.getAvatarGuidCount(); i++) { for (int i = 0; i < req.getAvatarGuidCount(); i++) {
session.getPlayer().removeExpeditionInfo(req.getAvatarGuid(i)); player.removeExpeditionInfo(req.getAvatarGuid(i));
} }
session.getPlayer().save(); player.save();
session.send(new PacketAvatarExpeditionCallBackRsp(session.getPlayer())); session.send(new PacketAvatarExpeditionCallBackRsp(player.getExpeditionInfo()));
} }
} }
package emu.grasscutter.server.packet.recv; package emu.grasscutter.server.packet.recv;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.drop.DropData;
import emu.grasscutter.game.expedition.ExpeditionInfo; import emu.grasscutter.game.expedition.ExpeditionInfo;
import emu.grasscutter.game.expedition.ExpeditionRewardData;
import emu.grasscutter.game.expedition.ExpeditionRewardDataList; import emu.grasscutter.game.expedition.ExpeditionRewardDataList;
import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.AvatarExpeditionGetRewardReqOuterClass.AvatarExpeditionGetRewardReq; import emu.grasscutter.net.proto.AvatarExpeditionGetRewardReqOuterClass.AvatarExpeditionGetRewardReq;
import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketAvatarExpeditionCallBackRsp;
import emu.grasscutter.server.packet.send.PacketAvatarExpeditionGetRewardRsp; import emu.grasscutter.server.packet.send.PacketAvatarExpeditionGetRewardRsp;
import emu.grasscutter.server.packet.send.PacketGadgetInteractRsp;
import emu.grasscutter.server.packet.send.PacketItemAddHintNotify; import emu.grasscutter.server.packet.send.PacketItemAddHintNotify;
import emu.grasscutter.utils.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List; import java.util.List;
@Opcodes(PacketOpcodes.AvatarExpeditionGetRewardReq) @Opcodes(PacketOpcodes.AvatarExpeditionGetRewardReq)
...@@ -29,33 +20,25 @@ public class HandlerAvatarExpeditionGetRewardReq extends PacketHandler { ...@@ -29,33 +20,25 @@ public class HandlerAvatarExpeditionGetRewardReq 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 {
AvatarExpeditionGetRewardReq req = AvatarExpeditionGetRewardReq.parseFrom(payload); AvatarExpeditionGetRewardReq req = AvatarExpeditionGetRewardReq.parseFrom(payload);
var player = session.getPlayer();
ExpeditionInfo expInfo = session.getPlayer().getExpeditionInfo(req.getAvatarGuid()); ExpeditionInfo expInfo = player.getExpeditionInfo(req.getAvatarGuid());
List<GameItem> items = new ArrayList<>();
List<GameItem> items = new LinkedList<>(); List<ExpeditionRewardDataList> expeditionRewardDataLists = session.getServer().getExpeditionSystem().getExpeditionRewardDataList().get(expInfo.getExpId());
if (session.getServer().getExpeditionSystem().getExpeditionRewardDataList().containsKey(expInfo.getExpId())) { if (expeditionRewardDataLists != null) {
for (ExpeditionRewardDataList RewardDataList : session.getServer().getExpeditionSystem().getExpeditionRewardDataList().get(expInfo.getExpId())) { expeditionRewardDataLists.stream()
if (RewardDataList.getHourTime() == expInfo.getHourTime()) { .filter(r -> r.getHourTime() == expInfo.getHourTime())
if (!RewardDataList.getExpeditionRewardData().isEmpty()) { .map(ExpeditionRewardDataList::getRewards)
for (ExpeditionRewardData RewardData :RewardDataList.getExpeditionRewardData()) { .forEach(items::addAll);
int num = RewardData.getMinCount();
if (RewardData.getMinCount() != RewardData.getMaxCount()) {
num = Utils.randomRange(RewardData.getMinCount(), RewardData.getMaxCount());
}
items.add(new GameItem(RewardData.getItemId(), num));
}
}
}
}
} }
session.getPlayer().getInventory().addItems(items); player.getInventory().addItems(items);
session.getPlayer().sendPacket(new PacketItemAddHintNotify(items, ActionReason.ExpeditionReward)); player.sendPacket(new PacketItemAddHintNotify(items, ActionReason.ExpeditionReward));
session.getPlayer().removeExpeditionInfo(req.getAvatarGuid()); player.removeExpeditionInfo(req.getAvatarGuid());
session.getPlayer().save(); player.save();
session.send(new PacketAvatarExpeditionGetRewardRsp(session.getPlayer(), items)); session.send(new PacketAvatarExpeditionGetRewardRsp(player.getExpeditionInfo(), items));
} }
} }
package emu.grasscutter.server.packet.recv; package emu.grasscutter.server.packet.recv;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
...@@ -14,10 +13,11 @@ public class HandlerAvatarExpeditionStartReq extends PacketHandler { ...@@ -14,10 +13,11 @@ public class HandlerAvatarExpeditionStartReq 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 {
AvatarExpeditionStartReq req = AvatarExpeditionStartReq.parseFrom(payload); AvatarExpeditionStartReq req = AvatarExpeditionStartReq.parseFrom(payload);
var player = session.getPlayer();
int startTime = Utils.getCurrentSeconds(); int startTime = Utils.getCurrentSeconds();
session.getPlayer().addExpeditionInfo(req.getAvatarGuid(), req.getExpId(), req.getHourTime(), startTime); player.addExpeditionInfo(req.getAvatarGuid(), req.getExpId(), req.getHourTime(), startTime);
session.getPlayer().save(); player.save();
session.send(new PacketAvatarExpeditionStartRsp(session.getPlayer())); session.send(new PacketAvatarExpeditionStartRsp(player.getExpeditionInfo()));
} }
} }
...@@ -33,7 +33,7 @@ public class PacketAllWidgetDataNotify extends BasePacket { ...@@ -33,7 +33,7 @@ public class PacketAllWidgetDataNotify extends BasePacket {
// Good luck, my boy. // Good luck, my boy.
.addAllNormalCoolDownDataList(List.of()); .addAllNormalCoolDownDataList(List.of());
if (player.getWidgetId() == null) { if (player.getWidgetId() == 0) { // TODO: check this logic later, it was null-checking an int before which made it dead code
proto.addAllSlotList(List.of()); proto.addAllSlotList(List.of());
} else { } else {
proto.addSlotList( proto.addSlotList(
......
package emu.grasscutter.server.packet.send; package emu.grasscutter.server.packet.send;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.expedition.ExpeditionInfo; import emu.grasscutter.game.expedition.ExpeditionInfo;
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;
import emu.grasscutter.net.proto.AvatarExpeditionAllDataRspOuterClass.AvatarExpeditionAllDataRsp; import emu.grasscutter.net.proto.AvatarExpeditionAllDataRspOuterClass.AvatarExpeditionAllDataRsp;
import emu.grasscutter.net.proto.AvatarExpeditionInfoOuterClass.AvatarExpeditionInfo;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class PacketAvatarExpeditionAllDataRsp extends BasePacket { public class PacketAvatarExpeditionAllDataRsp extends BasePacket {
public PacketAvatarExpeditionAllDataRsp(Player player) { public PacketAvatarExpeditionAllDataRsp(Map<Long, ExpeditionInfo> expeditionInfo, int expeditionCountLimit) {
super(PacketOpcodes.AvatarExpeditionAllDataRsp); super(PacketOpcodes.AvatarExpeditionAllDataRsp);
List<Integer> openExpeditionList = new ArrayList<>(List.of(306,305,304,303,302,301,206,105,204,104,203,103,202,101,102,201,106,205)); List<Integer> openExpeditionList = new ArrayList<>(List.of(306,305,304,303,302,301,206,105,204,104,203,103,202,101,102,201,106,205));
Map<Long, AvatarExpeditionInfo> avatarExpeditionInfoList = new HashMap<Long, AvatarExpeditionInfo>();
var expeditionInfo = player.getExpeditionInfo(); this.setData(AvatarExpeditionAllDataRsp.newBuilder()
for (Long key : player.getExpeditionInfo().keySet()) { .addAllOpenExpeditionList(openExpeditionList)
ExpeditionInfo e = expeditionInfo.get(key); .setExpeditionCountLimit(expeditionCountLimit)
avatarExpeditionInfoList.put(key, AvatarExpeditionInfo.newBuilder().setStateValue(e.getState()).setExpId(e.getExpId()).setHourTime(e.getHourTime()).setStartTime(e.getStartTime()).build()); .putAllExpeditionInfoMap(
}; expeditionInfo.entrySet().stream()
.collect(Collectors.toMap(
AvatarExpeditionAllDataRsp.Builder proto = AvatarExpeditionAllDataRsp.newBuilder() e -> e.getKey(),
.addAllOpenExpeditionList(openExpeditionList) e -> e.getValue().toProto())))
.setExpeditionCountLimit(5) .build());
.putAllExpeditionInfoMap(avatarExpeditionInfoList);
this.setData(proto.build());
} }
} }
package emu.grasscutter.server.packet.send; package emu.grasscutter.server.packet.send;
import java.util.Map;
import emu.grasscutter.game.expedition.ExpeditionInfo; import emu.grasscutter.game.expedition.ExpeditionInfo;
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;
import emu.grasscutter.net.proto.AvatarExpeditionCallBackRspOuterClass.AvatarExpeditionCallBackRsp; import emu.grasscutter.net.proto.AvatarExpeditionCallBackRspOuterClass.AvatarExpeditionCallBackRsp;
import emu.grasscutter.net.proto.AvatarExpeditionInfoOuterClass.AvatarExpeditionInfo;
public class PacketAvatarExpeditionCallBackRsp extends BasePacket { public class PacketAvatarExpeditionCallBackRsp extends BasePacket {
public PacketAvatarExpeditionCallBackRsp(Player player) { public PacketAvatarExpeditionCallBackRsp(Map<Long, ExpeditionInfo> expeditionInfo) {
super(PacketOpcodes.AvatarExpeditionCallBackRsp); super(PacketOpcodes.AvatarExpeditionCallBackRsp);
AvatarExpeditionCallBackRsp.Builder proto = AvatarExpeditionCallBackRsp.newBuilder(); AvatarExpeditionCallBackRsp.Builder proto = AvatarExpeditionCallBackRsp.newBuilder();
var expeditionInfo = player.getExpeditionInfo(); expeditionInfo.forEach((key, e) -> proto.putExpeditionInfoMap(key, e.toProto()));
for (Long key : player.getExpeditionInfo().keySet()) {
ExpeditionInfo e = expeditionInfo.get(key);
proto.putExpeditionInfoMap(key, AvatarExpeditionInfo.newBuilder().setStateValue(e.getState()).setExpId(e.getExpId()).setHourTime(e.getHourTime()).setStartTime(e.getStartTime()).build());
};
this.setData(proto.build()); this.setData(proto.build());
} }
......
package emu.grasscutter.server.packet.send; package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.expedition.ExpeditionInfo; import emu.grasscutter.game.expedition.ExpeditionInfo;
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;
import emu.grasscutter.net.proto.AvatarExpeditionDataNotifyOuterClass.AvatarExpeditionDataNotify; import emu.grasscutter.net.proto.AvatarExpeditionDataNotifyOuterClass.AvatarExpeditionDataNotify;
import emu.grasscutter.net.proto.AvatarExpeditionInfoOuterClass.AvatarExpeditionInfo;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class PacketAvatarExpeditionDataNotify extends BasePacket { public class PacketAvatarExpeditionDataNotify extends BasePacket {
public PacketAvatarExpeditionDataNotify(Player player) { public PacketAvatarExpeditionDataNotify(Map<Long, ExpeditionInfo> expeditionInfo) {
super(PacketOpcodes.AvatarExpeditionDataNotify); super(PacketOpcodes.AvatarExpeditionDataNotify);
Map<Long, AvatarExpeditionInfo> avatarExpeditionInfoList = new HashMap<Long, AvatarExpeditionInfo>(); this.setData(AvatarExpeditionDataNotify.newBuilder()
.putAllExpeditionInfoMap(
var expeditionInfo = player.getExpeditionInfo(); expeditionInfo.entrySet().stream()
for (Long key : player.getExpeditionInfo().keySet()) { .collect(Collectors.toMap(
ExpeditionInfo e = expeditionInfo.get(key); e -> e.getKey(),
avatarExpeditionInfoList.put(key, AvatarExpeditionInfo.newBuilder().setStateValue(e.getState()).setExpId(e.getExpId()).setHourTime(e.getHourTime()).setStartTime(e.getStartTime()).build()); e -> e.getValue().toProto())))
}; .build()
);
AvatarExpeditionDataNotify.Builder proto = AvatarExpeditionDataNotify.newBuilder()
.putAllExpeditionInfoMap(avatarExpeditionInfoList);
this.setData(proto.build());
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment