-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.ts
More file actions
282 lines (234 loc) · 10.6 KB
/
Copy pathdatabase.ts
File metadata and controls
282 lines (234 loc) · 10.6 KB
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
30
31
32
33
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { MongoClient, ObjectId } from "mongodb";
// Import our interfaces
import { Account } from "./interfaces/account";
import { ProfileInfo } from "./interfaces/profileInfo";
import { CustomPokemonStats } from "./interfaces/customPokemonStats";
import { UserPokemon } from "./interfaces/userPokemon";
const bcrypt = require('bcrypt');
const dotenv = require('dotenv');
dotenv.config();
console.log("[server] Successfully parsed ENV values")
const saltRounds: number = 10;
const client = new MongoClient(process.env.MONGODB_URI ?? "mongodb://localhost:27017");
client.connect();
console.log("[server] Successfully connected to MongoDB");
const main = client.db("main");
const accountsCollection = main.collection("accounts");
const profileinfoCollection = main.collection("profileinfo");
const userpokemonCollection = main.collection("userpokemon");
const custompokemonstatsCollection = main.collection("custompokemonstats");
console.log("[server] Successfully initialized collections");
export async function getAllPlayerCount() {
return await accountsCollection.countDocuments({});
}
export async function loginAccount(email: string, password: string): Promise<Account | null> {
const accountDetails: Account | null = await accountsCollection.findOne<Account>({ email });
if (!accountDetails) {
return null;
}
const isPasswordCorrect = await bcrypt.compareSync(password, accountDetails.password);
if (!isPasswordCorrect) {
return null;
}
return accountDetails;
}
export async function registerAccount(email: string, password: string) {
const accountDetails: Account | null = await accountsCollection.findOne<Account>({ email });
if (accountDetails) {
return null;
}
const hashedPassword = await bcrypt.hash(password, saltRounds);
const insertDocument = await accountsCollection.insertOne({ email, password: hashedPassword });
const newAccountDetails: Account | null = await accountsCollection.findOne<Account>({ _id: insertDocument.insertedId });
return newAccountDetails;
}
export async function hasProfile(email: string): Promise<boolean> {
const result: ProfileInfo | null = await profileinfoCollection.findOne<ProfileInfo>({ email });
if (!result) {
return false;
}
return true;
}
export async function registerPokemon(pokemonId: number, name: string) {
const count = await custompokemonstatsCollection.countDocuments();
const newPokemonId = count + 1;
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonId}`);
const data = await response.json();
const health = data.stats[0].base_stat;
const attack = data.stats[1].base_stat;
const defence = data.stats[2].base_stat;
let actual_name = "";
if (name == "None") {
actual_name = data.name;
} else {
actual_name = name;
}
actual_name = actual_name.toLowerCase();
const userPokemonSetup: CustomPokemonStats = {
_id: new ObjectId(),
customId: newPokemonId,
actualPokemonId: pokemonId,
health: health,
attack: attack,
defence: defence,
name: actual_name,
dateCaught: Math.floor(Date.now()),
amountBattlesWin: 0,
amountBattlesLose: 0
}
return await custompokemonstatsCollection.insertOne(userPokemonSetup), newPokemonId;
}
export async function setupProfileInfo(email: string, username: string, buddyId: number, profilePic: string, dateStart: number) {
const profileInfoSetup: ProfileInfo = {
_id: new ObjectId(),
email: email,
username: username,
buddyId: buddyId,
profilePic: profilePic,
badgeList: [],
dateStart: dateStart,
amountBattles: 0,
amountBattlesWin: 0,
amountBattlesLose: 0
}
return await profileinfoCollection.insertOne(profileInfoSetup);
}
export async function userPokemonSetup(email: string, customPokemonId: number, pokemonId: number) {
const userPokemonSetup: UserPokemon = {
_id: new ObjectId(),
email: email,
pokemonList: [customPokemonId],
idPokemonList: [pokemonId]
}
return await userpokemonCollection.insertOne(userPokemonSetup);
}
export async function findProfileInfo(email: string): Promise<ProfileInfo | null> {
const result: ProfileInfo | null = await profileinfoCollection.findOne<ProfileInfo>({ email });
return result;
}
export async function getAllPokemon(email: string): Promise<UserPokemon | null> {
const result: UserPokemon | null = await userpokemonCollection.findOne<UserPokemon>({ email });
return result;
}
export async function getPokemonStats(pokemonId: number): Promise<CustomPokemonStats | null> {
const result: CustomPokemonStats | null = await custompokemonstatsCollection.findOne<CustomPokemonStats>({ customId: pokemonId });
return result;
}
export async function changePassword(email: string, oldPassword: string, newPassword: string) {
const accountDetails: Account | null = await accountsCollection.findOne<Account>({ email });
if (!accountDetails) {
return null;
}
const isPasswordCorrect = await bcrypt.compareSync(oldPassword, accountDetails.password);
if (!isPasswordCorrect) {
return null;
}
const hashedPassword = await bcrypt.hashSync(newPassword, saltRounds);
const result = await accountsCollection.updateOne({ email }, { $set: { password: hashedPassword } });
return result;
}
export async function getAllCustom() {
return await custompokemonstatsCollection.find({}).toArray();
}
export async function changeBuddy(email: string, pokemonId: number) {
const profileinfoCheck = await findProfileInfo(email);
if (!profileinfoCheck) {
return false
}
return await profileinfoCollection.updateOne({ email }, { $set: { buddyId: pokemonId } });
}
export async function changeProfile(email: string, username: string, email2: string) {
return await profileinfoCollection.updateOne({ email }, { $set: { username, email: email2 } }) && await accountsCollection.updateOne({ email }, { $set: { email: email2 } }) && await userpokemonCollection.updateOne({ email }, { $set: { email: email2 } });
}
export async function addPokemon(email: string, customPokemonId: number, pokemonId: number) {
const userPokemon: UserPokemon | null = await getAllPokemon(email);
if (!userPokemon) {
return false
}
customPokemonId = Number(customPokemonId);
pokemonId = Number(pokemonId);
const updatedPokemonList = [...userPokemon.idPokemonList, pokemonId];
const updatedCustomPokemonList = [...userPokemon.pokemonList, customPokemonId];
return await userpokemonCollection.updateOne({ email }, { $set: { pokemonList: updatedCustomPokemonList, idPokemonList: updatedPokemonList } });
}
export async function updatePokemonStats(pokemonId: number, stat: string) {
const currentStats = await custompokemonstatsCollection.findOne({ customId: Number(pokemonId) });
if (stat == "health") {
return await custompokemonstatsCollection.updateOne({ customId: Number(pokemonId) }, { $set: { health: currentStats?.health + 1 } });
}
else if (stat == "attack") {
return await custompokemonstatsCollection.updateOne({ customId: Number(pokemonId) }, { $set: { attack: currentStats?.attack + 1 } });
}
else if (stat == "defence") {
return await custompokemonstatsCollection.updateOne({ customId: Number(pokemonId) }, { $set: { defence: currentStats?.defence + 1 } });
}
}
export async function updateAmountBattles(email: string, whatchanged: string, newAmountBattles: number) {
const profileinfoGet = await findProfileInfo(email);
if (!profileinfoGet) {
return false
}
const currentBattles = profileinfoGet.amountBattles;
const newCurrentBattles = currentBattles + 1;
if (whatchanged == "win") {
return await profileinfoCollection.updateOne({ email }, { $set: { amountBattles: newCurrentBattles, amountBattlesWin: newAmountBattles } });
}
else {
return await profileinfoCollection.updateOne({ email }, { $set: { amountBattles: newCurrentBattles, amountBattlesLose: newAmountBattles } });
}
}
export async function addBadge(email: string, badgeId: number) {
const profileinfoCheck = await findProfileInfo(email);
if (!profileinfoCheck) {
return false
}
const updatedBadgeList = [...profileinfoCheck.badgeList, badgeId];
return await profileinfoCollection.updateOne({ email }, { $set: { badgeList: updatedBadgeList } });
}
export async function getBadges(email: string) {
const profileinfoCheck = await findProfileInfo(email);
if (!profileinfoCheck) {
return null
}
return profileinfoCheck.badgeList;
}
export async function releasePokemon(email: string, customId: number) {
const userPokemon: UserPokemon | null = await getAllPokemon(email);
if (!userPokemon) {
return false
}
const customIdNum = Number(customId);
const actualId = await getPokemonStats(customIdNum);
const actualIdPokemon = actualId?.actualPokemonId;
let pokemonListNew: number[] = [];
let idPokemonListNew: number[] = [];
userPokemon.pokemonList.forEach(element => {
if (element != customIdNum) {
pokemonListNew.push(element);
}
});
userPokemon.idPokemonList.forEach(element => {
if (element != actualIdPokemon) {
idPokemonListNew.push(element);
}
});
return await userpokemonCollection.updateOne({ email }, { $set: { pokemonList: pokemonListNew, idPokemonList: idPokemonListNew } }) && await custompokemonstatsCollection.deleteOne({ customId: customIdNum });
}
export async function changeStats(customId: number, stat: string, plusOrMinus: string) {
const currentStats = await custompokemonstatsCollection.findOne({ customId: Number(customId) });
if (stat == "amountWon") {
if (plusOrMinus == "plus") {
return await custompokemonstatsCollection.updateOne({ customId: Number(customId) }, { $set: { amountBattlesWin: Number(currentStats?.amountBattlesWin + 1) } });
}
else {
return await custompokemonstatsCollection.updateOne({ customId: Number(customId) }, { $set: { amountBattlesWin: Number(currentStats?.amountBattlesWin - 1) } });
}
} else if (stat == "amountLost") {
if (plusOrMinus == "plus") {
return await custompokemonstatsCollection.updateOne({ customId: Number(customId) }, { $set: { amountBattlesLose: Number(currentStats?.amountBattlesLose + 1) } });
}
else {
return await custompokemonstatsCollection.updateOne({ customId: Number(customId) }, { $set: { amountBattlesLose: Number(currentStats?.amountBattlesLose - 1) } });
}
}
}