Settlr - Splitwise settling for Poker sessions (written in Python)
Tags: webdev, splitwiseapi, react, python
Published: 2023-03-03 08:37:38

## 
[`LIVE on Vercel + Heroku`](https://settlr-fe.vercel.app/)

Recently over the past couple of months, a bunch of friends - including my roommates and I get together on Fridays (and maybe also Saturday nights considering we're not sore losers lol) for Poker.
 
We begin dealing around 10 PM and go all night until 1 AM. 
By 1 AM almost all of us are exceptionally exhausted (not to mention losing multiple buy-in's certainly doesn't help).

Then comes the annoying part, besides counting each player's chips value, someone takes turns in uploading the winnings/loses as each activity/transaction in Splitwise.

Transactions in splitwise are mandatory to be accurate to the penny, followed by assignment of players as corresponding debtor and creditors. 
Carelessness in this regard is simply begging for arguments between the group members.

An age old way to tackling human error has been the concept of "machines" and more so in this case "an application", which is why I came up with **Settlr**.

Splitwise API documentation while being slightly obscure does contain sufficient examples to get you started.

Settlr is a python-flask web app backed with a minimalistic user interface built using Material UI, focused on ReactJS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tc74qv251k74uvy3b0x.png)

Source code - [Settlr](https://github.com/joqim/Settlr)
Live - [Vercel + Heroku](https://settlr-fe.vercel.app)

I began working on Settlr on tuesday, coming out hot out of last weekend's game and speedran developing the codebase just in time for the upcoming weekend. 

Considering the on and off intricacies of Splitwise API, I managed to present Settlr for user testing by Wednesday evening.

The high level overview is that at the end of each poker game, Settlr requests key information such as the buy-in amount (which corresponds to 1000 units value in chips). 

FYI, each player initially sets up with 1 "buy-in" which is, assume for this example, "$5" in real money and thus translates to 1000 units split into denominations of 10*_5_, 20*_5_, 50*_3_, 100*_2_ and 500*_1_.

As each player burns through his first buy-in, he requests the house for additional buy-in's thus spending additional $5's.

Besides, the buy-in amount, Settlr also requires the total number of individual players participated in the poker session.

Using the beauty of oauth2, Settlr makes a request to the dedicated poker group on Splitwise using my access token and renders each member of the group as a participant in the front-end.

At the end of the session, the user then specifies the value of chips left with each player in units as well as the number of buy-ins for each player.

Ultimately, a button click for "_Splitwise Update_" carries user provided data to the python backend and uses a custom distribution logic which then prepares an `Expense()` object rendering the transactions between each participant, all of which is encompassed as a single activity in Splitwise.

```
    expense = Expense()
    expense.setCost(total_money_paid)
    
    users = []
    for member in members:
        user = ExpenseUser()
        user.setId(member['id'])

        if(float (member['money']) > 0):
            user.setPaidShare(member['money'])
            #user.setPaidShare('2.00')
        else:
            money = abs(float (member['money']))
            print("money negative", money, flush=True)
            user.setOwedShare(str(money))

        users.append(user)

    expense.setDescription("Testing")
    expense.setUsers(users)
    expense.setGroupId(44555421)
    
    sObj.createExpense(expense)
```

`Thus essentially settling.`

<u>Life before Settlr:</u>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvykph1csl4i5qyqhp2l.jpeg)

User testing Settlr resulted in exponentially faster run-times (as expected) compared to manually updating each activity in Splitwise while also reiterating accuracy.

<u>Life after Settlr:</u>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/geh6rqxz7q849rlaq2gz.png)

I can't wait to test this out in the upcoming weekend.

Besides the plethora of quality of life changes I have in the pipeline for Settlr, I was researching into "playing cards and chip identification using computer vision".

Integrating this concept greatly improves the aspect of "settling" manifold.