Lobby ์ฌ์ฉ
Use Cloud Code and Lobby service together to trigger server-side actions when players join lobbies.
์ฝ๋ ์๊ฐ 2๋ถ์ต๊ทผ ์
๋ฐ์ดํธ: 12์๊ฐ ์
๊ฒ์์์ Lobby์ Cloud Code๋ฅผ ํจ๊ป ์ฌ์ฉํ์ฌ ์ผ๋ฐ์ ์ธ ํธ๋ฆฌ๊ฑฐ๋ฅผ ์ ์ํ ์ ์์ต๋๋ค. ๊ฒ์ ๋ก๋น๊ฐ ๋งค์น๋ฅผ ์์ํ ์ค๋น๊ฐ ๋ ์ํฉ์ด๋ผ๊ณ ๊ฐ์ ํฉ๋๋ค. ์ค๋น๋ ๊ฒ์ ๋ก๋น์ ๋ํ ์๋ต์ผ๋ก ๋ค์ ์คํฌ๋ฆฝํธ๋ฅผ ํธ๋ฆฌ๊ฑฐํ ์ ์์ต๋๋ค. ์ด ์์์์๋ Remote Config์์ ์ฐ๋๋ ํจ๊ป ๋ค๋ฃน๋๋ค. ์ด ์์๋ฅผ ๋ฐ๋ฅด๊ธฐ ์ ์ Unity Cloud Dashboard๋ฅผ ํตํด Lobby์ Cloud Code ๋ชจ๋๋ฅผ ํ์ฑํํ๊ณ ๋จ๊ณ์ ๋ฐ๋ผ ํ์ SDK๋ฅผ ์ค์นํ์๊ธฐ ๋ฐ๋๋๋ค. ์๋น์ค ์ธ์ฆ์ ์ฌ์ฉํ์ฌ ํ๋ก์ ํธ์์ ๋ก๋น๋ฅผ ์์ฑํ๊ณ ID๋ฅผ ๊ธฐ๋กํด ๋ก๋๋ค. Cloud Code๋ฅผ ํตํด ์ด๋ฅผ ์ํํ ์ ์์ต๋๋ค(Unity Lobby ์์ ํ์ธ). ๋ค์ ์์๋ ๋ก๋น ID, ๊ทธ๋ฆฌ๊ณ ๋ก๋น ์์ฑ์ ์ฌ์ฉ๋๋ ์๋น์ค ID๋ผ๋ ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์คํฌ๋ฆฝํธ์ ์ ๊ณตํฉ๋๋ค. ๋ก๋น๋ฅผ ๋ง๋ ๋ค์ ๋ฉํ๋ฐ์ดํฐ์ ์ฌ๋ฌ ๋ณ๊ฒฝ ์ฌํญ์ ์ ์ฉํ์ฌ ์ ๋ฐ์ดํธํฉ๋๋ค. ์ด๋ฌํ ๋ณ๊ฒฝ ์ฌํญ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ์ ๊ท ํ๋ ์ด์ด๊ฐ ๋ ์ด์ ์ฐธ์ฌํ ์ ์๋๋ก ๋ก๋น๋ฅผ ์ ๊ธ๋๋ค.
- ๋ก๋น์ ํ๋ ์ด์ด ID๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์์ ๋ฌด์์ โํ๋ ์ด์ด ์์โ๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.
- Remote Config์ ์ ์๋ ์ค์ ์ ์ฌ์ฉํ์ฌ ๊ฒ์์ ๋งต์ ๋ฌด์์๋ก ์ ํํฉ๋๋ค. ์คํฌ๋ฆฝํธ๊ฐ ์ฑ๊ณต ๋๋ ์คํจ๋ฅผ ๋ํ๋ด๋ ๊ธฐ๋ณธ ๋ถ์ธ์ ๋ฐํํฉ๋๋ค.
LOBBY_CURRENT_MAPS
JavaScript
const _ = require("lodash-4.17");const { LobbyApi, DataObjectVisibilityEnum } = require("@unity-services/lobby-1.2");const { SettingsApi } = require("@unity-services/remote-config-1.1");module.exports = async ({params,context,logger}) => { const { serviceId, lobbyId } = params; const { projectId, environmentId, playerId } = context; const lobbyApi = new LobbyApi(context); const remoteConfig = new SettingsApi(context); try { const {data: lobby} = await lobbyApi.getLobby(lobbyId, serviceId); return setUpGame(remoteConfig, projectId, environmentId, serviceId, lobbyApi, logger, lobby); } catch (err) { logger.error(`Failed to set up lobby: ${err.response.data.detail}`); return false; }};// Updates a lobby to set up for the start of a game, including reading a value from Remote Config to randomly assign to the lobby.async function setUpGame(remoteConfig, projectId, environmentId, serviceId, lobbyApi, logger, lobby) { let playerIds = []; for (let i = 0; i < lobby.players.length; i++) { playerIds.push(lobby.players[i].id); } // Generate a turn order for the game by shuffling the player IDs. _.shuffle(playerIds); // Load all of the maps that are currently configured as available in Remote Config and pick a random one for this game. let maps = getCurrentMapsFromRemoteConfig(remoteConfig, projectId, environmentId) if (maps == null) { throw "Maps loaded from Remote Config are null."; } let gameMap = _.sample(maps); try { const updateResponse = await lobbyApi.updateLobby( lobby.id, serviceId, null, { isLocked: true, // Lock the lobby so that new players cannot join. hostId: playerIds[0], // Transfer host ownership from the service to one of the players. data: { "playerOrder": { value: playerIds.toString(), visibility: DataObjectVisibilityEnum.Member // Set the visibility of the player order so that only lobby members can access it. }, "map": { value: gameMap, visibility: DataObjectVisibilityEnum.Public // Set the visibility of the game map so that anyone who views the lobby can access it. } } } ); return true; } catch (err) { throw err; } return lobbyUpdateSuccess;}// Loads the Remote Config JSON value for LOBBY_CURRENT_MAPS and returns the array of maps inside of it.// If the JSON is invalid, the return value is null.async function getCurrentMapsFromRemoteConfig(remoteConfig, projectId, environmentId) { const currentMapsId = 'LOBBY_CURRENT_MAPS'; const remoteConfigResponse = await remoteConfig.assignSettingsGet( projectId, environmentId, 'settings', [currentMapsId] ); let mapsJSON = remoteConfigResponse?.data?.configs?.settings?.[currentMapsId]; if (!mapsJSON || !mapsJSON.maps || mapsJSON.maps.length == 0) { return null; } return mapsJSON.maps;}// Uncomment the code below to enable the inline parameter definition// - Requires Cloud Code JS dev environment setup with NodeJS (https://docs.unity3d.com/Packages/com.unity.services.cloudcode@latest/index.html?subfolder=/manual/Authoring/javascript_project.html)//// module.exports.params = {// serviceId: { type: "String", required: true },// lobbyId: { type: "String", required: true },// };
์ด Remote Config JSON์ ๊ตฌ์กฐ๋ ๋จ์ํ์ง๋ง ์ด ์์ ๊ตฌ์ฑ์ ํ์ฅํ์ฌ ๊ฒ์์ ๋ ์ธ๋ฐํ๊ฒ ์ ์ดํ ์ ์์ต๋๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก ๊ฒ์ ์ค์ ์ธ์ Cloud Code์์ ๋ค์ํ ํธ๋ฆฌ๊ฑฐ๋ฅผ ๊ณ ๋ คํ ์ ์์ผ๋ฉฐ, ๊ทธ ์์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ๋ก๋น ์์ฑ์ ๋ฐ๋ผ ํธ๋ฆฌ๊ฑฐํ์ฌ ๋ฉํ๋ฐ์ดํฐ ๊ฐ์ ์กฐ์ ํ๊ณ ๋ฌธ์์ด์ ๋ค๋ฌ์ต๋๋ค.
- Cloud Save ํ๋ ์ด์ด ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ ์ด์ด์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ํํฐ๋งํ๊ณ , ์ฐธ์ฌ๋ฅผ ์น์ธ/๊ฑฐ๋ถํ๊ณ , ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ค์ ํฉ๋๋ค.
- Economy ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ ์ด์ด ๋๋ ๋ก๋น ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ค์ ํฉ๋๋ค.