• Print

Author Topic: Country of connected player  (Read 5021 times)

0 Members and 1 Guest are viewing this topic.

Offline ZeD

  • Newbie
  • *
  • Posts: 20
  • Karma: 0
Country of connected player
« on: March 23, 2015, 08:20:01 am »
Hello
I want to create message when player joined server,like: "Nickname" connected from "country"
but function http.Fetch returns 200, not text of webpage
Help me please
Code: Lua
  1. if SERVER then
  2.         AddCSLuaFile()
  3.         util.AddNetworkString("chat_AddText")
  4.         chat = {}
  5.         function chat.AddText(...)
  6.                 net.Start("chat_AddText")
  7.                         net.WriteTable({...})
  8.                 net.Broadcast()
  9.         end
  10.         hook.Add("PlayerInitialSpawn", "SteamIDDisplay", function(ply)
  11.         ip = ply:IPAddress():match( "%d+%.%d+%.%d+%.%d+" ) or ""
  12.     query_string = "http://www.telize.com/geoip/" .. ip
  13.         http.Fetch( query_string,
  14.         function( body, len, headers, code )
  15.                 -- The first argument is the HTML we asked for.
  16.                 TheReturnedHTML = code --same with body
  17.         end,
  18.         function( error )
  19.                 TheReturnedHTML="We failed. =("
  20.         end
  21. )      
  22.                 country=string.match(TheReturnedHTML,"\"country\":\"%a+%\"")
  23.                 chat.AddText(Color(255,0,0), "[??????] ", Color(255,255,255), ply:Nick().." connected from: "..country)
  24. end)
  25. else
  26.         net.Receive("chat_AddText", function(len)
  27.                 chat.AddText(unpack(net.ReadTable()))
  28.         end)
  29. end
« Last Edit: March 24, 2015, 02:13:33 am by ZeD »

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Country of connected player
« Reply #1 on: March 23, 2015, 11:50:28 am »
I assume that "code" returns the HTTP status code, where 200 would basically be "OK".
Try changing TheReturnedHTML to body instead.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline ZeD

  • Newbie
  • *
  • Posts: 20
  • Karma: 0
Re: Country of connected player
« Reply #2 on: March 23, 2015, 12:52:26 pm »
thank u, but i find some new errors, string TheReturnedHTML print info of previously connected player, not current, and some problems with parsing(can u help me to finish this lua script,please.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Country of connected player
« Reply #3 on: March 23, 2015, 08:20:31 pm »
I'm reasonably sure Bytewave's ulx geoip could be used to learn from.
/index.php/topic,7704.0.html
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Country of connected player
« Reply #4 on: March 23, 2015, 08:45:50 pm »
I'm reasonably sure Bytewave's ulx geoip could be used to learn from.
/index.php/topic,7704.0.html

Yay, I've been used as an example! :D
From what I can tell (which isn't much, because that indentation needs some work), you have a few variables that are undefined in the scope you have them in.
I'd say, before you progress any further, try fixing the formatting on your code. Keep with a consistent indentation style, etc. You have no idea how much that helps us.
Code: Lua
  1. print "You can also use [code=lua] tags to highlight your code based on syntax"

EDIT: bbcode pls ;~;
bw81@ulysses-forums ~ % whoami
Homepage

Offline ZeD

  • Newbie
  • *
  • Posts: 20
  • Karma: 0
Re: Country of connected player
« Reply #5 on: March 24, 2015, 02:45:12 am »
So here last version of code
Problem: string TheReturnedHTML returns information of previously connected player
Ex: "First player" joined:nothing will be added in chat, second player joined:in chat will be printed information about "First player". 3rd about 2nd, etc.
Code: Lua
  1. if SERVER then
  2.         AddCSLuaFile()
  3.         util.AddNetworkString("chat_AddText")
  4.         chat = {}
  5.         function chat.AddText(...)
  6.                 net.Start("chat_AddText")
  7.                         net.WriteTable({...})
  8.                 net.Broadcast()
  9.         end
  10.         country="unknown"
  11.         function get_geoip(ply)
  12.         ip = ply:IPAddress():match( "%d+%.%d+%.%d+%.%d+" ) or ""
  13.         query_string = "http://www.telize.com/geoip/" .. ip
  14.         http.Fetch( query_string,
  15.         function( body, len, headers, code )
  16.                 TheReturnedHTML = body
  17.         end,
  18.         function( error )
  19.                 TheReturnedHTML="We failed. =("
  20.         end
  21.     )  
  22.         end
  23.         hook.Add("PlayerInitialSpawn", "SteamIDDisplay", function(ply)
  24.             get_geoip(ply)
  25.         chat.AddText(TheReturnedHTML)
  26.                 --country=string.match(TheReturnedHTML,"\"country\":\"%a+%\"")
  27.                 chat.AddText(Color(255,0,0), "[Server:] ", Color(255,255,255), ply:Nick().." connected from: ")
  28.                
  29. end)
  30. else
  31.         net.Receive("chat_AddText", function(len)
  32.                 chat.AddText(unpack(net.ReadTable()))
  33.         end)
  34. end
« Last Edit: March 24, 2015, 02:47:43 am by ZeD »

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: Country of connected player
« Reply #6 on: March 24, 2015, 09:22:45 am »
The problem is http.Fetch isn't instant.  It will continue running code after it so as not to stall everything.  I would just put the print message in your Success function on http.Fetch.

Offline ZeD

  • Newbie
  • *
  • Posts: 20
  • Karma: 0
Re: Country of connected player
« Reply #7 on: March 24, 2015, 09:50:28 am »
Code: Lua
  1. if TheReturnedCode == 200 then
  2.         chat.AddText(TheReturnedHTML)
  3.                 --country=string.match(TheReturnedHTML,"\"country\":\"%a+%\"")
  4.                 chat.AddText(Color(255,0,0), "[Server] ", Color(255,255,255), ply:Nick().." connected from: ")
  5.                  end
Simple check dont helps me, I think I need to recheck TheReturnedCode value every second until it became 200, how can I do it?

Edited: Oh no, sorry, it helps, thank you my friend:)
« Last Edit: March 24, 2015, 09:54:50 am by ZeD »

  • Print