-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpetHandler.js
More file actions
113 lines (106 loc) · 3.75 KB
/
petHandler.js
File metadata and controls
113 lines (106 loc) · 3.75 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
import { grumblerCheck, formatAsPercentage } from './helperFunctions';
import { ALL_PETS, PET, THE_GRUMBLER } from '../constants';
/**
* Gathers the pet information
* @param {Map<{ ID: string, URL: string}, string>} msgMap - The message map to update
* @param {*} playerName - The player's name
* @param {*} extra - Additional information. See {@link https://github.com/pajlads/DinkPlugin/blob/master/docs/json-examples.md#pets} for all the information.
* @param {*} URL - The associated URL
*/
async function petHandler(msgMap, playerName, extra, MONGO_MIDDLEWARE, URL) {
const {
milestone: initialMilestone,
duplicate: isDuplicate,
petName,
} = extra;
const validatedPetName = grumblerCheck(petName);
let milestone =
validatedPetName === THE_GRUMBLER
? initialMilestone.replace('killcount', 'grumbles')
: initialMilestone;
async function getTotalPets(playername) {
const url = `${MONGO_MIDDLEWARE}/get-pets?playername=${encodeURIComponent(
playername
)}`;
try {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`/get-pets response status: ${res.status}`);
}
const json = await res.json();
return json.player?.totalPets != null
? Number(json.player.totalPets)
: null;
} catch (error) {
console.log('getTotalPets ', error.message);
return null;
}
}
async function incrementPetCount(playername, petName) {
const url = `${MONGO_MIDDLEWARE}/increment-pets`;
const today = new Date();
const formattedDate = `${String(today.getMonth() + 1).padStart(
2,
'0'
)}/${String(today.getDate()).padStart(2, '0')}/${today.getFullYear()}`;
try {
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
playername,
petName,
dateGot: formattedDate,
}),
});
if (!res.ok)
throw new Error(`Failed to increment pet count: ${res.status}`);
const json = await res.json();
console.log(
`Pet count and recent pet successfully updated for ${json.playername}`
);
} catch (error) {
console.log('incrementPetCount ', error.message);
}
}
return (async () => {
if (!isDuplicate) {
await incrementPetCount(playerName, validatedPetName);
}
const totalPets = await getTotalPets(playerName);
const totalPetsPercentage = totalPets
? formatAsPercentage(totalPets, ALL_PETS)
: '';
if (!validatedPetName || !milestone) {
const fallbackMsg = isDuplicate
? `**${playerName}** has a funny feeling like they would have been followed! ${
totalPets
? `| **${totalPets}/${ALL_PETS} (${totalPetsPercentage}%)**`
: ''
}
-# Pet name or milestone missing!`
: `**${playerName}** has a funny feeling like they're being followed! ${
totalPets
? `| **${totalPets}/${ALL_PETS} (${totalPetsPercentage}%)**`
: ''
}
-# Pet name or milestone missing!`;
msgMap.set({ ID: PET, URL }, fallbackMsg);
return msgMap;
}
const msg = isDuplicate
? `**${playerName}** has a funny feeling like they would have been followed by **${validatedPetName}** at **${milestone}!** ${
totalPets
? `| **${totalPets}/${ALL_PETS} (${totalPetsPercentage}%)**`
: ''
}`
: `**${playerName}** has a funny feeling like they're being followed by **${validatedPetName}** at **${milestone}!** ${
totalPets
? `| **${totalPets}/${ALL_PETS} (${totalPetsPercentage}%)**`
: ''
}`;
msgMap.set({ ID: PET, URL }, msg);
return msgMap;
})();
}
export default petHandler;