function PlayerSpots.cleanup(z,S)z:_removeUnusedSpots(S)for S,z in ipairs(z.placedSpots)do z.spot:removeArt()end z.placedSpots={}end function PlayerSpots._hasArtOnCanvas(z,S)for z,J in ipairs(z.placedSpots)do if J.canvas==S then return true end end return false end function PlayerSpots._assignRandomSpot(z,S)local J,U=S:getEmptySpot()if not J then warn("\067\097\110\110\111\116 \102\105\110\100 \097\110 \101\109\112\116\121 \115\108\111\116 \105\110 \099\097\110\118\097\115")return false,constants.Errors.SpotNotAvailable end table.insert(z.placedSpots,{canvas=S;spot=U;createdAt=(DateTime.now()).UnixTimestamp})return true,U end function PlayerSpots._removeOldestArt(S)S.toRemove=1 end function PlayerSpots._removeOldestArtFromCanvas(z,S)local J=nil for z,U in ipairs(z.placedSpots)do if U.canvas==S then J=z U.spot:removeArt()break end end table.remove(z.placedSpots,J)end function PlayerSpots._removeOldestArtFromOtherCanvas(z,S)for J,U in ipairs(z.placedSpots)do if U.canvas~=S then z.toRemove=J break end end end function PlayerSpots._removeUnusedSpots(z,S)z.placedSpots=Cryo.List.filter(z.placedSpots,function(z)return z.spot:hasArt()and z.spot.ownerUserId==S.UserId end)end return PlayerSpots
--[[
	Remove all art placed by a player.
]]
function PlayerSpots:cleanup(player)
	self:_removeUnusedSpots(player)

	for _, placed in ipairs(self.placedSpots) do
		placed.spot:removeArt()
	end

	self.placedSpots = {}
end

function PlayerSpots:_hasArtOnCanvas(canvas)
	for _, placed in ipairs(self.placedSpots) do
		if placed.canvas == canvas then
			return true
		end
	end

	return false
end

function PlayerSpots:_assignRandomSpot(canvas)
	local isSuccess, spot = canvas:getEmptySpot()
	if not isSuccess then
		warn("Cannot find an empty slot in canvas")
		return false, constants.Errors.SpotNotAvailable
	end

	table.insert(self.placedSpots, {
		canvas = canvas,
		spot = spot,
		createdAt = DateTime.now().UnixTimestamp,
	})

	return true, spot
end

function PlayerSpots:_removeOldestArt()
	self.toRemove = 1
end

function PlayerSpots:_removeOldestArtFromCanvas(canvas)
	local toRemove = nil
	for i, placed in ipairs(self.placedSpots) do
		if placed.canvas == canvas then
			-- A spot previously placed by the user has to be freed immediately
			toRemove = i
			placed.spot:removeArt()
			break
		end
	end

	table.remove(self.placedSpots, toRemove)
end

function PlayerSpots:_removeOldestArtFromOtherCanvas(currentCanvas)
	for i, placed in ipairs(self.placedSpots) do
		if placed.canvas ~= currentCanvas then
			self.toRemove = i
			break
		end
	end
end

function PlayerSpots:_removeUnusedSpots(player)
	self.placedSpots = Cryo.List.filter(self.placedSpots, function(placedSpot)
		return placedSpot.spot:hasArt() and placedSpot.spot.ownerUserId == player.UserId
	end)
end

return PlayerSpots
