-- Functions

function PlayerManager:SetGameRunning(running)
	GameRunning = running
end

function PlayerManager:AllowPlayerSpawn(allow)
	PlayersCanSpawn = allow
end

function PlayerManager:ClearPlayerScores()
	for _, player in ipairs(Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local KWA
			if Configurations.GRANT_ASSISTS then
				KWA = leaderstats:FindFirstChild("KO - WO - A")
				if KWA then
					KWA.Value = "0 - 0 - 0"
				end
			else
				KWA = leaderstats:FindFirstChild("KO - WO")
				if KWA then
					KWA.Value = "0 - 0"
				end
			end
		end
	end
end

function PlayerManager:LoadPlayers()
	for _, player in pairs(Players:GetPlayers()) do
		player:LoadCharacter()
	end
end

function PlayerManager:DestroyPlayers()
	for _, player in pairs(Players:GetPlayers()) do
		player.Character:Destroy()
		for _, item in pairs(player.Backpack:GetChildren()) do
			item:Destroy()
		end
	end
	ResetMouseIcon:FireAllClients()
end

function PlayerManager:PlayerHit(hitPlayer, hittingPlayer)
	if hitPlayer == hittingPlayer then
		return
	end
	
	if not HitArray[hitPlayer] then
		HitArray[hitPlayer] = { }
	end
	
	for i = 1, #HitArray[hitPlayer] do
		if HitArray[hitPlayer][i]== hittingPlayer then
			return
		end
	end
	table.insert(HitArray[hitPlayer], hittingPlayer)
end

function PlayerManager:PlayerDied(player)
	if HitArray[player] then
		for i = #HitArray[player], 1, -1 do
			local hittingPlayer = HitArray[player][i]
			if hittingPlayer ~= player then
				if i == #HitArray[player] then
					ScoreArray[hittingPlayer][1] = ScoreArray[hittingPlayer][1] + 1
					TeamManager:AddTeamScore(hittingPlayer.TeamColor, 1)
					spawn(function() pcall(function() PointsService:AwardPoints(hittingPlayer.userId, Configurations.PLAYER_POINTS_PER_KO) end) end)
				else
					ScoreArray[hittingPlayer][3] = ScoreArray[hittingPlayer][3] + 1
					spawn(function() pcall(function() PointsService:AwardPoints(hittingPlayer.userId, Configurations.PLAYER_POINTS_PER_ASSIST) end) end)
				end
				UpdatePlayerScore(hittingPlayer)
			end
		end
	end
	
	ScoreArray[player][2] = ScoreArray[player][2] + 1
	HitArray[player] = nil
	UpdatePlayerScore(player)
end
