annotate CensusPlayerList.lua @ 1:0a8c42242829

based on and compatible to CensusPlus-30300-4.2.2.zip (only one can be active)
author EmFor <EmFor.hg@mroe.de>
date Tue, 30 Mar 2010 13:50:24 +0200
parents edfa01041183
children 10c85be19b56
rev   line source
EmFor@0 1 --[[
EmFor@0 2 CensusPlus for World of Warcraft(tm).
EmFor@0 3
EmFor@0 4 Copyright 2005 - 2006 Cooper Sellers and WarcraftRealms.com
EmFor@0 5
EmFor@0 6 License:
EmFor@0 7 This program is free software; you can redistribute it and/or
EmFor@0 8 modify it under the terms of the GNU General Public License
EmFor@0 9 as published by the Free Software Foundation; either version 2
EmFor@0 10 of the License, or (at your option) any later version.
EmFor@0 11
EmFor@0 12 This program is distributed in the hope that it will be useful,
EmFor@0 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
EmFor@0 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
EmFor@0 15 GNU General Public License for more details.
EmFor@0 16
EmFor@0 17 You should have received a copy of the GNU General Public License
EmFor@0 18 along with this program(see GLP.txt); if not, write to the Free Software
EmFor@0 19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
EmFor@0 20 ]]
EmFor@0 21
EmFor@0 22
EmFor@0 23 ------------------------------------------------------------------------------------
EmFor@0 24 --
EmFor@0 25 -- CensusPlus
EmFor@0 26 -- A WoW UI customization by Cooper Sellers
EmFor@0 27 --
EmFor@0 28 --
EmFor@0 29 ------------------------------------------------------------------------------------
EmFor@0 30
EmFor@0 31 local g_PlayerList = {};
EmFor@0 32 local g_PlayerLookupTable = {};
EmFor@0 33 local CensusPlus_NumPlayerButtons = 20;
EmFor@0 34 local g_MaxNumListed = 1000;
EmFor@0 35
EmFor@0 36 function CensusPlus_ShowPlayerList()
EmFor@0 37 CP_PlayerListWindow:Show();
EmFor@0 38 end
EmFor@0 39
EmFor@0 40 function CensusPlus_PlayerListOnShow()
EmFor@0 41
EmFor@0 42 debugprofilestart();
EmFor@0 43
EmFor@0 44 local guildKey = nil;
EmFor@0 45 local raceKey = nil;
EmFor@0 46 local classKey = nil;
EmFor@0 47 local levelKey = nil;
EmFor@0 48
EmFor@0 49
EmFor@0 50 --
EmFor@0 51 -- Clear our character list
EmFor@0 52 --
EmFor@0 53 CensusPlus_ClearPlayerList();
EmFor@0 54
EmFor@0 55 --
EmFor@0 56 -- Get realm and faction
EmFor@0 57 --
EmFor@0 58 local realmName = g_CensusPlusLocale .. GetCVar("realmName");
EmFor@0 59 if( realmName == nil ) then
EmFor@0 60 return;
EmFor@0 61 end
EmFor@0 62
EmFor@0 63 local factionGroup = UnitFactionGroup("player");
EmFor@0 64 if( factionGroup == nil ) then
EmFor@0 65 return;
EmFor@0 66 end
EmFor@0 67
EmFor@0 68
EmFor@0 69 --
EmFor@0 70 -- Has the user made any selections?
EmFor@0 71 --
EmFor@0 72 if (g_GuildSelected ~= nil ) then
EmFor@0 73 guildKey = g_GuildSelected;
EmFor@0 74 end
EmFor@0 75 if (g_RaceSelected > 0) then
EmFor@0 76 local thisFactionRaces = CensusPlus_GetFactionRaces(factionGroup);
EmFor@0 77 raceKey = thisFactionRaces[g_RaceSelected];
EmFor@0 78 end
EmFor@0 79 if (g_ClassSelected > 0) then
EmFor@0 80 local thisFactionClasses = CensusPlus_GetFactionClasses(factionGroup);
EmFor@0 81 classKey = thisFactionClasses[g_ClassSelected];
EmFor@0 82 end
EmFor@0 83 if (g_LevelSelected > 0 or g_LevelSelected < 0) then
EmFor@0 84 levelKey = g_LevelSelected;
EmFor@0 85 end
EmFor@0 86
EmFor@0 87 debugprofilestart();
EmFor@0 88
EmFor@0 89 CensusPlus_ForAllCharacters( realmName, factionGroup, raceKey, classKey, guildKey, levelKey, CensusPlus_AddPlayerToList);
EmFor@0 90
EmFor@0 91 if( CensusPlus_EnableProfiling ) then
EmFor@0 92 CensusPlus_Msg( "PROFILE: Time to do calcs 1 " .. debugprofilestop() / 1000000000 );
EmFor@0 93 debugprofilestart();
EmFor@0 94 end
EmFor@0 95
EmFor@0 96
EmFor@0 97 --
EmFor@0 98 -- Build our list
EmFor@0 99 --
EmFor@0 100 CensusPlus_UpdatePlayerListButtons();
EmFor@0 101
EmFor@0 102 local totalCharactersText = format(CENSUSPlus_TOTALCHAR, table.getn( g_PlayerList ) );
EmFor@0 103 if( table.getn( g_PlayerList ) == g_MaxNumListed ) then
EmFor@0 104 totalCharactersText = totalCharactersText .. " -- " .. CENSUSPlus_MAXXED;
EmFor@0 105 end
EmFor@0 106
EmFor@0 107 CensusPlayerListCount:SetText(totalCharactersText);
EmFor@0 108
EmFor@0 109 end
EmFor@0 110
EmFor@0 111 ----------------------------------------------------------------------------------
EmFor@0 112 --
EmFor@0 113 -- Predicate function which can be used to compare two characters for sorting
EmFor@0 114 --
EmFor@0 115 ---------------------------------------------------------------------------------
EmFor@0 116 local function CharacterPredicate(lhs, rhs)
EmFor@0 117 --
EmFor@0 118 -- nil references are always less than
EmFor@0 119 --
EmFor@0 120 if (lhs == nil) then
EmFor@0 121 if (rhs == nil) then
EmFor@0 122 return false;
EmFor@0 123 else
EmFor@0 124 return true;
EmFor@0 125 end
EmFor@0 126 elseif (rhs == nil) then
EmFor@0 127 return false;
EmFor@0 128 end
EmFor@0 129 --
EmFor@0 130 -- Sort by name
EmFor@0 131 --
EmFor@0 132 if (lhs.m_name < rhs.m_name) then
EmFor@0 133 return true;
EmFor@0 134 elseif (rhs.m_name < lhs.m_name) then
EmFor@0 135 return false;
EmFor@0 136 end
EmFor@0 137
EmFor@0 138 --
EmFor@0 139 -- Sort by level
EmFor@0 140 --
EmFor@0 141 if (lhs.m_level < rhs.m_level) then
EmFor@0 142 return true;
EmFor@0 143 elseif (rhs.m_level < lhs.m_level) then
EmFor@0 144 return false;
EmFor@0 145 end
EmFor@0 146
EmFor@0 147 --
EmFor@0 148 -- identical
EmFor@0 149 --
EmFor@0 150 return false;
EmFor@0 151 end
EmFor@0 152
EmFor@0 153 local function CensusPlus_UpdatePlayerLookup( index, entry )
EmFor@0 154 --
EmFor@0 155 -- Have to update our table
EmFor@0 156 --
EmFor@0 157 g_PlayerLookupTable[entry.m_name] = index;
EmFor@0 158 end
EmFor@0 159
EmFor@0 160
EmFor@0 161
EmFor@0 162 ----------------------------------------------------------------------------------
EmFor@0 163 --
EmFor@0 164 -- Update the Player button contents
EmFor@0 165 --
EmFor@0 166 ---------------------------------------------------------------------------------
EmFor@0 167 function CensusPlus_UpdatePlayerListButtons()
EmFor@0 168 --
EmFor@0 169 -- Sort the list
EmFor@0 170 --
EmFor@0 171 local size = table.getn(g_PlayerList);
EmFor@0 172 if (size) then
EmFor@0 173 table.sort(g_PlayerList, CharacterPredicate);
EmFor@0 174
EmFor@0 175 table.foreach(g_PlayerList, CensusPlus_UpdatePlayerLookup );
EmFor@0 176
EmFor@0 177 end
EmFor@0 178
EmFor@0 179 --
EmFor@0 180 -- Determine where the scroll bar is
EmFor@0 181 --
EmFor@0 182 local offset = FauxScrollFrame_GetOffset( CensusPlusPlayerListScrollFrame );
EmFor@0 183 --
EmFor@0 184 -- Walk through all the rows in the frame
EmFor@0 185 --
EmFor@0 186 local i = 1;
EmFor@0 187 while( i <= CensusPlus_NumPlayerButtons ) do
EmFor@0 188 --
EmFor@0 189 -- Get the index to the ad displayed in this row
EmFor@0 190 --
EmFor@0 191 local iPlayer = i + offset;
EmFor@0 192 --
EmFor@0 193 -- Get the button on this row
EmFor@0 194 --
EmFor@0 195 local button = getglobal("CensusPlusPlayerButton"..i);
EmFor@0 196 --
EmFor@0 197 -- Is there a valid Player on this row?
EmFor@0 198 --
EmFor@0 199 if (iPlayer <= size) then
EmFor@0 200 local player = g_PlayerList[iPlayer];
EmFor@0 201 --
EmFor@0 202 -- Update the button text
EmFor@0 203 --
EmFor@0 204 button:Show();
EmFor@0 205 local textField = "CensusPlusPlayerButton"..i.."Name";
EmFor@0 206 if ( player.m_name == nil or player.m_name == "") then
EmFor@0 207 getglobal(textField):SetText( "None" );
EmFor@0 208 else
EmFor@0 209 getglobal(textField):SetText( player.m_name );
EmFor@0 210 end
EmFor@0 211
EmFor@0 212 local textField = "CensusPlusPlayerButton"..i.."Level";
EmFor@0 213 if ( player.m_level == nil or player.m_level == "") then
EmFor@0 214 getglobal(textField):SetText( "n/a" );
EmFor@0 215 else
EmFor@0 216 getglobal(textField):SetText( player.m_level );
EmFor@0 217 end
EmFor@0 218
EmFor@0 219 local textField = "CensusPlusPlayerButton"..i.."Class";
EmFor@0 220 if ( player.m_guild == nil or player.m_guild == "") then
EmFor@0 221 getglobal(textField):SetText( "Unguilded" );
EmFor@0 222 else
EmFor@0 223 getglobal(textField):SetText( player.m_guild );
EmFor@0 224 end
EmFor@0 225
EmFor@0 226 local textField = "CensusPlusPlayerButton"..i.."Seen";
EmFor@0 227 if ( player.m_seen == nil or player.m_seen == "") then
EmFor@0 228 getglobal(textField):SetText( "UNK" );
EmFor@0 229 else
EmFor@0 230 getglobal(textField):SetText( player.m_seen );
EmFor@0 231 end
EmFor@0 232 else
EmFor@0 233 --
EmFor@0 234 -- Hide the button
EmFor@0 235 --
EmFor@0 236 button:Hide();
EmFor@0 237 end
EmFor@0 238 --
EmFor@0 239 -- Next row
EmFor@0 240 --
EmFor@0 241 i = i + 1;
EmFor@0 242 end
EmFor@0 243 --
EmFor@0 244 -- Update the scroll bar
EmFor@0 245 --
EmFor@0 246 FauxScrollFrame_Update(CensusPlusPlayerListScrollFrame, size, CensusPlus_NumPlayerButtons, CensusPlus_GUILDBUTTONSIZEY);
EmFor@0 247 end
EmFor@0 248
EmFor@0 249 ----------------------------------------------------------------------------------
EmFor@0 250 --
EmFor@0 251 -- Find a characters in the g_PlayerList array by name
EmFor@0 252 --
EmFor@0 253 ---------------------------------------------------------------------------------
EmFor@0 254 function CensusPlus_PlayerButton_OnClick()
EmFor@0 255 local id = this:GetID();
EmFor@0 256 local offset = FauxScrollFrame_GetOffset( CensusPlusPlayerListScrollFrame );
EmFor@0 257 local newSelection = id + offset;
EmFor@0 258
EmFor@0 259 local player = g_PlayerList[newSelection];
EmFor@0 260 FriendsFrame_ShowDropdown(player.m_name, 1);
EmFor@0 261 end
EmFor@0 262
EmFor@0 263 ----------------------------------------------------------------------------------
EmFor@0 264 --
EmFor@0 265 -- Clear all the characters
EmFor@0 266 --
EmFor@0 267 ---------------------------------------------------------------------------------
EmFor@0 268 function CensusPlus_ClearPlayerList()
EmFor@0 269 g_PlayerList = nil;
EmFor@0 270 g_PlayerList = {};
EmFor@0 271
EmFor@0 272 g_PlayerLookupTable = nil;
EmFor@0 273 g_PlayerLookupTable = {};
EmFor@0 274 end
EmFor@0 275
EmFor@0 276 ----------------------------------------------------------------------------------
EmFor@0 277 --
EmFor@0 278 -- Add a character to the list
EmFor@0 279 --
EmFor@0 280 ---------------------------------------------------------------------------------
EmFor@0 281 function CensusPlus_AddPlayerToList( name, level, guild, raceName, className, lastseen )
EmFor@0 282 local size = table.getn( g_PlayerList );
EmFor@0 283
EmFor@0 284 if( size >= g_MaxNumListed ) then
EmFor@0 285 return;
EmFor@0 286 end
EmFor@0 287
EmFor@0 288 local index = g_PlayerLookupTable[name];
EmFor@0 289 if (index == nil) then
EmFor@0 290 local size = table.getn( g_PlayerList );
EmFor@0 291 index = size + 1;
EmFor@0 292 g_PlayerList[index] = { m_name = name, m_level = level, m_guild = guild, m_seen = lastseen };
EmFor@0 293 g_PlayerLookupTable[name] = index;
EmFor@0 294 end
EmFor@0 295 end
EmFor@0 296