Fixed polish characters, code clean up
This commit is contained in:
simpo
2019-02-27 09:32:00 +01:00
committed by GitHub
parent 7dda6a224d
commit 37543ec167

View File

@@ -1,40 +1,131 @@
-- Perun for DCS World https://github.com/szporwolik/perun -> DCS Hook component
-- Initial init
local Perun = {}
package.path = package.path..";"..lfs.currentdir().."/LuaSocket/?.lua"
package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll"
local Perun = {}
package.path = package.path..";"..lfs.currentdir().."/LuaSocket/?.lua"
package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll"
-- ########### SETTINGS ###########
Perun.RefreshStatus = 15 -- base refresh rate in seconds to send status update (values lower than 60 may affect performance!)
Perun.RefreshMission = 60 -- refresh rate in seconds to send mission information (values lower than 60 may affect performance!)
Perun.JsonStatusLocation = "Scripts\\Json\\" -- relatve do user's SaveGames DCS folder -> status file updated each RefreshMission
Perun.UDPTargetPort = 48620 -- UDP port to send data to
Perun.MOTD_L1 = "Witamy na serwerze Gildia.org !" -- Message send to players connecting the server - Line 1
Perun.MOTD_L2 = "Wymagamy obecnosci na 255Mhz (DCS SRS)" -- Message send to players connecting the server - Line 2
Perun.RefreshStatus = 15 -- base refresh rate in seconds to send status update (values lower than 60 may affect performance!)
Perun.RefreshMission = 60 -- refresh rate in seconds to send mission information (values lower than 60 may affect performance!)
Perun.JsonStatusLocation = "Scripts\\Json\\" -- folder relative do user's SaveGames DCS folder -> status file updated each RefreshMission
Perun.UDPTargetPort = 48620 -- UDP port to send data to
Perun.MOTD_L1 = "Witamy na serwerze Gildia.org !" -- Message send to players connecting the server - Line 1
Perun.MOTD_L2 = "Wymagamy obecnosci na 255Mhz (DCS SRS)" -- Message send to players connecting the server - Line 2
-- ########### END OF SETTINGS ###########
-- Variable init
Perun.Version = "v0.3.7"
Perun.StatusData = {}
Perun.SlotsData = {}
Perun.MissionData = {}
Perun.ServerData = {}
Perun.MissionHash=""
Perun.lastSentStatus =0
Perun.lastSentMission =0
Perun.JsonStatusLocation = lfs.writedir() .. Perun.JsonStatusLocation
Perun.socket = require("socket")
Perun.UDP = assert(Perun.socket.udp())
Perun.UDP:settimeout(0)
Perun.UDP:setsockname("*", 48621)
Perun.UDP:setpeername("127.0.0.1",Perun.UDPTargetPort)
-- Function definition
Perun.GetCategory = function(id)
-- Helper function via https://pastebin.com/GUAXrd2U
-- Variable init
Perun.Version = "v0.3.7"
Perun.StatusData = {}
Perun.SlotsData = {}
Perun.MissionData = {}
Perun.ServerData = {}
Perun.MissionHash=""
Perun.lastSentStatus =0
Perun.lastSentMission =0
Perun.JsonStatusLocation = lfs.writedir() .. Perun.JsonStatusLocation
Perun.socket = require("socket")
Perun.UDP = assert(Perun.socket.udp())
Perun.UDP:settimeout(0)
Perun.UDP:setsockname("*", 48621)
Perun.UDP:setpeername("127.0.0.1",Perun.UDPTargetPort)
-- ########### Helper function definitions ###########
function stripChars(str)
-- remove accents characters from string
-- via https://stackoverflow.com/questions/50459102/replace-accented-characters-in-string-to-standard-with-lua TBD: rewrite
tableAccents = {}
tableAccents["à"] = "a"
tableAccents["á"] = "a"
tableAccents["â"] = "a"
tableAccents["ã"] = "a"
tableAccents["ä"] = "a"
tableAccents["ç"] = "c"
tableAccents["è"] = "e"
tableAccents["é"] = "e"
tableAccents["ê"] = "e"
tableAccents["ë"] = "e"
tableAccents["ì"] = "i"
tableAccents["í"] = "i"
tableAccents["î"] = "i"
tableAccents["ï"] = "i"
tableAccents["ñ"] = "n"
tableAccents["ò"] = "o"
tableAccents["ó"] = "o"
tableAccents["ô"] = "o"
tableAccents["õ"] = "o"
tableAccents["ö"] = "o"
tableAccents["ù"] = "u"
tableAccents["ú"] = "u"
tableAccents["û"] = "u"
tableAccents["ü"] = "u"
tableAccents["ý"] = "y"
tableAccents["ÿ"] = "y"
tableAccents["À"] = "A"
tableAccents["Á"] = "A"
tableAccents["Â"] = "A"
tableAccents["Ã"] = "A"
tableAccents["Ä"] = "A"
tableAccents["Ç"] = "C"
tableAccents["È"] = "E"
tableAccents["É"] = "E"
tableAccents["Ê"] = "E"
tableAccents["Ë"] = "E"
tableAccents["Ì"] = "I"
tableAccents["Í"] = "I"
tableAccents["Î"] = "I"
tableAccents["Ï"] = "I"
tableAccents["Ñ"] = "N"
tableAccents["Ò"] = "O"
tableAccents["Ó"] = "O"
tableAccents["Ô"] = "O"
tableAccents["Õ"] = "O"
tableAccents["Ö"] = "O"
tableAccents["Ù"] = "U"
tableAccents["Ú"] = "U"
tableAccents["Û"] = "U"
tableAccents["Ü"] = "U"
tableAccents["Ý"] = "Y"
-- Polish accents
tableAccents["ę"] = "e"
tableAccents["Ę"] = "Ę"
tableAccents["ó"] = "o"
tableAccents["Ó"] = "O"
tableAccents["ą"] = "a"
tableAccents["Ą"] = "A"
tableAccents["ś"] = "s"
tableAccents["Ś"] = "S"
tableAccents["ć"] = "c"
tableAccents["Ć"] = "C"
tableAccents["ż"] = "z"
tableAccents["Ż"] = "Z"
tableAccents["ź"] = "z"
tableAccents["Ź"] = "Z"
tableAccents["ł"] = "l"
tableAccents["Ł"] = "L"
-- TBD additonal characters
normalizedString = ''
for strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do
if tableAccents[strChar] ~= nil then
normalizedString = normalizedString..tableAccents[strChar]
else
normalizedString = normalizedString..strChar
end
end
return normalizedString
end
Perun.GetCategory = function(id)
-- Returns object category basing on ID
-- Helper function via https://pastebin.com/GUAXrd2U TBD: rewrite
local _killed_target_category = DCS.getUnitTypeAttribute(id, "category")
if _killed_target_category == nil then
local _killed_target_cat_check_ship = DCS.getUnitTypeAttribute(id, "DeckLevel")
@@ -48,30 +139,31 @@
end
end
return _killed_target_category
end
end
Perun.SideID2Name = function(id)
-- Helper function
Perun.SideID2Name = function(id)
-- Helper function returns side name per side (coalition) id
local sides = {
[0] = 'SPECTATOR',
[1] = 'RED',
[2] = 'BLUE',
}
return sides[id]
end
end
Perun.AddLog = function(text)
-- ########### Main code ###########
Perun.AddLog = function(text)
-- Adds logs to DCS.log file
net.log("Perun : ".. text)
end
end
Perun.UpdateJsonStatus = function()
Perun.UpdateJsonStatus = function()
-- Updates status json file
TempData={}
TempData["1"]=Perun.ServerData
TempData["2"]=Perun.StatusData
TempData["3"]=Perun.SlotsData
-- TempData["4"]=Perun.MissionData -- TBD?
-- TempData["4"]=Perun.MissionData -- TBD: hangs for some large missions
_temp=net.lua2json(TempData)
@@ -79,9 +171,9 @@
perun_export:write(_temp .. "\n")
perun_export:close()
end
end
Perun.Send = function(data_id, data_package)
Perun.Send = function(data_id, data_package)
-- Sends data package
TempData={}
TempData["type"]=data_id
@@ -89,16 +181,18 @@
TempData["timestamp"]=os.date('%Y-%m-%d %H:%M:%S')
temp=net.lua2json(TempData)
temp=stripChars(temp)
Perun.UDP:send(temp)
Perun.AddLog("Packet send")
end
end
Perun.UpdateStatusPart = function(part_id, data_package)
Perun.UpdateStatusPart = function(part_id, data_package)
-- Helper for status update container
Perun.StatusData[part_id] = data_package
end
end
Perun.UpdateStatus = function()
Perun.UpdateStatus = function()
-- Main function for status updates
-- Diagnostic data
@@ -114,7 +208,7 @@
-- Send
Perun.Send(1,Perun.ServerData)
-- Update all subsections
-- Status data - update all subsections
-- 1 - Mission
temp={}
temp['name']=DCS.getMissionName()
@@ -146,31 +240,29 @@
end
-- Send
Perun.Send(3,Perun.SlotsData)
end
end
Perun.UpdateMission = function()
Perun.UpdateMission = function()
-- Main function for mission information updates
-- Update Mission data
Perun.MissionData=DCS.getCurrentMission()
end
end
Perun.LogChat = function(playerID,msg,all)
Perun.LogChat = function(playerID,msg,all)
-- Log chat messages
data={}
data['player']= net.get_player_info(playerID, "name")
data['msg']=msg
data['msg']=stripChars(msg)
data['all']=all
data['ucid']=net.get_player_info(playerID, 'ucid')
data['datetime']=os.date('%Y-%m-%d %H:%M:%S')
data['missionhash']=Perun.MissionHash
Perun.Send(50,data)
end
end
Perun.LogEvent = function(log_type,log_content)
Perun.LogEvent = function(log_type,log_content)
-- Log chat messages
data={}
@@ -180,9 +272,9 @@
data['log_missionhash']=Perun.MissionHash
Perun.Send(51,data)
end
end
Perun.LogStats = function(playerID)
Perun.LogStats = function(playerID)
-- Log player status
-- TBD : not working at the moment WIP
@@ -204,9 +296,9 @@
data['stat_missionhash']=Perun.MissionHash
Perun.Send(52,data)
end
end
Perun.LogLogin = function(playerID)
Perun.LogLogin = function(playerID)
-- Player logged in
data={}
@@ -216,24 +308,24 @@
data['login_datetime']=os.date('%Y-%m-%d %H:%M:%S')
Perun.Send(53,data)
end
end
--- Event callbacks
--- ########### Event callbacks ###########
Perun.onSimulationStart = function()
Perun.onSimulationStart = function()
Perun.MissionHash=DCS.getMissionName( ).."@"..os.date('%Y%m%d_%H%M%S');
Perun.LogEvent("SimStart","Mission" .. Perun.MissionHash .. " started");
end
end
Perun.onSimulationStop = function()
Perun.onSimulationStop = function()
Perun.LogEvent("SimStop","Mission" .. Perun.MissionHash .. " finished");
end
end
Perun.onPlayerDisconnect= function(id, err_code)
Perun.onPlayerDisconnect= function(id, err_code)
Perun.LogEvent("disconnect", "Player " .. net.get_player_info(id, "name") .. " disconnected; " .. err_code);
end
end
Perun.onSimulationFrame = function()
Perun.onSimulationFrame = function()
local _now = DCS.getRealTime()
-- First run
@@ -258,23 +350,23 @@
end
end
Perun.onPlayerStart = function (id)
Perun.onPlayerStart = function (id)
net.send_chat(Perun.MOTD_L1, id);
net.send_chat(Perun.MOTD_L2, id);
end
end
Perun.onPlayerTrySendChat = function (playerID, msg, all)
Perun.onPlayerTrySendChat = function (playerID, msg, all)
if msg~=Perun.MOTD_L1 and msg~=Perun.MOTD_L2 then
Perun.LogChat(playerID,msg,all)
end
return msg
end
end
Perun.onGameEvent = function (eventName,arg1,arg2,arg3,arg4,arg5,arg6,arg7)
Perun.onGameEvent = function (eventName,arg1,arg2,arg3,arg4,arg5,arg6,arg7)
if eventName == "friendly_fire" then
--"friendly_fire", playerID, weaponName, victimPlayerID
@@ -370,8 +462,8 @@
Perun.LogEvent(eventName,"Unknown event type");
end
end
end
-- Finalize and set callbacks
DCS.setUserCallbacks(Perun)
net.log("Loaded - Perun - VladMordock - " .. Perun.Version )
-- ########### Finalize and set callbacks ###########
DCS.setUserCallbacks(Perun)
net.log("Loaded - Perun - VladMordock - " .. Perun.Version )