Skip to content

Commit

Permalink
Rework sc2 person infoboxes (#3294)
Browse files Browse the repository at this point in the history
* Create infobox_person_custom.lua

* Delete components/infobox/wikis/starcraft2/infobox_person_custom_shared.lua

* Update infobox_person_map_maker_custom.lua

* Update infobox_person_user_custom.lua

* Update infobox_person_player_custom.lua

* mising args

* missing args

* Update infobox_person_user_custom.lua

* as per review

* fix typo and use self
  • Loading branch information
hjpalpha authored Oct 6, 2023
1 parent cb09b1e commit cff052f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
---
-- @Liquipedia
-- wiki=starcraft2
-- page=Module:Infobox/Person/Custom/Shared
-- page=Module:Infobox/Person/Custom
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

--Shared functions between SC2 person infoboxes (Player/Commentator and MapMaker)
--Extends the Infobox/Person class with shared functions between SC2 person infoboxes

local Array = require('Module:Array')
local Class = require('Module:Class')
local Faction = require('Module:Faction')
local FnUtil = require('Module:FnUtil')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local Namespace = require('Module:Namespace')
local String = require('Module:StringUtils')
local Table = require('Module:Table')
local Variables = require('Module:Variables')

local Person = Lua.import('Module:Infobox/Person', {requireDevIfEnabled = true})

local RACE_ALL = 'All'
local RACE_ALL_SHORT = 'a'
local RACE_ALL_ICON = '[[File:RaceIcon All.png|30px|link=]]'
Expand Down Expand Up @@ -47,20 +51,14 @@ MILITARY_DATA.pending = MILITARY_DATA.starting
MILITARY_DATA.started = MILITARY_DATA.ongoing
MILITARY_DATA.ending = MILITARY_DATA.ongoing

local _militaryStore
local _args

local _PAGENAME = mw.title.getCurrentTitle().prefixedText
---@class SC2CustomPerson: Person
local CustomPerson = Class.new(Person)

local CustomPerson = {}

function CustomPerson.setArgs(args)
_args = args
end

function CustomPerson.shouldStoreData()
---@param args table
---@return boolean
function CustomPerson:shouldStoreData(args)
if
Logic.readBool(_args.disable_lpdb) or Logic.readBool(_args.disable_storage)
Logic.readBool(args.disable_lpdb) or Logic.readBool(args.disable_storage)
or Logic.readBool(Variables.varDefault('disable_LPDB_storage'))
or not Namespace.isMain()
then
Expand All @@ -70,8 +68,10 @@ function CustomPerson.shouldStoreData()
return true
end

function CustomPerson.nameDisplay()
local raceData = CustomPerson.readFactions(_args.race or Faction.defaultFaction)
---@param args table
---@return string
function CustomPerson:nameDisplay(args)
local raceData = self:readFactions(args.race or Faction.defaultFaction)

local raceIcons
if raceData.isAll then
Expand All @@ -82,13 +82,16 @@ function CustomPerson.nameDisplay()
end))
end

local name = _args.id or _PAGENAME
local name = args.id or self.pagename

return raceIcons .. ' ' .. name
end

function CustomPerson.getRaceData(race, asCategory)
local factions = CustomPerson.readFactions(race).factions
---@param race string?
---@param asCategory boolean?
---@return string
function CustomPerson:getRaceData(race, asCategory)
local factions = self:readFactions(race).factions

return table.concat(Array.map(factions, function(faction)
if asCategory then
Expand All @@ -98,7 +101,9 @@ function CustomPerson.getRaceData(race, asCategory)
end) or {}, ', ')
end

function CustomPerson.readFactions(input)
---@param input string?
---@return {isAll: boolean, factions: string[]}
function CustomPerson:readFactions(input)
local factions
if input == RACE_ALL or input == RACE_ALL_SHORT then
factions = Array.copy(Faction.coreFactions)
Expand All @@ -113,21 +118,25 @@ function CustomPerson.readFactions(input)
return {isAll = isAll, factions = factions}
end

function CustomPerson.adjustLPDB(_, lpdbData)
---@param lpdbData table<string, string|number|table|nil>
---@param args table
---@param personType string
---@return table<string, string|number|table|nil>
function CustomPerson:adjustLPDB(lpdbData, args, personType)
local extradata = lpdbData.extradata or {}

local raceData = CustomPerson.readFactions(_args.race)
local raceData = self:readFactions(args.race)

extradata.race = raceData.isAll and RACE_ALL_SHORT or raceData.factions[1]
extradata.faction = raceData.isAll and RACE_ALL or raceData.factions[1]
extradata.faction2 = (not raceData.isAll) and raceData.factions[2] or nil
extradata.lc_id = string.lower(_PAGENAME)
extradata.teamname = _args.team
extradata.role = _args.role
extradata.role2 = _args.role2
extradata.militaryservice = _militaryStore
extradata.activeplayer = not CustomPerson.getStatusToStore()
and CustomPerson._isPlayer(_args)
extradata.lc_id = string.lower(self.pagename)
extradata.teamname = args.team
extradata.role = args.role
extradata.role2 = args.role2
extradata.militaryservice = self:military(args.military).storeValue
extradata.activeplayer = not CustomPerson:getStatusToStore(args)
and CustomPerson._isPlayer(args)
and Variables.varDefault('isActive', '') or ''

if Variables.varDefault('racecount') then
Expand All @@ -140,50 +149,52 @@ function CustomPerson.adjustLPDB(_, lpdbData)
return lpdbData
end

---@param args table
---@return boolean
function CustomPerson._isPlayer(args)
return not Logic.readBool(_args.isplayer) and
string.lower(_args.role or _args.defaultPersonType) ~= 'player'
return not Logic.readBool(args.isplayer) and
string.lower(args.role or args.defaultPersonType) ~= 'player'
end

function CustomPerson.military(military)
if military and military ~= 'false' then
local display = military
local militaryCategory = ''
military = string.lower(military)
for key, item in pairs(MILITARY_DATA) do
if String.contains(military, key) then
militaryCategory = '[[Category:' .. item.category .. ']]'
_militaryStore = item.storeValue
break
end
end
---@param military string?
---@return {category: string?, storeValue: string?}
function CustomPerson:military(military)
if not Logic.readBool(military) then return {} end

return display .. militaryCategory
military = military:lower()
for key, data in pairs(MILITARY_DATA) do
if String.contains(military, key) then return data end
end

return {}
end

function CustomPerson.getStatusToStore()
if _args.death_date then
---@param args table
---@return string?
function CustomPerson:getStatusToStore(args)
if args.death_date then
return 'Deceased'
elseif _args.retired then
elseif args.retired then
return 'Retired'
end
end

function CustomPerson.getPersonType()
if _args.isplayer == 'true' then
---@param args table
---@return {store: string, category: string}
function CustomPerson:getPersonType(args)
if args.isplayer == 'true' then
return {store = 'Player', category = 'Player'}
end

local role = _args.role or _args.occupation or _args.defaultPersonType
local role = args.role or args.occupation or args.defaultPersonType
role = string.lower(role or '')
local category = ROLES[role]
local store = category or CLEAN_OTHER_ROLES[role] or _args.defaultPersonType
local store = category or CLEAN_OTHER_ROLES[role] or args.defaultPersonType
if category == ROLES['map maker'] then
category = 'Mapmaker'
end

return {store = store, category = category or _args.defaultPersonType}
return {store = store, category = category or args.defaultPersonType}
end

return CustomPerson
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ local Lua = require('Module:Lua')
local String = require('Module:StringUtils')

local Injector = Lua.import('Module:Infobox/Widget/Injector', {requireDevIfEnabled = true})
local PersonSc2 = Lua.import('Module:Infobox/Person/Custom/Shared', {requireDevIfEnabled = true})
local Person = Lua.import('Module:Infobox/Person', {requireDevIfEnabled = true})
local CustomPerson = Lua.import('Module:Infobox/Person/Custom', {requireDevIfEnabled = true})

local Widgets = require('Module:Infobox/Widget/All')
local Cell = Widgets.Cell
Expand All @@ -23,31 +22,26 @@ local CustomMapMaker = Class.new()
local CustomInjector = Class.new(Injector)

local _args
local _map_maker

function CustomMapMaker.run(frame)
local player = Person(frame)
_args = player.args
PersonSc2.setArgs(_args)
local mapMaker = CustomPerson(frame)
_map_maker = mapMaker
_args = mapMaker.args

player.shouldStoreData = PersonSc2.shouldStoreData
player.getStatusToStore = PersonSc2.getStatusToStore
player.adjustLPDB = PersonSc2.adjustLPDB
player.getPersonType = PersonSc2.getPersonType
player.nameDisplay = PersonSc2.nameDisplay
mapMaker.calculateEarnings = CustomMapMaker.calculateEarnings
mapMaker.createBottomContent = CustomMapMaker.createBottomContent
mapMaker.createWidgetInjector = CustomMapMaker.createWidgetInjector

player.calculateEarnings = CustomMapMaker.calculateEarnings
player.createBottomContent = CustomMapMaker.createBottomContent
player.createWidgetInjector = CustomMapMaker.createWidgetInjector

return player:createInfobox()
return mapMaker:createInfobox()
end

function CustomInjector:parse(id, widgets)
if id == 'status' then
return {
Cell{
name = 'Race',
content = {PersonSc2.getRaceData(_args.race or 'unknown')}
content = {_map_maker:getRaceData(_args.race or 'unknown')}
}
}
elseif id == 'role' then return {}
Expand Down Expand Up @@ -75,7 +69,7 @@ end

function CustomInjector:addCustomCells(widgets)
return {
Cell{name = 'Military Service', content = {PersonSc2.military(_args.military)}},
Cell{name = 'Military Service', content = {_args.military}},
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ local Table = require('Module:Table')
local Variables = require('Module:Variables')

local Achievements = Lua.import('Module:Infobox/Extension/Achievements', {requireDevIfEnabled = true})
local Person = Lua.import('Module:Infobox/Person', {requireDevIfEnabled = true})
local PersonSc2 = Lua.import('Module:Infobox/Person/Custom/Shared', {requireDevIfEnabled = true})
local CustomPerson = Lua.import('Module:Infobox/Person/Custom', {requireDevIfEnabled = true})
local Opponent = Lua.import('Module:Opponent/Starcraft', {requireDevIfEnabled = true})

local Condition = require('Module:Condition')
Expand Down Expand Up @@ -62,28 +61,21 @@ local _args
local _player

function CustomPlayer.run(frame)
local player = Person(frame)
local player = CustomPerson(frame)
_args = player.args
_player = player
PersonSc2.setArgs(_args)

player.recentMatches = {}
player.infoboxAchievements = {}
player.awardAchievements = {}
player.achievements = {}
player.achievementsFallBack = {}
player.earningsGlobal = {}
player.shouldQueryData = PersonSc2.shouldStoreData()
player.shouldQueryData = player:shouldStoreData(_args)
if player.shouldQueryData then
player.yearsActive = CustomPlayer._getMatchupData(player.pagename)
end

player.shouldStoreData = PersonSc2.shouldStoreData
player.getStatusToStore = PersonSc2.getStatusToStore
player.adjustLPDB = PersonSc2.adjustLPDB
player.getPersonType = PersonSc2.getPersonType
player.nameDisplay = PersonSc2.nameDisplay

player.calculateEarnings = CustomPlayer.calculateEarnings
player.createBottomContent = CustomPlayer.createBottomContent
player.createWidgetInjector = CustomPlayer.createWidgetInjector
Expand All @@ -97,7 +89,7 @@ function CustomInjector:parse(id, widgets)
return {
Cell{
name = 'Race',
content = {PersonSc2.getRaceData(_args.race or 'unknown', RACE_FIELD_AS_CATEGORY_LINK)}
content = {_player:getRaceData(_args.race or 'unknown', RACE_FIELD_AS_CATEGORY_LINK)}
}
}
elseif id == 'role' then return {}
Expand Down Expand Up @@ -156,7 +148,7 @@ function CustomInjector:addCustomCells(widgets)
},
Cell{name = rank1.name or 'Rank', content = {rank1.rank}},
Cell{name = rank2.name or 'Rank', content = {rank2.rank}},
Cell{name = 'Military Service', content = {PersonSc2.military(_args.military)}},
Cell{name = 'Military Service', content = {_args.military}},
Cell{
name = Abbreviation.make('Years active', 'Years active as a player'),
content = {_player.yearsActive}
Expand Down Expand Up @@ -565,10 +557,12 @@ function CustomPlayer._getAllkills()
end

function CustomPlayer:getWikiCategories(categories)
for _, faction in pairs(PersonSc2.readFactions(_args.race).factions) do
for _, faction in pairs(self:readFactions(_args.race).factions) do
table.insert(categories, faction .. ' Players')
end

table.insert(categories, self:military(_args.military).category)

return categories
end

Expand Down
Loading

0 comments on commit cff052f

Please sign in to comment.