Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualSky Warden

Posted 26 April 2013 - 05:23 AM

Wait. Maybe I get it wrong, but you said alternative. Does that mean I can avoid 'eval'? I thought the instruction map is just a way to make sure that the sent instruction is available and valid. The client sends the instruction in string, and if the instruction is in the map, it uses 'eval' to execute it. Do I get it wrong or I just misunderstand the word 'alternative'?

 

You don't need `eval` to execute it.

 

def move_character(char_id, vector):
    # TODO:
    pass

def kill_character(char_id):
    # TODO:
    pass

command_map = {
    'move_character': move_character,
    'kill_character': kill_character,
}

# sent from client; some string
command_name = 'move_character'
# also sent from client; a tuple or list, for example, which can be empty
# if the command doesn't take any arguments
command_args = (15, <whatever vector is>)

command_func = command_map.get(command_name)
if command_func is not None:
    command_func(*command_args)
else:
    # Do something else if the command is invalid

 

To understand the * sytanx, read http://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions, specifically http://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists and http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists.

That's sweet. Thanks.

 

I'm going to write my own version of game server. I really want to test my knowledge. I must confess that actually I haven't analyzed that pymmo (I just analyzed one file). I spent my time to study something else. laugh.png

 

For the database I will use MySQL and SQLAlchemy, and Twisted for the network. If you know any interesting library which can help me doing this thing, tell me please. That is, if you don't mind it.

 

P.S. I love this community. biggrin.png


#2Sky Warden

Posted 26 April 2013 - 05:20 AM

Wait. Maybe I get it wrong, but you said alternative. Does that mean I can avoid 'eval'? I thought the instruction map is just a way to make sure that the sent instruction is available and valid. The client sends the instruction in string, and if the instruction is in the map, it uses 'eval' to execute it. Do I get it wrong or I just misunderstand the word 'alternative'?

 

You don't need `eval` to execute it.

 

def move_character(char_id, vector):
    # TODO:
    pass

def kill_character(char_id):
    # TODO:
    pass

command_map = {
    'move_character': move_character,
    'kill_character': kill_character,
}

# sent from client; some string
command_name = 'move_character'
# also sent from client; a tuple or list, for example, which can be empty
# if the command doesn't take any arguments
command_args = (15, <whatever vector is>)

command_func = command_map.get(command_name)
if command_func is not None:
    command_func(*command_args)
else:
    # Do something else if the command is invalid

 

To understand the * sytanx, read http://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions, specifically http://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists and http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists.

That's sweet. Thanks.

 

I'm going to write my own version of game server. I really want to test my knowledge. I must confess that actually I haven't analyzed that pymmo (I just analyzed one file) and I spent my time to study something else. laugh.png

 

For the database I will use MySQL and SQLAlchemy, and Twisted for the network. If you know any interesting library which can help me doing this thing, tell me please. That is, if you don't mind it.

 

P.S. I love this community. biggrin.png


#1Sky Warden

Posted 26 April 2013 - 05:19 AM

Wait. Maybe I get it wrong, but you said alternative. Does that mean I can avoid 'eval'? I thought the instruction map is just a way to make sure that the sent instruction is available and valid. The client sends the instruction in string, and if the instruction is in the map, it uses 'eval' to execute it. Do I get it wrong or I just misunderstand the word 'alternative'?

 

You don't need `eval` to execute it.

 

def move_character(char_id, vector):
    # TODO:
    pass

def kill_character(char_id):
    # TODO:
    pass

command_map = {
    'move_character': move_character,
    'kill_character': kill_character,
}

# sent from client; some string
command_name = 'move_character'
# also sent from client; a tuple or list, for example, which can be empty
# if the command doesn't take any arguments
command_args = (15, <whatever vector is>)

command_func = command_map.get(command_name)
if command_func is not None:
    command_func(*command_args)
else:
    # Do something else if the command is invalid

 

To understand the * sytanx, read http://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions, specifically http://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists and http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists.

That's sweet. Thanks.

 

I'm going to write my own version of game server. I really want to test my knowledge. I must confess that actually I haven't analyzed that pymmo (I just analyzed one file) and I spent my time to study something else. laugh.png

 

For the database I will use MySQL and SQLAlchemy, and Twisted for the network. If you know any interesting library which can help me doing this thing, tell me please. That is, if you don't mind it.


PARTNERS