Author Topic: Host only command?  (Read 2731 times)

so, people seem to ignore my post, so here it is:
Why do you capitalize stuff randomly and then leave out camelCase in important places? In Torque it does not matter, but in a real programming language it will.



There's actually a bunch of ways to do this, none of them 100% correct, which is probably why there's no one "isHost" method on the GameConnection.

Code: [Select]
%client.isLocalConnection()This is without a doubt the best method for detecting the host. If they're the local connection, they ARE the host. Does not work with dedicated servers.

Code: [Select]
%client.bl_id == getNumKeyID()Also good. If the bl_id is the same as the num key, they are the same CD Key as the host. Does not work with LAN.


Probably the best way to do it is something like this:

Code: [Select]
function GameConnection::isHost(%client)
{
if($Server::Dedicated)
{
if($Server::LAN)
{
return %client.isSuperAdmin;
}
else
{
return %client.bl_id == getNumKeyID();
}
}
else
{
return %client.isLocalConnection();
}
}

If the server is dedicated and it's a LAN server, the only proper way to check is to ask if the client is a super admin. Therefore, there can be multiple hosts.

If the server is dedicated and it's an Internet server, we check Blockland IDs.

If the server isn't dedicated, we only care if it's the local connection.

Why do you capitalize stuff randomly and then leave out camelCase in important places?
I can name functions whatever the forget i want lol, it doesn't have to be in camelCase if I don't want it to be.

I can name functions whatever the forget i want lol, it doesn't have to be in camelCase if I don't want it to be.
This is a wonderful foundation to build yourself up on. Legibility be damned! LOL

This is a wonderful foundation to build yourself up on. Legibility be damned! LOL
It's completely legible.

It's good practice to have your casing consistant through out a script.
« Last Edit: July 19, 2012, 05:49:35 PM by elm »

I never understood why people do that. All they're doing is saving a kb or two, but making it harder to read.

javascript, sometimse php use it formatted that way. it's not neccesary, but every example i've seen uses that formatting.

it seems to be habit.

This is what I use for detecting the host:

Code: [Select]
package Slayer_Dependencies_GameConnection
{
function GameConnection::autoAdminCheck(%this)
{
%this.isHost = (%this.isLocalConnection() || %this.getBLID() == Slayer_Support::getBlocklandID());

//snip

return parent::autoAdminCheck(%this);
}
};
activatePackage(Slayer_Dependencies_GameConnection);

function Slayer_Support::getBlocklandID()
{
if($Server::LAN)
return getLAN_BLID();
else
return getNumKeyID();
}

It's good practice to have your casing consistent through out a script.
Code: [Select]
function serverCmdisHost(%Client)
{
if(%Client.BL_ID == getNumKeyID())
messageClient(%Client, '', "\c6You're the host!");
else
messageClient(%Client, '', "\c6You're not the host!");
}
Happy now? :/

serverCmdIsHost
%client

cuz you don't caps the first word
but you caps everything else



unless you're pro like me #swag

serverCmdIsHost
%client
I capitalize all non-loop variables, and I treat serverCmd as a prefix, so the first word after that is not capitalized.

i see that but i don't feel like that's technically perfectly right but NO ONE CARES

jesus this is boring, arguing over capitalization where it literally doesn't matter

Thank you. This really helped

I never understood why people do that. All they're doing is saving a kb or two, but making it harder to read.
Looks prettier, more readable, more space on the screen for actual code instead of just filler. The real question is why people don't.

Looks prettier, more readable, more space on the screen for actual code instead of just filler. The real question is why people don't.
Not really. I mean, it's opinion, but most people find the first two assertions to be the opposite

Not really. I mean, it's opinion, but most people find the first two assertions to be the opposite
With Lunatic's code as a base, let's try writing it in a few different ways. Which do you prefer?

Code: [Select]
function GameConnection::isHost(%client)
{
if($Server::Dedicated)
{
if($Server::LAN)
{
return %client.isSuperAdmin;
}
else
{
return %client.bl_id == getNumKeyID();
}
}
else
{
return %client.isLocalConnection();
}
}

Code: ("Condensed" TorqueScript) [Select]
function GameConnection::isHost(%client) {
if($Server::Dedicated) {
if($Server::LAN) {
return %client.isSuperAdmin;
} else {
return %client.bl_id == getNumKeyID();
}
} else {
return %client.isLocalConnection();
}
}

Code: (Python) [Select]
class GameConnection(object): # As part of the original class, I don't remember how to add bound functions later, because it's generally considered a bad idea
    def is_host(self):
        if server_dedicated:
            if server_lan:
                return self.is_super_admin
            else:
                return self.bl_id == get_num_key_id()
        else:
            return self.is_local_connection()

Code: (CoffeeScript) [Select]
GameConnection.isHost = () ->
    if serverDedicated
        if serverLan
            @isSuperAdmin
        else
            @bl_id == getNumKeyID()
    else
        @isLocalConnection()

I know which one I prefer.