Advertisement
Omsigames

utils.lua

Dec 25th, 2024 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.94 KB | None | 0 0
  1. local utils = {}
  2. local Config = require("config")
  3. local internet = require("internet")
  4. local json = require("json")
  5. local controllers = require("controllers")
  6.  
  7. -- Function: utils.calcSwitchTextPos
  8. -- Description: Calculate the position of the switch text
  9. -- Parameters: switchTbl - table containing the switch data
  10. -- Returns: table containing the x and y position of the switch text
  11. utils.calcSwitchTextPos = function(switchTbl)
  12.     local result = {}
  13.     local x, y = switchTbl[1], switchTbl[2]
  14.  
  15.     if switchTbl[3] == "╗" or switchTbl[4] == "╗" then
  16.         y = switchTbl[2] - 1
  17.     elseif switchTbl[3] == "╝" or switchTbl[4] == "╝" then
  18.         y = switchTbl[2] + 1
  19.     elseif switchTbl[3] == "╚" or switchTbl[4] == "╚" then
  20.         y = switchTbl[2] + 1
  21.     elseif switchTbl[3] == "╔" or switchTbl[4] == "╔" then
  22.         y = switchTbl[2] - 1
  23.     end
  24.     x = switchTbl[5]:len() == 1 and x or x - 1
  25.  
  26.     result["x"] = x
  27.     result["y"] = y
  28.     return result
  29. end
  30.  
  31. -- Function: utils.calcSignalTextPos
  32. -- Description: Calculate the position of the signal text
  33. -- Parameters: signalTbl - table containing the signal data
  34. -- Returns: table containing the x and y position of the signal text
  35. utils.calcSignalTextPos = function(signalTbl)
  36.     local result = {}
  37.     local x, y = signalTbl[1], signalTbl[2]
  38.  
  39.     if signalTbl[4] == "<" or signalTbl[4] == "◀" or signalTbl[4] == "◁" then
  40.         x = signalTbl[1] - (string.len(signalTbl[3]) / 2)
  41.         if string.len(signalTbl[3]) % 2 == 0 then x = x + 1 end
  42.         y = signalTbl[2] - 1
  43.     elseif signalTbl[4] == ">" or signalTbl[4] == "▶" or signalTbl[4] == "▷" then
  44.         x = signalTbl[1] - (string.len(signalTbl[3]) / 2)
  45.         if string.len(signalTbl[3]) % 2 == 0 then x = x + 1 end
  46.         y = signalTbl[2] + 1
  47.     elseif signalTbl[4] == "^" or signalTbl[4] == "▲" or signalTbl[4] == "△" then
  48.         x = signalTbl[1] + 1
  49.         y = signalTbl[2]
  50.     elseif signalTbl[4] == "V" or signalTbl[4] == "▼" or signalTbl[4] == "▽" then
  51.         x = signalTbl[1] - string.len(signalTbl[3])
  52.         y = signalTbl[2]
  53.     end
  54.  
  55.     result["x"] = x
  56.     result["y"] = y
  57.     return result
  58. end
  59.  
  60. -- Function: string.split
  61. -- Description: Splits a string by a separator
  62. -- Parameters: inputstr - the string to split
  63. --             sep - the separator
  64. -- Returns: table containing the split strings
  65. function string.split(inputstr, sep)
  66.     if sep == nil then
  67.        sep = "%s"
  68.     end
  69.     local t={}
  70.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  71.        table.insert(t, str)
  72.     end
  73.     return t
  74. end
  75.  
  76. -- Function: utils.debugPrint
  77. -- Description: Logs a message to the log file if debug is enabled
  78. -- Parameters: message - the message to print
  79. utils.debugPrint = function(message)
  80.     if not Config.Debug then return end
  81.  
  82.     local file = io.open("log.txt", "a")
  83.     if not file then
  84.         print("Failed to open log file")
  85.         return os.exit()
  86.     end
  87.     local rawTime = json.decode(internet.request("https://worldtimeapi.org/api/timezone/Europe/Prague")())["datetime"]
  88.     local time = string.split(rawTime, "T")[1] .. " " .. string.split(string.split(rawTime, "T")[2], ".")[1]
  89.     file:write(time .. "  " .. tostring(message) .. "\n")
  90.     file:close()
  91. end
  92.  
  93. -- Define connected controllers
  94. local switchesConnected = controllers.isConnected("Switches")
  95. local signalsConnected  = controllers.isConnected("Signals")
  96. local crossingsConnected = controllers.isConnected("Crossings")
  97.  
  98. -- Function: utils.resetLayout
  99. -- Description: Resets the switches and signals according to the default layout
  100. utils.resetLayout = function()
  101.     if switchesConnected then
  102.         for _, switch in pairs(Config.Switches) do
  103.             if switch[3] == "╗" or switch[3] == "╝" or switch[3] == "╚" or switch[3] == "╔" or switch[3] == "╚" then
  104.                 controllers.Switches.setAspect(switch[5], 1)
  105.             else
  106.                 controllers.Switches.setAspect(switch[5], 5)
  107.             end
  108.         end
  109.     end
  110.    
  111.     if signalsConnected then controllers.Signals.setEveryState("Stuj") end
  112. end
  113.  
  114. -- Function: utils.toggleSwitch
  115. -- Description: Toggles the switch
  116. -- Parameters: switchName - the name of the switch
  117. utils.toggleSwitch = function(switchName)
  118.     if not switchesConnected then return end
  119.     if controllers.Switches.getAspect(switchName) == 1 then
  120.         controllers.Switches.setAspect(switchName, 5)
  121.     else
  122.         controllers.Switches.setAspect(switchName, 1)
  123.     end
  124. end
  125.  
  126. utils.toggleCrossing = function(switchName)
  127.     if not crossingsConnected then return end
  128.     if controllers.Crossings.getAspect(switchName) == 5 then
  129.         controllers.Crossings.setAspect(switchName, 1)
  130.     else
  131.         controllers.Crossings.setAspect(switchName, 5)
  132.     end
  133. end
  134.  
  135. -- Function: utils.sendStateToExpectSig
  136. -- Description: Sends the state of the signal to the expect signal
  137. -- Parameters: signalName - the name of the signal
  138. --             state - the state of the signal
  139. utils.sendStateToExpectSig = function(signalName, state)
  140.     if not signalsConnected then return end
  141.  
  142.     if state == "Stuj" then
  143.         state = "Vystraha"
  144.     elseif state == "PN" then
  145.         state = "Vystraha"
  146.     elseif state == "Vystraha" then
  147.         state = "Volno"
  148.     elseif state == "Volno" then
  149.         state = "Volno"
  150.     elseif string.sub(state, 1, 3) == "R40" then
  151.         state = "Ocek40"
  152.     elseif string.sub(state, 1, 3) == "R60" then
  153.         state = "Ocek60"
  154.     elseif string.sub(state, 1, 3) == "R80" then
  155.         state = "Ocek80"
  156.     elseif state.sub(state, 1, 4) == "Ocek" then
  157.         state = "Volno"
  158.     elseif state.sub(state, 1, 4) == "Opak" then
  159.         state = state.sub(state, 5)
  160.     else
  161.         state = "Vystraha"
  162.     end
  163.  
  164.     controllers.Signals.setState("Pr" .. signalName, state)
  165. end
  166.  
  167. -- Simple shallow copy of a table
  168. function table.clone(org)
  169.     return {table.unpack(org)}
  170. end
  171.  
  172. return utils, table.clone, string.split
  173.  
Tags: ORMS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement