-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0013f26
commit b3508bc
Showing
11 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "template", | ||
"version": "1.0.0", | ||
"data": [ | ||
{ "id": 101, "stage_id": 1001, "item_id": [1] }, | ||
{ "id": 201, "stage_id": 1002, "item_id": [1, 2] } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import express from 'express'; | ||
import { createServer } from 'http'; | ||
import initSocket from './init/socket.js'; | ||
import { loadGameAssets } from './init/assets.js'; | ||
|
||
const app = express(); | ||
const server = createServer(app); | ||
|
||
const PORT = 3000; | ||
|
||
app.use(express.static('tower_defense_client')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
initSocket(server); | ||
|
||
app.get('/', (req, res) => { | ||
res.send('<h1>Hello World</h1>'); | ||
}); | ||
|
||
server.listen(PORT, async () => { | ||
console.log(`서버가 ${PORT}번 포트로 실행 성공했습니다.`); | ||
|
||
try { | ||
const assets = await loadGameAssets(); | ||
console.log(assets); | ||
console.log('성공적으로 에셋을 로드했습니다.'); | ||
} catch (error) { | ||
console.error('에셋 데이터 로드에 실패했습니다:', error); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const CLIENT_VERSION = ['1.0.0', '1.0.1', '1.1.0']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { moveStageHandler } from './stage.handler.js'; | ||
|
||
const handlerMappings = { | ||
11: moveStageHandler, | ||
}; | ||
|
||
export default handlerMappings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { getGameAssets } from '../init/assets.js'; | ||
import { getUsers, removeUser } from '../models/user.model.js'; | ||
|
||
export const handleDisconnect = (socket, uuid) => { | ||
removeUser(socket.id); | ||
console.log(`사용자 접속 해제: ${socket.id}`); | ||
console.log('현재 접속 중인 사용자:', getUsers()); | ||
}; | ||
|
||
export const handleConnection = (socket, userUUID) => { | ||
console.log( | ||
`새로운 사용자가 접속했습니다: \"${socket.id}\"소켓으로 \"${userUUID}\"사용자가 접속했습니다.`, | ||
); | ||
console.log('현재 접속 중인 사용자:', getUsers()); | ||
|
||
const { templates } = getGameAssets(); | ||
|
||
socket.emit('connection', { uuid: userUUID }); | ||
}; | ||
|
||
export const handleEvent = (io, socket, data) => { | ||
if (!CLIENT_VERSION.includes(data.clientVersion)) { | ||
socket.emit('response', { | ||
status: 'fail', | ||
message: '클라이언트 버전이 매치되지 않습니다.', | ||
}); | ||
return; | ||
} | ||
|
||
const handler = handlerMappings[data.handlerId]; | ||
if (!handler) { | ||
socket.emit('response', { | ||
status: 'fail', | ||
message: '핸들러를 찾지 못했습니다.', | ||
}); | ||
return; | ||
} | ||
|
||
const response = handler(data.userId, data.payload); | ||
if (response.broadcast) { | ||
io.emit('response', 'broadcast'); | ||
return; | ||
} | ||
|
||
socket.emit('response', response); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { addUser } from '../models/user.model.js'; | ||
import { handleConnection, handleDisconnect, handleEvent } from './helper.js'; | ||
|
||
const registerHandler = (io) => { | ||
io.on('connection', (socket) => { | ||
const userUUID = uuidv4(); | ||
addUser({ uuid: userUUID, socketId: socket.id }); | ||
|
||
handleConnection(socket, userUUID); | ||
|
||
socket.on('event', (data) => handleEvent(io, socket, data)); | ||
socket.on('disconnect', () => handleDisconnect(socket, userUUID)); | ||
}); | ||
}; | ||
|
||
export default registerHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const basePath = path.join(__dirname, '../../assets'); | ||
let gameAssets = {}; | ||
|
||
const readFileAsync = (filename) => { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(path.join(basePath, filename), 'utf8', (err, data) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(JSON.parse(data)); | ||
}); | ||
}); | ||
}; | ||
|
||
export const loadGameAssets = async () => { | ||
try { | ||
const [templates, items] = await Promise.all([ | ||
readFileAsync('template.json'), | ||
]); | ||
gameAssets = { templates, items }; | ||
return gameAssets; | ||
} catch (error) { | ||
throw new Error('에셋 데이터 로드에 실패했습니다: ' + error.message); | ||
} | ||
}; | ||
|
||
export const getGameAssets = () => { | ||
return gameAssets; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Server as SocketIO } from 'socket.io'; | ||
import registerHandler from '../handlers/register.handler.js'; | ||
|
||
const initSocket = (server) => { | ||
const io = new SocketIO(); | ||
io.attach(server); | ||
|
||
registerHandler(io); | ||
}; | ||
|
||
export default initSocket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const users = []; | ||
|
||
export const addUser = (user) => { | ||
users.push(user); | ||
}; | ||
|
||
export const removeUser = (socketId) => { | ||
const index = users.findIndex((user) => user.socketId === socketId); | ||
if (index !== -1) { | ||
return users.splice(index, 1)[0]; | ||
} | ||
}; | ||
|
||
export const getUsers = () => { | ||
return users; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const CLIENT_VERSION = '1.0.0'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { CLIENT_VERSION } from './constants.js'; | ||
|
||
const socket = io('http://localhost:3000', { | ||
query: { | ||
clientVersion: CLIENT_VERSION, | ||
}, | ||
}); | ||
|
||
let userId = null; | ||
socket.on('response', (data) => { | ||
console.log(data); | ||
}); | ||
|
||
socket.on('connection', (data) => { | ||
console.log('connection: ', data); | ||
userId = data.uuid; | ||
}); | ||
|
||
const sendEvent = (handlerId, payload) => { | ||
socket.emit('event', { | ||
userId, | ||
clientVersion: CLIENT_VERSION, | ||
handlerId, | ||
payload, | ||
}); | ||
}; | ||
|
||
console.log('socket.js'); | ||
|
||
export { sendEvent }; |