Author Topic: Client-sided function to check if the client is the host of the server?  (Read 2297 times)

I need a client-sided function to check if the client is the host of the server they are connected to.
How can I go about doing this?

NPL_Window.getValue() gets you the text from the top of the f2 menu such as player count, max player count (edit: oh god), host name and server name. There might be a better way, but this is what I thought of off the top of my head.
« Last Edit: August 09, 2015, 01:50:54 PM by Dannu »

if($missionRunning)
{
     //is host
}
else
{
    //not host
}

if($missionRunning)
{
     //is host
}
else
{
    //not host
}


What about dedicated servers?


What about dedicated servers?
It's a client-sided function, why would that matter?

It's a client-sided function, why would that matter?
For when you're joining a dedicated server your hosting.

I can't think of any good way to determine that in that case.
We don't even know if that's needed

The best way in that case would be the player list GUI method I offered up before.

The best way in that case would be the player list GUI method I offered up before.

Great, now how do you parse just the player name out of that entire string?
You could try cutting out everything between the hyphen and apostrophe.
That would work....until someone puts an apostrophe in their name, now you only get part of their name.
Ok, so let's cut out everything between the hyphen and the last apostrophe. Cool, now you have their entire name, apostrophe or not.
Then someone puts an apostrophe in their server name. Now you have their name and part of the server name.

EDIT: Nvm, client-sided, wouldn't work

There really is no way to be able to figure out the bl_id of the server host as a client without the host being in the server.  The best you can do is figure out if the server is being hosted from the same program instance.  My preferred method of checking this is:

Code: [Select]
if(serverconnection.islocal())
{
    ...
}

Code: [Select]
function amIHosting() {
    if(serverConnection.isLocal()) return true;
    %serverInfo = NPL_Window.getValue();
    %serverName = getSubStr(%serverInfo, %start = strPos(%serverInfo, " - ")+3, strLen(%serverInfo)-%start);
    if((%pos = strPos(%serverName, $Pref::Player::NetName)) == 0)
        return getSubStr(%serverName, %pos + strLen($Pref::Player::NetName), 1) $= "\'";
    return false;
}

This should work well enough. The only situation in which you'll be able to trick it is if a host sets their servername to something like 'Setro's Granny's House,' in which case someone could set their name to "Setro's Granny" to trick it.

Which shouldn't be an issue, allowing clients to see that they can do things shouldn't be that much of an issue as long as there are serverside hosting checks for when they actually do do something.

Also, if they were that dedicated, they could just replace the entire check with "return true;" in the code.
« Last Edit: August 09, 2015, 09:34:27 PM by $trinick »

why are we struggling with this apostrophe thing?
just do a while loop and check all the apostrophes until one of them matches your name and if you run through all the apostrophes and there are no matches there is no way you are the server host

why are we struggling with this apostrophe thing?
just do a while loop and check all the apostrophes until one of them matches your name and if you run through all the apostrophes and there are no matches there is no way you are the server host
Or just do it the way Trinick did it. If your name is at the start of the server title and there's a ' in the server title after your name, you're the host.