Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/game_player/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
====================================
A SingleTable Design for Game Player
====================================

This example demonstrates a single-table design for a game player application using PynamoDB.

This is based on a workshop at AWS https://catalog.workshops.aws/dynamodb-labs/en-US/game-player-data

At the above workshop you will find 6 sections that go over the details of the design and how to implement it. It is
recommended to go through the workshop first before looking at the code here.

This examples show how to use polymorophism in PynamoDB to achieve the single-table design.

Fetch Game and Players
========================

Make sure to read below page -
https://catalog.workshops.aws/dynamodb-labs/en-US/game-player-data/core-usage/step4

The corresponding code (written using pynamodb) for this section is in the file `fetch_game_and_players.py`.

```bash
# Note the script will delete, create & seed the data in the DynamoDB table.
python fetch_game_and_players.py
```

Query the sparse GSI
========================

Make sure to read below page -
https://catalog.workshops.aws/dynamodb-labs/en-US/game-player-data/open-games/step3

The corresponding code (written using pynamodb) for this section is in the file `find_open_games_by_map.py`.

```bash
# Note the script will delete, create & seed the data in the DynamoDB table.
python find_open_games_by_map.py
```

Scan the sparse GSI
===================

Make sure to read below page -
https://catalog.workshops.aws/dynamodb-labs/en-US/game-player-data/open-games/step4

The corresponding code (written using pynamodb) for this section is in the file `find_open_games.py`.

```bash
# Note the script will delete, create & seed the data in the DynamoDB table.
python find_open_games.py
```

Add user to a game
==================

Make sure to read below page -
https://catalog.workshops.aws/dynamodb-labs/en-US/game-player-data/join-games/step1

The corresponding code (written using pynamodb) for this section is in the file `join_game.py`.

```bash
# Note the script will delete, create & seed the data in the DynamoDB table.
python join_game.py
```

Start a game
============

Make sure to read below page -
https://catalog.workshops.aws/dynamodb-labs/en-US/game-player-data/join-games/step2

The corresponding code (written using pynamodb) for this section is in the file `start_game.py`.

```bash
# You MUST run join_game.py first to add a user to the game.
# and then run this script to start the game.
python start_game.py
```

Retrieve games for a user (Inverted Index usecase)
==================================================

Make sure to read below page -
https://catalog.workshops.aws/dynamodb-labs/en-US/game-player-data/past-games/step2

The corresponding code (written using pynamodb) for this section is in the file `find_games_for_user.py`.

```bash
# Note the script will delete, create & seed the data in the DynamoDB table.
python find_games_for_user.py
```
Empty file.
19 changes: 19 additions & 0 deletions examples/game_player/fetch_game_and_players.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from models import GameEntityModel
from utils import create_table, delete_table, seed_table

GAME_ID = "3d4285f0-e52b-401a-a59b-112b38c4a26b"

GAME_PK = f"GAME#{GAME_ID}"
GAME_SK = f"#METADATA#{GAME_ID}"

delete_table()
create_table()
seed_table()

results = GameEntityModel.query(
GAME_PK,
GameEntityModel.SK.between(GAME_SK, "USER$")
)

for r in results:
print(f"Found Game: {r}")
24 changes: 24 additions & 0 deletions examples/game_player/fetch_games_for_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This show case inverted index
# find all past games for a user

from models import GameEntityModel
from utils import create_table, delete_table, seed_table

delete_table()
create_table()
seed_table()

USERNAME = "carrpatrick"

QUERY_PK = f"USER#{USERNAME}"

results = GameEntityModel.inverted_index.query(
QUERY_PK
)

for index, r in enumerate(results):
if index == 0:
print(f"Games for `{r.username}` are ...")

print(f"Game: {r.game_id}")

15 changes: 15 additions & 0 deletions examples/game_player/find_open_games.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# how to find any open game in the application, regardless of the type of map

from models import GameEntityModel
from utils import create_table, delete_table, seed_table

delete_table()
create_table()
seed_table()


results = GameEntityModel.sparse_index.scan()

for index, r in enumerate(results):
print(f"Game: {r.map_name}")

20 changes: 20 additions & 0 deletions examples/game_player/find_open_games_by_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This show cases sparse index
# make targeted queries to find open games.
from models import GameEntityModel
from utils import create_table, delete_table, seed_table

delete_table()
create_table()
seed_table()


MAP_NAME = "Green Grasslands"


results = GameEntityModel.sparse_index.query(
MAP_NAME
)

for index, r in enumerate(results):
print(f"Game: {r}")

Loading