// File: /workspace/github_cpp_makefile_star2_O3/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 0x7ff1(%rip), %rdi      # 0xdfc8
leaq 0x35d2(%rip), %rsi      # 0x95b0
movl $0x1b, %edx
callq 0x2290
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rax
movabsq $-0x34307aae9fc8985d, %r12 # imm = 0xCBCF8551603767A3
subq %rdi, %rax
je 0x6034
xorl %r15d, %r15d
xorl %r14d, %r14d
addq %r15, %rdi
movq %r14, %rsi
callq 0x518e
incq %r14
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rax
subq %rdi, %rax
sarq $0x3, %rax
imulq %r12, %rax
addq $0x2058, %r15           # imm = 0x2058
cmpq %rax, %r14
jb 0x6005
jmp 0x603c
sarq $0x3, %rax
imulq %r12, %rax
addl %eax, %eax
movl $0xf, %ecx
subl %eax, %ecx
leaq 0x81ec(%rip), %rax      # 0xe238
movl %ecx, (%rax)
leaq 0x40(%rbx), %rdi
movl 0x82b0(%rip), %esi      # 0xe308
leaq 0x8(%rsp), %rdx
movl $0x0, (%rdx)
callq 0x6e54
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %r14
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x8(%rsp), %rdi
leaq 0x37(%rsp), %rcx
movl $0x8, %esi
callq 0x6e88
subq %r15, %r14
sarq $0x3, %r14
imulq %r12, %r14
leaq 0x80(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0x36(%rsp), %rcx
movq %r14, %rsi
callq 0x6ef0
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x60c8
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
je 0x6103
sarq $0x3, %rdx
imulq %r12, %rdx
movq 0x80(%rsp), %rsi
cmpq $0x1, %rdx
adcq $0x0, %rdx
movq (%rsi), %rdi
andq $-0x100, (%rdi)
addq $0x28, %rsi
decq %rdx
jne 0x60f0
movq %rax, 0x20(%rbx)
leaq 0x50(%rsp), %r15
movl $0x1, 0x30(%rsp)
movq 0x7ead(%rip), %rbp      # 0xdfc8
leaq 0x8(%rsp), %r14
cmpq %rax, %rcx
je 0x6546
xorl %esi, %esi
movq %rsi, 0x38(%rsp)
imulq $0x2058, %rsi, %r12     # imm = 0x2058
addq %r12, %rax
movq %rax, 0x20(%rbx)
movl $0xb, %edx
movq %rbp, %rdi
leaq 0x347f(%rip), %rsi      # 0x95cc
callq 0x2290
movl $0x10, %edx
movq %rbp, %rdi
leaq 0x3477(%rip), %rsi      # 0x95d8
callq 0x2290
movq 0x20(%rbx), %rsi
movq %r14, %rdi
callq 0x4e9e
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %rdi
callq 0x2290
movb $0xa, 0x7(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7(%rsp), %rsi
callq 0x2290
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x61b7
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2230
cmpl $0x7, 0x30(%rsp)
jne 0x61d0
movb $0x1, 0x5c(%rbx)
movl $0x10, %edx
leaq 0x341b(%rip), %rsi      # 0x95e9
jmp 0x6200
movq 0x8(%rbx), %rdi
addq %r12, %rdi
callq 0x500a
testb %al, %al
leaq 0x3415(%rip), %rsi      # 0x95fa
leaq 0x33fd(%rip), %rcx      # 0x95e9
cmovneq %rcx, %rsi
movb %al, 0x5c(%rbx)
xorb $0x1, %al
movzbl %al, %eax
leaq 0x10(,%rax,4), %rdx
movq %rbp, %rdi
callq 0x2290
cmpb $0x0, 0x5c(%rbx)
je 0x622e
movq 0x8(%rbx), %rdi
addq %r12, %rdi
callq 0x4ede
movzbl %al, %eax
movl %eax, 0x58(%rbx)
movq %rbx, %rdi
callq 0x665c
movl 0x58(%rbx), %r14d
jmp 0x624a
movq %rbx, %rdi
callq 0x665c
movq 0x8(%rbx), %rdi
addq %r12, %rdi
callq 0x4ede
movzbl %al, %r14d
movl %r14d, 0x58(%rbx)
movq 0x8(%rbx), %rsi
addq %r12, %rsi
leaq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x4e9e
movq %r13, %rdi
leaq 0x33a7(%rip), %rsi      # 0x960f
callq 0x2410
movq %r15, 0x40(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6290
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x6297
movups (%rcx), %xmm0
movups %xmm0, (%r15)
decl %r14d
xorl %edi, %edi
cmpl $0x7, %r14d
setb %dil
movq 0x8(%rax), %rdx
movq %rdx, 0x48(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rsi
callq 0x80b8
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x62dd
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
leaq 0x8(%rsp), %r14
je 0x62fe
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x8(%rbx), %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0x4e9e
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %rdi
callq 0x2290
movq %rax, %r15
movl $0x10, %edx
movq %rax, %rdi
leaq 0x3306(%rip), %rsi      # 0x9637
callq 0x2290
movslq 0x58(%rbx), %rax
movq 0x7faf(%rip), %rcx      # 0xe2f0
shlq $0x5, %rax
movq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rdx
movq %r15, %rdi
callq 0x2290
movb $0xa, 0x7(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7(%rsp), %rsi
callq 0x2290
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x6389
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x80(%rsp), %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rcx,4), %r15
movslq 0x58(%rbx), %rcx
leal 0x3f(%rcx), %edx
testq %rcx, %rcx
cmovnsl %ecx, %edx
sarl $0x6, %edx
movslq %edx, %rdx
shlq $0x3, %rdx
addq (%rax,%r15,8), %rdx
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
leaq 0x3f(%rdi), %r14
movq %rcx, %rax
andq %r14, %rax
xorl %esi, %esi
cmpq %rdi, %rax
setbe %sil
movl $0x1, %r13d
shlq %cl, %r13
andq -0x8(%rdx,%rsi,8), %r13
addq 0x8(%rbx), %r12
movq %r12, %rsi
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x4e9e
movq %r12, %rdi
leaq 0x324a(%rip), %rsi      # 0x9648
callq 0x2410
leaq 0x70(%rsp), %r12
movq %r12, 0x60(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x642b
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x6433
movups (%rcx), %xmm0
movups %xmm0, (%r12)
xorl %edi, %edi
testq %r13, %r13
sete %dil
movq 0x8(%rax), %rdx
movq %rdx, 0x68(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x60(%rsp), %rsi
callq 0x80b8
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x6475
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
movabsq $-0x34307aae9fc8985d, %r12 # imm = 0xCBCF8551603767A3
je 0x649b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2230
movslq 0x58(%rbx), %rcx
leal 0x3f(%rcx), %eax
testq %rcx, %rcx
cmovnsl %ecx, %eax
movq 0x80(%rsp), %rdx
sarl $0x6, %eax
cltq
shlq $0x3, %rax
addq (%rdx,%r15,8), %rax
andq %rcx, %r14
xorl %edx, %edx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
cmpq %rsi, %r14
setbe %dl
movl $0x1, %esi
shlq %cl, %rsi
orq %rsi, -0x8(%rax,%rdx,8)
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %r14
cmpq %r14, %r15
movq 0x7ad7(%rip), %rbp      # 0xdfc8
je 0x650b
movzbl 0x58(%rbx), %esi
movq %r15, %rdi
callq 0x4f80
addq $0x2058, %r15           # imm = 0x2058
cmpq %r14, %r15
jne 0x64f3
movq %rbx, %rdi
callq 0x67a8
leaq 0x50(%rsp), %r15
leaq 0x8(%rsp), %r14
movq 0x38(%rsp), %rsi
incq %rsi
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r12, %rdx
cmpq %rdx, %rsi
jb 0x612b
jmp 0x6549
movq %rax, %rcx
movl 0x30(%rsp), %edx
incl %edx
movl %edx, 0x30(%rsp)
cmpl $0x8, %edx
jne 0x6120
movq 0x7a65(%rip), %rdi      # 0xdfc8
leaq 0x30fd(%rip), %rsi      # 0x9667
movl $0xe, %edx
callq 0x2290
movq %rbx, %rdi
callq 0x695c
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %r15
cmpq %r15, %r14
je 0x659a
movq %r14, %rdi
callq 0x4e22
addq $0x2058, %r14           # imm = 0x2058
jmp 0x6584
movb $0x1, (%rbx)
leaq 0x80(%rsp), %rdi
callq 0x6f88
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x6653
leaq 0x8(%rsp), %rdi
callq 0x271e
jmp 0x6653
movq %rax, %rbx
jmp 0x6653
jmp 0x6643
jmp 0x6643
jmp 0x6643
jmp 0x6620
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x6623
movq 0x70(%rsp), %rsi
jmp 0x6610
jmp 0x6643
jmp 0x6643
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6623
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x6623
jmp 0x6620
jmp 0x6643
jmp 0x6620
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x6646
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x6646
jmp 0x6643
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x6f88
movq %rbx, %rdi
callq 0x23c0
nop