Dice and Deck API!

So today I worked on making a small API just for funsies. It allows you to roll dice or draw a card from a deck. It was a thought I had while thinking of a sole TTRPG called Colostle. In Colostle you draw cards from a deck, and based on the card you get events that way. I thought it would be cool to have a deck of cards that I can easily come back to later and have in the same order. And I figured why not start with dice instead?

The dice part was pretty easy, simply parse the string into dice. So for instance hitting http://web.site/die/1d3 4d6 2d20 would roll one D3, four D6's and two D20's. The rolling is done via Python's builtin random.randint function. When done I total up all the die for each category (e.g., I total the 4D6) and I return them in JSON format.

Here's an example output from that call:

{
  "1d3": {
    "1": 3,
    "total": 3
  },
  "2d20": {
    "1": 4,
    "2": 7,
    "total": 11
  },
  "4d6": {
    "1": 4,
    "2": 5,
    "3": 2,
    "4": 6,
    "total": 17
  }
}

The deck part was a little trickier. I needed a way to store a shuffled deck and then recall it later. I ended up using SQLite to store the decks. I have table called DECKS that has the following DDL:

CREATE TABLE DECKS (DECK_NAME VARCHAR, CARD VARCHAR, DECK_ORDER INTEGER);

So when I put in a deck of cards, I add the name of the deck and the order in which they'll be pulled. Then to get the next card is as simple as:

SELECT CARD 
    FROM DECKS
    WHERE NAME = 'Some-Name-Here'
    ORDER BY DECK_ORDER
    LIMIT 1
;

Once I draw a card I run another SQL command that will delete that row from the table (this command is left as an exercise to the reader). The selecting of a card along with deleting it after gives the effect of drawing a card from the deck. And because I'm using SQLite it doesn't matter how long later I come back to this it should still be there. (Although, if other people use this (which I doubt) then I will have to have some way of culling it as to not fill up the storage space (but I really doubt that will ever happen) )

What about the names? Where do they come from?

I appreciate you conveniently asking me that. I have a big 'ol list of words in the database. I got it from some repo with an MIT license and imported them as part of the setup. Then I grab three random words and join them together with hyphens.

On a side note, be careful where you pull lists from. This one had several racial slurs. It was only after working with the list for awhile that I figured I should check on a hunch. Glad I did. Does anyone know where to get a list of words that don't have racial slurs? There are so many words that I'm sure I missed some.

Anyway that's the long and short of it. I haven't actually got an accessible end point set up yet, but when I do I'll post the link. If you wanted to judge me harshly read my code then you can do so >>here<<

I'm hoping to make other utilities over time and have the API turn in to some cyber swiss army knife!

#API #dice #cards #random #TTRPG


If you have something to say don't forget to tag me (@wyrm@wyrm.one) so I can respond!

Or email me at blog (at) lennys (dot) quest.