Skip to content

Commit

Permalink
refactor(server): add utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jul 14, 2023
1 parent d302ffe commit 99240c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
20 changes: 1 addition & 19 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,7 @@ exports('editDoor', function(id, data)
end
end)

local sounds do
local files = {}
local system = os.getenv('OS')
local command = system and system:match('Windows') and 'dir "' or 'ls "'
local path = GetResourcePath(cache.resource)
local types = path:gsub('//', '/') .. '/web/build/sounds'
local suffix = command == 'dir "' and '/" /b' or '/"'
local dir = io.popen(command .. types .. suffix)

if dir then
for line in dir:lines() do
local file = line:gsub('%.ogg', '')
files[#files+1] = file
end
dir:close()
end

sounds = files
end
local sounds = require 'server.utils'.getFilesInDirectory('web/build/sounds', '%.ogg')

local function createDoor(id, door, name)
local double = door.doors
Expand Down
27 changes: 27 additions & 0 deletions server/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local resourcePath = GetResourcePath(cache.resource):gsub('//', '/') .. '/'

local utils = {}

function utils.getFilesInDirectory(path, pattern)
local files = {}
local fileCount = 0
local system = os.getenv('OS')
local command = system and system:match('Windows') and 'dir "' or 'ls "'
local suffix = command == 'dir "' and '/" /b' or '/"'
local dir = io.popen(command .. resourcePath .. path .. suffix)

if dir then
for line in dir:lines() do
if line:match(pattern) then
fileCount += 1
files[fileCount] = line:gsub(pattern, '')
end
end

dir:close()
end

return files, fileCount
end

return utils

0 comments on commit 99240c6

Please sign in to comment.