Keep track of accuracy record in target practice challenges
authorWuzzy <Wuzzy2@mail.ru>
Mon, 14 Jan 2019 23:30:15 +0100
changeset 14591 b4089fa16b34
parent 14590 c5f18710a184
child 14592 32cf7472da06
Keep track of accuracy record in target practice challenges
QTfrontend/ui/page/pagetraining.cpp
share/hedgewars/Data/Missions/Challenge/Basic_Training_-_Sniper_Rifle.lua
share/hedgewars/Data/Scripts/TargetPractice.lua
share/hedgewars/Data/Scripts/Utils.lua
--- a/QTfrontend/ui/page/pagetraining.cpp	Mon Jan 14 21:19:57 2019 +0100
+++ b/QTfrontend/ui/page/pagetraining.cpp	Mon Jan 14 23:30:15 2019 +0100
@@ -359,6 +359,11 @@
                     //: Lowest score of a team
                     tr("Team lowscore: %1")
                     .arg(getMissionValue(missionName, teamName, "Lowscore").toString()) + "\n";
+            if (missionValueExists(missionName, teamName, "AccuracyRecord"))
+                highscoreText = highscoreText +
+                    //: Best accuracy of a team (in a challenge)
+                    tr("Team's top accuracy: %1%")
+                    .arg(getMissionValue(missionName, teamName, "AccuracyRecord").toString()) + "\n";
             if (missionValueExists(missionName, teamName, "TimeRecord"))
             {
                 double time = ((double) getMissionValue(missionName, teamName, "TimeRecord").toInt()) / 1000.0;
--- a/share/hedgewars/Data/Missions/Challenge/Basic_Training_-_Sniper_Rifle.lua	Mon Jan 14 21:19:57 2019 +0100
+++ b/share/hedgewars/Data/Missions/Challenge/Basic_Training_-_Sniper_Rifle.lua	Mon Jan 14 23:30:15 2019 +0100
@@ -405,23 +405,34 @@
 -- This function calculates the final score of the player and provides some texts and
 -- data for the final stats screen
 function generateStats()
-	local accuracy = 0
+	local accuracy
+	local accuracy_int
 	if shots > 0 then
 		accuracy = (score/shots)*100
+		accuracy_int = div(score*100, shots)
 	end
 	local end_score_targets = getTargetScore()
 	local end_score_overall
 	if not game_lost then
 		local end_score_time = math.ceil(time_goal/5)
-		local end_score_accuracy = math.ceil(accuracy * 100)
+		local end_score_accuracy = 0
+		if shots > 0 then
+			end_score_accuracy = math.ceil(accuracy * 100)
+		end
 		end_score_overall = end_score_time + end_score_targets + end_score_accuracy
 		SetTeamLabel(playerTeamName, tostring(end_score_overall))
 
 		SendStat(siGameResult, loc("You have successfully finished the sniper rifle training!"))
 		SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets (+%d points)."), score, score_goal, end_score_targets))
 		SendStat(siCustomAchievement, string.format(loc("You have made %d shots."), shots))
-		SendStat(siCustomAchievement, string.format(loc("Accuracy bonus: +%d points"), end_score_accuracy))
+		if end_score_accuracy > 0 then
+			SendStat(siCustomAchievement, string.format(loc("Accuracy bonus: +%d points"), end_score_accuracy))
+		end
 		SendStat(siCustomAchievement, string.format(loc("You had %.2fs remaining on the clock (+%d points)."), (time_goal/1000), end_score_time))
+
+		if(shots > 0) then
+			updateChallengeRecord("AccuracyRecord", accuracy_int)
+		end
 	else
 		SendStat(siGameResult, loc("Challenge over!"))
 
--- a/share/hedgewars/Data/Scripts/TargetPractice.lua	Mon Jan 14 21:19:57 2019 +0100
+++ b/share/hedgewars/Data/Scripts/TargetPractice.lua	Mon Jan 14 23:30:15 2019 +0100
@@ -272,12 +272,19 @@
 	end
 
 	_G.generateStats = function()
-		local accuracy = (scored/shots)*100
+		local accuracy, accuracy_int
+		if(shots > 0) then
+			accuracy = (scored/shots)*100
+			accuracy_int = div(scored*100, shots)
+		end
 		local end_score_targets = getTargetsScore()
 		local end_score_overall
 		if not game_lost then
 			local end_score_time = math.ceil(time_goal/(params.time/6000))
-			local end_score_accuracy = math.ceil(accuracy * 60)
+			local end_score_accuracy = 0
+			if(shots > 0) then
+				end_score_accuracy = math.ceil(accuracy * 60)
+			end
 			end_score_overall = end_score_time + end_score_targets + end_score_accuracy
 			SetTeamLabel(GetHogTeamName(player), tostring(end_score_overall))
 
@@ -285,8 +292,14 @@
 
 			SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets (+%d points)."), scored, total_targets, end_score_targets))
 			SendStat(siCustomAchievement, string.format(params.shootText, shots))
-			SendStat(siCustomAchievement, string.format(loc("Your accuracy was %.1f%% (+%d points)."), accuracy, end_score_accuracy))
+			if(shots > 0) then
+				SendStat(siCustomAchievement, string.format(loc("Your accuracy was %.1f%% (+%d points)."), accuracy, end_score_accuracy))
+			end
 			SendStat(siCustomAchievement, string.format(loc("You had %.1fs remaining on the clock (+%d points)."), (time_goal/1000), end_score_time))
+
+			if(shots > 0) then
+				updateChallengeRecord("AccuracyRecord", accuracy_int)
+			end
 		else
 			SendStat(siGameResult, loc("Challenge over!"))
 
--- a/share/hedgewars/Data/Scripts/Utils.lua	Mon Jan 14 21:19:57 2019 +0100
+++ b/share/hedgewars/Data/Scripts/Utils.lua	Mon Jan 14 23:30:15 2019 +0100
@@ -44,6 +44,8 @@
 		return string.format(loc("Team highscore: %d"), value)
 	elseif recordType == "Lowscore" then
 		return string.format(loc("Team lowscore: %d"), value)
+	elseif recordType == "AccuracyRecord" then
+		return string.format(loc("Team's top accuracy: %d%"), value)
 	end
 end
 
@@ -60,7 +62,7 @@
 	local oldRecord = tonumber(GetMissionVar(recordType))
 	local newRecord = false
 	if stat == nil then
-		stat = true
+		stat = recordType ~= "AccuracyRecord"
 	end
 	if type(oldRecord) ~= "number" then
 		newRecord = true