share/hedgewars/Data/Scripts/Utils.lua
changeset 14496 2113296b7a29
parent 14252 74bf2d906097
child 14546 029f40c609b4
--- a/share/hedgewars/Data/Scripts/Utils.lua	Wed Dec 19 01:47:41 2018 +0100
+++ b/share/hedgewars/Data/Scripts/Utils.lua	Wed Dec 19 05:50:02 2018 +0100
@@ -1,5 +1,7 @@
 -- Library for miscellaneous utilitiy functions and global helper variables
 
+HedgewarsScriptLoad("/Scripts/Locale.lua")
+
 --[[ FUNCTIONS ]]
 -- Check if a gear is inside a box
 function gearIsInBox(gear, x, y, w, h)
@@ -33,6 +35,59 @@
 	end
 end
 
+local function challengeRecordToString(recordType, value)
+	if recordType == "TimeRecord" or recordType == "TimeRecordHigh" then
+		return string.format(loc("Team record: %.3fs"), value/1000)
+	elseif recordType == "Highscore" then
+		return string.format(loc("Team high score: %d"), value)
+	else
+		return string.format(loc("Team record: %d"), value)
+	end
+end
+
+function getReadableChallengeRecord(recordType)
+	local record = tonumber(GetMissionVar(recordType))
+	if type(record) ~= "number" then
+		return ""
+	else
+		return challengeRecordToString(recordType, record)
+	end
+end
+
+function updateChallengeRecord(recordType, value, stat)
+	local oldRecord = tonumber(GetMissionVar(recordType))
+	local newRecord = false
+	if stat == nil then
+		stat = true
+	end
+	if type(oldRecord) ~= "number" then
+		newRecord = true
+	else
+		local recordBeaten = false
+		if recordType == "Lowscore" or recordType == "TimeRecord" then
+			if value < oldRecord then
+				recordBeaten = true
+				newRecord = true
+			end
+		else
+			if value > oldRecord then
+				recordBeaten = true
+				newRecord = true
+			end
+		end
+		if stat then
+			if recordBeaten then
+				SendStat(siCustomAchievement, loc("You have beaten the team record, congratulations!"))
+			else
+				SendStat(siCustomAchievement, challengeRecordToString(recordType, oldRecord))
+			end
+		end
+	end
+	if newRecord then
+		SaveMissionVar(recordType, value)
+	end
+end
+
 -- Completely fill the map with land. Requires MapGen=mgDrawn.
 -- If flush is false, FlushPoints() is not called.
 function fillMap(flush)