371 lines
13 KiB
Lua
371 lines
13 KiB
Lua
local previewCacheDirectory = "feedc0de_coop/map_previews"
|
|
local voteFrame
|
|
local voteState
|
|
|
|
surface.CreateFont("CustomCoopMapVoteTitle", {
|
|
font = "Roboto",
|
|
size = math.Clamp(math.floor(ScrH() * 0.035), 24, 40),
|
|
weight = 800,
|
|
})
|
|
|
|
surface.CreateFont("CustomCoopMapVoteHeading", {
|
|
font = "Roboto",
|
|
size = math.Clamp(math.floor(ScrH() * 0.023), 18, 28),
|
|
weight = 700,
|
|
})
|
|
|
|
surface.CreateFont("CustomCoopMapVoteBody", {
|
|
font = "Roboto",
|
|
size = math.Clamp(math.floor(ScrH() * 0.018), 15, 22),
|
|
weight = 500,
|
|
})
|
|
|
|
surface.CreateFont("CustomCoopMapVoteSmall", {
|
|
font = "Roboto",
|
|
size = math.Clamp(math.floor(ScrH() * 0.014), 12, 17),
|
|
weight = 500,
|
|
})
|
|
|
|
local colors = {
|
|
backdrop = Color(8, 12, 18, 248),
|
|
panel = Color(18, 27, 38, 250),
|
|
card = Color(29, 41, 55, 255),
|
|
cardHover = Color(38, 55, 72, 255),
|
|
border = Color(71, 92, 112, 255),
|
|
accent = Color(77, 176, 255, 255),
|
|
selected = Color(255, 181, 71, 255),
|
|
winner = Color(84, 210, 139, 255),
|
|
text = Color(238, 244, 250, 255),
|
|
muted = Color(159, 176, 191, 255),
|
|
}
|
|
|
|
local function DisplayMapName(mapName)
|
|
local words = string.Replace(mapName or "", "_", " ")
|
|
words = string.Replace(words, "-", " ")
|
|
return string.upper(string.sub(words, 1, 1)) .. string.sub(words, 2)
|
|
end
|
|
|
|
local function EnsurePreviewDirectory()
|
|
file.CreateDir("feedc0de_coop")
|
|
file.CreateDir(previewCacheDirectory)
|
|
end
|
|
|
|
local function SetCachedPreview(card, cachePath)
|
|
if not IsValid(card) or not file.Exists(cachePath, "DATA") then return end
|
|
local material = Material("data/" .. cachePath, "smooth")
|
|
if not material:IsError() then card.PreviewMaterial = material end
|
|
end
|
|
|
|
local function ExistingCachePath(cacheStem)
|
|
for _, extension in ipairs({ ".jpg", ".png" }) do
|
|
local path = cacheStem .. extension
|
|
if file.Exists(path, "DATA") then return path end
|
|
end
|
|
end
|
|
|
|
local function PreviewCachePath(cacheStem, body, headers)
|
|
local contentType = string.lower(tostring((headers or {})["content-type"] or ""))
|
|
if string.sub(body or "", 1, 8) == "\137PNG\r\n\26\n" or string.find(contentType, "image/png", 1, true) then
|
|
return cacheStem .. ".png"
|
|
end
|
|
return cacheStem .. ".jpg"
|
|
end
|
|
|
|
local function EncodeURLPathComponent(value)
|
|
local encoded = string.gsub(tostring(value or ""), "[^%w%-._~]", function(character)
|
|
return string.format("%%%02X", string.byte(character))
|
|
end)
|
|
return encoded
|
|
end
|
|
|
|
local function FetchHTTPPreview(card, url, cacheStem, fallback)
|
|
http.Fetch(url, function(body, _, headers, code)
|
|
local cachePath = PreviewCachePath(cacheStem, body, headers)
|
|
if code == 200 and body and #body > 0 and file.Write(cachePath, body) then
|
|
SetCachedPreview(card, cachePath)
|
|
return
|
|
end
|
|
if fallback then fallback() end
|
|
end, function()
|
|
if fallback then fallback() end
|
|
end)
|
|
end
|
|
|
|
local function FetchPreview(card, baseURL, mapName, workshopID)
|
|
EnsurePreviewDirectory()
|
|
|
|
local cacheKey = workshopID ~= "" and ("workshop:" .. workshopID) or (baseURL .. "/" .. mapName)
|
|
local cacheStem = previewCacheDirectory .. "/" .. util.CRC(cacheKey)
|
|
local cachePath = ExistingCachePath(cacheStem)
|
|
if cachePath then
|
|
SetCachedPreview(card, cachePath)
|
|
if os.time() - file.Time(cachePath, "DATA") < 86400 then return end
|
|
end
|
|
|
|
local function fetchPVCPreview()
|
|
if not baseURL or baseURL == "" then return end
|
|
local root = string.gsub(baseURL, "/+$", "")
|
|
FetchHTTPPreview(card, root .. "/" .. EncodeURLPathComponent(mapName) .. ".jpg", cacheStem)
|
|
end
|
|
|
|
if workshopID == "" then
|
|
fetchPVCPreview()
|
|
return
|
|
end
|
|
|
|
steamworks.FileInfo(workshopID, function(info)
|
|
if not info then
|
|
fetchPVCPreview()
|
|
return
|
|
end
|
|
|
|
if info.previewurl and info.previewurl ~= "" then
|
|
FetchHTTPPreview(card, info.previewurl, cacheStem, fetchPVCPreview)
|
|
return
|
|
end
|
|
|
|
if info.previewid then
|
|
steamworks.Download(info.previewid, true, function(path)
|
|
if not path then
|
|
fetchPVCPreview()
|
|
return
|
|
end
|
|
local material = AddonMaterial(path)
|
|
if IsValid(card) and material and not material:IsError() then
|
|
card.PreviewMaterial = material
|
|
end
|
|
end)
|
|
return
|
|
end
|
|
|
|
fetchPVCPreview()
|
|
end)
|
|
end
|
|
|
|
local function DrawPreviewCover(material, x, y, width, height)
|
|
local sourceWidth = math.max(material:Width(), 1)
|
|
local sourceHeight = math.max(material:Height(), 1)
|
|
local sourceAspect = sourceWidth / sourceHeight
|
|
local targetAspect = width / height
|
|
local u0, v0, u1, v1 = 0, 0, 1, 1
|
|
|
|
if sourceAspect > targetAspect then
|
|
local visible = targetAspect / sourceAspect
|
|
u0 = (1 - visible) / 2
|
|
u1 = 1 - u0
|
|
elseif sourceAspect < targetAspect then
|
|
local visible = sourceAspect / targetAspect
|
|
v0 = (1 - visible) / 2
|
|
v1 = 1 - v0
|
|
end
|
|
|
|
surface.SetDrawColor(255, 255, 255)
|
|
surface.SetMaterial(material)
|
|
surface.DrawTexturedRectUV(x, y, width, height, u0, v0, u1, v1)
|
|
end
|
|
|
|
local function DrawCard(card, width, height)
|
|
local state = voteState
|
|
local selected = state and state.selected == card.ChoiceIndex
|
|
local winner = state and state.winner == card.ChoiceIndex
|
|
local border = winner and colors.winner or (selected and colors.selected or colors.border)
|
|
local background = card:IsHovered() and colors.cardHover or colors.card
|
|
|
|
draw.RoundedBox(8, 0, 0, width, height, border)
|
|
draw.RoundedBox(7, 2, 2, width - 4, height - 4, background)
|
|
|
|
local imageHeight = math.floor((width - 4) * 9 / 16)
|
|
imageHeight = math.min(imageHeight, height - 68)
|
|
if card.PreviewMaterial then
|
|
DrawPreviewCover(card.PreviewMaterial, 2, 2, width - 4, imageHeight)
|
|
else
|
|
surface.SetDrawColor(20, 30, 42)
|
|
surface.DrawRect(2, 2, width - 4, imageHeight)
|
|
draw.SimpleText("PREVIEW UNAVAILABLE", "CustomCoopMapVoteSmall", width / 2, imageHeight / 2, colors.muted, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
|
|
end
|
|
|
|
draw.SimpleText(DisplayMapName(card.MapName), "CustomCoopMapVoteHeading", 12, imageHeight + 12, colors.text, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
|
|
draw.SimpleText(card.MapName, "CustomCoopMapVoteSmall", 12, height - 25, colors.muted, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
|
|
|
|
local voteCount = state and state.counts[card.ChoiceIndex] or 0
|
|
local badge = tostring(voteCount) .. (voteCount == 1 and " vote" or " votes")
|
|
surface.SetFont("CustomCoopMapVoteSmall")
|
|
local badgeWidth = surface.GetTextSize(badge) + 16
|
|
draw.RoundedBox(10, width - badgeWidth - 10, height - 29, badgeWidth, 21, Color(8, 13, 19, 220))
|
|
draw.SimpleText(badge, "CustomCoopMapVoteSmall", width - badgeWidth / 2 - 10, height - 19, colors.text, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
|
|
|
|
if selected or winner then
|
|
local label = winner and "WINNER" or "YOUR CHOICE"
|
|
draw.RoundedBox(4, 8, 8, 92, 24, border)
|
|
draw.SimpleText(label, "CustomCoopMapVoteSmall", 54, 20, Color(8, 13, 19), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
|
|
end
|
|
end
|
|
|
|
local function CloseVoteFrame()
|
|
if IsValid(voteFrame) then voteFrame:Remove() end
|
|
voteFrame = nil
|
|
end
|
|
|
|
local function OpenVoteFrame()
|
|
CloseVoteFrame()
|
|
if not voteState then return end
|
|
|
|
local frameWidth = math.min(math.floor(ScrW() * 0.92), 1320)
|
|
local frameHeight = math.min(math.floor(ScrH() * 0.9), 860)
|
|
voteFrame = vgui.Create("DFrame")
|
|
voteFrame:SetSize(frameWidth, frameHeight)
|
|
voteFrame:Center()
|
|
voteFrame:SetTitle("")
|
|
voteFrame:SetDraggable(false)
|
|
voteFrame:ShowCloseButton(false)
|
|
voteFrame:MakePopup()
|
|
voteFrame.Paint = function(_, width, height)
|
|
Derma_DrawBackgroundBlur(voteFrame, voteState.openedAt)
|
|
draw.RoundedBox(10, 0, 0, width, height, colors.backdrop)
|
|
surface.SetDrawColor(colors.accent)
|
|
surface.DrawRect(0, 0, width, 4)
|
|
end
|
|
|
|
local header = vgui.Create("DPanel", voteFrame)
|
|
header:Dock(TOP)
|
|
header:SetTall(math.Clamp(math.floor(ScrH() * 0.13), 92, 126))
|
|
header:DockMargin(20, 14, 20, 0)
|
|
header.Paint = function(_, width)
|
|
draw.SimpleText("CHOOSE THE NEXT MISSION", "CustomCoopMapVoteTitle", 0, 4, colors.text, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
|
|
draw.SimpleText(voteState.reason, "CustomCoopMapVoteBody", 0, 48, colors.muted, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
|
|
local remaining = math.max(math.ceil(voteState.endsAt - CurTime()), 0)
|
|
local timerText = voteState.finished and "DECIDED" or tostring(remaining) .. "s"
|
|
draw.SimpleText(timerText, "CustomCoopMapVoteTitle", width, 6, voteState.finished and colors.winner or colors.accent, TEXT_ALIGN_RIGHT, TEXT_ALIGN_TOP)
|
|
draw.SimpleText(
|
|
string.format("%d/%d players voted", voteState.submitted, voteState.players),
|
|
"CustomCoopMapVoteSmall",
|
|
width,
|
|
51,
|
|
colors.muted,
|
|
TEXT_ALIGN_RIGHT,
|
|
TEXT_ALIGN_TOP
|
|
)
|
|
end
|
|
|
|
local footer = vgui.Create("DPanel", voteFrame)
|
|
footer:Dock(BOTTOM)
|
|
footer:SetTall(50)
|
|
footer:DockMargin(20, 0, 20, 12)
|
|
footer.Paint = function(_, width, height)
|
|
draw.RoundedBox(6, 0, 5, width, height - 5, colors.panel)
|
|
local text = voteState.resultText or "Select one map. Your choice is highlighted; the vote ends early when everyone has chosen."
|
|
draw.SimpleText(text, "CustomCoopMapVoteBody", width / 2, height / 2 + 2, voteState.finished and colors.winner or colors.muted, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
|
|
end
|
|
|
|
local scroll = vgui.Create("DScrollPanel", voteFrame)
|
|
scroll:Dock(FILL)
|
|
scroll:DockMargin(18, 8, 10, 8)
|
|
|
|
local grid = vgui.Create("DPanel", scroll)
|
|
grid:Dock(TOP)
|
|
grid.Paint = nil
|
|
|
|
local availableWidth = math.max(frameWidth - 60, 220)
|
|
local columns = availableWidth >= 1120 and 4 or (availableWidth >= 760 and 3 or (availableWidth >= 500 and 2 or 1))
|
|
local spacing = 12
|
|
local cardWidth = math.floor((availableWidth - (columns - 1) * spacing) / columns)
|
|
local cardHeight = math.floor(cardWidth * 9 / 16) + 68
|
|
local rows = math.ceil(#voteState.choices / columns)
|
|
grid:SetTall(rows * cardHeight + math.max(rows - 1, 0) * spacing)
|
|
|
|
voteState.cards = {}
|
|
for index, mapName in ipairs(voteState.choices) do
|
|
local card = vgui.Create("DButton", grid)
|
|
local column = (index - 1) % columns
|
|
local row = math.floor((index - 1) / columns)
|
|
card:SetPos(column * (cardWidth + spacing), row * (cardHeight + spacing))
|
|
card:SetSize(cardWidth, cardHeight)
|
|
card:SetText("")
|
|
card.ChoiceIndex = index
|
|
card.MapName = mapName
|
|
card:SetTooltip(mapName)
|
|
card.Paint = DrawCard
|
|
card.DoClick = function()
|
|
if voteState.finished then return end
|
|
voteState.selected = index
|
|
net.Start("CustomCoopMapVoteCast")
|
|
net.WriteUInt(voteState.id, 16)
|
|
net.WriteUInt(index, 4)
|
|
net.SendToServer()
|
|
end
|
|
voteState.cards[index] = card
|
|
end
|
|
|
|
for index, card in ipairs(voteState.cards) do
|
|
local mapName = voteState.choices[index]
|
|
local succeeded, message = pcall(
|
|
FetchPreview,
|
|
card,
|
|
voteState.previewBaseURL,
|
|
mapName,
|
|
voteState.workshopIDs[index] or ""
|
|
)
|
|
if not succeeded then
|
|
ErrorNoHalt("[custom_coop] Could not request preview for " .. mapName .. ": " .. tostring(message) .. "\n")
|
|
end
|
|
end
|
|
end
|
|
|
|
net.Receive("CustomCoopMapVoteStart", function()
|
|
local id = net.ReadUInt(16)
|
|
local duration = net.ReadFloat()
|
|
local reason = net.ReadString()
|
|
local previewBaseURL = net.ReadString()
|
|
local choiceCount = net.ReadUInt(4)
|
|
local choices = {}
|
|
local workshopIDs = {}
|
|
for index = 1, choiceCount do
|
|
choices[index] = net.ReadString()
|
|
workshopIDs[index] = net.ReadString()
|
|
end
|
|
|
|
voteState = {
|
|
id = id,
|
|
openedAt = SysTime(),
|
|
endsAt = CurTime() + duration,
|
|
reason = reason,
|
|
previewBaseURL = previewBaseURL,
|
|
choices = choices,
|
|
workshopIDs = workshopIDs,
|
|
counts = {},
|
|
players = 0,
|
|
submitted = 0,
|
|
finished = false,
|
|
}
|
|
for index = 1, choiceCount do voteState.counts[index] = 0 end
|
|
OpenVoteFrame()
|
|
end)
|
|
|
|
net.Receive("CustomCoopMapVoteUpdate", function()
|
|
local id = net.ReadUInt(16)
|
|
local players = net.ReadUInt(8)
|
|
local submitted = net.ReadUInt(8)
|
|
local choiceCount = net.ReadUInt(4)
|
|
local counts = {}
|
|
for index = 1, choiceCount do counts[index] = net.ReadUInt(8) end
|
|
if not voteState or voteState.id ~= id then return end
|
|
voteState.players = players
|
|
voteState.submitted = submitted
|
|
voteState.counts = counts
|
|
end)
|
|
|
|
net.Receive("CustomCoopMapVoteResult", function()
|
|
local id = net.ReadUInt(16)
|
|
local winner = net.ReadString()
|
|
local resolution = net.ReadString()
|
|
local delay = net.ReadFloat()
|
|
if not voteState or voteState.id ~= id then return end
|
|
|
|
voteState.finished = true
|
|
voteState.resultText = resolution .. ". Loading " .. DisplayMapName(winner) .. "…"
|
|
for index, mapName in ipairs(voteState.choices) do
|
|
if mapName == winner then voteState.winner = index break end
|
|
end
|
|
timer.Create("CustomCoopMapVoteClose", math.max(delay + 4, 4), 1, CloseVoteFrame)
|
|
end)
|