0

I'm building a roulette game for fun, and the game will use a RESTful API. I'm building out the functionality for spinning the roulette wheel.

I'm using MongoDB and have a collection for players

_id
username
balance
games
    _id
    game_name
    last_bet_at
    created_at
last_logged_in_at
created_at

and another collection, player_game_logs, for all plays that happen in games

_id
player_game_id
game_name
bets (object)
result (object)
win_amount
loss_amount
balance
ip_address
created_at

I'm not sure what the RESTful API method should look like for telling the server to spin the roulette wheel and record a player game log for the play. Obviously I need to POST against a resource, and I'm not sure what that resource should be. Perhaps I even need an additional MongoDB collection.

Here are two possible methods I've come up with...

Method 1

POST /games/roulette/spin

request
{
    bets: {
        even: 1,
        red: 1
    }
}

response
{
    balance: 156,
    result: {
        "color": "black",
        "number": "31"
    },
    win_amount: 0,
    lose_bets: {
        even: 1,
        red: 1
    }
}

Method 2

POST /players/game_logs

request
{
    player_game_id: 1234,
    game_name: 'roulette',
    bets: {
        even: 1,
        red: 1
    }
}

response
{
    player_game_id: 1234,
    game_name: 'roulette',
    result: {
        color: 'black',
        number: 10
    },
    win_amount: 0,
    loss_amount: 2,
    balance: 156
}

I favor method 2. Is there anything wrong with this method, and what might be a better approach?

1

Method 2: Game logs doesn't sound to be good API name. Try and give better name for URI's. You can use some of of REST features in urls. For example you can use urls like POST /players/game/{game_id}. URL's can be more specific to the resource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.