Sedrowow

main server

Jul 28th, 2026 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.77 KB | None | 0 0
  1. -- Turtle Server Manager
  2. local PROTOCOL = "room_mgmt"
  3.  
  4. -- Define your currency item here
  5. local CURRENCY_ITEM = "minecraft:gold_nugget"
  6.  
  7. peripheral.find("modem", rednet.open)
  8. rednet.host(PROTOCOL, "server")
  9.  
  10. local inputChest = peripheral.wrap("top")
  11. local vaultChest = peripheral.wrap("bottom")
  12.  
  13. local db = { rooms = {}, users = {} }
  14. if fs.exists("database.json") then
  15.     local file = fs.open("database.json", "r")
  16.     db = textutils.unserializeJSON(file.readAll()) or { rooms = {}, users = {} }
  17.     file.close()
  18. end
  19.  
  20. local function saveDb()
  21.     local file = fs.open("database.json", "w")
  22.     file.write(textutils.serializeJSON(db))
  23.     file.close()
  24. end
  25.  
  26. local function updateRoomState(roomId)
  27.     local room = db.rooms[roomId]
  28.     local status = "Available"
  29.     local overdue = false
  30.    
  31.     if room.state == "rented" then
  32.         status = "Rented"
  33.         if os.epoch("utc") > room.due then overdue = true end
  34.     end
  35.     if room.state == "sold" then status = "Sold" end
  36.    
  37.     rednet.broadcast({
  38.         target = roomId,
  39.         command = "update",
  40.         status = status,
  41.         owner = room.owner,
  42.         isOpen = room.door_open or false,
  43.         overdue = overdue
  44.     }, PROTOCOL)
  45. end
  46.  
  47. local function networkListener()
  48.     while true do
  49.         local sender, msg, prot = rednet.receive(PROTOCOL)
  50.         if type(msg) == "table" then
  51.             local now = os.epoch("utc")
  52.            
  53.             -- Ensure user exists in db
  54.             if msg.user and not db.users[msg.user] then
  55.                 db.users[msg.user] = { balance = 0 }
  56.             end
  57.            
  58.             if msg.type == "register" then
  59.                 local id = msg.info.id
  60.                 if not db.rooms[id] then
  61.                     db.rooms[id] = { info = msg.info, state = "available", owner = nil, due = 0, door_open = false, password = nil }
  62.                 else
  63.                     db.rooms[id].info = msg.info
  64.                 end
  65.                 saveDb()
  66.                 updateRoomState(id)
  67.                
  68.             elseif msg.type == "request_list" then
  69.                 local userBal = msg.user and db.users[msg.user] and db.users[msg.user].balance or 0
  70.                 rednet.send(sender, { type = "list_response", data = db.rooms, balance = userBal, time = now }, PROTOCOL)
  71.                
  72.             elseif msg.type == "deposit" then
  73.                 if not inputChest or not vaultChest then
  74.                     rednet.send(sender, { type = "error", msg = "Server chests missing!" }, PROTOCOL)
  75.                 else
  76.                     local vaultName = peripheral.getName(vaultChest)
  77.                     local totalDeposited = 0
  78.                     for slot, item in pairs(inputChest.list()) do
  79.                         if item.name == CURRENCY_ITEM then
  80.                             -- Push specific item amounts to vault
  81.                             local pushed = inputChest.pushItems(vaultName, slot, item.count)
  82.                             totalDeposited = totalDeposited + pushed
  83.                         end
  84.                     end
  85.                    
  86.                     if totalDeposited > 0 then
  87.                         db.users[msg.user].balance = db.users[msg.user].balance + totalDeposited
  88.                         saveDb()
  89.                         rednet.send(sender, { type = "success", msg = "Deposited " .. totalDeposited .. " " .. CURRENCY_ITEM }, PROTOCOL)
  90.                     else
  91.                         rednet.send(sender, { type = "error", msg = "No valid currency in Drop Box." }, PROTOCOL)
  92.                     end
  93.                 end
  94.  
  95.             elseif msg.type == "rent" or msg.type == "buy" then
  96.                 local room = db.rooms[msg.id]
  97.                 local price = (msg.type == "rent") and room.info.rent_price or room.info.sale_price
  98.                
  99.                 if room and room.state == "available" then
  100.                     if db.users[msg.user].balance >= price then
  101.                         db.users[msg.user].balance = db.users[msg.user].balance - price
  102.                         room.state = (msg.type == "rent") and "rented" or "sold"
  103.                         room.owner = msg.user
  104.                         room.door_open = false
  105.                         room.password = nil
  106.                         room.due = (msg.type == "rent") and (now + room.info.rent_frequency) or 0
  107.                         saveDb()
  108.                         updateRoomState(msg.id)
  109.                         rednet.send(sender, { type = "success", msg = "Transaction successful!" }, PROTOCOL)
  110.                     else
  111.                         rednet.send(sender, { type = "error", msg = "Insufficient Bank Balance!" }, PROTOCOL)
  112.                     end
  113.                 end
  114.                
  115.             elseif msg.type == "cancel" or msg.type == "sell" then
  116.                 local room = db.rooms[msg.id]
  117.                 if room and room.owner == msg.user then
  118.                     if msg.type == "sell" and room.state == "sold" then
  119.                         db.users[msg.user].balance = db.users[msg.user].balance + math.floor(room.info.sale_price / 2)
  120.                     end
  121.                     room.state = "available"
  122.                     room.owner = nil
  123.                     room.door_open = false
  124.                     room.password = nil
  125.                     room.due = 0
  126.                     saveDb()
  127.                     updateRoomState(msg.id)
  128.                     rednet.send(sender, { type = "success", msg = "Room relinquished." }, PROTOCOL)
  129.                 end
  130.                
  131.             elseif msg.type == "set_password" then
  132.                 local room = db.rooms[msg.id]
  133.                 if room and room.owner == msg.user then
  134.                     room.password = msg.password
  135.                     saveDb()
  136.                     rednet.send(sender, { type = "success", msg = "Password updated!" }, PROTOCOL)
  137.                 end
  138.                
  139.             elseif msg.type == "toggle_door" then
  140.                 local room = db.rooms[msg.id]
  141.                 if room and (room.owner == msg.user or room.password == msg.password) then
  142.                     room.door_open = not room.door_open
  143.                     saveDb()
  144.                     updateRoomState(msg.id)
  145.                     if msg.user then rednet.send(sender, { type = "success", msg = "Door toggled." }, PROTOCOL) end
  146.                 else
  147.                     if msg.user then rednet.send(sender, { type = "error", msg = "Access Denied." }, PROTOCOL) end
  148.                 end
  149.             end
  150.         end
  151.     end
  152. end
  153.  
  154. local function timerLoop()
  155.     while true do
  156.         local now = os.epoch("utc")
  157.         for id, room in pairs(db.rooms) do
  158.             if room.state == "rented" and now >= room.due then
  159.                 -- Attempt auto-deduct
  160.                 if db.users[room.owner] and db.users[room.owner].balance >= room.info.rent_price then
  161.                     db.users[room.owner].balance = db.users[room.owner].balance - room.info.rent_price
  162.                     room.due = now + room.info.rent_frequency
  163.                     saveDb()
  164.                     updateRoomState(id)
  165.                 else
  166.                     -- Check eviction grace period
  167.                     if now >= (room.due + room.info.grace_period) then
  168.                         room.state = "available"
  169.                         room.owner = nil
  170.                         room.due = 0
  171.                         room.door_open = false
  172.                         room.password = nil
  173.                         saveDb()
  174.                         updateRoomState(id)
  175.                     else
  176.                         updateRoomState(id) -- Pushes overdue status update
  177.                     end
  178.                 end
  179.             end
  180.         end
  181.         sleep(10)
  182.     end
  183. end
  184.  
  185. print("Room Bank Server Running.")
  186. parallel.waitForAny(networkListener, timerLoop)
Advertisement
Add Comment
Please, Sign In to add comment