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

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
Could this work?
echo(strPos(getSubStr(NPL_Window.getValue(), strPos("Players - ", NPL_Window.getValue())+1, strLen(NPL_Window.getValue())-1), $Pref::Player::NetName) >= 0);

Of course, this can cause issues if someone else has the exact name on it.
« Last Edit: August 09, 2015, 11:56:20 PM by Kyuande »

There's a couple of problems with that. First, I assume "Players" becomes "Player" if there's only one player on the server, which would cause the start position to be 0 (-1 + 1). Second, you'd want to add the length of "Players - " (if we're still using that, which is 9) because otherwise your starting position will be 1 after the start of "Players" which is "layers - x". Third, the end point will almost always be past the length of the string because the end point is specified as number of characters to grab, not the end point.

Also, you'd want to set NPL_Window.getValue() to a variable to avoid calling it a bunch of times.

If you wanted to compress the code to one line, you could do this:

serverConnection.isLocal() ? true : ((%pos = strPos(%serverName = getSubStr(%serverInfo = NPL_Window.getValue(), %start = strPos(%serverInfo, " - ")+3, strLen(%serverInfo)-%start), $Pref::Player::NetName)) == 0 && (getSubStr(%serverName, %pos + strLen($Pref::Player::NetName), 1) $= "\'"));

Buuut, that's pretty un-readable.

If you're just checking if the server name begins with the player name, then what if someone has a name that's the same as yours, but with extra characters after?

Granted that's a pretty specific edge case, but it's not extremely unlikely (I go by "Adam487" in game, and I've seen a server hosted under the name "adam") and something to keep in mind if being released publicly.
« Last Edit: August 10, 2015, 12:17:33 PM by Headcrab Zombie »

If you need to check for admin status, there's a built in $IAmAdmin variable that is set to 0, 1, or 2, based on no admin, admin, and superadmin respectively.

If the add-on is supposed to be used by the host the server will most likely have it too, just send a command to the client to tell him he can use it.

If you're just checking if the server name begins with the player name, then what if someone has a name that's the same as yours, but with extra characters after?

Granted that's a pretty specific edge case, but it's not extremely unlikely (I go by "Adam487" in game, and I've seen a server hosted under the name "adam") and something to keep in mind if being released publicly.
Checking if it begins with the player name, AND there's an apostrophe in the server title after the player's name.

Example:
Your name is Adam487.
Server's title is Adam487's Blockland Server.
Server title starts with Adam487, and has a ' following that.
Return true.

Someone else's name is Adam4
Server's title is Adam487's Blockland Server.
Server title starts with Adam4, but the ' is not immediately after.
Return false.

The only issue here is if someone named Joe was hosting a server called "and Bob's Blockland Server"
Name is Joe.
Server title is Joe's and Bob's Blockland Server.
Server title starts with Joe, and is followed by a '.
Return true.

Someone for some bizarre reason is named Joe's and Bob
Server title starts with Joe's and Bob, and is followed by a '.
Return true.

You can put up a dedicated server and then change your name as far as I know, so there really is no point in trying to work out some perfect solution because as far as I am concerned there really isn't one.

You can put up a dedicated server and then change your name as far as I know, so there really is no point in trying to work out some perfect solution because as far as I am concerned there really isn't one.

The server name will update at the next posting. But it really doesn't matter: these things all have to be done intentionally to trick the system (who's going to throw up a server then change their name or just happen to have a conflicted name?) and if people really want to trick it they could just edit the code. It doesn't matter if the client thinks they're the host as long as any servercommands check to see if they actually are the host. But, if you're going to be doing that, you may as well just have a host handshake: when the host connects, send them a commandToClient that tells them they're host. But, again, this is also circumventable by people.
« Last Edit: August 11, 2015, 07:24:39 PM by $trinick »

The server name will update at the next posting. But it really doesn't matter: these things all have to be done intentionally to trick the system (who's going to throw up a server then change their name or just happen to have a conflicted name?) and if people really want to trick it they could just edit the code. It doesn't matter if the client thinks they're the host as long as any servercommands check to see if they actually are the host. But, if you're going to be doing that, you may as well just have a host handshake: when the host connects, send them a commandToClient that tells them they're host. But, again, this is also circumventable by people.
Yeah unfortunately we have no clue what he's using any of this for and he's not paying attention to the thread any more

Honestly, if you are checking whether or not you are the host of the server, you might as well set up a handshake system where the client sends a servercmd and the server replies with a clientcmd of some sort.

the host's name isnt always in the npl window tho

the host's name isnt always in the npl window tho
It's not always in NPL_List, but it's always in the title of NPL_Window because it's always the first thing in the server name.

It's not always in NPL_List, but it's always in the title of NPL_Window because it's always the first thing in the server name.
It's never there for me, but that's probably because of some mod I have.

It's not always in NPL_List, but it's always in the title of NPL_Window because it's always the first thing in the server name.
For some reason it'll strip out the host's name until some unknown factor decides to fix it.

For some reason it'll strip out the host's name until some unknown factor decides to fix it.

What will?