1
0

MojangAPI: Added a UseCachedOnly param to GetUUIDsFromPlayerNames().

This commit is contained in:
madmaxoft 2014-07-31 10:02:50 +02:00
parent 0336e12cee
commit 8b519bf6e2
5 changed files with 55 additions and 11 deletions

View File

@ -1630,7 +1630,7 @@ a_Player:OpenWindow(Window);
Functions = Functions =
{ {
AddPlayerNameToUUIDMapping = { Params = "PlayerName, UUID", Return = "", Notes = "Adds the specified PlayerName-to-UUID mapping into the cache, with current timestamp." }, AddPlayerNameToUUIDMapping = { Params = "PlayerName, UUID", Return = "", Notes = "Adds the specified PlayerName-to-UUID mapping into the cache, with current timestamp." },
GetUUIDsFromPlayerNames = { Params = "PlayerNames", Return = "table", Notes = "Returns a table that contains the map, 'PlayerName' -> 'UUID', for all valid playernames in the input array-table. PlayerNames not recognized will not be set in the returned map. Queries the Mojang servers for the results. <b>WARNING</b>: Do NOT use this function while the server is running. Only use it when the server is starting up (inside the Initialize() method), otherwise you will lag the server severely." }, GetUUIDsFromPlayerNames = { Params = "PlayerNames, [UseOnlyCached]", Return = "table", Notes = "Returns a table that contains the map, 'PlayerName' -> 'UUID', for all valid playernames in the input array-table. PlayerNames not recognized will not be set in the returned map. If UseOnlyCached is false (the default), queries the Mojang servers for the results that are not in the cache. <br /><b>WARNING</b>: Do NOT use this function with UseOnlyCached set to false while the server is running. Only use it when the server is starting up (inside the Initialize() method), otherwise you will lag the server severely." },
MakeUUIDDashed = { Params = "UUID", Return = "DashedUUID", Notes = "(STATIC) Converts the UUID to a dashed format (\"01234567-8901-2345-6789-012345678901\"). Accepts both dashed and short UUIDs. Logs a warning and returns an empty string if UUID format not recognized." }, MakeUUIDDashed = { Params = "UUID", Return = "DashedUUID", Notes = "(STATIC) Converts the UUID to a dashed format (\"01234567-8901-2345-6789-012345678901\"). Accepts both dashed and short UUIDs. Logs a warning and returns an empty string if UUID format not recognized." },
MakeUUIDShort = { Params = "UUID", Return = "ShortUUID", Notes = "(STATIC) Converts the UUID to a short format (without dashes, \"01234567890123456789012345678901\"). Accepts both dashed and short UUIDs. Logs a warning and returns an empty string if UUID format not recognized." }, MakeUUIDShort = { Params = "UUID", Return = "ShortUUID", Notes = "(STATIC) Converts the UUID to a short format (without dashes, \"01234567890123456789012345678901\"). Accepts both dashed and short UUIDs. Logs a warning and returns an empty string if UUID format not recognized." },
}, },

View File

@ -307,8 +307,38 @@ function TestUUIDFromName()
"aloe_vera", "aloe_vera",
} }
UUIDs = cMojangAPI:GetUUIDsFromPlayerNames(ValidPlayerNames); UUIDs = cMojangAPI:GetUUIDsFromPlayerNames(ValidPlayerNames);
-- Log the results:
for _, name in ipairs(ValidPlayerNames) do
local UUID = UUIDs[name]
if (UUID == nil) then
LOG(" UUID(" .. name .. ") not found.")
else
LOG(" UUID(" .. name .. ") = \"" .. UUID .. "\"")
end
end
-- Test yet again, cache-only:
LOG("Testing once more, cache only...")
local PlayerNames3 =
{
"xoft",
"aloe_vera",
"notch", -- Valid player name, but not cached (most likely :)
}
UUIDs = cMojangAPI:GetUUIDsFromPlayerNames(PlayerNames3, true)
LOG("UUID-from-Name resolution test finished.") -- Log the results:
for _, name in ipairs(PlayerNames3) do
local UUID = UUIDs[name]
if (UUID == nil) then
LOG(" UUID(" .. name .. ") not found.")
else
LOG(" UUID(" .. name .. ") = \"" .. UUID .. "\"")
end
end
LOG("UUID-from-Name resolution tests finished.")
end end

View File

@ -2163,7 +2163,7 @@ static int tolua_cMojangAPI_GetUUIDsFromPlayerNames(lua_State * L)
if ( if (
!S.CheckParamUserTable(1, "cMojangAPI") || !S.CheckParamUserTable(1, "cMojangAPI") ||
!S.CheckParamTable(2) || !S.CheckParamTable(2) ||
!S.CheckParamEnd(3) !S.CheckParamEnd(4)
) )
{ {
return 0; return 0;
@ -2177,19 +2177,27 @@ static int tolua_cMojangAPI_GetUUIDsFromPlayerNames(lua_State * L)
{ {
lua_rawgeti(L, 2, i); lua_rawgeti(L, 2, i);
AString Name; AString Name;
S.GetStackValue(3, Name); S.GetStackValue(-1, Name);
if (!Name.empty()) if (!Name.empty())
{ {
PlayerNames.push_back(Name); PlayerNames.push_back(Name);
} }
lua_pop(L, 1); lua_pop(L, 1);
} }
// If the UseOnlyCached param was given, read it; default to false
bool ShouldUseCacheOnly = false;
if (lua_gettop(L) == 3)
{
ShouldUseCacheOnly = (lua_toboolean(L, 3) != 0);
lua_pop(L, 1);
}
// Push the output table onto the stack: // Push the output table onto the stack:
lua_newtable(L); // stack index 3 lua_newtable(L);
// Get the UUIDs: // Get the UUIDs:
AStringVector UUIDs = cRoot::Get()->GetMojangAPI().GetUUIDsFromPlayerNames(PlayerNames); AStringVector UUIDs = cRoot::Get()->GetMojangAPI().GetUUIDsFromPlayerNames(PlayerNames, ShouldUseCacheOnly);
if (UUIDs.size() != PlayerNames.size()) if (UUIDs.size() != PlayerNames.size())
{ {
// A hard error has occured while processing the request, no UUIDs were returned. Return an empty table: // A hard error has occured while processing the request, no UUIDs were returned. Return an empty table:
@ -2197,7 +2205,8 @@ static int tolua_cMojangAPI_GetUUIDsFromPlayerNames(lua_State * L)
} }
// Convert to output table, PlayerName -> UUID: // Convert to output table, PlayerName -> UUID:
for (int i = 0; i < NumNames; i++) size_t len = UUIDs.size();
for (size_t i = 0; i < len; i++)
{ {
if (UUIDs[i].empty()) if (UUIDs[i].empty())
{ {

View File

@ -129,7 +129,7 @@ void cMojangAPI::Start(cIniFile & a_SettingsIni)
AStringVector cMojangAPI::GetUUIDsFromPlayerNames(const AStringVector & a_PlayerNames) AStringVector cMojangAPI::GetUUIDsFromPlayerNames(const AStringVector & a_PlayerNames, bool a_UseOnlyCached)
{ {
// Convert all playernames to lowercase: // Convert all playernames to lowercase:
AStringVector PlayerNames; AStringVector PlayerNames;
@ -140,7 +140,10 @@ AStringVector cMojangAPI::GetUUIDsFromPlayerNames(const AStringVector & a_Player
} // for itr - a_PlayerNames[] } // for itr - a_PlayerNames[]
// Request the cache to populate any names not yet contained: // Request the cache to populate any names not yet contained:
CacheNamesToUUIDs(PlayerNames); if (!a_UseOnlyCached)
{
CacheNamesToUUIDs(PlayerNames);
}
// Retrieve from cache: // Retrieve from cache:
size_t idx = 0; size_t idx = 0;

View File

@ -50,8 +50,10 @@ public:
/** Converts the player names into UUIDs. /** Converts the player names into UUIDs.
a_PlayerName[idx] will be converted to UUID and returned as idx-th value a_PlayerName[idx] will be converted to UUID and returned as idx-th value
The UUID will be empty on error. The UUID will be empty on error.
Blocking operation, do not use in world-tick thread! */ If a_UseOnlyCached is true, only the cached values are returned.
AStringVector GetUUIDsFromPlayerNames(const AStringVector & a_PlayerName); If a_UseOnlyCached is false, the names not found in the cache are looked up online, which is a blocking
operation, do not use this in world-tick thread! */
AStringVector GetUUIDsFromPlayerNames(const AStringVector & a_PlayerName, bool a_UseOnlyCached = false);
// tolua_begin // tolua_begin