ULX

Author Topic: Passing Player Data to MOTD  (Read 6393 times)

0 Members and 1 Guest are viewing this topic.

Offline Wully616

  • Jr. Member
  • **
  • Posts: 64
  • Karma: 10
    • WullysGamers
Passing Player Data to MOTD
« on: April 29, 2013, 07:24:08 AM »
Hello,

Is there any way to pass player or server  data from the MOTD to the URL which it loads, much in the same way the sv_loadingurl can grab the players steamID and the map name?

Code: [Select]
If you run sv_loadingurl "http://mywebsite.com/loadingpage.php?steamid=%s&mapname=%m" then the following $_GET values are available to PHP.

    $_GET["steamid"] is the player's steam community id.
    $_GET["mapname"] is the current map name. EX: gm_construct

My lua is more or less non existent so please forgive me, now that uni's nearly over I'm hoping to learn though.

I've looked through the documentation for ulib/ulx and the gmod wiki, I've seen like the getUser, game.GetMap() ply:SteamID() functions and stuff but in all honestly I'm not 100% what I'm looking for.
I need a function to grab the players steamID  when they execute the command "ulx motd", save it to a variable so it may be appended to the motd URL.
Which PHP should be able to grab and I can do whatever with as I've done for my loading screen.

http://www.wullysgamers.co.uk/loading_background/index.php?steamid=76561197989877834&mapname=gm_flatgrass_revolution



Code: [Select]
function ulx.showMotdMenu()
local window = vgui.Create( "DFrame" )
if ScrW() > 640 then -- Make it larger if we can.
window:SetSize( ScrW()*0.9, ScrH()*0.9 )
else
window:SetSize( 640, 480 )
end
window:Center()
window:SetTitle( "ULX MOTD" )
window:SetVisible( true )
window:MakePopup()

local html = vgui.Create( "HTML", window )

local button = vgui.Create( "DButton", window )
button:SetText( "Close" )
button.DoClick = function() window:Close() end
button:SetSize( 100, 40 )
button:SetPos( (window:GetWide() - button:GetWide()) / 2, window:GetTall() - button:GetTall() - 10 )

html:SetSize( window:GetWide() - 20, window:GetTall() - button:GetTall() - 50 )
html:SetPos( 10, 30 )
if not isUrl then
html:SetHTML( ULib.fileRead( "data/ulx_motd.txt" ) )
else
local mapname = game.GetMap() //Get the map name
local steamid = ply:SteamID() //get the players steam ID
html:OpenURL( url.."?steamid="..steamid.."&mapname="..mapname) //append data onto URL for php to grab
end
end

Would the above function work? Or is there any easier way to do this?
[/url]

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Passing Player Data to MOTD
« Reply #1 on: April 29, 2013, 07:37:06 AM »
First off, that number is not a regular steamid, but a 64-bit steamid. You'd use this function to get that. Also there is no "ply" object in the code to grab the ID from. So you should be using "LocalPlayer():SteamID64()". Other than that it looks like it should work.
Experiencing God's grace one day at a time.

Offline Wully616

  • Jr. Member
  • **
  • Posts: 64
  • Karma: 10
    • WullysGamers
Re: Passing Player Data to MOTD
« Reply #2 on: April 29, 2013, 08:34:43 AM »
Thanks Megiddo!
One problem I'm having with it now though is that it doesn't get the localPlayers steamID64 when first joining the server and the motd opens. I would guess this is because it isn't the player thats technically executing the command?

Code: [Select]
[ERROR] addons/ulx/lua/ulx/modules/cl/motdmenu.lua:32: attempt to call method 'SteamID64' (a nil value)
  1. fn - addons/ulx/lua/ulx/modules/cl/motdmenu.lua:32
   2. func - addons/ulib/lua/ulib/client/cl_util.lua:22
    3. unknown - lua/includes/modules/net.lua:31

Code: [Select]
ulx.motdmenu_exists = true

local isUrl
local url

function ulx.showMotdMenu()
local window = vgui.Create( "DFrame" )
if ScrW() > 640 then -- Make it larger if we can.
window:SetSize( ScrW()*0.9, ScrH()*0.9 )
else
window:SetSize( 640, 480 )
end
window:Center()
window:SetTitle( "ULX MOTD" )
window:SetVisible( true )
window:MakePopup()

local html = vgui.Create( "HTML", window )

local button = vgui.Create( "DButton", window )
button:SetText( "Close" )
button.DoClick = function() window:Close() end
button:SetSize( 100, 40 )
button:SetPos( (window:GetWide() - button:GetWide()) / 2, window:GetTall() - button:GetTall() - 10 )

html:SetSize( window:GetWide() - 20, window:GetTall() - button:GetTall() - 50 )
html:SetPos( 10, 30 )
if not isUrl then
html:SetHTML( ULib.fileRead( "data/ulx_motd.txt" ) )
else
local mapname = game.GetMap() //Get the map name
local steamid = LocalPlayer():SteamID64() //get the players steam ID
html:OpenURL( url.."?steamid="..steamid.."&mapname="..mapname) //append data onto URL for php to grab
end
end

function ulx.rcvMotd( isUrl_, text )
isUrl = isUrl_
if not isUrl then
file.Write( "ulx_motd.txt", text )
else
if text:find( "://", 1, true ) then
url = text
else
url = "http://" .. text
end
end
end



Other than that, once the player types !motd in chat or ulx motd it opens and passes the steamID64 over perfectly.
I can change the steam64ID into the nicer looking one with a script that's no problem.

[/url]

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Passing Player Data to MOTD
« Reply #3 on: April 29, 2013, 10:02:21 AM »
Okay, so it's calling the function before the player is created, probably. To fix that you'd need to wait to bring up the MOTD until the player is created, but that's not a simple modification. Another (less robust) idea would be bringing the MOTD up ten seconds after a player joins.
Experiencing God's grace one day at a time.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Passing Player Data to MOTD
« Reply #4 on: April 29, 2013, 11:12:08 AM »
Another idea is having the server inform the player what their steamid is. It seems silly to need to do that, but it would work.
Experiencing God's grace one day at a time.

Offline Wully616

  • Jr. Member
  • **
  • Posts: 64
  • Karma: 10
    • WullysGamers
Re: Passing Player Data to MOTD
« Reply #5 on: April 29, 2013, 12:47:40 PM »
Ah I see, thanks again for the info!
I think letting the player spawn, begin to move then stopping them with the MOTD a few seconds later might annoy them.
I tried this sort of delay (My lua isn't great but finding scripts online to do the job works too). The delay function didn't work when joining the server, only when the client executed !motd it froze their client for 10 seconds, then opened the MOTD, oops  :-[ .
Code: [Select]
function ulx.showMotdMenu()
local window = vgui.Create( "DFrame" )
if ScrW() > 640 then -- Make it larger if we can.
window:SetSize( ScrW()*0.9, ScrH()*0.9 )
else
window:SetSize( 640, 480 )
end
window:Center()
window:SetTitle( "ULX MOTD" )
window:SetVisible( true )
window:MakePopup()

local html = vgui.Create( "HTML", window )

local button = vgui.Create( "DButton", window )
button:SetText( "Close" )
button.DoClick = function() window:Close() end
button:SetSize( 100, 40 )
button:SetPos( (window:GetWide() - button:GetWide()) / 2, window:GetTall() - button:GetTall() - 10 )

html:SetSize( window:GetWide() - 20, window:GetTall() - button:GetTall() - 50 )
html:SetPos( 10, 30 )
if not isUrl then
html:SetHTML( ULib.fileRead( "data/ulx_motd.txt" ) )
else
local mapname = game.GetMap() //Get the map name
delay_s(10)
steamid = LocalPlayer():SteamID64() //get the players steam ID
html:OpenURL( url.."?steamid="..steamid.."&mapname="..mapname) //append data onto URL for php to grab
end
end

function delay_s(delay)
delay = delay or 1
local time_to = os.time() + delay
while os.time() < time_to do end
end

Is it possible to check if the player is created, before calling the function? So it may not have the players SteamID when they initially join, but open opening it later the function would work?
I tried:
Code: [Select]

if LocalPlayer()~=nil then
steamid = LocalPlayer():SteamID64() //get the players steam ID
else
steamid = "NOID"
end
This still tried to call LocalPlayer():SteamID64() when the player joined the server. I'm lost now as to what I can do.

Otherwise, how could I get the server to inform the player their steamID?
[/url]

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Passing Player Data to MOTD
« Reply #6 on: April 29, 2013, 04:46:50 PM »
Try "if IsValid( LocalPlayer() ) then"
Experiencing God's grace one day at a time.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Re: Passing Player Data to MOTD
« Reply #7 on: April 30, 2013, 12:44:26 AM »
I agree with Megiddo, I would also try putting the above in a timer with a delay of 1.5 (more or less) seconds, so that it can give the server a little bit more time to think.
I cry every time I see that I am not a respected member of this community.

Offline Wully616

  • Jr. Member
  • **
  • Posts: 64
  • Karma: 10
    • WullysGamers
Re: Passing Player Data to MOTD
« Reply #8 on: April 30, 2013, 04:51:11 AM »
Woohoo that did it!

Thank you very much gents and for being patient with my lack of knowledge.



I also got it to grab other info with GetConVarString( "hostname" ); and GetConVarString( "ip" );

My hope is to make it detect which of my servers it is being loaded on, then loads the appropiete rules for that server  :)

Thanks again for the help!
[/url]

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Passing Player Data to MOTD
« Reply #9 on: April 30, 2013, 06:06:20 AM »
No problem, glad you were able to get it to work satisfactorily!
Experiencing God's grace one day at a time.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Passing Player Data to MOTD
« Reply #10 on: April 30, 2013, 05:48:11 PM »
*sigh*
Oh how I miss the time I had for UMotd and Umotd revived.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming