Files
WoW-Hybrid-gateway/compat/glue/source_realm_district_adapter.lua

305 lines
14 KiB
Lua

-- Owned Source realm presentation for the original SuperDistrict ("ruleset")
-- screen. GlueParent controls how long the selected realm is shown while connecting.
return function(provider, preference)
local adapter = {};
local active = false;
local original = nil;
local elapsed = 0;
local category = "octo-source";
local typeLabels = {[0] = "Normal", [1] = "PvP", [6] = "Roleplay", [8] = "RP-PvP"};
local generation, autoAttemptGeneration, autoRequested, autoName;
local accountKey, pendingChoice;
local connectingRealm, shownConnectionName;
local visibleRows = {};
local displayedGeneration;
-- The current realm list is authoritative. A stored address is only a
-- compatibility hint for the old v1 value; it is never a connect target.
local function escape(value)
return (value:gsub("[^%w_.-]", function(char)
return string.format("%%%02X", string.byte(char));
end));
end
local function stableIdentity(info)
if not info or type(info.name) ~= "string" or type(info.sourceAddress) ~= "string"
or type(info.sourceWireID) ~= "number" then return nil; end
local value = "v2|" .. escape(info.name) .. "|" .. tostring(info.sourceWireID);
return #value <= 255 and value or nil;
end
local function legacyIdentity(info)
local stable = stableIdentity(info);
if not stable then return nil; end
local value = "v1|" .. escape(info.name) .. "|" .. escape(info.sourceAddress)
.. "|" .. tostring(info.sourceWireID);
return #value <= 255 and value or nil;
end
local function matchPreference(saved)
local name, address, wire = saved:match("^v1|([^|]+)|([^|]+)|(%d+)$");
local exact = name and address and wire and saved or nil;
if not exact then name, wire = saved:match("^v2|([^|]+)|(%d+)$"); end
if not name or not wire then return nil; end
local stable = "v2|" .. name .. "|" .. wire;
local exactMatch, exactInfo, exactCount = nil, nil, 0;
local stableMatch, stableInfo, stableCount = nil, nil, 0;
for _, index in ipairs(provider.GetRealmsInCategory(category)) do
local row = provider.GetRealmInfo(index);
if row and row.populationState ~= "OFFLINE" then
if stableIdentity(row) == stable then
stableMatch, stableInfo, stableCount = index, row, stableCount + 1;
end
if exact and legacyIdentity(row) == exact then
exactMatch, exactInfo, exactCount = index, row, exactCount + 1;
end
end
end
if exactCount == 1 then return exactMatch, exactInfo, stableCount; end
if exactCount > 1 then return nil; end
if stableCount == 1 then return stableMatch, stableInfo, stableCount; end
return nil;
end
local function readPreference()
if not accountKey or type(preference) ~= "table" or type(preference.Read) ~= "function" then return nil; end
local ok, value = pcall(preference.Read, accountKey);
return ok and type(value) == "string" and #value > 0 and #value <= 255 and value or nil;
end
-- Commit the explicit choice only after the character list arrives for
-- that authenticated account, generation and selected realm.
function adapter.CharacterListReceived()
local choice = pendingChoice;
if not choice then return; end
pendingChoice = nil;
if not provider.Refresh() then return; end
local status = provider.GetLoginStatus();
if status and status.generation == choice.generation and status.account_preference_key == choice.account
and status.state == 2 and status.transport_prepared == 1
and status.selected_index == choice.index
and stableIdentity(provider.GetRealmInfo(choice.index)) == choice.realm then
pcall(preference.Write, choice.account, choice.realm);
end
end
local function pending(frame)
connectingRealm = nil;
if not provider.Refresh() then pendingChoice = nil; return false; end
local status = provider.GetLoginStatus();
if status and (status.generation ~= generation or status.account_preference_key ~= accountKey) then
generation, accountKey = status.generation, status.account_preference_key;
autoAttemptGeneration, autoRequested, autoName, pendingChoice = nil, false, nil, nil;
end
if status and status.state == 2 and status.transport_prepared == 1 and status.selected_index >= 0 then
local selected = provider.GetRealmInfo(status.selected_index);
local native = C_Login and C_Login.GetState and C_Login.GetState();
if selected and native and native.connectedToWoW == false then
connectingRealm = selected.name;
return true;
end
end
local waiting = status ~= nil and status.state == 5 and status.selected_index == -1
and status.transport_prepared == 0;
if waiting and not autoRequested and autoAttemptGeneration ~= generation and not frame.selectedSuperDistrictID then
local saved = readPreference();
if saved then
local match, info = matchPreference(saved);
if match then
autoAttemptGeneration = generation;
local ok = provider.ConnectToRealm(match);
if ok then
autoRequested, autoName = true, info.name;
end
end
end
end
-- A selected Source realm can still be preparing transport. Keep the
-- owned Connecting view until it succeeds or the native state fails.
if status and status.state == 5 and not waiting and active then
autoRequested = true;
return true;
end
return waiting;
end
-- Display wording only; the advertised Source value is kept on the row.
local function populationLabel(value)
local number = tonumber(value);
if not number then return nil; end
if number <= 0.5 then return "Low"; end
if number <= 1.0 then return "Medium"; end
return "High";
end
local function describe(info)
if info.populationState == "OFFLINE" then return "Offline"; end
local parts = {(typeLabels[info.sourceType] or "Normal") .. " realm"};
local population = populationLabel(info.sourcePopulation);
if population then parts[#parts + 1] = "Population: " .. population; end
local count = info.numCharacters or 0;
parts[#parts + 1] = count == 1 and "1 character" or (count .. " characters");
return table.concat(parts, "\n");
end
local function remember(frame)
if original then return; end
original = {
title = frame.Title:GetText(),
description = frame.Description:GetText(),
select = frame.SelectButton:GetText(),
};
end
local function restore(frame)
if not original then return; end
frame.Title:SetText(original.title);
frame.Description:SetText(original.description);
frame.SelectButton:SetText(original.select);
if frame.SubRegionDropdown then
frame.SubRegionDropdown:SetShown(#C_Login.GetSubRegions() > 1);
end
original = nil;
end
function adapter.IsActive() return active; end
-- Returns true when the Source realms own the button list.
function adapter.Refresh(frame)
if not pending(frame) then
if active then active = false; restore(frame); end
return false;
end
remember(frame);
active = true;
frame.Title:SetText("Choose Your Realm");
frame.Description:SetText("Select the Octo realm you want to play on.");
frame.SelectButton:SetText("Enter Realm");
if frame.SubRegionDropdown then frame.SubRegionDropdown:Hide(); end
if connectingRealm or autoRequested then
displayedGeneration = generation;
shownConnectionName = connectingRealm or autoName or "realm";
frame.districtButtonPool:ReleaseAll();
frame.districtButtons = {};
frame.selectedSuperDistrictID = nil;
frame.ButtonLayout:Layout();
frame.Description:SetText("Connecting to " .. shownConnectionName .. "...");
frame.SelectButton:SetEnabled(false);
return true;
end
shownConnectionName = nil;
local icon;
for _, button in ipairs(frame.districtButtons or {}) do
if button.Icon and button.Icon:GetTexture() then icon = button.Icon:GetTexture(); break; end
end
local previousRows, previousGeneration = visibleRows, displayedGeneration;
visibleRows, displayedGeneration = {}, generation;
frame.districtButtonPool:ReleaseAll();
frame.districtButtons = {};
local keep = false;
for layoutIndex, index in ipairs(provider.GetRealmsInCategory(category)) do
local info = provider.GetRealmInfo(index);
if info then
local id = "source:" .. index;
local disabled = info.populationState == "OFFLINE";
local button = frame.districtButtonPool:Acquire("SuperDistrictButtonTemplate");
button.superDistrictID = id;
button.layoutIndex = layoutIndex;
button:SetEnabled(not disabled);
button:SetDisabledVisuals(disabled);
button:SetMotionScriptsWhileDisabled(disabled);
button:SetDisabledTooltip(disabled and "This realm is offline." or nil, "ANCHOR_BOTTOM");
button:SetScript("OnEnter", button.OnEnter);
button:SetScript("OnLeave", button.OnLeave);
button.Title:SetText(info.name);
button.Description:SetText(describe(info));
if icon then button.Icon:SetTexture(icon); end
button:Show();
table.insert(frame.districtButtons, button);
visibleRows[id] = {realm=stableIdentity(info), disabled=disabled};
local previous = previousRows[id];
keep = keep or (frame.selectedSuperDistrictID == id and previousGeneration == generation
and previous and previous.realm == visibleRows[id].realm and not disabled);
end
end
frame.ButtonLayout:Layout();
if not keep then frame.selectedSuperDistrictID = nil; end
frame:UpdateSelection(frame.selectedSuperDistrictID);
return true;
end
-- Returns true when the selection belonged to a Source realm.
function adapter.Confirm(frame, id)
if connectingRealm then return true; end
if type(id) ~= "string" then return false; end
local index = tonumber(id:match("^source:(%d+)$"));
if not index then return false; end
if autoRequested then return true; end
local shown = visibleRows[id];
local fresh = provider.Refresh();
local status = fresh and provider.GetLoginStatus();
local info = fresh and provider.GetRealmInfo(index);
if not shown or shown.disabled or not status or status.generation ~= displayedGeneration
or status.account_preference_key ~= accountKey or not info or info.populationState == "OFFLINE"
or stableIdentity(info) ~= shown.realm then
frame.selectedSuperDistrictID = nil;
frame:RefreshSuperDistrictButtons();
return true;
end
local ok, reason = provider.ConnectToRealm(index);
if not ok then
StaticPopup_Show("OKAY", "This realm cannot be selected right now (" .. tostring(reason) .. ").");
return true;
end
if accountKey and preference and type(preference.Write) == "function" then
pendingChoice = {generation=generation, account=accountKey, index=index, realm=shown.realm};
end
autoRequested, autoName = true, info.name;
frame.SelectButton:SetEnabled(false);
frame.Description:SetText("Connecting to " .. ((info and info.name) or "realm") .. "...");
return true;
end
-- Cancel while a Source realm choice is pending retires that login, so the
-- next submission starts a fresh session instead of a stale one.
function adapter.Cancel(frame)
pendingChoice = nil;
if not active or connectingRealm then return false; end
return provider.Cancel();
end
-- The Source snapshot raises no native event; poll gently while shown.
function adapter.OnUpdate(frame, delta)
elapsed = elapsed + (delta or 0);
if elapsed < 0.5 then return; end
elapsed = 0;
local wasActive = active;
local count = #(frame.districtButtons or {});
if pending(frame) then
local changed = displayedGeneration ~= generation;
if connectingRealm or autoRequested then
changed = changed or count > 0 or shownConnectionName ~= (connectingRealm or autoName or "realm");
else
local indices = provider.GetRealmsInCategory(category);
changed = changed or shownConnectionName ~= nil or count ~= #indices;
for _, index in ipairs(indices) do
local info = provider.GetRealmInfo(index);
local shown = visibleRows["source:" .. index];
if not shown or not info or shown.realm ~= stableIdentity(info)
or shown.disabled ~= (info.populationState == "OFFLINE") then changed = true; break; end
end
end
if not wasActive or changed then adapter.Refresh(frame); end
elseif wasActive then
active = false;
restore(frame);
frame:RefreshSuperDistrictButtons();
end
end
return adapter;
end