Friendship.java 2.76 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.friends;

3
import emu.grasscutter.net.proto.PlatformTypeOuterClass;
Melledy's avatar
Melledy committed
4
5
6
7
import org.bson.types.ObjectId;

import dev.morphia.annotations.*;
import emu.grasscutter.database.DatabaseHelper;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
9
10
11
12
import emu.grasscutter.net.proto.FriendBriefOuterClass.FriendBrief;
import emu.grasscutter.net.proto.FriendOnlineStateOuterClass.FriendOnlineState;
import emu.grasscutter.net.proto.HeadImageOuterClass.HeadImage;

KingRainbow44's avatar
KingRainbow44 committed
13
@Entity(value = "friendships", useDiscriminator = false)
Melledy's avatar
Melledy committed
14
15
16
public class Friendship {
	@Id private ObjectId id;
	
17
	@Transient private Player owner;
Melledy's avatar
Melledy committed
18
19
20
21
22
23
24
25
26
27
28
	
	@Indexed private int ownerId;
	@Indexed private int friendId;
	private boolean isFriend;
	private int askerId;
	
	private PlayerProfile profile;
	
	@Deprecated // Morphia use only
	public Friendship() { }
	
29
	public Friendship(Player owner, Player friend, Player asker) {
Melledy's avatar
Melledy committed
30
		this.setOwner(owner);
31
32
		this.ownerId = owner.getUid();
		this.friendId = friend.getUid();
Melledy's avatar
Melledy committed
33
		this.profile = friend.getProfile();
34
		this.askerId = asker.getUid();
Melledy's avatar
Melledy committed
35
36
	}

37
	public Player getOwner() {
Melledy's avatar
Melledy committed
38
39
40
		return owner;
	}

41
	public void setOwner(Player owner) {
Melledy's avatar
Melledy committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
		this.owner = owner;
	}

	public boolean isFriend() {
		return isFriend;
	}

	public void setIsFriend(boolean b) {
		this.isFriend = b;
	}

	public int getOwnerId() {
		return ownerId;
	}

	public int getFriendId() {
		return friendId;
	}

	public int getAskerId() {
		return askerId;
	}

	public void setAskerId(int askerId) {
		this.askerId = askerId;
	}

	public PlayerProfile getFriendProfile() {
		return profile;
	}
	
73
	public void setFriendProfile(Player character) {
74
		if (character == null || this.friendId != character.getUid()) return;
Melledy's avatar
Melledy committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
		this.profile = character.getProfile();
	}
	
	public boolean isOnline() {
		return getFriendProfile().getPlayer() != null;
	}

	public void save() {
		DatabaseHelper.saveFriendship(this);
	}

	public void delete() {
		DatabaseHelper.deleteFriendship(this);
	}
	
	public FriendBrief toProto() {
		FriendBrief proto = FriendBrief.newBuilder()
92
				.setUid(getFriendProfile().getUid())
Melledy's avatar
Melledy committed
93
94
				.setNickname(getFriendProfile().getName())
				.setLevel(getFriendProfile().getPlayerLevel())
95
				.setAvatarId(HeadImage.newBuilder().setAvatarId(getFriendProfile().getAvatarId()).getAvatarId())
Melledy's avatar
Melledy committed
96
97
				.setWorldLevel(getFriendProfile().getWorldLevel())
				.setSignature(getFriendProfile().getSignature())
98
				.setOnlineState(getFriendProfile().isOnline() ? FriendOnlineState.FRIEND_ONLINE : FriendOnlineState.FREIEND_DISCONNECT)
Melledy's avatar
Melledy committed
99
100
101
102
				.setIsMpModeAvailable(true)
				.setLastActiveTime(getFriendProfile().getLastActiveTime())
				.setNameCardId(getFriendProfile().getNameCard())
				.setParam(getFriendProfile().getDaysSinceLogin())
103
104
				.setIsGameSource(true)
				.setPlatformType(PlatformTypeOuterClass.PlatformType.PC)
Melledy's avatar
Melledy committed
105
106
107
108
109
				.build();

		return proto;
	}
}