Skip to content

Commit

Permalink
Merge pull request #122 from HyeokjinKang/socket.io
Browse files Browse the repository at this point in the history
feat: socket.io
  • Loading branch information
HyeokjinKang authored Nov 21, 2023
2 parents b1afe77 + 3e36659 commit d698a80
Show file tree
Hide file tree
Showing 20 changed files with 331 additions and 29 deletions.
1 change: 1 addition & 0 deletions config/config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"url": "https://example.com",
"api": "https://api.example.com",
"cdn": "https://cdn.example.com",
"game": "https://game.example.com",
"secretKey": "Something Random",
"mode": "production",
"port": 3000
Expand Down
12 changes: 10 additions & 2 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
"playtime": "Playtime",
"number_of_1th": "1st",
"last_played": "Last Played",
"alias": "Newbie,Geoplex Fan,Greenstar Fan,mkhito Fan,Neko Hacker Fan,fusq Fan,Yunomi Fan,Synthion Fan,Kunaru Fan,Sakuzyo Fan,MYUKKE. Fan,Author wind Fan,HyuN Fan",
"alias": "Newbie,Curse of 1,The taste of AP,The taste of FC,Talented,EZPZ,MID diff,Master",
"recent": "Recent Played",
"nothing_here": "Nothing here I think ;-;",
"bestplay": "Best Play",
Expand All @@ -235,5 +235,13 @@
"rank_subtitle": "랭킹",
"profile": "Profile",
"banner": "Display",
"image_error": "Image upload failed. Please make sure the image is less than 1MB."
"socket_connection_conflict": "Connection has been disconnected because you logged in from another device. Please log in again.",
"socket_connection_error": "Connection has been disconnected due to an error. Please log in again.",
"socket_connection_unauthorized": "Your login has been revoked and the connection has been disconnected. Please log in again.",
"socket_status_connected": "You are connected to the server.",
"socket_status_reconnecting": "Trying to reconnect..",
"alias_select": "Select alias",
"picture_message": "Select what you want to change.",
"socket_achievement_reward": "Reward",
"socket_achievement_alias": "Alias"
}
13 changes: 11 additions & 2 deletions locales/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
"playtime": "플레이 횟수",
"number_of_1th": "1등을 기록한 횟수",
"last_played": "마지막 플레이",
"alias": "뉴비,Geoplex 팬,Greenstar 팬,mkhito 팬,Neko Hacker 팬,fusq 팬,Yunomi 팬,Synthion 팬,Kunaru 팬,Sakuzyo 팬,MYUKKE. 팬,Author wind 팬,HyuN 팬",
"alias": "뉴비,1의 저주,AP의 맛,FC의 맛,고수?,EZPZ,미드차이,고수",
"recent": "최근 플레이",
"nothing_here": "여긴.. 아무것도 없네요 ;-;",
"bestplay": "최고 기록",
Expand All @@ -219,5 +219,14 @@
"rank_subtitle": "RANKING",
"profile": "프로필",
"banner": "장식장",
"image_error": "이미지 업로드에 실패하였습니다. 이미지 용량이 1MB 이하인지 확인해주세요."
"image_error": "이미지 업로드에 실패하였습니다. 이미지 용량이 1MB 이하인지 확인해주세요.",
"socket_connection_conflict": "다른 기기에서 접속하여 연결이 끊어졌습니다. 다시 로그인해주세요.",
"socket_connection_error": "에러가 발생하여 연결이 끊어졌습니다. 다시 로그인해주세요.",
"socket_connection_unauthorized": "로그인이 해제되어 연결이 끊어졌습니다. 다시 로그인해주세요.",
"socket_status_connected": "게임 서버에 다시 연결되었습니다.",
"socket_status_reconnecting": "게임 서버에 다시 연결 시도 중..",
"alias_select": "칭호를 선택하세요.",
"picture_message": "바꾸고 싶은 항목을 선택하세요.",
"socket_achievement_reward": "보상",
"socket_achievement_alias": "칭호"
}
2 changes: 1 addition & 1 deletion public/css/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ body {
padding: 0;
text-align: center;
font-family: "Pretendard JP Variable", "나눔바른고딕", arial;
letter-spacing: -0.5px;
letter-spacing: -0.3px;
overflow-x: hidden;
overflow-y: hidden;
-webkit-font-smoothing: antialiased;
Expand Down
2 changes: 1 addition & 1 deletion public/css/game.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ body {
padding: 0;
text-align: center;
font-family: "Pretendard JP Variable", "나눔바른고딕", arial;
letter-spacing: -0.5px;
letter-spacing: -0.3px;
overflow-x: hidden;
overflow-y: hidden;
}
Expand Down
119 changes: 119 additions & 0 deletions public/css/globalSocket.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
.achievementOverlay {
letter-spacing: 0px;
z-index: 999;
position: fixed;
width: 100vw;
height: 10vh;
left: 0;
bottom: 0;
pointer-events: none;
display: flex;
align-items: center;
justify-content: center;
animation: achievementOverlay 4s cubic-bezier(0.75, 0, 0.25, 1) forwards;
transition-duration: 0.8s;
transition-timing-function: cubic-bezier(0.65, 0, 0.35, 1);
}

@keyframes achievementOverlay {
0% {
opacity: 0;
transform: translateY(10vh);
}
20% {
opacity: 1;
transform: translateY(0vh);
}
80% {
opacity: 1;
}
100% {
opacity: 0;
}
}

.achievementInner {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
padding: 0vh 1.3vw;
background-color: rgba(0, 0, 0, 0.7);
border-radius: 0.7vh;
height: 6vh;
box-shadow: 0 0.5vh 2vh 1vh rgba(0, 0, 0, 0.3);
}

.achievementInner::before {
content: "";
width: auto;
height: auto;
filter: blur(4px);
}

.achievementInnerLeft {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}

.achievementMedal {
display: flex;
align-items: center;
justify-content: center;
border: 1px solid white;
border-radius: 100vw;
padding: 2px 0.4vw;
margin-right: 0.7vw;
}

.achievementMedal > img {
height: 1.5vh;
}

.achievementContentVertical {
color: white;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: center;
}

.achievementContentTitle {
font-size: 1.4vh;
font-weight: 600;
}

.achievementContentDesc {
margin-top: 0.2vh;
font-size: 1.1vh;
font-weight: 300;
}

.achievementLine {
width: 1px;
height: 2vh;
background-color: white;
margin-left: 2vw;
margin-right: 0.7vw;
}

.achievementInnerRight {
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

.achievementRewardTitle {
font-size: 1vh;
font-weight: 300;
}

.achievementRewardDesc {
margin-top: 0.4vh;
font-size: 1.1vh;
font-weight: 500;
}
2 changes: 1 addition & 1 deletion public/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ body {
padding: 0;
text-align: center;
font-family: "Pretendard JP Variable", "나눔바른고딕", arial;
letter-spacing: -0.5px;
letter-spacing: -0.3px;
overflow: hidden;
user-select: none;
}
Expand Down
2 changes: 1 addition & 1 deletion public/css/info.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ body {
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
letter-spacing: -0.5px;
letter-spacing: -0.3px;
font-family: "Montserrat", "Pretendard JP Variable", system-ui, -apple-system, arial;
background-color: white;
margin: 3vh 3vw;
Expand Down
2 changes: 1 addition & 1 deletion public/css/join.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ body {
padding: 0;
background-color: #eee;
font-family: "Pretendard JP Variable", "나눔바른고딕", arial;
letter-spacing: -0.5px;
letter-spacing: -0.3px;
display: flex;
flex-direction: column;
align-items: center;
Expand Down
2 changes: 1 addition & 1 deletion public/css/play.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ body {
padding: 0;
text-align: center;
font-family: "Montserrat", "Pretendard JP Variable", "나눔바른고딕", arial;
letter-spacing: -0.5px;
letter-spacing: -0.3px;
overflow-x: hidden;
overflow-y: hidden;
-webkit-font-smoothing: antialiased;
Expand Down
1 change: 1 addition & 0 deletions public/images/parts/icons/medal-solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 2 additions & 12 deletions public/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const warningContainer = document.getElementById("warningContainer");
const intro1container = document.getElementById("intro1container");
const warningInner = document.getElementById("warningInner");
const langSelector = document.getElementById("langSelector");
const registerBtn = document.getElementById("registerBtn");
const selectSongContainer = document.getElementById("selectSongContainer");
const trackModsText = document.getElementById("trackModsText");
const selectTitle = document.getElementById("selectTitle");
Expand All @@ -25,15 +24,6 @@ const overlayCodeContainer = document.getElementById("overlayCodeContainer");
const overlayLoadingContainer = document.getElementById("overlayLoadingContainer");
const loadingCircle = document.getElementById("loadingCircle");
const loadingOverlayCircle = document.getElementById("loadingOverlayCircle");
const DLCinfoDLCName = document.getElementById("DLCinfoDLCName");
const DLCinfoArtistName = document.getElementById("DLCinfoArtistName");
const DLCbasketButton = document.getElementById("DLCbasketButton");
const DLCinfoSongsContainer = document.getElementById("DLCinfoSongsContainer");
const SkinInfoSkinName = document.getElementById("SkininfoSkinName");
const skinInfoPreview = document.getElementById("skinInfoPreview");
const skinBasketButton = document.getElementById("skinBasketButton");
const basketsButtonContainer = document.getElementById("basketsButtonContainer");
const purchasingContainer = document.getElementById("purchasingContainer");
const goldMedal = document.getElementById("goldMedal");
const silverMedal = document.getElementById("silverMedal");
const checkMedal = document.getElementById("checkMedal");
Expand Down Expand Up @@ -1937,7 +1927,7 @@ const changeProfile = (e) => {
id: "inputs",
zindex: 999,
timeout: 20000,
title: "Select alias",
title: aliasSelect,
progressBarColor: "#999",
message: "",
position: "center",
Expand Down Expand Up @@ -1994,7 +1984,7 @@ const changeProfile = (e) => {
timeout: 20000,
zindex: 999,
theme: "dark",
title: "What do you want to change?",
title: pictureMessage,
position: "center",
progressBarColor: "#999",
buttons: [
Expand Down
115 changes: 115 additions & 0 deletions public/js/globalSocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const body = document.querySelector("body");
let isErrorOccured = false;
let achievementCount = 0;
const socket = io(game, {
withCredentials: true,
});

document.addEventListener("DOMContentLoaded", () => {
const img = new Image();
img.src = "/images/parts/icons/medal-solid.svg";
const div = document.createElement("div");
div.id = "achievementsContainer";
body.appendChild(div);
});

socket.on("connect", () => {
console.log("[Socket.IO] Connected to server");
if (isErrorOccured) {
iziToast.success({
title: "Connected",
message: socketi18n.connected,
timeout: 2000,
layout: 2,
transitionIn: "fadeInLeft",
});
}
});

socket.on("disconnect", (reason) => {
if (reason === "io server disconnect") {
alert(socketi18n.error);
window.location.href = `${api}/auth/logout?redirect=true`; // Session error, need to relogin
} else if (reason === "io client disconnect") return;
console.error("[Socket.IO] Disconnected from server");
iziToast.warning({
title: "Disconnected",
message: socketi18n.reconnecting,
timeout: 2000,
layout: 2,
transitionIn: "fadeInLeft",
});
isErrorOccured = true;
});

socket.on("connect_error", () => {
console.error("[Socket.IO] Cannot connect to server");
iziToast.error({
title: "Connection error",
message: socketi18n.reconnecting,
timeout: 2000,
layout: 2,
transitionIn: "fadeInLeft",
});
setTimeout(() => {
socket.connect();
}, 1000);
isErrorOccured = true;
});

socket.on("connection:conflict", () => {
socket.disconnect();
alert(socketi18n.conflict);
window.location.href = `${api}/auth/logout?redirect=true`;
});

socket.on("connection:unauthorized", () => {
socket.disconnect();
alert(socketi18n.unauthorized);
window.location.href = `${api}/auth/logout?redirect=true`;
});

socket.on("achievement", (data) => {
data = JSON.parse(data);
console.log("[Socket.IO] Achievement");
console.log(data);
data.rewards.forEach((reward) => {
[...document.getElementsByClassName("achievementOverlay")].forEach((element, i) => {
element.style.bottom = `${7 * (i + 1)}vh`;
});
const div = document.createElement("div");
div.classList.add("achievementOverlay");
document.getElementById("achievementsContainer").appendChild(div);
document.getElementsByClassName("achievementOverlay")[achievementCount].innerHTML = `
<div class="achievementInner">
<div class="achievementInnerLeft">
<div class="achievementMedal">
<img src="/images/parts/icons/medal-solid.svg" />
</div>
<div class="achievementContentVertical">
<span class="achievementContentTitle">${data[`title_${lang}`]}</span>
<span class="achievementContentDesc">${data[`detail_${lang}`]}</span>
</div>
</div>
<div class="achievementLine"></div>
<div class="achievementInnerRight">
<span class="achievementRewardTitle">${socketi18n[reward[0]]}</span>
<span class="achievementRewardDesc">${getReward(reward[0], reward[1])}</span>
</div>
</div>`;
achievementCount++;
setTimeout(() => {
achievementCount--;
document.getElementsByClassName("achievementOverlay")[0].remove();
}, 4000);
});
});

const getReward = (type, id) => {
if (type == "reward") return "-";
if (type == "alias") return alias[id];
};

const ping = setInterval(() => {
socket.emit("ping");
}, 5000);
Loading

0 comments on commit d698a80

Please sign in to comment.