local H=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local u=game:GetService("\066\097\100\103\101\083\101\114\118\105\099\101")local z=require((H:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101"))local A=(require((H:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\083\101\114\118\101\114\083\099\114\105\112\116\083\101\114\118\105\099\101"))):GetContext(script)local Z=z:GetResource("\069\110\118\105\114\111\110\109\101\110\116\067\111\110\102\105\103\117\114\097\116\105\111\110")local W=(z:GetResource("\069\120\116\101\114\110\097\108\085\116\105\108\046\078\101\120\117\115\073\110\115\116\097\110\099\101\046\078\101\120\117\115\079\098\106\101\099\116")):Extend()W:SetClassName("\065\099\104\105\101\118\101\109\101\110\116\083\101\114\118\105\099\101")W.CachedAwardedAchievements={}A:SetContextResource(W)function W.PlayerOwnsBadge(A,H,z)local Z=false xpcall(function()Z=u:UserHasBadgeAsync(H.UserId,z)end,function(u)warn("\070\097\105\108\101\100 \116\111 \100\101\116\101\114\109\105\110\101 \098\097\100\103\101 \111\119\110\101\114\115\104\105\112 \111\102 "..(tostring(H.Name)..(" \102\111\114 "..(tostring(z)..(" \098\101\099\097\117\115\101 "..tostring(u))))))end)return Z end function W.AwardAchievement(A,H,z)if not A.CachedAwardedAchievements[H.UserId]then A.CachedAwardedAchievements[H.UserId]={}end if A.CachedAwardedAchievements[H.UserId][z]then return end A.CachedAwardedAchievements[H.UserId][z]=true;(coroutine.wrap(function()local W=Z.Badges[z]if not A:PlayerOwnsBadge(H,W)then xpcall(function()u:AwardBadge(H.UserId,W)end,function(u)warn("\070\097\105\108\101\100 \116\111 \097\119\097\114\100 \098\097\100\103\101 "..(tostring(z)..(" \040"..(tostring(W)..("\041  \102\111\114"..(tostring(H.Name)..(" \098\101\099\097\117\115\101 "..tostring(u))))))))end)end if z=="\084\104\101\080\111\114\116\097\108"and A:PlayerOwnsBadge(H,134237042)then A:AwardAchievement(H,"\066\108\111\120\104\105\108\100\097\115\082\101\118\101\110\103\101")end end))()end return W
--[[
TheNexusAvenger

Awards badges for completing achievements.
Class is static (should not be created).
--]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BadgeService = game:GetService("BadgeService")

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))
local ServerScriptServiceProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ServerScriptService")):GetContext(script)

local EnvironmentConfiguration = ReplicatedStorageProject:GetResource("EnvironmentConfiguration")

local AchievementService = ReplicatedStorageProject:GetResource("ExternalUtil.NexusInstance.NexusObject"):Extend()
AchievementService:SetClassName("AchievementService")
AchievementService.CachedAwardedAchievements = {}
ServerScriptServiceProject:SetContextResource(AchievementService)



--[[
Returns if the player owns a badge.
--]]
function AchievementService:PlayerOwnsBadge(Player,BadgeId)
    local BadgeOwned = false
    xpcall(function()
        BadgeOwned = BadgeService:UserHasBadgeAsync(Player.UserId,BadgeId)
    end,function(ErrorMessage)
        warn("Failed to determine badge ownership of "..tostring(Player.Name).." for "..tostring(BadgeId).." because "..tostring(ErrorMessage))
    end)
    return BadgeOwned
end
--[[
Awards an achievement for a player.
--]]
function AchievementService:AwardAchievement(Player,AchievementName)
    --Return if the achievement was given.
    if not self.CachedAwardedAchievements[Player.UserId] then
        self.CachedAwardedAchievements[Player.UserId] = {}
    end
    if self.CachedAwardedAchievements[Player.UserId][AchievementName] then
        return
    end
    self.CachedAwardedAchievements[Player.UserId][AchievementName] = true

    coroutine.wrap(function()
        --Award the badge if it hasn't been awarded before.
        local BadgeId = EnvironmentConfiguration.Badges[AchievementName]
        if not self:PlayerOwnsBadge(Player,BadgeId) then
            xpcall(function()
                BadgeService:AwardBadge(Player.UserId,BadgeId)
            end,function(ErrorMessage)
                warn("Failed to award badge "..tostring(AchievementName).." ("..tostring(BadgeId)..")  for"..tostring(Player.Name).." because "..tostring(ErrorMessage))
            end)
        end

        --Award the Bloxhilda's Revenge achievement.
        if AchievementName == "ThePortal" and self:PlayerOwnsBadge(Player,134237042) then
            self:AwardAchievement(Player,"BloxhildasRevenge")
        end
    end)()
end



return AchievementService