Friendship.java 2.81 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
8
9
10
11
12
import org.bson.types.ObjectId;

import dev.morphia.annotations.*;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.GenshinPlayer;
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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Friendship {
	@Id private ObjectId id;
	
	@Transient private GenshinPlayer owner;
	
	@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(GenshinPlayer owner, GenshinPlayer friend, GenshinPlayer asker) {
		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
38
39
40
41
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
73
	}

	public GenshinPlayer getOwner() {
		return owner;
	}

	public void setOwner(GenshinPlayer owner) {
		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;
	}
	
	public void setFriendProfile(GenshinPlayer 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;
	}
}