• Create Account

## Understanding PyMMO

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

18 replies to this topic

### #1Herwin P  Members

852
Like
3Likes
Like

Posted 23 April 2013 - 04:00 AM

Hi everybody. I saw a good source code that is made for MMO development with Python here. I've tried it and it worked. I really am interested in this source code and I want to learn it, but when I opened the script, it was pretty confusing for me. I'm new to network programming, so it's hard for me to understand it.

What I want to know is what does the source code do and how does it work. Can someone please add some comments in the Python scripts explaining parts of it. You can just explain one script or one part if this is bothering you too much, like maybe just the client part, etc. Every help will be appreciated.

Sorry if this sounds so newbie, but I really have no idea.

Thank you.

### #2thok  Members

733
Like
2Likes
Like

Posted 23 April 2013 - 04:39 AM

I think you're going to get much more from people on this forum if you ask for help to understand a very specific piece of code. I doubt anyone is going to want to dig into someone else's code and comment it for you.

I've got a better idea: Why not try to comment it yourself? Take a copy of the code and read through it, but don't just read it like a book from start to finish. Get your self whiteboard or a big blank piece of paper and make notes and sketches about the various components of the code. For example, you can start by making a list of the source files:

- client.py

- encoding.py

- mkuser.py

- player.py

- server.py

- world.py

Next, open each file and take note of what it contains. Does it import anything? Make a note of that. What classes does it contain? What functions does it contain? Don't look at the code in too much detail, just look for names. If the code is written well, the names should give you good conceptual ideas.

Also look for main functions or a if __name__ == "__main__" block in the code, or something like that. Find the "entry point" of the client and server (see the README.txt). From the entry point, read through the first few steps of what it is doing. What kind of functions is it calling? What kind of objects is it creating? What are the relationships of those objects?

Once you think understand the conceptual idea about a piece of code (a class, a function, etc.), add some comments. Even if you're wrong, just add comments about what you THINK the code is doing. Correct them later if you need to. Comments can even be questions, like "# why is this necessary?".

Have you run the code? If not, do it. Try the different examples in the README. Become a "power user" first. Then, start thinking about what feature you might add. Make it something small. Try to build it, and ask questions if you need help.

I hope that helps. =)

Edited by thok, 23 April 2013 - 04:41 AM.

### #3Herwin P  Members

852
Like
0Likes
Like

Posted 23 April 2013 - 07:28 AM

I think you're going to get much more from people on this forum if you ask for help to understand a very specific piece of code. I doubt anyone is going to want to dig into someone else's code and comment it for you.

I've got a better idea: Why not try to comment it yourself? Take a copy of the code and read through it, but don't just read it like a book from start to finish. Get your self whiteboard or a big blank piece of paper and make notes and sketches about the various components of the code. For example, you can start by making a list of the source files:

- client.py

- encoding.py

- mkuser.py

- player.py

- server.py

- world.py

Next, open each file and take note of what it contains. Does it import anything? Make a note of that. What classes does it contain? What functions does it contain? Don't look at the code in too much detail, just look for names. If the code is written well, the names should give you good conceptual ideas.

Also look for main functions or a if __name__ == "__main__" block in the code, or something like that. Find the "entry point" of the client and server (see the README.txt). From the entry point, read through the first few steps of what it is doing. What kind of functions is it calling? What kind of objects is it creating? What are the relationships of those objects?

Once you think understand the conceptual idea about a piece of code (a class, a function, etc.), add some comments. Even if you're wrong, just add comments about what you THINK the code is doing. Correct them later if you need to. Comments can even be questions, like "# why is this necessary?".

Have you run the code? If not, do it. Try the different examples in the README. Become a "power user" first. Then, start thinking about what feature you might add. Make it something small. Try to build it, and ask questions if you need help.

I hope that helps. =)

Yes. That does help me. I found out that my trouble was the way I tried to understand the code. Your guide helps me indeed.

Do you have any other tips to understand people's code? I'm going to use Twisted for the network. Just trying.

By the way, if I find something I can't understand, can I ask you here?

### #4thok  Members

733
Like
1Likes
Like

Posted 23 April 2013 - 07:47 AM

Do you have any other tips to understand people's code? I'm going to use Twisted for the network. Just trying.

Yep. One thing you can do is write tests for it, if it's not already tested. This way, you have a concrete method of exercising the code and to test if your assumptions about the code are correct.

Another thing you can do is to re-write parts of the code, meaning, copy it exactly by typing out each character. This is not the same as copy/paste. Copying the code by writing will give you a "muscle memory" for the code and you will notice subtle things as you are copying it that you wouldn't normally notice when you are just reading.

Last but not least, actually write some code of your own. Figure out what you want to build and see if you can use or modify the code to suit your needs.

By the way, if I find something I can't understand, can I ask you here?

Of course you can post questions to the forum. If you have a specific question, it's probably best to create a new post (and link back to this one for reference).

### #5ByteTroll  Members

2908
Like
0Likes
Like

Posted 23 April 2013 - 12:42 PM

Thok has a very good point and I will add on to it quick!  You need to learn how to read and understand code yourself.  Half of being a programmer is being able to read and understand other peoples code and follow logic.  As Thok said, you are more likely to get help if you ask about a specific block of code or system.

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

### #6Kylotan  Moderators

6550
Like
1Likes
Like

Posted 23 April 2013 - 03:41 PM

I think you're going to get much more from people on this forum if you ask for help to understand a very specific piece of code.

Normally that's 100% true. However, in this case, the person who wrote that code is the moderator of the Multiplayer + Networking forum here on Gamedev.net. So some specific questions may get answered over in that forum. However, I doubt anybody will have the time to go through and explain all of it.

### #7Herwin P  Members

852
Like
1Likes
Like

Posted 24 April 2013 - 09:37 PM

Well, I have a small question about this. Since this question is too... simple, I don't think making a new thread is a good idea, so here we go.

Like I said, I'm new to network programming (I learn programming from the internet. I even haven't been in college). My question is simple. What kind of information does the client send to the server, and vice versa?

My current opinion is it's something like "the character moves. The character id is x, and it moves to [insert the movement vector here]." Is my assumption correct? My idea is to send a string which is the name of an available function in the server, along with the arguments needed. For example, here the command is move, so it will call move(char_id, vector) with Python exec(). Is it correct? Or is there a better way (I'm sure there is)?

Also, what things I should pay attention at while doing this? Is using exec() bad? Do you have any advice for doing this?

Thank you again. Sorry if this sounds too newbie, but well... I am.

### #8thok  Members

733
Like
1Likes
Like

Posted 25 April 2013 - 01:33 AM

Well, I have a small question about this. Since this question is too... simple, I don't think making a new thread is a good idea, so here we go.

Like I said, I'm new to network programming (I learn programming from the internet. I even haven't been in college). My question is simple. What kind of information does the client send to the server, and vice versa?

My current opinion is it's something like "the character moves. The character id is x, and it moves to [insert the movement vector here]." Is my assumption correct? My idea is to send a string which is the name of an available function in the server, along with the arguments needed. For example, here the command is move, so it will call move(char_id, vector) with Python exec(). Is it correct? Or is there a better way (I'm sure there is)?

Also, what things I should pay attention at while doing this? Is using exec() bad? Do you have any advice for doing this?

Thank you again. Sorry if this sounds too newbie, but well... I am.

I think you're mistaking exec for eval. And yes, you should be very careful using eval. Probably a better idea is creating a map of valid commands. Something like this:

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,
}


So if the command sent the server is not one of the registered commands in command_map, you ignore it. This is a simple way you can start defining (and enforcing) the contract between client and server.

Try something simple like this and build on it as necessary.

### #9Herwin P  Members

852
Like
0Likes
Like

Posted 25 April 2013 - 07:40 AM

I think you're mistaking exec for eval. And yes, you should be very careful using eval. Probably a better idea is creating a map of valid commands. Something like this:

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,
}


So if the command sent the server is not one of the registered commands in command_map, you ignore it. This is a simple way you can start defining (and enforcing) the contract between client and server.

Try something simple like this and build on it as necessary.

Oh, yes. It's eval. I forgot about their difference.

About the command_map, I actually have been thinking about that before. Using dictionary and check if the thing is in the dictionary. So, my assumption is right? The type of data sent between client and server is the instruction and the arguments needed? I have never coded any server stuff before, and I couldn't find such information in the Internet. I just used my own logic, which is questionable.

Can you please explain a little more about the reason why I must be careful while using eval? I feel like it's rather risky, but I'm not sure why. I guess I'm just paranoid.

P.S. I just read an article about being careful with eval, but I don't really understand the namespace thingy.

Edited by Sky Warden, 25 April 2013 - 08:32 AM.

### #10thok  Members

733
Like
2Likes
Like

Posted 25 April 2013 - 08:15 AM

Oh, yes. It's eval. I forgot about their difference.

About the command_map, I actually have been thinking about that before. Using dictionary and check if the thing is in the dictionary. So, my assumption is right? The type of data sent between client and server is the instruction and the arguments needed? I have never coded any server stuff before, and I couldn't find such information in the Internet. I just used my own logic, which is questionable.

Yes, pretty much. You need to figure out a clean way to pack 1) the instruction name and 2) the arguments on the client side, and a way to unpack them properly on the server side. A simple way to do this would be to encode this in JSON; that may not be terribly efficient, but it's a simple place to start. Python has standard libs for encoding and decoding JSON: http://docs.python.org/2/library/json.html.

Can you please explain a little more about the reason why I must be careful while using eval? I feel like it's rather risky, but I'm not sure why. I guess I'm just paranoid.

P.S. I just read an article about being careful with eval, but I don't really understand the namespace thingy.

Two words: "code injection". http://en.wikipedia.org/wiki/Code_injection

If you use eval to execute an arbitrary instruction, it is possible that a client could, for example, send the instruction name '__import__("sys").exit()' as a string, which would crash the server process once eval executes it. If the server process is being run as superuser (which it shouldn't be), technically this security hole could result in the server box getting completely 0wnz3d. If you're using an eval in a program which runs in isolation on a single machine and never hits the network, probably you don't need to care. But once you open the program up to a network and, more importantly, provide a server interface _directly_ to an eval, you do need to be careful.

Regardless of the security risks, there are almost always better alternatives to eval. In your case, using an explicitly defined map of instructions is one of those alternatives.

eval for Python is kind of like goto for C: Just because you CAN use it doesn't necessarily mean you SHOULD. =P

Edited by thok, 25 April 2013 - 08:15 AM.

### #11Herwin P  Members

852
Like
0Likes
Like

Posted 25 April 2013 - 08:47 AM

Yes, pretty much. You need to figure out a clean way to pack 1) the instruction name and 2) the arguments on the client side, and a way to unpack them properly on the server side. A simple way to do this would be to encode this in JSON; that may not be terribly efficient, but it's a simple place to start. Python has standard libs for encoding and decoding JSON: http://docs.python.org/2/library/json.html.

Thank you. This kind of information is what I'm always looking for. It will be very nice if someone can give me a list of "things or libraries you should learn for your programming."

I will learn it right away. Maybe tomorrow. It's late in the night here.

Two words: "code injection". http://en.wikipedia.org/wiki/Code_injection

Regardless of the security risks, there are almost always better alternatives to eval. In your case, using an explicitly defined map of instructions is one of those alternatives.

Ah yes. Code injection. I didn't realize that.

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'?

### #12thok  Members

733
Like
3Likes
Like

Posted 25 April 2013 - 09: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


### #13Herwin P  Members

852
Like
0Likes
Like

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


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.

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.

Edited by Sky Warden, 26 April 2013 - 05:23 AM.

### #14thok  Members

733
Like
2Likes
Like

Posted 26 April 2013 - 05:38 AM

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.

The philosophy of Python's standard libraries is "batteries included", so pretty much everything you'll need exists in the standard lib (apart from things like Twisted and SQLAlchemy). So it depends on what you want to do. Google is your friend in this case. If you want to do some particular thing in your code, just search "python <particular thing>". This will tell you pretty quickly if there is a package in the standard lib to help you with this.

It would be pointless to suggest particular libraries with knowing exactly what you're building. However, being rather passionate about Python, I will suggest a few other subjects for further reading:

1. Python style guide (PEP8) - http://www.python.org/dev/peps/pep-0008/  <--- read this twice
2. pep8 tool (for automatically checking if your code meets style guidelines) - https://pypi.python.org/pypi/pep8
3. Learn about unit testing and write tests for your code - http://docs.python.org/2/library/unittest.html
4. From a command prompt, type this: python -c "import this". Read.

Edited by thok, 26 April 2013 - 05:38 AM.

### #15Herwin P  Members

852
Like
0Likes
Like

Posted 26 April 2013 - 06:49 AM

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.

The philosophy of Python's standard libraries is "batteries included", so pretty much everything you'll need exists in the standard lib (apart from things like Twisted and SQLAlchemy). So it depends on what you want to do. Google is your friend in this case. If you want to do some particular thing in your code, just search "python <particular thing>". This will tell you pretty quickly if there is a package in the standard lib to help you with this.

It would be pointless to suggest particular libraries with knowing exactly what you're building. However, being rather passionate about Python, I will suggest a few other subjects for further reading:

1. Python style guide (PEP8) - http://www.python.org/dev/peps/pep-0008/  <--- read this twice
2. pep8 tool (for automatically checking if your code meets style guidelines) - https://pypi.python.org/pypi/pep8
3. Learn about unit testing and write tests for your code - http://docs.python.org/2/library/unittest.html
4. From a command prompt, type this: python -c "import this". Read.

I've read some of the style guide before, but haven't finished yet. I think I will finish it now, but don't worry, my code is pretty neat at the moment. Also, that tool will be useful.

I will start coding now. Is there a place here to post my code (when it's finished) so I can get feedback or review about it? I think getting some feedbacks will be nice.

### #16thok  Members

733
Like
1Likes
Like

Posted 26 April 2013 - 07:22 AM

I've read some of the style guide before, but haven't finished yet. I think I will finish it now, but don't worry, my code is pretty neat at the moment. Also, that tool will be useful.

I'm not worried. But someone else (including the future you) trying to read your code might. =)

I will start coding now. Is there a place here to post my code (when it's finished) so I can get feedback or review about it? I think getting some feedbacks will be nice.

Yes, there are many places to store your code online and receive feedback, which leads me to another important software development topic: version control. Are you using version control? If not, start using a version control system now.

One of the best places (in my opinion) to post code is GitHub.com. You'll need to learn how to use git, which is a great skill to have. Many people are using git. You can either store your code in your repository, or you can use gists which are a lighter-weight method of storing code and text. gists are ideal for small bodies of code (scripts, prototypes, examples, etc.), whereas a full repository is ideal for large bodies of code (a project, a library, etc.).

I recommend starting with gists first, because you don't need to learn git commands to use them. Gists are also good because they're tied to your github account, so you can find them easily. (As a software developer, github is a create place to build a portfolio. Many potential employers will ask for code samples; it's great to just be able to point them to your github page.)

Another simpler, quicker, and slightly more crude way to post code is to use something like pastebin.com or pastie.org. Both services can do syntax highlighting for a bunch of major languages (bash, C, XML, Java, Python, etc.).

If you're going to post code, make sure the license of the code is clear. This can either be done with a license header in the file (example: https://gist.github.com/larsbutler/5330230) or with a LICENSE file in the repository (example: https://github.com/larsbutler/geomet/blob/master/LICENSE.txt). If the code belongs to someone else, be sure to note who wrote it and how it is licensed. A lot of people who post code on their blogs do not specify a license for their code, which is a mistake in my opinion. (This leads to people either using code in such a way that the original author didn't intended or doesn't like, or simply not being able to use the code for fear of infringing on a copyright. The pymmo code, for example, did not include a license, as far as I can tell.

For your original code, you can choose an open source license so people can use your code. See http://en.wikipedia.org/wiki/List_of_software_licenses and http://opensource.org/licenses to find out more. If you don't really care that much and want to license your code in an extremely "free" way, the BSD or MIT license is a good choice.

If you want to post your code publicly, but you don't necessarily want to use an open source license, you can simply include a statement in your code that you are the copyright owner and that use of this code is not permitted for commercial/personal/educational (be specific as you want) purposes. Just be aware that if your code derived from open source licensed code, the license of that code may prohibit you from "closing" your code in this way.

Edited by thok, 26 April 2013 - 07:28 AM.

### #17Herwin P  Members

852
Like
0Likes
Like

Posted 26 April 2013 - 07:59 AM

I'm not worried. But someone else (including the future you) trying to read your code might. =)

Perhaps. I remember reading a code I typed a year ago, and... I don't want to talk about it.

Oh good sir. I think I can't thank you enough. I was keeping this question and waiting for the right time to ask, and suddenly, you answered it.

As far as my concern goes, what if I make something that uses other libraries? Just say, I make this game server thingy and it uses Twisted and SQLAlchemy. Do I have to write the credits, ask for permission to the developers, or something like that? I'm not so good at this thing called 'law'.

Also, is a bunch of libraries, modules, etc can be called a framework? Then what makes a framework a good framework? Maybe sounds random, but I would like to know if something that I make is a framework or not. It will be so embarrassing if I call things which isn't a framework "a framework" or vice versa.

### #18thok  Members

733
Like
1Likes
Like

Posted 26 April 2013 - 09:21 AM

For the python standard libs, you shouldn't need any special citation. For other libs, check the project web page or source code for info about the license. When in doubt, ask the authors via email or IRC. Even if you do make a slight faux pas and forget to cite a lib properly, this shouldn't be a big problem unless you are trying to sell a product. But I'm not a lawyer, so do not take this as legal advice. Again, when in doubt, ask the author.

Regarding frameworks and libs, I see that you're trying to understand terminology. Knowing jargon and using it properly is good.

A library is simply a collection of functionality which encapsulates the details of a particular set of related tasks. An XML library is a good example (like the standard python 'xml' lib).

A framework may contain library-like code (and indeed, can be imported and used only as a library), but the distinguishing feature is that it suggests/forces you to construct your application a certain way. While a library will help you solve a particular problem in your application by abstracting away complexity, a framework will help you design, structure, and organize your code, in addition to abstracting away complexity.

Those are the best definitions I can come up with, in my own words. Someone else might have a better definition.

### #19Herwin P  Members

852
Like
0Likes
Like

Posted 27 April 2013 - 06:10 AM

For the python standard libs, you shouldn't need any special citation. For other libs, check the project web page or source code for info about the license. When in doubt, ask the authors via email or IRC. Even if you do make a slight faux pas and forget to cite a lib properly, this shouldn't be a big problem unless you are trying to sell a product. But I'm not a lawyer, so do not take this as legal advice. Again, when in doubt, ask the author.

Regarding frameworks and libs, I see that you're trying to understand terminology. Knowing jargon and using it properly is good.

A library is simply a collection of functionality which encapsulates the details of a particular set of related tasks. An XML library is a good example (like the standard python 'xml' lib).

A framework may contain library-like code (and indeed, can be imported and used only as a library), but the distinguishing feature is that it suggests/forces you to construct your application a certain way. While a library will help you solve a particular problem in your application by abstracting away complexity, a framework will help you design, structure, and organize your code, in addition to abstracting away complexity.

Those are the best definitions I can come up with, in my own words. Someone else might have a better definition.

Thank you for the information. It gives me enough idea about what a framework is. Like Code Igniter right? I think I will make one that ease mmo development (everyone needs a project to improve right?), but sure I still need to learn many things. Well, I will try to make a game server first. I will figure out about the framework later.

Thank you again.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.