forked from ynoproject/forest-orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.js
93 lines (84 loc) · 3.88 KB
/
account.js
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
let loginToken = null;
function initAccountControls() {
document.getElementById('loginButton').onclick = () => {
document.getElementById('loginErrorRow').classList.add('hidden');
openModal('loginModal');
};
document.getElementById('logoutButton').onclick = () => {
apiFetch('logout')
.then(response => {
if (!response.ok)
console.error(response.statusText);
setCookie('sessionId', '');
fetchAndUpdatePlayerInfo();
}).catch(err => console.error(err));
};
document.getElementById('loginForm').onsubmit = function () {
const form = this;
closeModal();
apiFetch(`login?${new URLSearchParams(new FormData(form)).toString()}`)
.then(response => {
if (!response.ok) {
response.text().then(_ => {
document.getElementById('loginError').innerHTML = getMassagedLabel(localizedMessages.account.login.errors.invalidLogin, true);
document.getElementById('loginErrorRow').classList.remove('hidden');
openModal('loginModal');
});
return;
}
return response.text();
}).then(sId => {
if (sId) {
setCookie('sessionId', sId);
fetchAndUpdatePlayerInfo();
}
}).catch(err => console.error(err));
return false;
};
document.getElementById('registerForm').onsubmit = function () {
const form = this;
if (document.getElementById('registerPassword').value !== document.getElementById('registerConfirmPassword').value) {
document.getElementById('registerError').innerHTML = getMassagedLabel(localizedMessages.account.register.errors.confirmPasswordMismatch, true);
document.getElementById('registerErrorRow').classList.remove('hidden');
return false;
}
closeModal();
apiFetch(`register?${new URLSearchParams(new FormData(form)).toString()}`)
.then(response => {
if (!response.ok) {
response.text().then(error => {
document.getElementById('registerError').innerHTML = getMassagedLabel(localizedMessages.account.register.errors[error === 'user exists' ? 'usernameTaken' : 'invalidCredentials'], true);
document.getElementById('registerErrorRow').classList.remove('hidden');
openModal('registerModal');
});
return;
}
document.getElementById('loginErrorRow').classList.add('hidden');
openModal('loginModal');
})
.catch(err => console.error(err));
return false;
};
document.getElementById('accountSettingsButton').onclick = () => {
initAccountSettingsModal();
openModal('accountSettingsModal', null, 'settingsModal');
};
}
function initAccountSettingsModal() {
const badgeId = playerData?.badge || 'null';
const badge = playerData?.badge ? badgeCache.find(b => b.badgeId === badgeId) : null;
const accountBadgeButton = document.getElementById('accountBadgeButton');
const badgeButton = document.getElementById('badgeButton');
accountBadgeButton.innerHTML = getBadgeItem(badge || { badgeId: 'null' }, false, true, false, true).innerHTML;
badgeButton.innerHTML = getBadgeItem(badge || { badgeId: 'null' }, false, true).innerHTML;
if (gameId === '2kki' && badge?.overlayType & BadgeOverlayType.LOCATION) {
handle2kkiBadgeOverlayLocationColorOverride(accountBadgeButton.querySelector('.badgeOverlay'), accountBadgeButton.querySelector('.badgeOverlay2'), cachedLocations);
handle2kkiBadgeOverlayLocationColorOverride(badgeButton.querySelector('.badgeOverlay'), badgeButton.querySelector('.badgeOverlay2'), cachedLocations);
}
}
function showAccountToastMessage(key, icon, username) {
if (!notificationConfig.account.all || (notificationConfig.account.hasOwnProperty(key) && !notificationConfig.account[key]))
return;
let message = getMassagedLabel(localizedMessages.toast.account[key], true).replace('{USER}', username);
showToastMessage(message, icon, true);
}