DatabaseHelper.java 5.69 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;
7
import emu.grasscutter.GameConstants;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.Account;
9
import emu.grasscutter.game.avatar.Avatar;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.friends.Friendship;
11
import emu.grasscutter.game.inventory.GameItem;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
13

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
		// Make sure there are no id collisions
		if (reservedId > 0) {
			// Cannot make account with the same uid as the server console
29
			if (reservedId == GameConstants.SERVER_CONSOLE_UID) {
Melledy's avatar
Melledy committed
30
31
32
33
34
35
36
37
38
39
40
41
				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

93
94
	public static List<Player> getAllPlayers() {
		return DatabaseManager.getDatastore().find(Player.class).stream().toList();
Benjamin Elsdon's avatar
Benjamin Elsdon committed
95
96
	}

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

Melledy's avatar
Melledy committed
101
	public static boolean checkPlayerExists(int id) {
102
		return DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("_id", id)).first() != null;
Melledy's avatar
Melledy committed
103
	}
KingRainbow44's avatar
KingRainbow44 committed
104

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

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

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

141
	public static void saveAvatar(Avatar avatar) {
Melledy's avatar
Melledy committed
142
143
		DatabaseManager.getDatastore().save(avatar);
	}
KingRainbow44's avatar
KingRainbow44 committed
144

145
146
	public static List<Avatar> getAvatars(Player player) {
		return DatabaseManager.getDatastore().find(Avatar.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
147
	}
KingRainbow44's avatar
KingRainbow44 committed
148

149
	public static void saveItem(GameItem item) {
Melledy's avatar
Melledy committed
150
151
		DatabaseManager.getDatastore().save(item);
	}
KingRainbow44's avatar
KingRainbow44 committed
152

153
	public static boolean deleteItem(GameItem item) {
KingRainbow44's avatar
KingRainbow44 committed
154
		DeleteResult result = DatabaseManager.getDatastore().delete(item);
Melledy's avatar
Melledy committed
155
156
		return result.wasAcknowledged();
	}
KingRainbow44's avatar
KingRainbow44 committed
157

158
159
	public static List<GameItem> getInventoryItems(Player player) {
		return DatabaseManager.getDatastore().find(GameItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
160
	}
161
	public static List<Friendship> getFriends(Player player) {
162
		return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
163
	}
KingRainbow44's avatar
KingRainbow44 committed
164

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

	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
178
179
180
181
		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
182
	}
memetrollsXD's avatar
memetrollsXD committed
183
184

	public static char AWJVN = 'e';
Melledy's avatar
Melledy committed
185
}