Add function to write result to disk

This commit is contained in:
2025-04-05 21:57:36 +08:00
parent 686bba057c
commit f7a02b1983
2 changed files with 133 additions and 10 deletions
+133 -10
View File
@@ -1,7 +1,7 @@
Range = {}
Range.DebugMod = false
Range.DebugMod = true
Range.ZoneIndex = 1
Range.MonitorRepeatTime = 5
@@ -14,6 +14,12 @@ Range.TotalBlueNum = Range.TotalTargetNum-Range.TotalRedNum
Range.TotalStaticNum = Range.TotalTargetNum/2
Range.TotalMovingNum = Range.TotalTargetNum/2
Range.WriteFile = false
Range.WriteFilePath = 'Missions/Buta/Apache_Training_Range/Saves/'
if lfs and io then
Range.WriteFile = true
end
Range.SpawnedGroups_Default = {
[coalition.side.RED] = {
static = nil,
@@ -26,7 +32,6 @@ Range.SpawnedGroups_Default = {
}
Range.PlayerData_Default = {
startTime = nil,
timeUsed = 0,
enemyKilled = 0,
allyKilled = 0,
cloesestEnemyDistance = 15000,
@@ -137,6 +142,7 @@ do
self.playerUnit = playerUnit
self.playerData = Range.PlayerData_Default
self.spawnedGroups = Range.SpawnedGroups_Default
self.playSound = true
if playerUnit then
self:CreatePlayerMenu(self.playerUnit)
@@ -242,6 +248,7 @@ do
self.RefreshTargetsMenu = missionCommands.addCommandForGroup(goupID,'刷新目标',nil,self.SpawnTargets,{context = self})
self.EndTrainingMenu = missionCommands.addCommandForGroup(goupID,'结束训练',nil,self.EndTraining,{context = self,reason = '玩家手动结束'})
self.SoundSwichMenu = missionCommands.addCommandForGroup(goupID,'提示音开关',nil,self.SoundSwich,{context = self})
end
function Range:GetRandSpawnPoint()
@@ -640,7 +647,7 @@ do
return
end
self.playerData.startTime = Range.Format_time()
self.playerData.startTime = timer.getAbsTime()
local startTime = Range.Format_time(self.playerData.startTime)
local msg = ''
@@ -659,6 +666,11 @@ do
trigger.action.setMarkupColorFill(self.assignedRangeZoneDrawID,{1,0,0,0.2})
trigger.action.setMarkupTypeLine(self.assignedRangeZoneDrawID,1)
--Sound
if self.playSound then
trigger.action.outSoundForGroup(playerUnit:getGroup():getID(),'audio/SFX/success.ogg')
end
if Range.DebugMod then
Utils.Debug('timer.getTime(): '..Range.Format_time(timer.getTime()))
Utils.Debug('timer.getAbsTime(): '..Range.Format_time(timer.getAbsTime()))
@@ -673,14 +685,38 @@ do
local self = vars.context
local notShowResults = vars.notShowResults
local reason = vars.reason
local Error = vars.Error
if Range.DebugMod then
Utils.Debug('Range.EndTraining')
end
self:DespawnTargets()
if not notShowResults then self:ShowResults(reason) end
self:Stop()
self:DespawnTargets()
if not notShowResults then self:ShowResults(reason,Error) end
end
function Range.SoundSwich(vars)
local self = vars.context
self.playSound = not self.playSound
local playerUnit = self.playerUnit
if not playerUnit then
self:Init()
return
end
if not playerUnit:isExist() then
self:Init()
return
end
if self.playSound then
trigger.action.outTextForUnit(playerUnit:getID(),'音效: 开',5)
else
trigger.action.outTextForUnit(playerUnit:getID(),'音效: 关',5)
end
end
--@param table or groupName
@@ -701,7 +737,83 @@ do
end
end
function Range:ShowResults(reason)
function Range:writeFile(reason)
if not Range.WriteFile then
env.info('Range.writeFile() - lfs Not avaulible')
return
end
if not io then
net.log("Range.writeFile() - io unavailable, fail to write RangeData")
return
end
local playerUnit = self.playerUnit
if not playerUnit then
self:Init()
return
end
if not playerUnit:isExist() then
self:Init()
return
end
local playerName = playerUnit:getPlayerName()
local dir_path = lfs.writedir()..Range.WriteFilePath..playerName..'/'
local fildName = 'Data_'..playerName
local ok, err = lfs.mkdir(dir_path)
if not ok then
env.info('Range:writeFile() - fail to create folder: '..dir_path)
end
local startTime = Range.Format_time(self.playerData.startTime)
local usedTime = (math.floor((timer.getAbsTime() - self.playerData.startTime)*10)/10)..'s'
local enemyKilled = self.playerData.enemyKilled
local allyKilled = self.playerData.allyKilled
local cloesestEnemyDistanceTest = (math.floor(self.playerData.cloesestEnemyDistance*10)/10)..'米'
if self.playerData.cloesestEnemyDistance > 15000 then
cloesestEnemyDistanceTest = '非常远.'
end
local reason_text = '无'
if reason then reason_text = reason end
local txt_file = io.open(dir_path.."/"..fildName..".txt","w")
local txt_data = {
['玩家'] = playerName,
['开始时间'] = startTime,
['持续时间'] = usedTime,
['击杀敌方目标'] = enemyKilled,
['击杀友方目标'] = allyKilled,
['敌方最近时的距离'] = cloesestEnemyDistanceTest,
['结束原因'] = reason_text,
}
txt_data = net.lua2json(txt_data)
local csv_file = io.open(dir_path.."/"..fildName..".csv","w")
local csv_data = "玩家,开始时间,持续时间,击杀敌方目标,击杀友方目标,敌方最近时的距离,结束原因\n"
csv_data = csv_data..playerName..','
csv_data = csv_data..startTime..','
csv_data = csv_data..usedTime..','
csv_data = csv_data..enemyKilled..','
csv_data = csv_data..allyKilled..','
csv_data = csv_data..cloesestEnemyDistanceTest..','
csv_data = csv_data..reason_text
-- txt_file:write(txt_data)
txt_file:close()
csv_file:write(csv_data)
csv_file:close()
env.info("Range:writeFile() - write successful at: "..dir_path)
end
function Range:ShowResults(reason,Error)
local playerUnit = self.playerUnit
if not playerUnit then
self:Init()
@@ -719,7 +831,7 @@ do
if self.playerData.startTime then
msg = msg..'\n开始时间: '..Range.Format_time(self.playerData.startTime)
msg = msg..'\n持续时间: '..Range.Format_time(timer.getAbsTime() - self.playerData.startTime)
msg = msg..'\n持续时间: '..(math.floor((timer.getAbsTime() - self.playerData.startTime)*10)/10)..'s'
end
msg = msg..'\n击杀敌方目标: '..self.playerData.enemyKilled
@@ -738,7 +850,18 @@ do
end
trigger.action.outTextForUnit(playerUnit:getID(),msg,60,true)
--Sound
if self.playSound then
if not Error then
trigger.action.outSoundForGroup(playerUnit:getGroup():getID(),'audio/SFX/success.ogg')
end
if Error then
trigger.action.outSoundForGroup(playerUnit:getGroup():getID(),'audio/SFX/fail.ogg')
end
end
self:writeFile(reason)
end
function Range._MonitorFunction(vars,time)
@@ -777,7 +900,7 @@ do
return
end
if Utils.pointInPolygon(playerPoint,self.zonePolygon) then
self.EndTraining({context = self,reason = '玩家进入了靶场'})
self.EndTraining({context = self,reason = '玩家进入了靶场',Error = true})
return
end
@@ -862,7 +985,7 @@ do
-- 玩家飞行速度低于30节
if playerSpeed < 30/1.944 then
self.EndTraining({context = self,reason = '玩家速度小于 30 节'})
self.EndTraining({context = self,reason = '玩家速度小于 30 节',Error = true})
return
end
end