// File: /workspace/github_cpp_makefile_star2_O2/Thomas1995[P]Rentz/src/table.cpp
// Function: Table::Start()
void Table::Start() {

  std::cout << "\n\nA new game has started!\n\n";

  for(size_t i = 0; i < players.size(); ++i)
    players[i].sendIndex(i);

  // choose players in play

  Card::lowestCard = 15 - players.size() * 2;

  score.resize(TABLE_SIZE, 0);

  // true if a game was already played but a certain connection, false otherwise
  std::vector<std::vector<bool>> gamesPlayed(players.size(), std::vector<bool>(gamesNumber + 1, false));

  for(size_t i=0;i<players.size();++i)
    for(int j=0;j<gamesNumber+1;++j)
      gamesPlayed[i][j] = false;

  // assign the first connection
  firstPlayer = players.begin();

  for(int gameChoice = 1; gameChoice <= gamesNumber; ++gameChoice) {

    for(size_t i=0;i<players.size();++i) {
      // set the first player
      firstPlayer = players.begin() + i;

      std::cout << "Pre-round:\n";
      std::cout << "First player is " << firstPlayer->getName() << '\n';

      // check if connection wants to play NV mode and enforce it for the last choice
      if(gameChoice < gamesNumber) {
        modeNV = players[i].getNVChoice();
      } else {
        modeNV = true;
      }

      // check if modeNV was chosed
      if(modeNV) {
        std::cout << "NV Mode chosen!\n";
      } else {
        std::cout << "NV Mode not chosen!\n";
      }

      // if NV mode not chosen, give cards before game choice
      if(modeNV == false) {
        GiveHands();
        gameType = players[i].getGameChoice();
      } else {
        gameType = players[i].getGameChoice();
        GiveHands();
      }

      // check if connection can play that game
      require(gameType >= 1 && gameType <= gamesNumber,
          players[i].getName() + " has chosen a game index out of bounds\n");

      std::cout << players[i].getName() << " chose to play: " << GameName[gameType] << '\n';

      require(gamesPlayed[i][gameType] == false,
          players[i].getName() + " had already chosen that game\n");

      gamesPlayed[i][gameType] = true;

      // let the other players know the game type
      for(auto &connection : players)
        connection.sendGameChoice(static_cast<uint8_t>(gameType));

      // start the round
      PlayRound();
    }
  }
  std::cout << "Game is over!\n";
  printScores();

  for(auto &connection: players)
    connection.gameEnd();

  GAME_END = true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq 0x7645(%rip), %rdi      # 0xcfc8
leaq 0x2c06(%rip), %rsi      # 0x8590
callq 0x2230
xorl %r15d, %r15d
movl $0x2058, %r12d          # imm = 0x2058
xorl %r14d, %r14d
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rax
subq %rdi, %rax
cqto
idivq %r12
cmpq %rax, %r14
jae 0x59c7
addq %r15, %rdi
movq %r14, %rsi
callq 0x4c18
incq %r14
addq $0x2058, %r15           # imm = 0x2058
jmp 0x599b
addl %eax, %eax
pushq $0xf
popq %rcx
subl %eax, %ecx
leaq 0x7873(%rip), %rax      # 0xd248
movl %ecx, (%rax)
leaq 0x40(%rbx), %rdi
movl 0x7937(%rip), %esi      # 0xd318
leaq 0x18(%rsp), %rdx
andl $0x0, (%rdx)
callq 0x6382
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %r14
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x18(%rsp), %rdi
pushq $0x8
popq %rsi
leaq 0xe(%rsp), %rcx
callq 0x63b6
subq %r15, %r14
movl $0x2058, %r15d          # imm = 0x2058
movq %r14, %rax
cqto
idivq %r15
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rdx
leaq 0xd(%rsp), %rcx
movq %rax, %rsi
callq 0x641a
leaq 0x18(%rsp), %rdi
callq 0x6470
xorl %r12d, %r12d
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rsi
movq %rsi, %rax
subq %rcx, %rax
cqto
idivq %r15
cmpq %rax, %r12
jae 0x5a8a
imulq $0x28, %r12, %r13
xorl %r14d, %r14d
cmpq $0x8, %r14
je 0x5a85
movq 0x40(%rsp), %rdi
addq %r13, %rdi
movq %r14, %rsi
callq 0x6480
notq %rdx
andq %rdx, (%rax)
incq %r14
jmp 0x5a64
incq %r12
jmp 0x5a45
movq %rcx, 0x20(%rbx)
pushq $0x1
popq %r12
leaq 0x18(%rsp), %r13
cmpl $0x8, %r12d
je 0x5d0e
xorl %r8d, %r8d
movq %rsi, %rax
subq %rcx, %rax
cqto
movl $0x2058, %edi           # imm = 0x2058
idivq %rdi
cmpq %rax, %r8
jae 0x5d06
movq %r8, 0x10(%rsp)
imulq $0x2058, %r8, %rbp      # imm = 0x2058
addq %rbp, %rcx
movq %rcx, 0x20(%rbx)
movq 0x74f1(%rip), %rdi      # 0xcfc8
leaq 0x2ace(%rip), %rsi      # 0x85ac
callq 0x2230
movq 0x74de(%rip), %rdi      # 0xcfc8
leaq 0x2ac7(%rip), %rsi      # 0x85b8
callq 0x2230
movq %rax, %r15
movq 0x20(%rbx), %rsi
movq %r13, %rdi
callq 0x495c
movq %r15, %rdi
movq %r13, %rsi
callq 0x2220
movq %rax, %rdi
pushq $0xa
popq %rsi
callq 0x22a0
movq %r13, %rdi
callq 0x2110
cmpl $0x7, %r12d
jne 0x5b36
movb $0x1, 0x5c(%rbx)
leaq 0x2a95(%rip), %rsi      # 0x85c9
jmp 0x5b59
movq 0x8(%rbx), %rdi
addq %rbp, %rdi
callq 0x4ab2
movb %al, 0x5c(%rbx)
testb %al, %al
leaq 0x2a8c(%rip), %rsi      # 0x85da
leaq 0x2a74(%rip), %rax      # 0x85c9
cmovneq %rax, %rsi
movq 0x7468(%rip), %rdi      # 0xcfc8
callq 0x2230
cmpb $0x0, 0x5c(%rbx)
je 0x5b8b
movq 0x8(%rbx), %rdi
addq %rbp, %rdi
callq 0x4988
movzbl %al, %eax
movl %eax, 0x58(%rbx)
movq %rbx, %rdi
callq 0x5dd2
movl 0x58(%rbx), %r15d
jmp 0x5ba7
movq %rbx, %rdi
callq 0x5dd2
movq 0x8(%rbx), %rdi
addq %rbp, %rdi
callq 0x4988
movzbl %al, %r15d
movl %r15d, 0x58(%rbx)
movq 0x8(%rbx), %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0x495c
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0x2a27(%rip), %rdx      # 0x85ef
callq 0x649d
decl %r15d
xorl %edi, %edi
cmpl $0x7, %r15d
setb %dil
movq %r14, %rsi
callq 0x7154
movq %r14, %rdi
callq 0x2110
movq %r13, %rdi
callq 0x2110
movq 0x8(%rbx), %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0x495c
movq 0x73c0(%rip), %rdi      # 0xcfc8
movq %r13, %rsi
callq 0x2220
movq %rax, %rdi
leaq 0x29fd(%rip), %rsi      # 0x8617
callq 0x2230
movslq 0x58(%rbx), %rsi
shlq $0x5, %rsi
addq 0x76d2(%rip), %rsi      # 0xd300
movq %rax, %rdi
callq 0x2220
movq %rax, %rdi
pushq $0xa
popq %rsi
callq 0x22a0
movl %r12d, %r14d
movq %r13, %rdi
callq 0x2110
imulq $0x28, 0x10(%rsp), %r12
movq 0x40(%rsp), %rdi
addq %r12, %rdi
movslq 0x58(%rbx), %rsi
callq 0x6480
movq %rdx, %r15
andq (%rax), %r15
addq 0x8(%rbx), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x495c
leaq 0x58(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x299e(%rip), %rdx      # 0x8628
callq 0x649d
xorl %edi, %edi
testq %r15, %r15
sete %dil
movq %rbp, %rsi
callq 0x7154
movq %rbp, %rdi
callq 0x2110
movq %r13, %rdi
callq 0x2110
addq 0x40(%rsp), %r12
movslq 0x58(%rbx), %rsi
movq %r12, %rdi
callq 0x6480
orq %rdx, (%rax)
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %r12
cmpq %r12, %r15
je 0x5ce6
movzbl 0x58(%rbx), %esi
movq %r15, %rdi
callq 0x4a2c
addq $0x2058, %r15           # imm = 0x2058
jmp 0x5ccc
movq %rbx, %rdi
callq 0x5ec8
movl %r14d, %r12d
movq 0x10(%rsp), %r8
incq %r8
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rsi
jmp 0x5aa4
incl %r12d
jmp 0x5a97
movq 0x72b3(%rip), %rdi      # 0xcfc8
leaq 0x292b(%rip), %rsi      # 0x8647
callq 0x2230
movq %rbx, %rdi
callq 0x603c
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %r15
cmpq %r15, %r14
je 0x5d47
movq %r14, %rdi
callq 0x48e6
addq $0x2058, %r14           # imm = 0x2058
jmp 0x5d31
movb $0x1, (%rbx)
leaq 0x40(%rsp), %rdi
callq 0x64bc
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x6470
jmp 0x5dc9
movq %rax, %rbx
jmp 0x5dc9
jmp 0x5dbc
jmp 0x5dbc
jmp 0x5dbc
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0x5d9a
jmp 0x5dab
jmp 0x5dbc
jmp 0x5dbc
jmp 0x5dbc
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x2110
jmp 0x5dae
jmp 0x5dbc
jmp 0x5dab
jmp 0x5dbc
jmp 0x5dbc
jmp 0x5dab
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2110
jmp 0x5dbf
jmp 0x5dbc
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x64bc
movq %rbx, %rdi
callq 0x23e0
nop