Files
roblox-project/bunsik-tycoon/src/server/Data.luau
2026-10-14 01:34:37 +09:00

119 lines
3.7 KiB
Lua

--!strict
-- 세이브 데이터(ProfileStore). 세션 잠금으로 서버 이동 때 덮어쓰기·복사를 막습니다.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ServerScriptService = game:GetService("ServerScriptService")
local ProfileStore = require(ServerScriptService.Vendor.ProfileStore)
local Economy = require(game:GetService("ReplicatedStorage").Shared.Economy)
local Floor = require(game:GetService("ReplicatedStorage").Shared.Floor)
local Data = {}
Data.TEMPLATE = {
Money = 0,
Bought = 0, -- 예전(버전 1) 한 줄 업그레이드 수. 버전 2로 옮긴 뒤에는 쓰지 않음
Levels = {} :: { [string]: number }, -- 업그레이드 갈래 레벨(Economy.TRACKS)
Layout = {} :: any, -- 배치(Floor.Layout). 빈 표면 기본 배치
Staff = {} :: { [string]: number }, -- 고용한 알바 수
ShopName = "", -- 가게 이름 원문("" = 기본 이름)
Rainbow = true, -- 무지개 이름 켜기(게임패스가 있을 때만 보임)
Design = "Basic",
RushLeft = 0, -- 손님 러시 남은 초
KickCredits = 0, -- 산 "손님 쫓아내기" 남은 횟수
PurchaseIdCache = {} :: { string },
TutorialDone = false,
Lang = "", -- "" = 계정 언어 자동
MouseLook = true, -- PC: 우클릭 없이 마우스 이동으로 화면 돌리기
Sfx = true, -- 효과음
Served = 0,
Earned = 0,
Version = 2,
}
export type SaveData = typeof(Data.TEMPLATE)
-- 데이터 구조를 바꾸면 store 이름을 올리지 말고 Version과 Reconcile로 이전합니다.
local STORE_NAME = "PlayerData_v1"
local store = ProfileStore.New(STORE_NAME, Data.TEMPLATE)
-- Studio 테스트는 실제 저장소를 건드리지 않습니다.
local activeStore = if RunService:IsStudio() then store.Mock else store
local profiles: { [Player]: any } = {}
function Data.keyFor(userId: number): string
return "Player_" .. userId
end
-- 플레이어 데이터를 불러옵니다. 실패하거나 그 사이 나가면 nil.
function Data.load(player: Player): any
local profile = activeStore:StartSessionAsync(Data.keyFor(player.UserId), {
Cancel = function()
return player.Parent ~= Players
end,
})
if profile == nil then
return nil
end
profile:AddUserId(player.UserId)
-- Reconcile 전에 버전을 봐야 합니다(빠진 Version은 템플릿 값 2로 채워지므로).
local version = profile.Data.Version
profile:Reconcile()
Data.migrate(profile.Data, if typeof(version) == "number" then version else 1)
profile.OnSessionEnd:Connect(function()
profiles[player] = nil
if player.Parent == Players then
player:Kick(
"세이브 세션이 다른 서버로 넘어갔어요. 다시 접속해 주세요 / Your save session moved. Please rejoin"
)
end
end)
if player.Parent ~= Players then
profile:EndSession()
return nil
end
profiles[player] = profile
return profile
end
-- 버전 1(한 줄 업그레이드 Bought) -> 버전 2(갈래 레벨, 배치, 알바생)
function Data.migrate(data: any, version: number)
if version >= 2 then
return
end
local levels, seats, tables, cooks = Economy.migrate(data.Bought or 0)
data.Levels = levels
data.Staff = { Cook = cooks }
local stage = Economy.stats(levels, seats).stage
local layout = Floor.default()
for _ = 2, tables do
Floor.addTable(layout, stage)
end
for _ = #layout.chairs + 1, seats do
Floor.addChair(layout, stage)
end
data.Layout = layout
data.Version = 2
end
function Data.get(player: Player): any
return profiles[player]
end
function Data.release(player: Player)
local profile = profiles[player]
profiles[player] = nil
if profile then
profile:EndSession()
end
end
-- 테스트용: 실제 저장소 대신 Mock 저장소를 씁니다.
function Data.useMock()
activeStore = store.Mock
end
return Data