Friendship.java 2.68 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package emu.grasscutter.game.friends;

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;

@Entity(value = "friendships", noClassnameStored = true)
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);
30
31
		this.ownerId = owner.getUid();
		this.friendId = friend.getUid();
Melledy's avatar
Melledy committed
32
		this.profile = friend.getProfile();
33
		this.askerId = asker.getUid();
Melledy's avatar
Melledy committed
34
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
	}

	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) {
73
		if (character == null || this.friendId != character.getUid()) return;
Melledy's avatar
Melledy committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
		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()
91
				.setUid(getFriendProfile().getUid())
Melledy's avatar
Melledy committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
				.setNickname(getFriendProfile().getName())
				.setLevel(getFriendProfile().getPlayerLevel())
				.setAvatar(HeadImage.newBuilder().setAvatarId(getFriendProfile().getAvatarId()))
				.setWorldLevel(getFriendProfile().getWorldLevel())
				.setSignature(getFriendProfile().getSignature())
				.setOnlineState(getFriendProfile().isOnline() ? FriendOnlineState.FRIEND_ONLINE : FriendOnlineState.FRIEND_DISCONNECT)
				.setIsMpModeAvailable(true)
				.setLastActiveTime(getFriendProfile().getLastActiveTime())
				.setNameCardId(getFriendProfile().getNameCard())
				.setParam(getFriendProfile().getDaysSinceLogin())
				.setUnk1(1)
				.setUnk2(3)
				.build();

		return proto;
	}
}