Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- floor_manager.lua
- local floorsFile = "floors.txt"
- local colorMap = {
- white = colors.white,
- orange = colors.orange,
- magenta = colors.magenta,
- lightBlue = colors.lightBlue,
- yellow = colors.yellow,
- lime = colors.lime,
- pink = colors.pink,
- gray = colors.gray,
- lightGray = colors.lightGray,
- cyan = colors.cyan,
- purple = colors.purple,
- blue = colors.blue,
- brown = colors.brown,
- green = colors.green,
- red = colors.red,
- black = colors.black,
- }
- function saveFloors(floors)
- local file = fs.open(floorsFile, "w")
- file.write(textutils.serialize(floors))
- file.close()
- end
- function loadFloors()
- if not fs.exists(floorsFile) then
- return {}
- else
- local file = fs.open(floorsFile, "r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- end
- local floors = loadFloors()
- function drawMenu()
- term.clear()
- term.setCursorPos(1, 1)
- print("Elevator Monitoring\n")
- print("Floors:")
- for i, floor in ipairs(floors) do
- for name, value in pairs(colorMap) do
- if value == floor.col then
- print(i .. ". " .. floor.name .. " (" .. floor.num .. ") - Color: " .. name)
- break
- end
- end
- end
- print("\n1. Add Floor")
- print("2. Delete Floor")
- print("3. Edit Floor")
- print("4. Exit")
- print("5. Exit & Run")
- end
- function getColorFromName(name)
- return colorMap[name]
- end
- function addFloor()
- print("\nAdding a new floor...")
- print("Enter floor number:")
- local num = read()
- print("Enter floor name:")
- local name = read()
- print("Enter color name (e.g., pink, blue, white):")
- local colorName = read()
- local col = getColorFromName(colorName)
- if col then
- table.insert(floors, {num = num, name = name, col = col})
- saveFloors(floors)
- print("Floor added.")
- else
- print("Invalid color name.")
- end
- end
- function deleteFloor()
- print("\nEnter floor index to delete:")
- local index = tonumber(read())
- if index and index > 0 and index <= #floors then
- table.remove(floors, index)
- saveFloors(floors)
- print("Floor deleted.")
- else
- print("Invalid index.")
- end
- end
- function editFloor()
- print("\nEnter floor index to edit:")
- local index = tonumber(read())
- if floors[index] then
- print("Enter new floor number (currently " .. floors[index].num .. "):")
- floors[index].num = read()
- print("Enter new floor name (currently " .. floors[index].name .. "):")
- floors[index].name = read()
- print("Enter new color name (e.g., pink, blue, white):")
- local colorName = read()
- local col = getColorFromName(colorName)
- if col then
- floors[index].col = col
- saveFloors(floors)
- print("Floor updated.")
- else
- print("Invalid color name.")
- end
- else
- print("Invalid index.")
- end
- end
- function mainMenu()
- drawMenu()
- local choice = read()
- if choice == "1" then
- addFloor()
- elseif choice == "2" then
- deleteFloor()
- elseif choice == "3" then
- editFloor()
- elseif choice == "4" then
- return false -- Exit the program
- elseif choice == "5" then
- shell.run('controller')
- end
- return true
- end
- while mainMenu() do
- -- Loop back to the main menu after each action
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement