Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
/ game-score-api Public archive

Spring based score api for generic games written with Kotlin. Uses PostgreSQL as data store, Flyway for database migrations. Runs as docker image. Junit Jupiter + Mockito for testing. Might be suitable as a Spring Kotlin template since its pretty easy to clean.

License

Notifications You must be signed in to change notification settings

norkator/game-score-api

Repository files navigation

Game score api

Spring based score api for generic games. I use this for Spring and Kotlin learning. Requirement for this project is to be as simple as possible with as less code as possible. Final implementation most likely uses basic authentication for clients to post and get results which is security wise bad, but PR's could bring more security schemas like OAuth or something else.

Table of contents

Stack

Structure

Sketch of planned use case. game-score-api-sequence-drawio

  • User account is owned by a developer who manages games.
  • Less we know about game clients the better. Find a way to get device specific unique identifier to populate clientId.

Sketch of planned database structure. game-score-api-db-drawio

Environment

You need to have Postgresql server installed. This app needs it as data store.

Installing

Run standalone with ide having env var string

DATABASE_URL=localhost;DATABASE_USERNAME=someuser;DATABASE_PASSWORD=somepassword

or use docker via docker-compose up.

  1. Create psql database called game-scores.
  2. Rename docker-compose.yml_tpl to docker-compose.yml
  3. Set environment variable values:
  - DATABASE_URL=
  - DATABASE_USERNAME=
  - DATABASE_PASSWORD=
  1. Run docker-compose up.
  2. Open https://localhost:8080/.

API docs

Admin only

Create and get games:

curl -X POST -H "Authorization: Basic <content>" -H "Content-Type: application/json" -d '{"name":"test game", "description":"example game", "userId": 1}' https://localhost:8080/games/game 
curl -X GET -H "Authorization: Basic <content>" -H "Content-Type: application/json" https://localhost:8080/games/games\?gameId\=1 

Create achievements for game:

curl -X POST -H "Authorization: Basic <content>" -H "Content-Type: application/json" -d '{"gameId":1, "name": "achievement name", "description":"example description"}' https://localhost:8080/achievements/achievementType

Everyone

Get random generated uid for a client in case if unique identifiers are not provided by a device.

curl -X GET -H "Content-Type: application/json" https://localhost:8080/clients/generateUid

Post a new client, like device running game. Use this if unique uid is generated at a device side.

curl -X POST -H "Content-Type: application/json" -d '{"clientUid":"testclientuid", "nickname": "SomeNickName"}' https://localhost:8080/clients/client 

Change client nickname:

curl -X PATCH -H "Content-Type: application/json" -d '{"clientUid":"testclientuid", "nickname": "SomeNickName2"}' https://localhost:8080/clients/nickname 

Get scores, different ways:

curl -X GET -H "Content-Type: application/json" https://localhost:8080/scores/score\?gameId\=1\&clientUid\=testclientuid
curl -X GET -H "Content-Type: application/json" https://localhost:8080/scores/bestByGame\?gameId\=1

Post new score:

curl -X POST -H "Content-Type: application/json" -d '{"gameId":1, "clientUid": "testclientuid", "score": 100}' https://localhost:8080/scores/score 

Post new achievement:

curl -X POST -H "Content-Type: application/json" -d '{"gameId":1, "clientUid": "testclientuid", "achievementTypeId": 1}' https://localhost:8080/achievements/achievement 

Get achievements, different ways:

curl -X GET -H "Content-Type: application/json" https://localhost:8080/achievements/achievementsByGameId\?gameId\=1
curl -X GET -H "Content-Type: application/json" https://localhost:8080/achievements/achievementsByGameIdAndClientUid\?gameId\=1\&clientUid\=testclientuid

Using Github container registry image

Create file docker-compose.yml and paste in following contents:

version: '3'
services:
  game-score-api:
    container_name: game-score-api
    image: ghcr.io/norkator/game-score-api:latest
    ports:
      - "8080:8080"
    environment:
      - DATABASE_URL=
      - DATABASE_USERNAME=
      - DATABASE_PASSWORD=

then run sudo docker-compose up --build and you have pre-built container running.

rebuild with the latest changes (this deletes image):

sudo docker image ls
sudo docker image rm <IMAGE ID> -f
sudo docker-compose up --build

start as service docker-compose up -d --build

Development

Notes and stuff for development.

Todo list

  • Implement skeleton structure.
  • Create score controller.
  • Create score model.
  • Create score entity.
  • Create score repository.
  • Create score service.
  • Learn migrations.
    • Create score entity and other tables.
  • Learn and create tests.
    • Add a skeleton of actions builds pipeline.
    • Add tests testing all controllers.
  • Docker configurations.
  • Create build pipeline.
    • Github container registry image via actions.
  • Craft fully functional game score api.
    • Planning.
  • Some simple authentication system.
    • Planning /Basic auth?
    • Multi-user basic auth.
  • Documentations and drawings.
    • Database structure drawing.
    • Getting started.

About

Spring based score api for generic games written with Kotlin. Uses PostgreSQL as data store, Flyway for database migrations. Runs as docker image. Junit Jupiter + Mockito for testing. Might be suitable as a Spring Kotlin template since its pretty easy to clean.

Topics

Resources

License

Stars

Watchers

Forks

Packages