Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: How to Python multiplayer?  (Read 5670 times)

Girlinhat

  • Bay Watcher
  • [PREFSTRING:large ears]
    • View Profile
How to Python multiplayer?
« on: July 16, 2013, 01:24:22 pm »

So, basic question.  Assume that I and a friend both know each other's IP addresses and we want to send information via Python, what would be the functions or libraries?  In particular, I'm looking to make a game that would have short bursts of messages from one client to another, so it doesn't really need to maintain a constant connection, just packets.

Anvilfolk

  • Bay Watcher
  • Love! <3
    • View Profile
    • Portuguese blacksmithing forum!
Re: How to Python multiplayer?
« Reply #1 on: July 16, 2013, 01:32:29 pm »

I'd be more worried about networking architecture working well with your game than with the technical part of sending bits and bytes.

Either way, low-level communication is usually at the socket level: http://docs.python.org/3.3/library/socket.html
 
UDP sends packets, but doesn't guarantee it will arrive, and if it does, whether the packets are ordered. TCP sends a stream, but guarantees it arrives and in order. I feel TCP would be more reliable if the game isn't real-time.

To send stuff I believe Python allows serialisation of some objects - which means you can encode them into a bytestream and then decode them. Have never tried it, but there's a library for it (careful with the warnings): http://docs.python.org/3.3/library/pickle.html
« Last Edit: July 16, 2013, 01:45:01 pm by Anvilfolk »
Logged

Antur

  • Bay Watcher
    • View Profile
Re: How to Python multiplayer?
« Reply #2 on: July 16, 2013, 01:33:16 pm »

All connections are just packets :P But sockets standard library is probably what are you're looking for.
Logged

Girlinhat

  • Bay Watcher
  • [PREFSTRING:large ears]
    • View Profile
Re: How to Python multiplayer?
« Reply #3 on: July 16, 2013, 01:52:48 pm »

I'd be more worried about networking architecture working well with your game than with the technical part of sending bits and bytes.

Either way, low-level communication is usually at the socket level: http://docs.python.org/3.3/library/socket.html
 
UDP sends packets, but doesn't guarantee it will arrive, and if it does, whether the packets are ordered. TCP sends a stream, but guarantees it arrives and in order. I feel TCP would be more reliable if the game isn't real-time.

To send stuff I believe Python allows serialisation of some objects - which means you can encode them into a bytestream and then decode them. Have never tried it, but there's a library for it (careful with the warnings): http://docs.python.org/3.3/library/pickle.html
Right now the information I want to send would be about the size of a hand-written note card.  Just very simple things.  In the game, two players are active, both flagged [PLAYER] so the AI doesn't touch them.  When the game receives a packet of information, it'd just be "HP is 15, action chosen is to move north" and it'd just plug in some of that info.  In this way, the bulk of the computation is client-side.

I'll check out socket library though.

Anvilfolk

  • Bay Watcher
  • Love! <3
    • View Profile
    • Portuguese blacksmithing forum!
Re: How to Python multiplayer?
« Reply #4 on: July 16, 2013, 02:19:46 pm »

Yeah, just create a class for each type of action or so and then you can serialise it, or encode it easily :) Then send it over the network and decode it client-side :)

Girlinhat

  • Bay Watcher
  • [PREFSTRING:large ears]
    • View Profile
Re: How to Python multiplayer?
« Reply #5 on: July 16, 2013, 02:26:51 pm »

Done right, that could end up being core code mechanics.  If "move right" was made into a class, then creature_list
  • .move[1,0] could be done client-side, or sent as a packet for easy interpretation of commands, since it uses the same tool for both single and multi player...

Anvilfolk

  • Bay Watcher
  • Love! <3
    • View Profile
    • Portuguese blacksmithing forum!
Re: How to Python multiplayer?
« Reply #6 on: July 16, 2013, 05:30:07 pm »

Yeah. I've been reading quite a bit about engine development, and I can recommend the first chapters of Game Coding Complete, 4th ed. Turns out game making isn't make graphics, and doing if keydown.code == Up: player.moveUp(); player.draw()...

Component-driven development and Entity systems are two things worth reading about, as is GUI design.

Very quickly though:
- You have a model, which represents the game. It knows nothing about how to display it, or how to parse player input. It's just an API for the gameworld, of sorts.
- Whenever your scene receives input, you parse it into an action and send it to the game model. The game model updates, then sends out an Event saying what happened.
- Views are EventListeners. The HumanView is the graphical representation of the game, and it listens to game Events to be able to update the graphics on the screen. A RemoteView can also receive these events, serialise them, and send them over the network. This can update a remote game model.

Entity systems are a bit more complex and I have a feeling that they might not be entirely suited to TBS games like RLs, but perhaps more to real-time games. Still have to think a bit more about this.

wierd

  • Bay Watcher
  • I like to eat small children.
    • View Profile
Re: How to Python multiplayer?
« Reply #7 on: July 17, 2013, 07:07:35 pm »

If you intend to play with more than just people you personally know, (eg, have a popular game), remember the golden rule with networked play.

Never trust the client.

Since the data being exchanged is small, and likely unencoded in any fashion, it is very easy to poke one client with a memory tweaker so it sends unrealistic or innacurate data to the other clients.  If the other clients blindly trust what they are told, it leads to cheating being rampant.

Since your simple program doesn't have a dedicated server daemon, I would suggest that clients reach a concensus, or that they do a sanity check on all data they attempt to synch.

(Otherwise, you run the risk of a player using a memory poking tool to make an uber item, which they then drop from their inventory. Their client accurately describes the item they dropped to other clients. If those clients don't have rules to check the item against, they believe what they are told, and populate an item entity with that data when the local player picks the item up. This kind of thing happened a lot with the initial release of Diablo in LAN play.)

I realize it is a bit early to state such things, or may seem to be, but it really isn't.

Right now your network code is primitive, and maleable. When it becomes matue, and many routines depend on it, adding sanity checks as an addon can poe considerable headaches. It's better to think about it now, before you have a lot invested in a specific design.
Logged

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: How to Python multiplayer?
« Reply #8 on: July 17, 2013, 07:16:12 pm »

If you intend to play with more than just people you personally know, (eg, have a popular game), remember the golden rule with networked play.

Never trust the client.

Since the data being exchanged is small, and likely unencoded in any fashion, it is very easy to poke one client with a memory tweaker so it sends unrealistic or innacurate data to the other clients.  If the other clients blindly trust what they are told, it leads to cheating being rampant.

Since your simple program doesn't have a dedicated server daemon, I would suggest that clients reach a concensus, or that they do a sanity check on all data they attempt to synch.

(Otherwise, you run the risk of a player using a memory poking tool to make an uber item, which they then drop from their inventory. Their client accurately describes the item they dropped to other clients. If those clients don't have rules to check the item against, they believe what they are told, and populate an item entity with that data when the local player picks the item up. This kind of thing happened a lot with the initial release of Diablo in LAN play.)

I realize it is a bit early to state such things, or may seem to be, but it really isn't.

Right now your network code is primitive, and maleable. When it becomes matue, and many routines depend on it, adding sanity checks as an addon can poe considerable headaches. It's better to think about it now, before you have a lot invested in a specific design.

Definitely this. Never has the client directly send messages to the server concerning information about the game (basic example "Player 2 moves to X 10 Y 20"). Instead, the server would inform all the clients of where a player was. When a player presses 'w' (or whatever), they would send that command to the server, and the server would move them, rather than the client move, then inform the server.

Maybe there's a better method, but at it's secure.

wierd

  • Bay Watcher
  • I like to eat small children.
    • View Profile
Re: How to Python multiplayer?
« Reply #9 on: July 17, 2013, 09:46:07 pm »

I've actually put a little thought into how a "serverless", persistent world could work in the face of such problems, given the costs of things like datacenters and bandwidth.


I haven't really tried to implement that, but I think it would deal with naughty players pretty well. Not perfectly, but pretty well.


Logged

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: How to Python multiplayer?
« Reply #10 on: July 18, 2013, 06:38:54 am »

Well that sounds good, but how does one tell if a change is reasonable?

Spoiler: My Response (click to show/hide)

Besides, serverless means the they (the clients) would have to connect to each other, which means people would have to open ports, which is not so easy for most people.

Also, I suggest we take this to PM's if you want to continue the conversation, so we don't derail his thread.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: How to Python multiplayer?
« Reply #11 on: July 18, 2013, 09:44:57 am »

I think a new thread in GD or creative projects may be better.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Girlinhat

  • Bay Watcher
  • [PREFSTRING:large ears]
    • View Profile
Re: How to Python multiplayer?
« Reply #12 on: July 18, 2013, 11:18:01 am »

Right now the basic idea was simple trusted player co-op.  Grab a friend, and play something like Dungeon Crawl Stone Soup with a partner.  Or Portal 2 type gameplay.  So while all that is fancy, it's not entirely relevant :v

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: How to Python multiplayer?
« Reply #13 on: July 18, 2013, 02:21:50 pm »

Right now the basic idea was simple trusted player co-op.  Grab a friend, and play something like Dungeon Crawl Stone Soup with a partner.  Or Portal 2 type gameplay.  So while all that is fancy, it's not entirely relevant :v

Although its not actually that fancy, I get your point.

Is there anything you need help with besides knowing that solution is to use sockets? For example, do you intend this to be just 2 players, or more?

Girlinhat

  • Bay Watcher
  • [PREFSTRING:large ears]
    • View Profile
Re: How to Python multiplayer?
« Reply #14 on: July 18, 2013, 02:36:22 pm »

Learning how to link multiple players to one host would be nice.

Ideally, I'd like to see two example Python scripts for "Hello World" sent via socket, assuming that the IP of both computers would be entered manually.
Pages: [1] 2