--!strict -- 가게 한 곳의 규칙(손님, 주문, 조리, 서빙, 돈, 업그레이드, 알바생, 시급)을 담은 순수 로직입니다. -- 로블록스 API를 쓰지 않아서 lune 단위 테스트와 시뮬레이터에서 그대로 돌립니다. -- 화면(NPC, 접시 모형, 효과)은 서버의 PlotController가 이벤트를 받아 그립니다. -- 테이블·의자 위치는 모릅니다. 자리 수(seats)와 자리별 걷는 시간(seatWalk)만 압니다. -- 로블록스에서는 script.Parent, lune(테스트)에서는 파일 경로로 불러옵니다. local Economy: typeof(require(script.Parent.Economy)) = if script then require(script.Parent.Economy) else (require :: any)("./Economy") type Stats = Economy.Stats type Levels = Economy.Levels export type CustomerState = "walking" | "waiting" | "eating" export type Customer = { id: number, seat: number, menu: string, state: CustomerState, untilT: number, -- walking/eating이 끝나는 시각. 플레이어 손님은 기다리다 일어나는 시각 guest: number?, -- 다른 플레이어가 손님으로 왔으면 그 UserId transfer: boolean?, -- 옆 가게에서 데려온 손님 } -- 서빙 알바가 나르는 중인 접시 export type Trip = { staff: number, -- 서빙 알바 번호(1부터) id: number, -- 손님 id menu: string, doneT: number, } export type Event = { kind: "spawn", id: number, seat: number, menu: string, transfer: boolean? } | { kind: "seated", id: number } | { kind: "leave", id: number, seat: number, guest: number?, served: boolean } | { kind: "counter", menu: string, staff: number } | { kind: "deliver", id: number, menu: string, staff: number, index: number } | { kind: "delivered", id: number, staff: number, earn: number } | { kind: "returned", menu: string, staff: number } | { kind: "wage", amount: number } | { kind: "strike", on: boolean } export type Shop = { money: number, levels: Levels, stats: Stats, moneyMult: number, customers: { [number]: Customer }, seatTaken: { [number]: number }, seatWalk: { [number]: number }, -- 자리별 입구 -> 자리 걷는 시간 hand: { string }, counter: { string }, trips: { [number]: Trip }, -- 서빙 알바 번호 -> 나르는 중 staff: Economy.Staff, strike: boolean, -- 시급이 모자라 알바생이 쉬는 중 wageDue: number, -- 다음 시급 주는 시각 cookNext: { [number]: number }, -- 요리 알바 번호 -> 다음에 만들 수 있는 시각 rushLeft: number, -- 손님 러시 남은 초 now: number, nextSpawn: number, menuIndex: number, lastId: number, served: number, earned: number, } export type Options = { money: number, levels: Levels?, seats: number?, staff: Economy.Staff?, now: number, rushLeft: number?, } local Shop = {} function Shop.new(opts: Options): Shop local levels = Economy.cleanLevels(opts.levels) local seats = math.max(1, opts.seats or 1) local staff: Economy.Staff = {} for _, r in Economy.ROLES do local n = if opts.staff then opts.staff[r.id] else nil staff[r.id] = if typeof(n) == "number" then math.clamp(math.floor(n), 0, r.max) else 0 end return { money = opts.money, levels = levels, stats = Economy.stats(levels, seats), moneyMult = 1, customers = {}, seatTaken = {}, seatWalk = {}, hand = {}, counter = {}, trips = {}, staff = staff, strike = false, wageDue = opts.now + Economy.WAGE_PERIOD, cookNext = {}, rushLeft = math.max(0, opts.rushLeft or 0), now = opts.now, nextSpawn = opts.now, -- 접속하자마자 첫 손님 menuIndex = 0, lastId = 0, served = 0, earned = 0, } end local function staffWorking(self: Shop, role: string): number if self.strike or not self.stats.staffUnlocked then return 0 end return self.staff[role] or 0 end function Shop.freeSeat(self: Shop): number? for seat = 1, self.stats.seats do if self.seatTaken[seat] == nil then return seat end end return nil end function Shop.freeSeatCount(self: Shop): number local n = 0 for seat = 1, self.stats.seats do if self.seatTaken[seat] == nil then n += 1 end end return n end -- 손에 든 접시·카운터·나르는 중인 접시 중 기다리는 손님이 없는 메뉴(주인 없는 접시). -- 손님이 옆 가게로 가 버려서 남은 접시입니다. 다음 손님이 이 메뉴를 주문하게 해서 접시가 버려지지 않게 합니다. local function orphanMenu(self: Shop): string? local have: { [string]: number } = {} for _, m in self.hand do have[m] = (have[m] or 0) + 1 end for _, m in self.counter do have[m] = (have[m] or 0) + 1 end for _, t in self.trips do have[t.menu] = (have[t.menu] or 0) + 1 end for _, c in self.customers do if c.state ~= "eating" and have[c.menu] then have[c.menu] -= 1 end end for _, m in Economy.MENU_ORDER do if (have[m] or 0) > 0 and table.find(self.stats.menus, m) then return m end end return nil end -- 다음 손님이 주문할 메뉴(주인 없는 접시가 있으면 그 메뉴, 아니면 해금된 메뉴를 돌아가며) local function nextOrder(self: Shop): string local orphan = orphanMenu(self) if orphan then return orphan end local menus = self.stats.menus local menu = menus[(self.menuIndex % #menus) + 1] self.menuIndex += 1 return menu end -- 아직 접시가 정해지지 않은 주문(먼저 온 손님부터). 걷는 중인 손님도 주문은 이미 정해져 있습니다. function Shop.openOrders(self: Shop): { Customer } local covered: { [string]: number } = {} for _, m in self.hand do covered[m] = (covered[m] or 0) + 1 end for _, m in self.counter do covered[m] = (covered[m] or 0) + 1 end local tripFor: { [number]: boolean } = {} for _, t in self.trips do tripFor[t.id] = true end local list = {} for _, c in self.customers do if c.state ~= "eating" and not tripFor[c.id] then table.insert(list, c) end end table.sort(list, function(a, b) return a.id < b.id end) local open = {} for _, c in list do local n = covered[c.menu] or 0 if n > 0 then covered[c.menu] = n - 1 else table.insert(open, c) end end return open end -- 지금 조리대에서 만들 메뉴(없으면 nil) function Shop.nextCookMenu(self: Shop): string? local open = Shop.openOrders(self) return if open[1] then open[1].menu else nil end -- 메뉴별로 아직 접시가 정해지지 않은 주문 수(요리 화면의 "주문" 표시) function Shop.openCounts(self: Shop): { [string]: number } local counts = {} for _, c in Shop.openOrders(self) do counts[c.menu] = (counts[c.menu] or 0) + 1 end return counts end function Shop.handFull(self: Shop): boolean return #self.hand >= self.stats.carry end function Shop.spawnInterval(self: Shop): number return Economy.spawnInterval(self.stats, staffWorking(self, "Promoter"), self.rushLeft > 0) end local function addCustomer(self: Shop, seat: number, menu: string, walk: number, transfer: boolean?): Customer self.lastId += 1 local c: Customer = { id = self.lastId, seat = seat, menu = menu, state = "walking", untilT = self.now + walk, transfer = transfer, } self.customers[c.id] = c self.seatTaken[seat] = c.id return c end -- 손님 자리까지 서빙 알바가 가는 시간 local function tripTime(self: Shop, seat: number): number local walk = self.seatWalk[seat] or Economy.WALK_IN return math.max(1.5, Economy.SERVER_TRIP * walk / Economy.WALK_IN) end local function payWages(self: Shop, now: number, events: { Event }) if now < self.wageDue then return end local total = Economy.totalWage(self.staff, self.stats) if total <= 0 or not self.stats.staffUnlocked then self.wageDue = now + Economy.WAGE_PERIOD if self.strike then self.strike = false table.insert(events, { kind = "strike", on = false } :: Event) end return end if self.money >= total then self.money -= total self.wageDue = now + Economy.WAGE_PERIOD table.insert(events, { kind = "wage", amount = total } :: Event) if self.strike then self.strike = false table.insert(events, { kind = "strike", on = false } :: Event) end else self.wageDue = now + Economy.STRIKE_RETRY if not self.strike then self.strike = true table.insert(events, { kind = "strike", on = true } :: Event) end end end -- 시간을 now까지 진행합니다. 일어난 일을 이벤트로 돌려줍니다. function Shop.tick(self: Shop, now: number): { Event } local dt = math.max(0, now - self.now) self.now = now local events: { Event } = {} if self.rushLeft > 0 then self.rushLeft = math.max(0, self.rushLeft - dt) end payWages(self, now, events) for id, c in self.customers do if c.state == "walking" and now >= c.untilT then c.state = "waiting" table.insert(events, { kind = "seated", id = id } :: Event) elseif c.state ~= "walking" and now >= c.untilT and (c.state == "eating" or c.guest ~= nil) then -- 다 먹었거나, 플레이어 손님이 너무 오래 기다렸으면 나갑니다. self.customers[id] = nil self.seatTaken[c.seat] = nil table.insert( events, { kind = "leave", id = id, seat = c.seat, guest = c.guest, served = c.state == "eating" } :: Event ) end end if now >= self.nextSpawn then local seat = Shop.freeSeat(self) if seat then local menu = nextOrder(self) local c = addCustomer(self, seat, menu, self.seatWalk[seat] or Economy.WALK_IN) table.insert(events, { kind = "spawn", id = c.id, seat = seat, menu = menu } :: Event) end self.nextSpawn = now + Shop.spawnInterval(self) end -- 요리 알바: 한 명마다 조리 수준(초)에 한 접시씩 카운터에 만들어 둡니다. local cooks = staffWorking(self, "Cook") for i = 1, cooks do if now >= (self.cookNext[i] or 0) and #self.counter < Economy.COUNTER_MAX * cooks then local menu = Shop.nextCookMenu(self) if menu then table.insert(self.counter, menu) table.insert(events, { kind = "counter", menu = menu, staff = i } :: Event) self.cookNext[i] = now + self.stats.cook end end end -- 서빙 알바: 다 나른 접시는 손님에게 놓고, 빈 손이면 카운터 접시를 기다리는 손님에게 가져갑니다. for i, t in self.trips do if now >= t.doneT then self.trips[i] = nil local c = self.customers[t.id] if c and c.state == "waiting" then c.state = "eating" c.untilT = now + Economy.EAT local earn = Shop.pay(self, c) table.insert(events, { kind = "delivered", id = c.id, staff = i, earn = earn } :: Event) else table.insert(self.counter, t.menu) table.insert(events, { kind = "returned", menu = t.menu, staff = i } :: Event) end end end local servers = staffWorking(self, "Server") for i = 1, servers do if not self.trips[i] and #self.counter > 0 then local busy: { [number]: boolean } = {} for _, t in self.trips do busy[t.id] = true end local best: Customer? = nil local bestIdx = 0 for _, c in self.customers do if c.state == "waiting" and not busy[c.id] and (not best or c.id < best.id) then local idx = table.find(self.counter, c.menu) if idx then best = c bestIdx = idx end end end if best then table.remove(self.counter, bestIdx) self.trips[i] = { staff = i, id = best.id, menu = best.menu, doneT = now + tripTime(self, best.seat) } table.insert( events, { kind = "deliver", id = best.id, menu = best.menu, staff = i, index = bestIdx } :: Event ) end end end return events end -- 손님 한 명 값(접시 값, 플레이어 손님이면 2배)을 받습니다. function Shop.pay(self: Shop, c: Customer): number local earn = Economy.dishPrice(c.menu, self.stats, self.moneyMult) if c.guest then earn *= Economy.GUEST_MULT end self.money += earn self.earned += earn self.served += 1 return earn end -- 플레이어가 요리 화면에서 다 만들었을 때. 만든 메뉴를 돌려줍니다. -- menu를 주면 그 메뉴를 기다리는 주문이 있을 때만 만들고, 없으면 먼저 온 손님 메뉴를 만듭니다. function Shop.cook(self: Shop, menu: string?): string? if Shop.handFull(self) then return nil end if menu then if not Shop.openCounts(self)[menu] then return nil end else menu = Shop.nextCookMenu(self) end if menu then table.insert(self.hand, menu) end return menu end -- 요리 알바가 만들어 둔 접시를 손에 듭니다. 든 접시 수를 돌려줍니다. function Shop.pickup(self: Shop): number local n = 0 while #self.counter > 0 and not Shop.handFull(self) do table.insert(self.hand, table.remove(self.counter, 1) :: string) n += 1 end return n end -- 손에 든 접시를 손님에게 줍니다. 번 돈을 돌려줍니다(못 주면 nil). function Shop.serve(self: Shop, id: number): number? local c = self.customers[id] if not c or c.state ~= "waiting" then return nil end for _, t in self.trips do if t.id == id then return nil -- 서빙 알바가 가져가는 중 end end local idx = table.find(self.hand, c.menu) if not idx then return nil end table.remove(self.hand, idx) c.state = "eating" c.untilT = self.now + Economy.EAT return Shop.pay(self, c) end -- 돈을 씁니다. 모자라면 false function Shop.spend(self: Shop, cost: number): boolean if cost < 0 or self.money < cost then return false end self.money -= cost return true end local function refreshStats(self: Shop) local before = self.stats self.stats = Economy.stats(self.levels, before.seats) if self.stats.spawn < before.spawn then self.nextSpawn = math.min(self.nextSpawn, self.now + Shop.spawnInterval(self)) end end -- 업그레이드 갈래의 다음 단계를 삽니다. 성공하면 산 단계(Level)를 돌려줍니다. -- 실패 이유: "max"(다 삼), "stage"(건물 단계 부족), "money" function Shop.buyLevel(self: Shop, trackId: string): (Economy.Level?, string?) local nl = Economy.nextLevel(self.levels, trackId) if not nl then return nil, "max" end if nl.stage > self.stats.stage then return nil, "stage" end if not Shop.spend(self, nl.cost) then return nil, "money" end self.levels[trackId] += 1 refreshStats(self) if trackId == "Menu" then -- 새 메뉴를 사면 다음 손님이 바로 그 메뉴를 주문해서, 산 효과가 곧바로 보이게 합니다. self.menuIndex = #self.stats.menus - 1 self.nextSpawn = math.min(self.nextSpawn, self.now + 1) end return nl, nil end -- 자리 수가 바뀌었을 때(의자를 사거나 놓음) function Shop.setSeats(self: Shop, seats: number) self.stats = Economy.stats(self.levels, seats) self.stats.seats = math.max(1, seats) -- 새 자리가 생기면 손님이 곧 옵니다. self.nextSpawn = math.min(self.nextSpawn, self.now + 1) end -- 알바생 고용·해고. 고용은 고용비를 냅니다. 실패 이유: "locked", "max", "money", "none" function Shop.hire(self: Shop, roleId: string): (boolean, string?) local r = Economy.role(roleId) if not r then return false, "none" end if not self.stats.staffUnlocked then return false, "locked" end if (self.staff[roleId] or 0) >= r.max then return false, "max" end if not Shop.spend(self, r.hire) then return false, "money" end self.staff[roleId] = (self.staff[roleId] or 0) + 1 if self.wageDue < self.now then self.wageDue = self.now + Economy.WAGE_PERIOD end if roleId == "Promoter" then self.nextSpawn = math.min(self.nextSpawn, self.now + Shop.spawnInterval(self)) end return true, nil end function Shop.fire(self: Shop, roleId: string): boolean local n = self.staff[roleId] or 0 if n <= 0 then return false end self.staff[roleId] = n - 1 if roleId == "Server" and self.trips[n] then -- 나르던 접시는 카운터로 돌려놓습니다. table.insert(self.counter, self.trips[n].menu) self.trips[n] = nil end self.cookNext[n] = nil return true end -- 손님 러시(유료): 남은 시간을 늘립니다. function Shop.addRush(self: Shop, seconds: number) self.rushLeft += seconds self.nextSpawn = math.min(self.nextSpawn, self.now + Shop.spawnInterval(self)) end -- 옆 가게에서 손님을 데려갈 때: 아직 음식을 못 받은(먹는 중이 아닌) NPC 손님을 최대 n명 빼서 돌려줍니다. -- 그 손님 자리로 가던 서빙 알바 접시는 카운터로 돌아갑니다. function Shop.takeCustomers(self: Shop, n: number): { Customer } local list = {} for _, c in self.customers do if c.state ~= "eating" and not c.guest then table.insert(list, c) end end -- 늦게 온 손님부터(오래 기다린 손님은 곧 음식을 받으니까) table.sort(list, function(a, b) return a.id > b.id end) local out = {} for i = 1, math.min(n, #list) do local c = list[i] self.customers[c.id] = nil self.seatTaken[c.seat] = nil for s, t in self.trips do if t.id == c.id then table.insert(self.counter, t.menu) self.trips[s] = nil end end table.insert(out, c) end return out end -- 옆 가게에서 온 손님을 빈자리에 앉힙니다(walk초 걸어옴). 자리가 없으면 nil function Shop.addTransfer(self: Shop, menu: string, walk: number): Customer? local seat = Shop.freeSeat(self) if not seat then return nil end -- 우리 가게에 없는 메뉴를 원하던 손님은 우리 메뉴로 바꿔 주문합니다. if not table.find(self.stats.menus, menu) then menu = nextOrder(self) end return addCustomer(self, seat, menu, walk, true) end -- 다른 플레이어가 손님으로 와서 빈자리에 앉습니다(걷지 않고 바로 주문). 자리가 없거나 이미 앉아 있으면 nil. function Shop.addGuest(self: Shop, userId: number): Customer? for _, c in self.customers do if c.guest == userId then return nil end end local seat = Shop.freeSeat(self) if not seat then return nil end local c = addCustomer(self, seat, nextOrder(self), 0) c.state = "waiting" c.untilT = self.now + Economy.GUEST_WAIT c.guest = userId return c end -- 플레이어 손님이 먼저 일어났을 때(점프, 퇴장). 이벤트 없이 자리만 비웁니다. function Shop.removeGuest(self: Shop, id: number): Customer? local c = self.customers[id] if not c or not c.guest then return nil end self.customers[id] = nil self.seatTaken[c.seat] = nil return c end function Shop.addMoney(self: Shop, amount: number) self.money += amount end return Shop