mirror of
https://github.com/DaKerboul/perun.git
synced 2026-08-09 18:05:40 +02:00
@@ -1,40 +1,131 @@
|
|||||||
-- Perun for DCS World https://github.com/szporwolik/perun -> DCS Hook component
|
-- Perun for DCS World https://github.com/szporwolik/perun -> DCS Hook component
|
||||||
|
|
||||||
-- Initial init
|
-- Initial init
|
||||||
local Perun = {}
|
local Perun = {}
|
||||||
package.path = package.path..";"..lfs.currentdir().."/LuaSocket/?.lua"
|
package.path = package.path..";"..lfs.currentdir().."/LuaSocket/?.lua"
|
||||||
package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll"
|
package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll"
|
||||||
|
|
||||||
-- ########### SETTINGS ###########
|
-- ########### SETTINGS ###########
|
||||||
|
|
||||||
Perun.RefreshStatus = 15 -- base refresh rate in seconds to send status update (values lower than 60 may affect performance!)
|
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.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.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.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_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.MOTD_L2 = "Wymagamy obecnosci na 255Mhz (DCS SRS)" -- Message send to players connecting the server - Line 2
|
||||||
|
|
||||||
-- ########### END OF SETTINGS ###########
|
-- ########### 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
|
-- Variable init
|
||||||
Perun.GetCategory = function(id)
|
Perun.Version = "v0.3.7"
|
||||||
-- Helper function via https://pastebin.com/GUAXrd2U
|
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")
|
local _killed_target_category = DCS.getUnitTypeAttribute(id, "category")
|
||||||
if _killed_target_category == nil then
|
if _killed_target_category == nil then
|
||||||
local _killed_target_cat_check_ship = DCS.getUnitTypeAttribute(id, "DeckLevel")
|
local _killed_target_cat_check_ship = DCS.getUnitTypeAttribute(id, "DeckLevel")
|
||||||
@@ -48,30 +139,31 @@
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
return _killed_target_category
|
return _killed_target_category
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.SideID2Name = function(id)
|
Perun.SideID2Name = function(id)
|
||||||
-- Helper function
|
-- Helper function returns side name per side (coalition) id
|
||||||
local sides = {
|
local sides = {
|
||||||
[0] = 'SPECTATOR',
|
[0] = 'SPECTATOR',
|
||||||
[1] = 'RED',
|
[1] = 'RED',
|
||||||
[2] = 'BLUE',
|
[2] = 'BLUE',
|
||||||
}
|
}
|
||||||
return sides[id]
|
return sides[id]
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.AddLog = function(text)
|
-- ########### Main code ###########
|
||||||
|
Perun.AddLog = function(text)
|
||||||
-- Adds logs to DCS.log file
|
-- Adds logs to DCS.log file
|
||||||
net.log("Perun : ".. text)
|
net.log("Perun : ".. text)
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.UpdateJsonStatus = function()
|
Perun.UpdateJsonStatus = function()
|
||||||
-- Updates status json file
|
-- Updates status json file
|
||||||
TempData={}
|
TempData={}
|
||||||
TempData["1"]=Perun.ServerData
|
TempData["1"]=Perun.ServerData
|
||||||
TempData["2"]=Perun.StatusData
|
TempData["2"]=Perun.StatusData
|
||||||
TempData["3"]=Perun.SlotsData
|
TempData["3"]=Perun.SlotsData
|
||||||
-- TempData["4"]=Perun.MissionData -- TBD?
|
-- TempData["4"]=Perun.MissionData -- TBD: hangs for some large missions
|
||||||
|
|
||||||
_temp=net.lua2json(TempData)
|
_temp=net.lua2json(TempData)
|
||||||
|
|
||||||
@@ -79,9 +171,9 @@
|
|||||||
perun_export:write(_temp .. "\n")
|
perun_export:write(_temp .. "\n")
|
||||||
perun_export:close()
|
perun_export:close()
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.Send = function(data_id, data_package)
|
Perun.Send = function(data_id, data_package)
|
||||||
-- Sends data package
|
-- Sends data package
|
||||||
TempData={}
|
TempData={}
|
||||||
TempData["type"]=data_id
|
TempData["type"]=data_id
|
||||||
@@ -89,16 +181,18 @@
|
|||||||
TempData["timestamp"]=os.date('%Y-%m-%d %H:%M:%S')
|
TempData["timestamp"]=os.date('%Y-%m-%d %H:%M:%S')
|
||||||
|
|
||||||
temp=net.lua2json(TempData)
|
temp=net.lua2json(TempData)
|
||||||
|
temp=stripChars(temp)
|
||||||
|
|
||||||
Perun.UDP:send(temp)
|
Perun.UDP:send(temp)
|
||||||
Perun.AddLog("Packet send")
|
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
|
-- Helper for status update container
|
||||||
Perun.StatusData[part_id] = data_package
|
Perun.StatusData[part_id] = data_package
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.UpdateStatus = function()
|
Perun.UpdateStatus = function()
|
||||||
-- Main function for status updates
|
-- Main function for status updates
|
||||||
|
|
||||||
-- Diagnostic data
|
-- Diagnostic data
|
||||||
@@ -114,7 +208,7 @@
|
|||||||
-- Send
|
-- Send
|
||||||
Perun.Send(1,Perun.ServerData)
|
Perun.Send(1,Perun.ServerData)
|
||||||
|
|
||||||
-- Update all subsections
|
-- Status data - update all subsections
|
||||||
-- 1 - Mission
|
-- 1 - Mission
|
||||||
temp={}
|
temp={}
|
||||||
temp['name']=DCS.getMissionName()
|
temp['name']=DCS.getMissionName()
|
||||||
@@ -146,31 +240,29 @@
|
|||||||
end
|
end
|
||||||
-- Send
|
-- Send
|
||||||
Perun.Send(3,Perun.SlotsData)
|
Perun.Send(3,Perun.SlotsData)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
Perun.UpdateMission = function()
|
Perun.UpdateMission = function()
|
||||||
-- Main function for mission information updates
|
-- Main function for mission information updates
|
||||||
|
|
||||||
-- Update Mission data
|
|
||||||
Perun.MissionData=DCS.getCurrentMission()
|
Perun.MissionData=DCS.getCurrentMission()
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.LogChat = function(playerID,msg,all)
|
Perun.LogChat = function(playerID,msg,all)
|
||||||
-- Log chat messages
|
-- Log chat messages
|
||||||
|
|
||||||
data={}
|
data={}
|
||||||
data['player']= net.get_player_info(playerID, "name")
|
data['player']= net.get_player_info(playerID, "name")
|
||||||
data['msg']=msg
|
data['msg']=stripChars(msg)
|
||||||
data['all']=all
|
data['all']=all
|
||||||
data['ucid']=net.get_player_info(playerID, 'ucid')
|
data['ucid']=net.get_player_info(playerID, 'ucid')
|
||||||
data['datetime']=os.date('%Y-%m-%d %H:%M:%S')
|
data['datetime']=os.date('%Y-%m-%d %H:%M:%S')
|
||||||
data['missionhash']=Perun.MissionHash
|
data['missionhash']=Perun.MissionHash
|
||||||
|
|
||||||
Perun.Send(50,data)
|
Perun.Send(50,data)
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.LogEvent = function(log_type,log_content)
|
Perun.LogEvent = function(log_type,log_content)
|
||||||
-- Log chat messages
|
-- Log chat messages
|
||||||
|
|
||||||
data={}
|
data={}
|
||||||
@@ -180,9 +272,9 @@
|
|||||||
data['log_missionhash']=Perun.MissionHash
|
data['log_missionhash']=Perun.MissionHash
|
||||||
|
|
||||||
Perun.Send(51,data)
|
Perun.Send(51,data)
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.LogStats = function(playerID)
|
Perun.LogStats = function(playerID)
|
||||||
-- Log player status
|
-- Log player status
|
||||||
|
|
||||||
-- TBD : not working at the moment WIP
|
-- TBD : not working at the moment WIP
|
||||||
@@ -204,9 +296,9 @@
|
|||||||
data['stat_missionhash']=Perun.MissionHash
|
data['stat_missionhash']=Perun.MissionHash
|
||||||
|
|
||||||
Perun.Send(52,data)
|
Perun.Send(52,data)
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.LogLogin = function(playerID)
|
Perun.LogLogin = function(playerID)
|
||||||
-- Player logged in
|
-- Player logged in
|
||||||
|
|
||||||
data={}
|
data={}
|
||||||
@@ -216,24 +308,24 @@
|
|||||||
data['login_datetime']=os.date('%Y-%m-%d %H:%M:%S')
|
data['login_datetime']=os.date('%Y-%m-%d %H:%M:%S')
|
||||||
|
|
||||||
Perun.Send(53,data)
|
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.MissionHash=DCS.getMissionName( ).."@"..os.date('%Y%m%d_%H%M%S');
|
||||||
Perun.LogEvent("SimStart","Mission" .. Perun.MissionHash .. " started");
|
Perun.LogEvent("SimStart","Mission" .. Perun.MissionHash .. " started");
|
||||||
end
|
end
|
||||||
|
|
||||||
Perun.onSimulationStop = function()
|
Perun.onSimulationStop = function()
|
||||||
Perun.LogEvent("SimStop","Mission" .. Perun.MissionHash .. " finished");
|
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);
|
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()
|
local _now = DCS.getRealTime()
|
||||||
|
|
||||||
-- First run
|
-- 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_L1, id);
|
||||||
net.send_chat(Perun.MOTD_L2, 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
|
if msg~=Perun.MOTD_L1 and msg~=Perun.MOTD_L2 then
|
||||||
Perun.LogChat(playerID,msg,all)
|
Perun.LogChat(playerID,msg,all)
|
||||||
end
|
end
|
||||||
|
|
||||||
return msg
|
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
|
if eventName == "friendly_fire" then
|
||||||
--"friendly_fire", playerID, weaponName, victimPlayerID
|
--"friendly_fire", playerID, weaponName, victimPlayerID
|
||||||
@@ -370,8 +462,8 @@
|
|||||||
Perun.LogEvent(eventName,"Unknown event type");
|
Perun.LogEvent(eventName,"Unknown event type");
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Finalize and set callbacks
|
-- ########### Finalize and set callbacks ###########
|
||||||
DCS.setUserCallbacks(Perun)
|
DCS.setUserCallbacks(Perun)
|
||||||
net.log("Loaded - Perun - VladMordock - " .. Perun.Version )
|
net.log("Loaded - Perun - VladMordock - " .. Perun.Version )
|
||||||
|
|||||||
Reference in New Issue
Block a user