Skip to content

Commit

Permalink
Finished login features, fixed several bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Parking-Master committed Dec 30, 2024
1 parent 22abdae commit 6a86745
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 46 deletions.
35 changes: 9 additions & 26 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@
}
};

if (user.loggedIn) {
document.querySelector(".tab[data-tab='account']").textContent = "ACCOUNT";
}

function onWindowResize() {
let averageScale = window.innerHeight / 900;
let minWidth = 1440 * averageScale;
Expand All @@ -521,32 +525,9 @@
window.addEventListener("resize", onWindowResize);
onWindowResize();

function fetchUserData(callback = () => {}) {
if (localStorage["loginToken"]) {
let loginToken = localStorage["loginToken"];

fetch("https://hardy-beetle-free.ngrok-free.app/account/data", {
method: "POST",
headers: {
"Content-Type": "application/json",
"ngrok-skip-browser-warning": "69420"
},
body: JSON.stringify({
token: loginToken
})
}).then(response => response.json()).then(userData => {
user.logIn(userData);
callback();

document.querySelector(".wrapper").style.backgroundImage = `url(/images/other/menu/${user.preferences.menuBackground}.png)`;
});

document.querySelector(".tab[data-tab='account']").textContent = "ACCOUNT";
} else {
document.querySelector(".tab[data-tab='account']").textContent = "LOG IN";
}
}
fetchUserData();
user.logInCallback = function() {
document.querySelector(".wrapper").style.backgroundImage = `url(/images/other/menu/${user.preferences.menuBackground}.png)`;
};

function menuDialog() {
let output = swal.apply(null, arguments);
Expand Down Expand Up @@ -951,6 +932,7 @@
fetchUserData(function() {
//notification(`Welcome back, ${user.username}!`);
});
document.querySelector(".tab[data-tab='account']").textContent = "ACCOUNT";
} else {
error();
}
Expand Down Expand Up @@ -1100,6 +1082,7 @@
user.logOut();
selectTab(document.querySelector(".tab[data-tab='play']"));
fetchUserData();
document.querySelector(".tab[data-tab='account']").textContent = "LOG IN";
//notification("successfully logged out")
});
}
Expand Down
40 changes: 39 additions & 1 deletion lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ user = {
let oldData = user;
user = userData;
user.oldData = oldData;
if (oldData.logInCallback) user.logInCallback = oldData.logInCallback;

let loginToken = localStorage["loginToken"];
user.set = function(key, value) {
Expand Down Expand Up @@ -82,7 +83,12 @@ user = {
user.logOut = function() {
user = user.oldData;
localStorage.removeItem("loginToken");
localStorage.removeItem("user-login-cache");
};

localStorage.setItem("user-login-cache", JSON.stringify({
username: user.username
}));
}
};

Expand All @@ -91,4 +97,36 @@ if (localStorage["user-preferences"]) {
user.preferences = savedPreferences;
} else {
user.save();
}
}

function fetchUserData() {
if (localStorage["loginToken"]) {
user.loggedIn = true;

let loginToken = localStorage["loginToken"];
let userCache = localStorage["user-login-cache"];
if (userCache) {
userCache = JSON.parse(userCache);
user.username = userCache.username;
}

fetch("https://hardy-beetle-free.ngrok-free.app/account/data", {
method: "POST",
headers: {
"Content-Type": "application/json",
"ngrok-skip-browser-warning": "69420"
},
body: JSON.stringify({
token: loginToken
})
}).then(response => response.json()).then(userData => {
user.logIn(userData);
user.loggedIn = true;

if (typeof user.logInCallback === "function") user.logInCallback();
});
} else {
user.loggedIn = false;
}
}
fetchUserData();
74 changes: 55 additions & 19 deletions src.html
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@
utils.player.animations.play(playerObject, "rifle_idle");

let character = params().character || "SWAT";
gametime.run("join", [gametime.player.position, gametime.player.uuid, "SWAT", sandbox.username, character]);
gametime.run("join", [gametime.player.position, gametime.player.uuid, character, sandbox.username]);
});

// All multiplayer functions go here
Expand Down Expand Up @@ -1123,7 +1123,7 @@
scoreLimit: 100,
animationSpeed: 1
},
username: generateName(),
username: user.username || generateName(),
map: null,
primaryWeapon: null,
secondaryWeapon: null,
Expand Down Expand Up @@ -3558,37 +3558,69 @@
return isInView;
},
shake: async function(intensity = 1, duration = 50, repeat = false, defaultFov = 60) {
const MAX_ROTATION_X = Math.PI / 2;

async function next(repeat = false) {
if (utils.options.get("CancelCameraShake")) return utils.options.set("CancelCameraShake", false);
if (utils.options.get("CameraShaking")) return;
utils.options.set("CameraShaking", true);

let outFov = defaultFov + intensity;
let inFov = defaultFov;
let rotationX = 0.01 * intensity;
let rotationY = 0.005 * intensity;

new TWEEN.Tween(camera).to({ fov: outFov }, duration).easing(TWEEN.Easing.Quadratic.InOut).start().onUpdate(function() {
camera.rotateX(rotationX);
camera.rotateY(rotationY);
// Store the current z-rotation
const currentZRotation = camera.rotation.z;

// Create quaternions for x and y rotations
const qx = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), rotationX);
const qy = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), rotationY);

// Combine rotations
const combinedRotation = qx.multiply(qy);

// Apply the combined rotation to the camera's quaternion
camera.quaternion.premultiply(combinedRotation);

// Clamp the x-rotation
const euler = new THREE.Euler().setFromQuaternion(camera.quaternion, 'YXZ');
euler.x = Math.max(-MAX_ROTATION_X, Math.min(MAX_ROTATION_X, euler.x));

// Set the camera's quaternion from the clamped euler angles, preserving z-rotation
camera.quaternion.setFromEuler(euler);
camera.rotation.z = currentZRotation;

camera.updateProjectionMatrix();
});

await plugins.delay(duration);

new TWEEN.Tween(camera).to({ fov: inFov }, duration).easing(TWEEN.Easing.Quadratic.InOut).start().onUpdate(function() {
camera.rotateX(-rotationX);
camera.rotateY(-rotationY);
// Repeat the process for the reverse motion
const currentZRotation = camera.rotation.z;
const qx = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), -rotationX);
const qy = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), -rotationY);
const combinedRotation = qx.multiply(qy);
camera.quaternion.premultiply(combinedRotation);

const euler = new THREE.Euler().setFromQuaternion(camera.quaternion, 'YXZ');
euler.x = Math.max(-MAX_ROTATION_X, Math.min(MAX_ROTATION_X, euler.x));

camera.quaternion.setFromEuler(euler);
camera.rotation.z = currentZRotation;

camera.updateProjectionMatrix();
});

await plugins.delay(duration);

utils.options.set("CameraShaking", false);

if (repeat) next(true);
}

if (repeat) {
next(true);
} else {
Expand Down Expand Up @@ -4693,7 +4725,7 @@
}
if (!event.repeat && keyStates[keyBindings.zoom]) utils.weapons.zoom();

if (keyStates[keyBindings.shoot] && !keyRepeats[keyBindings.shoot]) {
if (keyStates[keyBindings.shoot] && !shootRapid) {
shootRapid = true;
utils.weapons.shoot(event.repeat);
}
Expand Down Expand Up @@ -4741,10 +4773,12 @@
gametime.run("halt", [gametime.player.uuid]);
}

if (shootRapid) {
utils.weapons.stopShoot();
if (event.code === keyBindings.shoot) {
if (shootRapid) {
utils.weapons.stopShoot();
}
shootRapid = false;
}
shootRapid = false;
});

document.addEventListener("mousemove", (event) => {
Expand Down Expand Up @@ -4815,7 +4849,7 @@
window.LoadingManager.onfinish = function() {
// Let other players know this player's presence
let character = params().character || "SWAT";
gametime.run("join", [gametime.player.position, gametime.player.uuid, "SWAT", sandbox.username, character]);
gametime.run("join", [gametime.player.position, gametime.player.uuid, character, sandbox.username]);
};

window.LoadingManager.oncountdown = function() {
Expand Down Expand Up @@ -4966,6 +5000,8 @@
TouchControls.speed = user.preferences.game.touchSpeed || 2;
GamepadControls.speed = user.preferences.game.gamepadSpeed || 1;
screenControls = [PointerControls, TouchControls];
PointerControls.minPolarAngle = Math.PI / 2 - THREE.MathUtils.degToRad(80);
PointerControls.maxPolarAngle = Math.PI / 2 + THREE.MathUtils.degToRad(80);
// screenControls.forEach(controls => controls.isLocked = true);

// Here, we'll set up a touch screen UI
Expand Down

0 comments on commit 6a86745

Please sign in to comment.