Skip to content

Commit

Permalink
Refactor player sync packet
Browse files Browse the repository at this point in the history
  • Loading branch information
Melledy committed Aug 4, 2024
1 parent 4cd7a51 commit 9ce053c
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void execute(CommandArgs args) {

if (changeList.size() > 0) {
// Send packet
args.getTarget().sendPacket(new PacketPlayerSyncScNotify(changeList.toArray(GameAvatar[]::new)));
args.getTarget().sendPacket(new PacketPlayerSyncScNotify(changeList));
// Send message
args.sendMessage("Set avatar(s) properties successfully");
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emu/lunarcore/game/avatar/AvatarData.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AvatarData {
@Setter private int rank; // Eidolons
private Int2IntMap skills; // Skill tree

@Setter private transient IAvatar baseAvatar;
@Setter private transient BaseAvatar baseAvatar;
private transient Int2ObjectMap<GameItem> equips;

@Deprecated // Morphia only
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/emu/lunarcore/game/avatar/AvatarMultiPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import emu.lunarcore.proto.AvatarSkillTreeOuterClass.AvatarSkillTree;
import emu.lunarcore.proto.EquipRelicOuterClass.EquipRelic;
import emu.lunarcore.proto.MultiPathAvatarInfoOuterClass.MultiPathAvatarInfo;
import emu.lunarcore.proto.PlayerSyncScNotifyOuterClass.PlayerSyncScNotify;
import lombok.Getter;
import lombok.Setter;

@Getter
@Entity(value = "multiPaths", useDiscriminator = false)
public class AvatarMultiPath implements IAvatar {
public class AvatarMultiPath extends BaseAvatar {
@Id private ObjectId id;
@Indexed private int ownerUid;

Expand Down Expand Up @@ -62,6 +63,16 @@ public Map<Integer, Integer> getSkills() {
return this.getData().getSkills();
}

// Player sync

// Player sync

public void onSync(PlayerSyncScNotify proto) {
proto.addMultiPathAvatarInfoList(this.toProto());
}

// Proto

public MultiPathAvatarInfo toProto() {
var proto = MultiPathAvatarInfo.newInstance()
.setAvatarIdValue(this.getExcelId())
Expand All @@ -83,6 +94,8 @@ public MultiPathAvatarInfo toProto() {
return proto;
}

// Database

public void save() {
LunarCore.getGameDatabase().save(this);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/emu/lunarcore/game/avatar/AvatarStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ public int getAvatarCount() {

// Base avatars

public IAvatar getBaseAvatarById(int id) {
IAvatar baseAvatar = this.getMultiPathById(id);
public BaseAvatar getBaseAvatarById(int id) {
BaseAvatar baseAvatar = this.getMultiPathById(id);
if (baseAvatar == null) {
baseAvatar = this.getAvatarById(id);
}

return baseAvatar;
}

public IAvatar getBaseAvatarById(ObjectId id) {
IAvatar baseAvatar = this.getMultiPathById(id);
public BaseAvatar getBaseAvatarById(ObjectId id) {
BaseAvatar baseAvatar = this.getMultiPathById(id);
if (baseAvatar == null) {
baseAvatar = this.getAvatarById(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,46 @@
import emu.lunarcore.data.excel.AvatarExcel;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.server.game.Syncable;
import emu.lunarcore.server.packet.send.PacketPlayerSyncScNotify;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

public interface IAvatar {
public ObjectId getId();
public abstract class BaseAvatar implements Syncable {
// Properties

public Player getOwner();
public abstract ObjectId getId();

public AvatarData getData();
public abstract Player getOwner();

public abstract AvatarData getData();

// Excels

public int getExcelId();
public abstract int getExcelId();

public AvatarExcel getExcel();
public abstract AvatarExcel getExcel();

// Equip handlers

public default Int2ObjectMap<GameItem> getEquips() {
public Int2ObjectMap<GameItem> getEquips() {
return this.getData().getEquips();
}

public default GameItem getEquipBySlot(int slot) {
public GameItem getEquipBySlot(int slot) {
return this.getEquips().get(slot);
}

public default GameItem getEquipment() {
public GameItem getEquipment() {
return this.getEquips().get(GameConstants.EQUIPMENT_SLOT_ID);
}

public default boolean equipItem(GameItem item) {
public boolean equipItem(GameItem item) {
// Sanity check
int slot = item.getEquipSlot();
if (slot == 0) return false;

// Check if other avatars have this item equipped
IAvatar otherAvatar = item.getEquipAvatar();
BaseAvatar otherAvatar = item.getEquipAvatar();
if (otherAvatar != null) {
// Unequip this item from the other avatar
if (otherAvatar.unequipItem(slot) != null) {
Expand Down Expand Up @@ -75,7 +78,7 @@ public default boolean equipItem(GameItem item) {
return true;
}

public default GameItem unequipItem(int slot) {
public GameItem unequipItem(int slot) {
GameItem item = getEquips().remove(slot);

if (item != null) {
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/emu/lunarcore/game/avatar/GameAvatar.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import emu.lunarcore.proto.EquipRelicOuterClass.EquipRelic;
import emu.lunarcore.proto.LineupAvatarOuterClass.LineupAvatar;
import emu.lunarcore.proto.MotionInfoOuterClass.MotionInfo;
import emu.lunarcore.proto.PlayerSyncScNotifyOuterClass.PlayerSyncScNotify;
import emu.lunarcore.proto.SceneActorInfoOuterClass.SceneActorInfo;
import emu.lunarcore.proto.SceneEntityInfoOuterClass.SceneEntityInfo;
import emu.lunarcore.proto.SpBarInfoOuterClass.SpBarInfo;
Expand All @@ -40,7 +41,7 @@

@Getter
@Entity(value = "avatars", useDiscriminator = false)
public class GameAvatar implements GameEntity, IAvatar {
public class GameAvatar extends BaseAvatar implements GameEntity {
@Id private ObjectId id;
@Indexed @Getter private int ownerUid; // Uid of player that this avatar belongs to

Expand Down Expand Up @@ -223,6 +224,18 @@ public void addBuff(int buffId, int duration) {
this.buffs.put(buffId, System.currentTimeMillis() + (duration * 1000));
}

// Player sync

public void onSync(PlayerSyncScNotify proto) {
// Add to avatar sync
proto.getMutableAvatarSync().addAvatarList(this.toProto());

// Also update multipath info
if (this.getMultiPath() != null) {
proto.addMultiPathAvatarInfoList(this.getMultiPath().toProto());
}
}

// Proto

public Avatar toProto() {
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/emu/lunarcore/game/inventory/GameItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
import emu.lunarcore.data.excel.ItemExcel;
import emu.lunarcore.data.excel.RelicMainAffixExcel;
import emu.lunarcore.data.excel.RelicSubAffixExcel;
import emu.lunarcore.game.avatar.IAvatar;
import emu.lunarcore.game.avatar.BaseAvatar;
import emu.lunarcore.game.enums.AvatarPropertyType;
import emu.lunarcore.game.enums.ItemMainType;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.proto.EquipmentOuterClass.Equipment;
import emu.lunarcore.proto.ItemOuterClass.Item;
import emu.lunarcore.proto.MaterialOuterClass.Material;
import emu.lunarcore.proto.PileItemOuterClass.PileItem;
import emu.lunarcore.proto.PlayerSyncScNotifyOuterClass.PlayerSyncScNotify;
import emu.lunarcore.proto.RelicOuterClass.Relic;
import emu.lunarcore.server.game.Syncable;
import emu.lunarcore.util.Utils;
import emu.lunarcore.util.WeightedList;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
Expand All @@ -31,7 +33,7 @@

@Getter
@Entity(value = "items", useDiscriminator = false)
public class GameItem {
public class GameItem implements Syncable {
@Id private ObjectId id;
@Indexed private int ownerUid; // Uid of player that this avatar belongs to

Expand All @@ -53,7 +55,7 @@ public class GameItem {
private List<GameItemSubAffix> subAffixes;

@Indexed private ObjectId equipAvatarId; // Object id of the avatar this item is equipped to
private transient IAvatar equipAvatar;
private transient BaseAvatar equipAvatar;

@LoadOnly @AlsoLoad("equipAvatar")
private int equipAvatarExcelId; // Deprecated legacy field
Expand Down Expand Up @@ -151,7 +153,7 @@ public boolean setCount(int count) {
return false;
}

public boolean setEquipAvatar(IAvatar baseAvatar) {
public boolean setEquipAvatar(BaseAvatar baseAvatar) {
if (baseAvatar == null && this.isEquipped()) {
this.equipAvatarId = null;
this.equipAvatar = null;
Expand Down Expand Up @@ -271,6 +273,33 @@ public void save() {
LunarCore.getGameDatabase().delete(this);
}
}

// Player sync

public void onSync(PlayerSyncScNotify proto) {
switch (this.getExcel().getItemMainType().getTabType()) {
case MATERIAL -> {
proto.addMaterialList(this.toMaterialProto());
}
case RELIC -> {
if (this.getCount() > 0) {
proto.addRelicList(this.toRelicProto());
} else {
proto.addDelRelicList(this.getInternalUid());
}
}
case EQUIPMENT -> {
if (this.getCount() > 0) {
proto.addEquipmentList(this.toEquipmentProto());
} else {
proto.addDelEquipmentList(this.getInternalUid());
}
}
default -> {
// Skip
}
}
}

// Proto

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/emu/lunarcore/game/inventory/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import emu.lunarcore.data.excel.ItemExcel;
import emu.lunarcore.game.avatar.AvatarStorage;
import emu.lunarcore.game.avatar.GameAvatar;
import emu.lunarcore.game.avatar.IAvatar;
import emu.lunarcore.game.avatar.BaseAvatar;
import emu.lunarcore.game.enums.ItemMainType;
import emu.lunarcore.game.enums.ItemSubType;
import emu.lunarcore.game.inventory.tabs.EquipInventoryTab;
Expand Down Expand Up @@ -554,7 +554,7 @@ public List<GameItem> useItem(int itemId, int count, int avatarId) {
// Equips

public boolean equipItem(int avatarId, int equipId) {
IAvatar avatar = getPlayer().getAvatars().getBaseAvatarById(avatarId);
BaseAvatar avatar = getPlayer().getAvatars().getBaseAvatarById(avatarId);
GameItem item = this.getItemByUid(equipId);

if (avatar != null && item != null) {
Expand All @@ -565,7 +565,7 @@ public boolean equipItem(int avatarId, int equipId) {
}

public boolean unequipItem(int avatarId, int slot) {
IAvatar avatar = getPlayer().getAvatars().getBaseAvatarById(avatarId);
BaseAvatar avatar = getPlayer().getAvatars().getBaseAvatarById(avatarId);

if (avatar != null) {
GameItem unequipped = avatar.unequipItem(slot);
Expand Down Expand Up @@ -614,7 +614,7 @@ public boolean loadItem(GameItem item) {

// Equip to a character if possible
if (item.isEquipped() || item.getEquipAvatarExcelId() > 0) {
IAvatar avatar = null;
BaseAvatar avatar = null;
boolean hasEquipped = false;

if (item.getEquipAvatarExcelId() > 0) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/emu/lunarcore/game/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@
import emu.lunarcore.proto.PlatformTypeOuterClass.PlatformType;
import emu.lunarcore.proto.PlayerBasicInfoOuterClass.PlayerBasicInfo;
import emu.lunarcore.proto.PlayerDetailInfoOuterClass.PlayerDetailInfo;
import emu.lunarcore.proto.PlayerSyncScNotifyOuterClass.PlayerSyncScNotify;
import emu.lunarcore.proto.RogueCurVirtualItemInfoOuterClass.RogueCurVirtualItemInfo;
import emu.lunarcore.proto.SimpleInfoOuterClass.SimpleInfo;
import emu.lunarcore.server.game.GameServer;
import emu.lunarcore.server.game.GameSession;
import emu.lunarcore.server.game.Syncable;
import emu.lunarcore.server.game.Tickable;
import emu.lunarcore.server.packet.BasePacket;
import emu.lunarcore.server.packet.CmdId;
Expand All @@ -80,7 +82,7 @@

@Entity(value = "players", useDiscriminator = false)
@Getter
public class Player implements Tickable {
public class Player implements Tickable, Syncable {
@Id private int uid;
@Indexed private String accountUid;
private String name;
Expand Down Expand Up @@ -1026,6 +1028,12 @@ private void loadUnlocksFromDatabase() {
}
}

// Player sync

public void onSync(PlayerSyncScNotify proto) {
proto.setBasicInfo(this.toProto());
}

// Protobuf serialization

public PlayerBasicInfo toProto() {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/emu/lunarcore/game/player/PlayerUnlockData.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import emu.lunarcore.data.GameData;
import emu.lunarcore.game.avatar.GameAvatar;
import emu.lunarcore.game.enums.PersonalizeShowType;
import emu.lunarcore.proto.PlayerSyncScNotifyOuterClass.PlayerSyncScNotify;
import emu.lunarcore.server.game.Syncable;
import emu.lunarcore.server.packet.BasePacket;
import emu.lunarcore.server.packet.send.PacketPlayerSyncScNotify;
import emu.lunarcore.server.packet.send.PacketUnlockChatBubbleScNotify;
Expand All @@ -17,7 +19,7 @@

@Getter
@Entity(value = "unlocks", useDiscriminator = false)
public class PlayerUnlockData {
public class PlayerUnlockData implements Syncable {
private transient Player owner;

@Id private int ownerUid;
Expand Down Expand Up @@ -89,7 +91,7 @@ public void addHeadIcon(int headIconId) {
boolean success = this.getHeadIcons().add(headIconId);

if (success && this.getOwner().isLoggedIn()) {
this.sendPacket(new PacketPlayerSyncScNotify(getOwner().toBoardData()));
this.sendPacket(new PacketPlayerSyncScNotify(this));
this.save();
}
}
Expand All @@ -115,6 +117,14 @@ public void addPhoneTheme(int phoneThemeId) {
private void sendPacket(BasePacket packet) {
this.getOwner().sendPacket(packet);
}

// Player sync

public void onSync(PlayerSyncScNotify proto) {
proto.setBoardDataSync(this.getOwner().toBoardData());
}

// Database

public void save() {
LunarCore.getGameDatabase().save(this);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/emu/lunarcore/server/game/Syncable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package emu.lunarcore.server.game;

import emu.lunarcore.proto.PlayerSyncScNotifyOuterClass.PlayerSyncScNotify;

public interface Syncable {

public void onSync(PlayerSyncScNotify proto);

}
Loading

0 comments on commit 9ce053c

Please sign in to comment.