/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#include "player.h"

uint8_t pokerPlayerAdd(poker_t *poker) {
  pokerplayer_t *player;
  uint8_t i = poker->playerCount++;
  
  player = poker->players + i;
  player->cardCount = 0;
  player->chips = 0;
  player->currentBet = 0;
  player->timesRaised = 0;
  player->state = POKER_PLAYER_STATE_OUT;
  
  return i;
}

void pokerPlayerChipsAdd(pokerplayer_t *player, int32_t chips) {
  player->chips += chips;
  if(player->chips > 0) {
    player->state = flagOff(player->state, POKER_PLAYER_STATE_OUT);
  }
}

bool pokerPlayerDoesNeedToBetThisRound(poker_t *poker, uint8_t playerIndex) {
  pokerplayer_t *player;
  player = poker->players + playerIndex;
  if(player->state & POKER_PLAYER_STATE_FOLDED) return false;
  if(player->chips <= 0) return false;
  if(!(player->state & POKER_PLAYER_STATE_HAS_BET_THIS_ROUND)) return true;
  if(player->currentBet < pokerBetGetCurrentCallValue(poker)) return true;
  return false;
}

bool pokerPlayerCanCheck(poker_t *poker, pokerplayer_t *player) {
  return pokerBetGetCurrentCallValue(poker) <= player->currentBet;
}

void pokerPlayerGetFullHand(
  poker_t *poker, pokerplayer_t *player, card_t *cards
) {
  uint8_t i;

  // Add the dealer hand
  for(i = 0; i < poker->communitySize; i++) {
    cards[i] = poker->community[i];
  }

  // Add the player hand
  for(i = 0; i < player->cardCount; i++) {
    cards[i+poker->communitySize] = player->cards[i];
  }
}
