fix: player profile changes are not reflected cuz Java is object-oriented (#2322)

* fix: player profile changes are not reflected.
fix: deob FriendBrief.proto

* fix: tentatively sync profile when log out

* fix: allow offline player

* Update src/main/java/emu/grasscutter/game/friends/PlayerProfile.java

Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com>

* Update src/main/java/emu/grasscutter/game/player/Player.java

Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com>

* Update src/main/java/emu/grasscutter/game/player/Player.java

Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com>

* fix: server friend

---------

Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com>
This commit is contained in:
hamusuke
2023-08-30 12:23:46 +09:00
committed by GitHub
parent 8e4e3dd89e
commit 7049cfdb58
4 changed files with 119 additions and 144 deletions

View File

@@ -14,19 +14,24 @@ import org.bson.types.ObjectId;
@Entity(value = "friendships", useDiscriminator = false)
public class Friendship {
@Id private ObjectId id;
@Id
private ObjectId id;
@Transient private Player owner;
@Transient
private Player owner;
@Indexed private int ownerId;
@Indexed private int friendId;
@Indexed
private int ownerId;
@Indexed
private int friendId;
private boolean isFriend;
private int askerId;
private PlayerProfile profile;
@Deprecated // Morphia use only
public Friendship() {}
public Friendship() {
}
public Friendship(Player owner, Player friend, Player asker) {
this.setOwner(owner);
@@ -90,28 +95,28 @@ public class Friendship {
}
public FriendBrief toProto() {
FriendBrief proto =
FriendBrief.newBuilder()
.setUid(getFriendProfile().getUid())
.setNickname(getFriendProfile().getName())
.setLevel(getFriendProfile().getPlayerLevel())
.setProfilePicture(
ProfilePicture.newBuilder().setAvatarId(getFriendProfile().getAvatarId()))
.setWorldLevel(getFriendProfile().getWorldLevel())
.setSignature(getFriendProfile().getSignature())
.setOnlineState(
getFriendProfile().isOnline()
? FriendOnlineState.FRIEND_ONLINE_STATE_ONLINE
: FriendOnlineState.FRIEND_ONLINE_STATE_DISCONNECT)
.setIsMpModeAvailable(true)
.setLastActiveTime(getFriendProfile().getLastActiveTime())
.setNameCardId(getFriendProfile().getNameCard())
.setParam(getFriendProfile().getDaysSinceLogin())
.setIsGameSource(true)
.setPlatformType(PlatformTypeOuterClass.PlatformType.PLATFORM_TYPE_PC)
.setFriendEnterHomeOptionValue(getFriendProfile().getEnterHomeOption())
.build();
var player = this.getFriendProfile().getPlayer(); // get latest player and sync.
return proto;
return FriendBrief.newBuilder()
.setUid(getFriendProfile().getUid())
.setNickname(getFriendProfile().getName())
.setLevel(getFriendProfile().getPlayerLevel())
.setProfilePicture(
ProfilePicture.newBuilder().setAvatarId(getFriendProfile().getAvatarId()))
.setWorldLevel(getFriendProfile().getWorldLevel())
.setSignature(getFriendProfile().getSignature())
.setOnlineState(
player != null && player.isOnline()
? FriendOnlineState.FRIEND_ONLINE_STATE_ONLINE
: FriendOnlineState.FRIEND_ONLINE_STATE_DISCONNECT)
.setIsMpModeAvailable(true)
.setLastActiveTime(getFriendProfile().getLastActiveTime())
.setNameCardId(getFriendProfile().getNameCard())
.setParam(getFriendProfile().getDaysSinceLogin())
.setIsGameSource(true)
.setPlatformType(PlatformTypeOuterClass.PlatformType.PLATFORM_TYPE_PC)
.setIsInDuel(getFriendProfile().isInDuel())
.setIsDuelObservable(getFriendProfile().isDuelObservable())
.build();
}
}

View File

@@ -2,17 +2,21 @@ package emu.grasscutter.game.friends;
import dev.morphia.annotations.AlsoLoad;
import dev.morphia.annotations.Entity;
import emu.grasscutter.Grasscutter;
import dev.morphia.annotations.Transient;
import emu.grasscutter.game.home.GameHome;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.proto.FriendEnterHomeOptionOuterClass;
import emu.grasscutter.utils.Utils;
import lombok.Getter;
import org.jetbrains.annotations.Nullable;
@Entity
@Getter
public class PlayerProfile {
@Transient private Player player;
@AlsoLoad("id")
private int uid;
@@ -24,9 +28,14 @@ public class PlayerProfile {
private int playerLevel;
private int worldLevel;
private int lastActiveTime;
private boolean isInDuel = false; // TODO: Implement duels. (TCG)
private boolean isDuelObservable = false; // TODO: Implement duels. (TCG)
@Getter
private int enterHomeOption;
@Deprecated // Morphia only
public PlayerProfile() {}
@@ -35,46 +44,13 @@ public class PlayerProfile {
this.syncWithCharacter(player);
}
public int getUid() {
return uid;
}
@Nullable
public Player getPlayer() {
var player = Grasscutter.getGameServer().getPlayerByUid(this.getUid(), true);
this.syncWithCharacter(player);
return player;
}
public synchronized void setPlayer(Player player) {
this.player = player;
}
public String getName() {
return name;
}
public int getNameCard() {
return nameCard;
}
public int getAvatarId() {
return avatarId;
}
public String getSignature() {
return signature;
}
public int getPlayerLevel() {
return playerLevel;
}
public int getWorldLevel() {
return worldLevel;
}
public int getLastActiveTime() {
return lastActiveTime;
}
public void updateLastActiveTime() {
this.lastActiveTime = Utils.getCurrentSeconds();
}
@@ -83,10 +59,6 @@ public class PlayerProfile {
return (int) Math.floor((Utils.getCurrentSeconds() - getLastActiveTime()) / 86400.0);
}
public boolean isOnline() {
return this.getPlayer() != null;
}
public void syncWithCharacter(Player player) {
if (player == null) {
return;

View File

@@ -1488,7 +1488,6 @@ public class Player implements PlayerHook, FieldFetch {
// register
getServer().registerPlayer(this);
getProfile().setPlayer(this); // Set online
}
public void onLogout() {
@@ -1509,7 +1508,6 @@ public class Player implements PlayerHook, FieldFetch {
// Status stuff
this.getProfile().syncWithCharacter(this);
this.getProfile().setPlayer(null); // Set offline
this.getCoopRequests().clear();
this.getEnterHomeRequests().values().forEach(req -> this.expireEnterHomeRequest(req, true));