Garry's Mod

Garry's Mod

New Scoreboard + Loading players + GeoIP
 This topic has been pinned, so it's probably important
Cédric GÉRILLE [GiletJaune]  [developer] 5 Feb, 2014 @ 11:25am
Developpers, here is what you need
If you need the LUA functions that come with this addon, here are their usages and descriptions.
This list will be updated, so stay in touch with it!

Get loading users
Event "OnReloadAllConnected" (to make hooks) [SERVER and CLIENT]
  This event is triggered each time the list is reloaded (for example when a player joins or when a player leaves).

table player.AllConnected [SERVER and CLIENT]
  Internally there is a player.AllConnected table containing every player's data. I will call each element in this table player data.
  It exists serverside and it is replicated clientside without the IP address.
  Each player data is a table with the following named keys provided by the hook player_connect: address, index, bot, networkid, name, userid. In addition, the IP2Location library brings other keys (details below).
  Each player data has a data:GetPlayerEnt() method. It returns the player entity if it exists or nil if the player is loading.

function player.SendAnyPlayerList ([ Entity ply ]) [SERVER]
  If you ever need to resynchronize the clientside player.AllConnected table, you can use this function.
  If you want to broadcast the modifications instead of sending it to only 1 client, then ignore the first argument.

function player.GetAny () [shared]
  This function just returns the table player.AllConnected.
  I recommend that you use this function instead of directly using the table.

function player.GetLoading () [shared]
  This function works the same way as the previous function but it returns a table with only the clients who are not ready to play yet (loading screen).

IP2Location
Additional keys in table player.AllConnected [SERVER and CLIENT]
  The IP2Location brings a few more keys to player data.
  Serverside, the language key exists when the client has sent its language to the server. This happens only when he/she has finished loading.
  Clientside, the country key should be always available. It can be nil in a few cases.
  Clientside, after using geoip.GetUserIDInfo(), you will have the following keys. Always check that the values are not nil!
  • geoip, a table which has the same content as the table returned by geoip.GetIpInfo(), except that nil values become empty strings.
  • language, a string, empty if unavailable value.

function geoip.GetUserIDInfo (number UserID) [CLIENT]
  This function sends a query to the server to get the GeoIP data and the user's language.
  UserID is the UserID of the player.
  When the server sends the answer, the player.AllConnected.{player data}.geoip table and the player.AllConnected.{player data}.language string are filled.
  The user's language is not available if he/she is loading.
  Always check that the table and every value exist before using them!

function geoip.GetIpInfo (string address) [SERVER]
  This function returns a table with the following content:
  • current_ip_data[geoip.ip_from]
  • current_ip_data[geoip.ip_to]
  • current_ip_data[geoip.country_code]
  • current_ip_data[geoip.country_name]
  • current_ip_data[geoip.region_name]
  • current_ip_data[geoip.city_name]
  Note that if you have not installed a DB3-lite or DB3 database, the region and the city will be nil instead of a string (serverside).
  You can use IP addresses as "address" or "address:port". It does not matter.
  IP addresses are only IPv4.

Examples
  Get the player data of a player entity clientside and display some properties
local developper = LocalPlayer() -- send results to me local ply = player.GetAll()[1] -- the player that I want information about local UserID = ply:UserID() for _, data in pairs(player.GetAny()) do if data.userid == UserID then -- The player data is found. developper:ChatPrint(data.name .. " is from " .. tostring(data.country)) -- display country code developper:ChatPrint(data.name .. "'s Steam ID is " .. data.networkid) -- display Steam ID break end end
  Note: tostring() is used on values that can be nil. This prevents errors.

  Make the geoip information of a player's UserID available clientside as early as possible. There is a delay before information is loaded. The language will not be included in the information because it is not available yet at this point.
gameevent.Listen( "player_connect" ) hook.Add( "player_connect", "MyAddonName", function( data ) geoip.GetUserIDInfo( data.userid ) end )
Last edited by Cédric GÉRILLE [GiletJaune]; 27 Jul, 2017 @ 10:01am