## pymultiplayer
A library for adding multiplayer functionality to python games.
## Note
This is a work in progress. Don't expect all features to work.
I don't know what version of python is needed for this package to work.
I will figure this stuff out in due course.
This is mainly just a passion project/project to help future projects (in fact I'm using it right now in [Elemental Defense](https://www.github.com/iamdeedz/elemental-defense/)) but it would be nice if it had some use.
---
## Usage
**A full example of this package's usage will be uploaded to [iamdeedz.github.io](https://iamdeedz.github.io) in due course!**
---
### Installation
`pip install pymultiplayer` or `python -m pip install pymultiplayer`
---
### Client Representation
The client is represented by a class with the following attributes:
```python
class _Client:
def __init__(self, ws, id):
self.ws = ws
self.id = id
```
#### Parameters
- `ws` - The websocket connection. Used for sending messages with `ws.send()`.
- `id` - The client's id. Used for identifying clients.
The client class is not meant to be initialized by the user. It is created by the server and passed to the message handler function.
---
### MultiplayerClient
```
class MultiplayerClient:
def __init__(self, msg_handler, ip="127.0.0.1", port=1300, auth_handler=None):
```
---
### TCPMultiplayerServer
```
class TCPMultiplayerServer:
def __init__(self, msg_handler, ip="127.0.0.1", port=1300, auth_func=None):
```
#### Parameters
- `msg_handler` - The function that gets ran for every message the server receives. Takes in the message as the first argument and the client as the second. <span style="color:darkred">***REQUIRED***</span>
- `ip` - The ip address the server will run on. Default is localhost or 127.0.0.1.
- `port` - The port the server will run on. Default is 1300.
- `auth_func` - If you want to use authentication, you can pass a function here. If an auth_func is given, it will be run every time a client connects. Takes in the websocket connection. Take a look at [this](tests/echo+basic_auth) example. The auth code is in server.py and authenticator.py.
#### Usage
Full examples of these functions will be uploaded to [iamdeedz.github.io](https://iamdeedz.github.io) in due course but for now, here's a basic example.
Let's start with the server.
First, create a file called `server.py`.
Now, create the required function(s).
```python
# server.py
def msg_handler(msg, client):
print(f"Received message from {client}: {msg}")
```
Then, create the server object.
```python
# server.py
from pymultiplayer import TCPMultiplayerServer
def msg_handler(msg, client):
print(f"Received message from {client}: {msg}")
server = TCPMultiplayerServer(msg_handler)
```
Next, run the server.
```python
# server.py
from pymultiplayer import TCPMultiplayerServer
def msg_handler(msg, client):
print(f"Received message from {client}: {msg}")
server = TCPMultiplayerServer(msg_handler)
server.run()
```
Raw data
{
"_id": null,
"home_page": null,
"name": "pymultiplayer",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3",
"maintainer_email": null,
"keywords": "coop, cooperative, game, multiplayer, pygame",
"author": "iamdeedz",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/ca/b7/3649290b3682fdc01f12b6403717b6ba755c748aa7d18c6ea2e3c9826563/pymultiplayer-0.3.1.tar.gz",
"platform": null,
"description": "## pymultiplayer\nA library for adding multiplayer functionality to python games.\n\n## Note\nThis is a work in progress. Don't expect all features to work.\nI don't know what version of python is needed for this package to work.\nI will figure this stuff out in due course.\n\nThis is mainly just a passion project/project to help future projects (in fact I'm using it right now in [Elemental Defense](https://www.github.com/iamdeedz/elemental-defense/)) but it would be nice if it had some use.\n\n---\n## Usage\n\n**A full example of this package's usage will be uploaded to [iamdeedz.github.io](https://iamdeedz.github.io) in due course!**\n\n---\n\n### Installation\n`pip install pymultiplayer` or `python -m pip install pymultiplayer`\n\n---\n\n### Client Representation\nThe client is represented by a class with the following attributes:\n```python\nclass _Client:\n def __init__(self, ws, id):\n self.ws = ws\n self.id = id\n```\n\n#### Parameters\n- `ws` - The websocket connection. Used for sending messages with `ws.send()`.\n- `id` - The client's id. Used for identifying clients.\n\nThe client class is not meant to be initialized by the user. It is created by the server and passed to the message handler function.\n\n---\n\n### MultiplayerClient\n```\nclass MultiplayerClient:\n def __init__(self, msg_handler, ip=\"127.0.0.1\", port=1300, auth_handler=None):\n```\n\n---\n\n### TCPMultiplayerServer\n```\nclass TCPMultiplayerServer:\n def __init__(self, msg_handler, ip=\"127.0.0.1\", port=1300, auth_func=None):\n```\n#### Parameters\n- `msg_handler` - The function that gets ran for every message the server receives. Takes in the message as the first argument and the client as the second. <span style=\"color:darkred\">***REQUIRED***</span>\n- `ip` - The ip address the server will run on. Default is localhost or 127.0.0.1.\n- `port` - The port the server will run on. Default is 1300.\n- `auth_func` - If you want to use authentication, you can pass a function here. If an auth_func is given, it will be run every time a client connects. Takes in the websocket connection. Take a look at [this](tests/echo+basic_auth) example. The auth code is in server.py and authenticator.py.\n\n#### Usage\nFull examples of these functions will be uploaded to [iamdeedz.github.io](https://iamdeedz.github.io) in due course but for now, here's a basic example.\n\nLet's start with the server.\n\nFirst, create a file called `server.py`.\n\nNow, create the required function(s).\n\n```python\n# server.py\n\ndef msg_handler(msg, client):\n print(f\"Received message from {client}: {msg}\")\n```\n\nThen, create the server object.\n```python\n# server.py\n\nfrom pymultiplayer import TCPMultiplayerServer\n\ndef msg_handler(msg, client):\n print(f\"Received message from {client}: {msg}\")\n\nserver = TCPMultiplayerServer(msg_handler)\n\n```\n\nNext, run the server.\n```python\n# server.py\n\nfrom pymultiplayer import TCPMultiplayerServer\n\ndef msg_handler(msg, client):\n print(f\"Received message from {client}: {msg}\")\n\nserver = TCPMultiplayerServer(msg_handler)\nserver.run()\n```\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "A package for adding multiplayer functionality to python games.",
"version": "0.3.1",
"project_urls": {
"Bug Tracker": "https://github.com/iamdeedz/pymultiplayer/issues",
"Homepage": "https://github.com/iamdeedz/pymultiplayer"
},
"split_keywords": [
"coop",
" cooperative",
" game",
" multiplayer",
" pygame"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "70ae13b712aad1488f35f76c501578735bd96f06b6deb4004f1969c9b6fdf948",
"md5": "50f151fc3273bf736d5091baa5e69ff7",
"sha256": "e4f22cf29047499d26310f709ecb3857cfe06f9e7b1b33ca4a8c6af8c8d639d3"
},
"downloads": -1,
"filename": "pymultiplayer-0.3.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "50f151fc3273bf736d5091baa5e69ff7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3",
"size": 9361,
"upload_time": "2024-12-16T21:07:42",
"upload_time_iso_8601": "2024-12-16T21:07:42.595458Z",
"url": "https://files.pythonhosted.org/packages/70/ae/13b712aad1488f35f76c501578735bd96f06b6deb4004f1969c9b6fdf948/pymultiplayer-0.3.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "cab73649290b3682fdc01f12b6403717b6ba755c748aa7d18c6ea2e3c9826563",
"md5": "d428c6801d3aae6ec4a0e0bbe55f2ca6",
"sha256": "1b2b26ff3cad0204e20870202efb3ff918f404574549d0507673a3af114872b3"
},
"downloads": -1,
"filename": "pymultiplayer-0.3.1.tar.gz",
"has_sig": false,
"md5_digest": "d428c6801d3aae6ec4a0e0bbe55f2ca6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3",
"size": 9606,
"upload_time": "2024-12-16T21:07:43",
"upload_time_iso_8601": "2024-12-16T21:07:43.653916Z",
"url": "https://files.pythonhosted.org/packages/ca/b7/3649290b3682fdc01f12b6403717b6ba755c748aa7d18c6ea2e3c9826563/pymultiplayer-0.3.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-16 21:07:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "iamdeedz",
"github_project": "pymultiplayer",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pymultiplayer"
}