Skip to content

Commit cf7a54a

Browse files
chore: cleanup files and readme (#37)
Light refactor for zone request/responses Change log level to debug Improve readme
1 parent f0e1224 commit cf7a54a

File tree

8 files changed

+391
-165
lines changed

8 files changed

+391
-165
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ go.work.sum
2525
.env
2626

2727
*.json
28+
!config.docker.json
2829

2930
# test build
3031
tcg-api

README.md

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# tcg-api
1+
# TCG API
22
A lightweight REST API for simulating card decks, built with Go. The API features a modular card interface design that separates game-specific mechanics from general deck mechanics, making it extensible for different types of card games.
33

44
## Technical Stack
@@ -23,7 +23,93 @@ A lightweight REST API for simulating card decks, built with Go. The API feature
2323
- Shuffle
2424
- Draw (TODO)
2525

26-
## Resource Design
26+
## Getting Started
27+
28+
### Running with Docker
29+
30+
1. Clone the repository:
31+
```bash
32+
git clone https://github.com/jwebster45206/tcg-api.git
33+
cd tcg-api
34+
```
35+
36+
2. Edit `docker-compose.yml` file, or use project defaults.
37+
38+
3. Run:
39+
```bash
40+
docker compose up --build
41+
```
42+
43+
4. The API will be available at `http://localhost:8080/v1`.
44+
45+
> Sample config is provided in [`config.docker.json`](./config.docker.json).
46+
47+
### Example Usage
48+
49+
#### Health Endpoint
50+
```bash
51+
curl --location 'http://localhost:8080/health'
52+
```
53+
54+
#### List Decks
55+
````bash
56+
curl --location 'http://localhost:8080/v1/decks'
57+
````
58+
59+
#### Create a DeckState
60+
````bash
61+
curl --location 'http://localhost:8080/v1/deckstates' \
62+
--header 'Content-Type: application/json' \
63+
--data '{
64+
"deck_id": "d0000000-0000-0000-0000-000000000001",
65+
"player_count": 2
66+
}'
67+
````
68+
69+
#### Shuffle Deck
70+
````bash
71+
curl --location 'http://localhost:8080/v1/deckstates/11111111-1111-1111-1111-111111111111/actions/sort-zone?include=meta%2Citems' \
72+
--header 'Content-Type: application/json' \
73+
--data '{
74+
"zone": "draw",
75+
"sort": "shuffle"
76+
}'
77+
````
78+
79+
#### Order Deck by Definition
80+
````bash
81+
curl --location 'http://localhost:8080/v1/deckstates/11111111-1111-1111-1111-111111111111/actions/sort-zone?include=meta%2Citems' \
82+
--header 'Content-Type: application/json' \
83+
--data '{
84+
"zone": "draw",
85+
"sort": "definition"
86+
}'
87+
````
88+
89+
#### Add a Zone
90+
```bash
91+
curl --location 'http://localhost:8080/v1/deckstates/11111111-1111-1111-1111-111111111111/actions/add-zone' \
92+
--header 'Content-Type: application/json' \
93+
--data '{
94+
"zone": "foo",
95+
"size": 10
96+
}'
97+
```
98+
99+
#### Remove a Zone
100+
Note: Only empty zones may be removed.
101+
````bash
102+
curl --location 'http://localhost:8080/v1/deckstates/11111111-1111-1111-1111-111111111111/actions/remove-zone' \
103+
--header 'Content-Type: application/json' \
104+
--data '{
105+
"zone": "foo"
106+
}'
107+
````
108+
109+
### Draw cards
110+
TODO
111+
112+
## Resource Design
27113

28114
Resources are separated into two main packages, `deckdef` and `deckstate`.
29115

@@ -97,4 +183,12 @@ Performance and modeling notes:
97183
1. Role-based access control
98184
- Admin roles for card management
99185
- Player roles for deck management and simulation
100-
- Card details dependent on card facing and user claims
186+
- Card details dependent on card facing and user claims
187+
188+
## License
189+
190+
MIT
191+
192+
## Contributing
193+
194+
Pull requests ok. For major changes, please open an issue first.

cmd/tcg-api/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ func main() {
4848
}
4949

5050
// Set up structured logging
51-
logger := config.NewLogger(cfg.Logger)
51+
loggerConfig := cfg.Logger
52+
loggerConfig.Level = config.LogLevelDebug // TODO: Add log level configuration to config
53+
logger := config.NewLogger(loggerConfig)
5254
config.SetDefaultLogger(logger)
5355

5456
logger.Info("Starting TCG API",

config.docker.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"env": "development",
3+
"port": "8080",
4+
"db": {
5+
"host": "mysql",
6+
"port": "3306",
7+
"user": "tcg_user",
8+
"password": "password",
9+
"dbname": "tcg"
10+
},
11+
"db_reader": {
12+
"host": "mysql",
13+
"port": "3306",
14+
"user": "tcg_user",
15+
"password": "password",
16+
"dbname": "tcg"
17+
},
18+
"redis": {
19+
"host": "redis",
20+
"port": "6379",
21+
"password": "",
22+
"db": 0
23+
},
24+
"logger": {
25+
"level": "info",
26+
"format": "json"
27+
}
28+
}

internal/handlers/deckstate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ func TestDeckStateHandler_SortZoneAction(t *testing.T) {
571571
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
572572
}
573573

574-
var response SortZoneResponse
574+
var response ZoneResponse
575575
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
576576
t.Errorf("Failed to unmarshal response: %v", err)
577577
}

internal/handlers/shuffle.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ type SortZoneRequest struct {
2323
Sort string `json:"sort"`
2424
}
2525

26-
// SortZoneResponse represents the response from a sort zone operation
27-
type SortZoneResponse struct {
28-
Zone *deckstate.Zone `json:"zone"`
29-
Meta *SortZoneMeta `json:"meta,omitempty"`
30-
}
31-
32-
// SortZoneMeta contains metadata about the sort operation
33-
type SortZoneMeta struct {
34-
ZoneLength int `json:"zoneLength"`
35-
Sort string `json:"sort"`
36-
DurationMS float64 `json:"durationMS"`
37-
}
38-
3926
// shuffleZone performs a shuffle operation on a zone, returning
4027
// a measurement of the time taken to perform the shuffle.
4128
func shuffleZone(zone *deckstate.Zone) (time.Duration, error) {
@@ -202,15 +189,18 @@ func (h *DeckStateHandler) handleSortZone(w http.ResponseWriter, r *http.Request
202189
responseZone.Items = nil
203190
}
204191

205-
sortResponse := SortZoneResponse{
206-
Zone: &responseZone,
192+
sortResponse := ZoneResponse{
193+
Success: true,
194+
Zone: &responseZone,
207195
}
208196

209197
if includeMeta {
210-
sortResponse.Meta = &SortZoneMeta{
211-
ZoneLength: len(zone.Items),
212-
Sort: req.Sort,
198+
zoneLength := len(zone.Items)
199+
sortResponse.Meta = &ZoneResponseMeta{
200+
Operation: "sort_zone",
213201
DurationMS: float64(duration.Microseconds()) / 1000, // Convert to milliseconds
202+
ZoneLength: &zoneLength,
203+
Sort: &req.Sort,
214204
}
215205
}
216206

0 commit comments

Comments
 (0)