DatabaseHelper.java 5.65 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
package emu.grasscutter.database;

import java.util.List;

KingRainbow44's avatar
KingRainbow44 committed
5
6
import com.mongodb.client.result.DeleteResult;
import dev.morphia.query.experimental.filters.Filters;
Melledy's avatar
Melledy committed
7
8
9
10
11
12
13
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.friends.Friendship;
import emu.grasscutter.game.inventory.GenshinItem;

KingRainbow44's avatar
KingRainbow44 committed
14
public final class DatabaseHelper {
Melledy's avatar
Melledy committed
15
16
17
	public static Account createAccount(String username) {
		return createAccountWithId(username, 0);
	}
KingRainbow44's avatar
KingRainbow44 committed
18

Melledy's avatar
Melledy committed
19
20
21
22
23
24
	public static Account createAccountWithId(String username, int reservedId) {
		// Unique names only
		Account exists = DatabaseHelper.getAccountByName(username);
		if (exists != null) {
			return null;
		}
KingRainbow44's avatar
KingRainbow44 committed
25

Melledy's avatar
Melledy committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
		// Make sure there are no id collisions
		if (reservedId > 0) {
			// Cannot make account with the same uid as the server console
			if (reservedId == GenshinConstants.SERVER_CONSOLE_UID) {
				return null;
			}
			exists = DatabaseHelper.getAccountByPlayerId(reservedId);
			if (exists != null) {
				return null;
			}
		}

		// Account
		Account account = new Account();
		account.setUsername(username);
		account.setId(Integer.toString(DatabaseManager.getNextId(account)));
KingRainbow44's avatar
KingRainbow44 committed
42

Melledy's avatar
Melledy committed
43
44
		if (reservedId > 0) {
			account.setPlayerId(reservedId);
KingRainbow44's avatar
KingRainbow44 committed
45
		}
Melledy's avatar
Melledy committed
46
47
48
49
50
51
52
53
54
55
56
57

		DatabaseHelper.saveAccount(account);
		return account;
	}

	@Deprecated
	public static Account createAccountWithPassword(String username, String password) {
		// Unique names only
		Account exists = DatabaseHelper.getAccountByName(username);
		if (exists != null) {
			return null;
		}
KingRainbow44's avatar
KingRainbow44 committed
58

Melledy's avatar
Melledy committed
59
60
61
62
63
		// Account
		Account account = new Account();
		account.setId(Integer.toString(DatabaseManager.getNextId(account)));
		account.setUsername(username);
		account.setPassword(password);
KingRainbow44's avatar
KingRainbow44 committed
64
		DatabaseHelper.saveAccount(account);
Melledy's avatar
Melledy committed
65
66
67
68
		return account;
	}

	public static void saveAccount(Account account) {
69
		DatabaseManager.getAccountDatastore().save(account);
Melledy's avatar
Melledy committed
70
	}
KingRainbow44's avatar
KingRainbow44 committed
71

Melledy's avatar
Melledy committed
72
	public static Account getAccountByName(String username) {
KingRainbow44's avatar
KingRainbow44 committed
73
		return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).first();
Melledy's avatar
Melledy committed
74
	}
KingRainbow44's avatar
KingRainbow44 committed
75

Melledy's avatar
Melledy committed
76
	public static Account getAccountByToken(String token) {
KingRainbow44's avatar
KingRainbow44 committed
77
78
		if(token == null) return null;
		return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
Melledy's avatar
Melledy committed
79
	}
KingRainbow44's avatar
KingRainbow44 committed
80

Melledy's avatar
Melledy committed
81
	public static Account getAccountById(String uid) {
KingRainbow44's avatar
KingRainbow44 committed
82
		return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
Melledy's avatar
Melledy committed
83
	}
KingRainbow44's avatar
KingRainbow44 committed
84

85
	public static Account getAccountByPlayerId(int playerId) {
KingRainbow44's avatar
KingRainbow44 committed
86
		return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("playerId", playerId)).first();
Melledy's avatar
Melledy committed
87
	}
KingRainbow44's avatar
KingRainbow44 committed
88

Melledy's avatar
Melledy committed
89
	public static boolean deleteAccount(String username) {
KingRainbow44's avatar
KingRainbow44 committed
90
		return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).delete().getDeletedCount() > 0;
Melledy's avatar
Melledy committed
91
	}
KingRainbow44's avatar
KingRainbow44 committed
92

Melledy's avatar
Melledy committed
93
	public static GenshinPlayer getPlayerById(int id) {
KingRainbow44's avatar
KingRainbow44 committed
94
		return DatabaseManager.getDatastore().find(GenshinPlayer.class).filter(Filters.eq("_id", id)).first();
Melledy's avatar
Melledy committed
95
	}
KingRainbow44's avatar
KingRainbow44 committed
96

Melledy's avatar
Melledy committed
97
	public static boolean checkPlayerExists(int id) {
KingRainbow44's avatar
KingRainbow44 committed
98
		return DatabaseManager.getDatastore().find(GenshinPlayer.class).filter(Filters.eq("_id", id)).first() != null;
Melledy's avatar
Melledy committed
99
	}
KingRainbow44's avatar
KingRainbow44 committed
100

Melledy's avatar
Melledy committed
101
102
	public static synchronized GenshinPlayer createPlayer(GenshinPlayer character, int reservedId) {
		// Check if reserved id
KingRainbow44's avatar
KingRainbow44 committed
103
		int id;
Melledy's avatar
Melledy committed
104
105
		if (reservedId > 0 && !checkPlayerExists(reservedId)) {
			id = reservedId;
106
			character.setUid(id);
Melledy's avatar
Melledy committed
107
108
109
110
111
		} else {
			do {
				id = DatabaseManager.getNextId(character);
			}
			while (checkPlayerExists(id));
112
			character.setUid(id);
Melledy's avatar
Melledy committed
113
114
115
116
117
		}
		// Save to database
		DatabaseManager.getDatastore().save(character);
		return character;
	}
KingRainbow44's avatar
KingRainbow44 committed
118

Melledy's avatar
Melledy committed
119
120
	public static synchronized int getNextPlayerId(int reservedId) {
		// Check if reserved id
KingRainbow44's avatar
KingRainbow44 committed
121
		int id;
Melledy's avatar
Melledy committed
122
123
124
125
126
127
128
129
130
131
		if (reservedId > 0 && !checkPlayerExists(reservedId)) {
			id = reservedId;
		} else {
			do {
				id = DatabaseManager.getNextId(GenshinPlayer.class);
			}
			while (checkPlayerExists(id));
		}
		return id;
	}
KingRainbow44's avatar
KingRainbow44 committed
132

Melledy's avatar
Melledy committed
133
134
135
	public static void savePlayer(GenshinPlayer character) {
		DatabaseManager.getDatastore().save(character);
	}
KingRainbow44's avatar
KingRainbow44 committed
136

Melledy's avatar
Melledy committed
137
138
139
	public static void saveAvatar(GenshinAvatar avatar) {
		DatabaseManager.getDatastore().save(avatar);
	}
KingRainbow44's avatar
KingRainbow44 committed
140

Melledy's avatar
Melledy committed
141
	public static List<GenshinAvatar> getAvatars(GenshinPlayer player) {
142
		return DatabaseManager.getDatastore().find(GenshinAvatar.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
143
	}
KingRainbow44's avatar
KingRainbow44 committed
144

Melledy's avatar
Melledy committed
145
146
147
	public static void saveItem(GenshinItem item) {
		DatabaseManager.getDatastore().save(item);
	}
KingRainbow44's avatar
KingRainbow44 committed
148

Melledy's avatar
Melledy committed
149
	public static boolean deleteItem(GenshinItem item) {
KingRainbow44's avatar
KingRainbow44 committed
150
		DeleteResult result = DatabaseManager.getDatastore().delete(item);
Melledy's avatar
Melledy committed
151
152
		return result.wasAcknowledged();
	}
KingRainbow44's avatar
KingRainbow44 committed
153

Melledy's avatar
Melledy committed
154
	public static List<GenshinItem> getInventoryItems(GenshinPlayer player) {
KingRainbow44's avatar
KingRainbow44 committed
155
		return DatabaseManager.getDatastore().find(GenshinItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
156
157
	}
	public static List<Friendship> getFriends(GenshinPlayer player) {
158
		return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
159
	}
KingRainbow44's avatar
KingRainbow44 committed
160

Melledy's avatar
Melledy committed
161
	public static List<Friendship> getReverseFriends(GenshinPlayer player) {
KingRainbow44's avatar
KingRainbow44 committed
162
		return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
163
164
165
166
167
168
169
170
171
172
173
	}

	public static void saveFriendship(Friendship friendship) {
		DatabaseManager.getDatastore().save(friendship);
	}

	public static void deleteFriendship(Friendship friendship) {
		DatabaseManager.getDatastore().delete(friendship);
	}

	public static Friendship getReverseFriendship(Friendship friendship) {
KingRainbow44's avatar
KingRainbow44 committed
174
175
176
177
		return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.and(
				Filters.eq("ownerId", friendship.getFriendId()),
				Filters.eq("friendId", friendship.getOwnerId())
		)).first();
Melledy's avatar
Melledy committed
178
179
	}
}