# bridgepy
[bridgepy](https://pypi.org/project/bridgepy/) is a Python package for playing floating bridge!
[Visit repository on Github](https://github.com/papillonbee/bridgepy)
---
## Game state
The heart of this project is in the game state below
<img src="https://gist.githubusercontent.com/papillonbee/7f512954267c8340eb20b52ef8016ccf/raw/8673f95b534665510e9a286cb3d92d43dc16cb77/bridgepy-game-state.svg">
---
## Quick Guide
```python
from bridgepy.datastore import Datastore
from bridgepy.player import PlayerId, PlayerAction
from bridgepy.game import GameId, Game
from bridgepy.card import Card
from bridgepy.bid import Bid
from bridgepy.bridge import BridgeClient
```
### Step 1: Create your own datastore for `Game` and override `insert`, `update`, `delete`, and `query`
In this example, is a local datastore
```python
class GameLocalDataStore(Datastore[GameId, Game]):
def __init__(self) -> None:
self.games: list[Game] = []
def insert(self, game: Game) -> None:
i = self.__query_index(game.id)
if i is not None:
return
self.games.append(game)
def update(self, game: Game) -> None:
i = self.__query_index(game.id)
if i is None:
return
self.games = self.games[:i] + [game] + self.games[i+1:]
def delete(self, id: GameId) -> None:
i = self.__query_index(id)
if i is None:
return
self.games = self.games[:i] + self.games[i+1:]
def query(self, id: GameId) -> Game | None:
i = self.__query_index(id)
if i is None:
return None
return self.games[i]
def __query_index(self, id: GameId) -> int | None:
for i in range(len(self.games)):
if self.games[i].id == id:
return i
return None
```
### Step 2: Inject your game datastore to bridge client
In this example, is injecting the local datastore to the bridge client
```python
game_datastore = GameLocalDataStore()
client = BridgeClient(game_datastore)
```
### Step 3: Let 1 player create a game, and let 3 other players join the game
```python
game_id = GameId("1")
player_id1 = PlayerId("111")
player_id2 = PlayerId("222")
player_id3 = PlayerId("333")
player_id4 = PlayerId("444")
client.create_game(player_id1, game_id)
client.join_game(player_id2, game_id)
client.join_game(player_id3, game_id)
client.join_game(player_id4, game_id)
```
### Step 4: Let players bid, choose partner, and trick
```python
player_id = player_id2 # replace with player_id1, player_id2, player_id3, player_id4
# player polls for their game snapshot (maybe every 5 seconds)
snapshot = client.view_game(player_id, game_id)
print(snapshot)
if snapshot.player_action == PlayerAction.VIEW:
# player keeps polling for their game snapshot
print("view")
if snapshot.player_action == PlayerAction.BID:
print("bid turn")
bid_input = input() # 1C for 1 club, 2NT for 2 no trump, None for pass
bid = None if bid_input == "" else Bid.from_string(bid_input)
client.bid(player_id, game_id, bid)
if snapshot.player_action == PlayerAction.CHOOSE_PARTNER:
print("choose partner")
partner_input = input() # AC for ace club, 2H for 2 heart, 10S for 10 spade
client.choose_partner(player_id, game_id, Card.from_string(partner_input))
if snapshot.player_action == PlayerAction.TRICK:
print("trick turn")
trick_input = input() # AC for ace club, 2H for 2 heart, 10S for 10 spade
client.trick(player_id, game_id, Card.from_string(trick_input))
if snapshot.player_action is None:
# player stops polling for their game snapshot
print("ended")
```
Raw data
{
"_id": null,
"home_page": "https://github.com/papillonbee/bridgepy",
"name": "bridgepy",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "floating bridge, singaporean brdige",
"author": "Papan Yongmalwong",
"author_email": "papillonbee@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d5/86/49a140839a8d0d4be6735a093a48aa31ebcb78b58c99c26f1438517ff2ef/bridgepy-0.0.10.tar.gz",
"platform": null,
"description": "# bridgepy\n[bridgepy](https://pypi.org/project/bridgepy/) is a Python package for playing floating bridge!\n\n[Visit repository on Github](https://github.com/papillonbee/bridgepy)\n\n---\n\n## Game state\n\nThe heart of this project is in the game state below\n\n<img src=\"https://gist.githubusercontent.com/papillonbee/7f512954267c8340eb20b52ef8016ccf/raw/8673f95b534665510e9a286cb3d92d43dc16cb77/bridgepy-game-state.svg\">\n\n---\n## Quick Guide\n```python\nfrom bridgepy.datastore import Datastore\nfrom bridgepy.player import PlayerId, PlayerAction\nfrom bridgepy.game import GameId, Game\nfrom bridgepy.card import Card\nfrom bridgepy.bid import Bid\nfrom bridgepy.bridge import BridgeClient\n```\n\n### Step 1: Create your own datastore for `Game` and override `insert`, `update`, `delete`, and `query`\nIn this example, is a local datastore\n\n```python\nclass GameLocalDataStore(Datastore[GameId, Game]):\n\n def __init__(self) -> None:\n self.games: list[Game] = []\n\n def insert(self, game: Game) -> None:\n i = self.__query_index(game.id)\n if i is not None:\n return\n self.games.append(game)\n\n def update(self, game: Game) -> None:\n i = self.__query_index(game.id)\n if i is None:\n return\n self.games = self.games[:i] + [game] + self.games[i+1:]\n\n def delete(self, id: GameId) -> None:\n i = self.__query_index(id)\n if i is None:\n return\n self.games = self.games[:i] + self.games[i+1:]\n\n def query(self, id: GameId) -> Game | None:\n i = self.__query_index(id)\n if i is None:\n return None\n return self.games[i]\n\n def __query_index(self, id: GameId) -> int | None:\n for i in range(len(self.games)):\n if self.games[i].id == id:\n return i\n return None\n```\n### Step 2: Inject your game datastore to bridge client\nIn this example, is injecting the local datastore to the bridge client\n\n```python\ngame_datastore = GameLocalDataStore()\nclient = BridgeClient(game_datastore)\n```\n\n### Step 3: Let 1 player create a game, and let 3 other players join the game\n\n```python\ngame_id = GameId(\"1\")\nplayer_id1 = PlayerId(\"111\")\nplayer_id2 = PlayerId(\"222\")\nplayer_id3 = PlayerId(\"333\")\nplayer_id4 = PlayerId(\"444\")\n\nclient.create_game(player_id1, game_id)\nclient.join_game(player_id2, game_id)\nclient.join_game(player_id3, game_id)\nclient.join_game(player_id4, game_id)\n```\n\n### Step 4: Let players bid, choose partner, and trick\n\n```python\nplayer_id = player_id2 # replace with player_id1, player_id2, player_id3, player_id4\n# player polls for their game snapshot (maybe every 5 seconds)\nsnapshot = client.view_game(player_id, game_id)\nprint(snapshot)\nif snapshot.player_action == PlayerAction.VIEW:\n # player keeps polling for their game snapshot\n print(\"view\")\nif snapshot.player_action == PlayerAction.BID:\n print(\"bid turn\")\n bid_input = input() # 1C for 1 club, 2NT for 2 no trump, None for pass\n bid = None if bid_input == \"\" else Bid.from_string(bid_input)\n client.bid(player_id, game_id, bid)\nif snapshot.player_action == PlayerAction.CHOOSE_PARTNER:\n print(\"choose partner\")\n partner_input = input() # AC for ace club, 2H for 2 heart, 10S for 10 spade\n client.choose_partner(player_id, game_id, Card.from_string(partner_input))\nif snapshot.player_action == PlayerAction.TRICK:\n print(\"trick turn\")\n trick_input = input() # AC for ace club, 2H for 2 heart, 10S for 10 spade\n client.trick(player_id, game_id, Card.from_string(trick_input))\nif snapshot.player_action is None:\n # player stops polling for their game snapshot\n print(\"ended\")\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "bridgepy is a python package for playing floating bridge!",
"version": "0.0.10",
"project_urls": {
"Homepage": "https://github.com/papillonbee/bridgepy"
},
"split_keywords": [
"floating bridge",
" singaporean brdige"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "d4a3353200784661b8467bfeb0cb769bad1decf2250c087e44808a672e079310",
"md5": "ed99a5d549caf42245a1034e3b8c6899",
"sha256": "c92e0b5cd0b6e98fd36ed884851a8082149f55a8809894db9ed9a65eaf6c0a91"
},
"downloads": -1,
"filename": "bridgepy-0.0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ed99a5d549caf42245a1034e3b8c6899",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 10397,
"upload_time": "2025-03-09T07:47:48",
"upload_time_iso_8601": "2025-03-09T07:47:48.214715Z",
"url": "https://files.pythonhosted.org/packages/d4/a3/353200784661b8467bfeb0cb769bad1decf2250c087e44808a672e079310/bridgepy-0.0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d58649a140839a8d0d4be6735a093a48aa31ebcb78b58c99c26f1438517ff2ef",
"md5": "f5a44a2918982e542f844a5c390f625c",
"sha256": "20704cf18b7e6816984790e88883ae2d4abe77d0dbc01619f424d7185e4137eb"
},
"downloads": -1,
"filename": "bridgepy-0.0.10.tar.gz",
"has_sig": false,
"md5_digest": "f5a44a2918982e542f844a5c390f625c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 11416,
"upload_time": "2025-03-09T07:47:52",
"upload_time_iso_8601": "2025-03-09T07:47:52.999087Z",
"url": "https://files.pythonhosted.org/packages/d5/86/49a140839a8d0d4be6735a093a48aa31ebcb78b58c99c26f1438517ff2ef/bridgepy-0.0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-03-09 07:47:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "papillonbee",
"github_project": "bridgepy",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "bridgepy"
}