# Poker Hilo
- Hi-Lo stands for High-Low which is a poker game
- Dealer first draw two cards, 1 face-up, 1 face-down. Then user have to
guess whether the face-down card is higher or lower than the face-up card
- User will win if they guess right, and push if two cards are tie
- The face-up card must not be `King` or `Ace`
- The odds depends on the face-up card and the choosen `game_type`. For example
if the face-up card is `Queen` and choosen `game_type` is `LO` the odds is 0.1,
the reason the odds is very low in this case because there are 11 cards which are
lower than `Queen` so user got a very high chance to win the game.
- In this game FE need to make 2 request. The first request to get the face-up card
and the second request(AKA bet request) to place a bet
# DB
- `hi_lo_game_record`
- `id(bigint)`: primary key
- `user_id(int)`: reference to user table
- `partner_id(int)`: reference to partner table
- `hi_lo_batch_id(bigint)`: reference to hi_lo_card_batch table
- `first_card(text)`:
- `second_card(text)`:
- `game_type(text)`: either HI or LO
- `bet_amount(bigint)`:
- `result_type(int)`
- `reward_amount(bigint)`:
- `ctime, mtime, status`
- `hi_lo_config`
- `id(bigint)`: primary key
- `partner_id(int)`: reference to partner table
- `bet_min(bigint)`: minimum amount of money user have to bet in a game
- `bet_max(bigint)`: maximum amount of money user can bet in a game
- `odds([]float)`: this odds aray for `LO` game type. When build the Config object
on application level odds array for `HI` must be the reverse of odds array for `LO`
- `ctime, mtime, status`
- `hi_lo_card_batch`
- `id(bigint)`: primary key
- `first_card(text)`:
- `user_id(int)`:reference to user table
- `ctime, mtime, status`
# Card value
- card value is a string with 2 character
- The first character is the suit of the card
- The second character is the face of the card
- There are 4 suits: DIAMOND(D), HEART(H), SPADE(S), AND CLUB(C)
- There are 13 faces: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K
# Odds table
| Value | Card | Lower | Higher |
| ----- | ---- | ----- | ------ |
| 13 | K | - | - |
| 12 | Q | 0.1 | 9.7 |
| 11 | J | 0.1 | 4.3 |
| 10 | 10 | 0.1 | 2.5 |
| 9 | 9 | 0.3 | 1.6 |
| 8 | 8 | 0.5 | 1.1 |
| 7 | 7 | 0.7 | 0.7 |
| 6 | 6 | 1.1 | 0.5 |
| 5 | 5 | 1.6 | 0.3 |
| 4 | 4 | 2.5 | 0.1 |
| 3 | 3 | 4.3 | 0.1 |
| 2 | 2 | 9.7 | 0.1 |
| 1 | A | - | - |
# API specs
## Fetching API
- This API must be called before the betting API
- This API is used to fetch the first card
### Endpoint
`api/game/hi_lo/gen`
### Request
- empty request body
### Response
- `batch_id(bigint)`: is used along with the betting API to indentify `first_card`
- `first_card(text)`:
### Example
```sh
http POST $cards_url/api/game/hi_lo/gen
HTTP/1.1 200
{
"batch_id": 1,
"first_card": "H3"
}
```
### BE implementation
- Validate token
- Generate card
- Insert `hi_lo_card_batch` into DB
## Betting API
### Endpoint
`api/game/hi_lo/bet`
### Request
- `batch_id(bigint)`: is used to identify `first_card`
- `game_type(text)`: either HI or LO
- `bet_amount(bigint)`: amount of money user bet in
### Response
- `first_cards(text)`:
- `second_card(text)`:
- `game_type(text)`: either HI or LO
- `game_result(int)`: either WIN(1), LOSE(2), DRAW(3)
- `bet_amount(bigint)`: amount of money user bet in
- `reward_amount(bigint)`: amount of money user rewardded
### Example
```sh
echo '{
"batch_id": 1,
"game_type": 1,
"bet_amount: 10000
}' | http $card_url/api/game/hi_lo/bet
HTTP/1.1 200
{
"first_cards": "H3",
"second_cards": "DK"
"game_type": 1,
"game_result": 1,
"bet_amount": 100000,
"reward_amount": 200000,
}
```
### BE implementation
- Validate session, config, bets, balance
- Fetch `hi_lo_card_batch` and validate it
- Whether `hi_lo_card_batch.user_id` identical to `user_id` from token
- Whether `hi_lo_card_batch.status` is Active(1)
- Whether `hi_lo_card_batch.ctime` is within 10 minutes from current timestamp
- Update `hi_lo_card_batch.status` to InActive(1)
- Create bet transaction, and deduct money from user's wallet
- Generate second card, and check if user win
- Insert game record and bet records into db
- If reward greater than zero, create a reward transaction and add up money
to the user's wallet