// File: /workspace/github_cpp_makefile_star2_O2/milzar[P]Multiplayer-Hangman-Game/src/server.cpp
// Function: main
int main(int argc,char *argv[]){

        int maxPlayers =atoi(argv[2]);
        bool beginGame=false;
        vector<Player> players;
        cout<<"Server runnin on Port "<<argv[1]<<endl;


        Server myserver(argv[1],maxPlayers);

        fd_set masterfds;
        FD_ZERO(&masterfds);
        FD_SET(myserver.mastersocket,&masterfds);
        int max_file ;
        max_file = myserver.mastersocket;


        while(!beginGame){
          masterfds = initFdset(players,myserver.mastersocket);
          select( max_file + 1 , &masterfds , NULL , NULL , NULL);
          //Activity on main socket : Incoming Connection before game started
          if(FD_ISSET(myserver.mastersocket, &masterfds)){
              int newSocket = accept(myserver.mastersocket,(sockaddr*)NULL  ,NULL);
              string newName = myserver.receive(newSocket);
              Player tmp = Player(newName,newSocket);
              players.push_back(tmp);

              cout<<"New User Added:\t"<<tmp.name<<endl;
              max_file = updateMaxFD(players,max_file);
              myserver.forward(newSocket,"Welcome to Battle Royale Hangman(2018)" );
          }

          cout<<"No of Players:\t"<<players.size()<<endl;
          if(players.size()>= maxPlayers  )
              beginGame=true;

        }
        sleep(1);
        //Begin Game
        string root_word = GameData::getRandomWord();
        GameData game(root_word.size());
        sendGameNotification(players,myserver,game);
        //GAMEDATA

        int turn=0;
        while(true){
            int currentPlayer = players[turn].socket_id;
            for(auto player : players){
                if(player.socket_id == currentPlayer)
                  myserver.forward(player.socket_id, "play");
                else
                  myserver.forward(player.socket_id, "wait");
            }
            //Receive Client guess
            string guess = myserver.receive(currentPlayer);
            if(game.play(root_word,guess) ){
                cout<<"Game Over\t"<<players[turn].name<<" Wins\t"<<endl;
                string res = "gameover"+players[turn].name + "||" +root_word;
                for(auto player : players){
                      myserver.forward(player.socket_id, res);
                }
                // cout<<"Play Again?"<<endl;
                // sleep(1);
                // res = myserver.receive();
                break;
            }
            else{
                string res = game.serialize();
                for(auto player : players){
                      myserver.forward(player.socket_id, res);
                }
                turn = (turn +1)%maxPlayers;
            }

            sleep(1);
          }
     return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rsi, %rbx
movq 0x10(%rsi), %rdi
callq 0x2300
movl %eax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq 0x4a16(%rip), %rdi      # 0x6fd8
leaq 0x1a3b(%rip), %rsi      # 0x4004
callq 0x2240
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x2130
movq 0x8(%rbx), %rsi
leaq 0xc(%rsp), %rdi
movl 0x10(%rsp), %edx
callq 0x38ec
xorl %eax, %eax
cmpq $0x10, %rax
je 0x260a
andq $0x0, 0x278(%rsp,%rax,8)
incq %rax
jmp 0x25f6
leaq 0xc(%rsp), %rax
movl (%rax), %r14d
pushq $0x40
popq %rcx
movl %r14d, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %r14d, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, 0x278(%rsp,%rdx,8)
movslq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
leaq 0x118(%rsp), %r15
leaq 0x130(%rsp), %r13
testb $0x1, %al
jne 0x27e9
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x2c96
movl 0xc(%rsp), %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x3abf
pushq $0x10
popq %rcx
leaq 0x278(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
callq 0x2cf0
leal 0x1(%r14), %edi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2220
movl 0xc(%rsp), %edi
movl %edi, %eax
cltd
pushq $0x40
popq %rcx
idivl %ecx
cltq
movq 0x278(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x2792
xorl %esi, %esi
xorl %edx, %edx
callq 0x20c0
movl %eax, %ebx
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
movl %eax, %edx
callq 0x3910
leaq 0x230(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x22c0
movq %r13, %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x3a5a
movq %r12, %rdi
callq 0x2120
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x2d12
movq 0x48ba(%rip), %rdi      # 0x6fd8
leaq 0x18f6(%rip), %rsi      # 0x401b
callq 0x2240
movq %rax, %rdi
movq %r13, %rsi
callq 0x21f0
movq %rax, %rdi
callq 0x2130
leaq 0xe0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2c96
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
callq 0x3a93
movl %eax, %r14d
movq %r12, %rdi
callq 0x2cf0
leaq 0xc(%rsp), %rdi
movl %ebx, %esi
leaq 0x18b1(%rip), %rdx      # 0x402c
callq 0x39c0
movq %r13, %rdi
callq 0x2120
leaq 0x48(%rsp), %rdi
callq 0x2120
movq 0x483f(%rip), %rdi      # 0x6fd8
leaq 0x18b3(%rip), %rsi      # 0x4053
callq 0x2240
movq %rax, %rdi
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
movq %rax, %rsi
callq 0x2180
movq %rax, %rdi
callq 0x2130
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq 0x18(%rsp), %rax
setae %al
jmp 0x2650
pushq $0x1
popq %rdi
callq 0x21a0
leaq 0x48(%rsp), %rdi
callq 0x32d4
movl 0x50(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x3114
leaq 0x88(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2c96
movl 0xc(%rsp), %esi
movups 0xa0(%rsp), %xmm0
movups 0xb0(%rsp), %xmm1
leaq 0x250(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm1, 0x10(%rdx)
leaq 0x88(%rsp), %rdi
callq 0x24ec
leaq 0x88(%rsp), %rdi
callq 0x2cf0
movl $0x0, 0x14(%rsp)
leaq 0xc(%rsp), %r15
leaq 0x130(%rsp), %r14
leaq 0x1f0(%rsp), %rbx
movslq 0x14(%rsp), %rax
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r12
imulq $0x28, %rax, %rax
movq %rax, 0x18(%rsp)
movl 0x20(%r13,%rax), %ebp
cmpq %r12, %r13
je 0x28d6
movq %r14, %rdi
movq %r13, %rsi
callq 0x2c78
movl 0x150(%rsp), %esi
cmpl %ebp, %esi
leaq 0x17b3(%rip), %rdx      # 0x4068
leaq 0x17a7(%rip), %rax      # 0x4063
cmoveq %rax, %rdx
movq %r15, %rdi
callq 0x39c0
movq %r14, %rdi
callq 0x2120
addq $0x28, %r13
jmp 0x2895
leaq 0xc0(%rsp), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x3910
leaq 0xf8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x22c0
leaq 0x210(%rsp), %r13
movq %r13, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x22c0
leaq 0xa0(%rsp), %rdi
leaq 0xf8(%rsp), %r12
movq %r12, %rsi
movq %r13, %rdx
callq 0x3146
movl %eax, %ebp
movq %r13, %rdi
callq 0x2120
movq %r12, %rdi
callq 0x2120
movl %ebp, 0x44(%rsp)
testl %ebp, %ebp
je 0x2a51
movq 0x4686(%rip), %rdi      # 0x6fd8
leaq 0x1714(%rip), %rsi      # 0x406d
callq 0x2240
movq 0x20(%rsp), %rsi
addq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
leaq 0x16fe(%rip), %rsi      # 0x4078
callq 0x2240
movq %rax, %rdi
callq 0x2130
movq 0x18(%rsp), %rdx
addq 0x20(%rsp), %rdx
movq %rbx, %rdi
leaq 0x16e4(%rip), %rsi      # 0x407f
callq 0x2d7c
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x16db(%rip), %rdx      # 0x4088
callq 0x2d5d
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
leaq 0x48(%rsp), %rdx
callq 0x2d3e
movq %r14, %r15
movq %r14, %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x2120
movq 0x20(%rsp), %rbp
movq 0x28(%rsp), %r12
cmpq %r12, %rbp
je 0x2a3a
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2c78
movq %rbx, %r14
movl 0x150(%rsp), %r13d
leaq 0x1d0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
callq 0x22c0
leaq 0xc(%rsp), %rdi
movl %r13d, %esi
movq %rbx, %rdx
callq 0x3966
movq %rbx, %rdi
callq 0x2120
movq %r15, %rdi
callq 0x2120
addq $0x28, %rbp
movq %r14, %rbx
jmp 0x29e1
leaq 0x68(%rsp), %rdi
callq 0x2120
movq %r15, %r14
leaq 0xc(%rsp), %r15
jmp 0x2aec
leaq 0x68(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3222
movq 0x20(%rsp), %rbp
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq 0x18(%rsp), %rbp
je 0x2acb
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2c78
movq %rbx, %r12
movl 0x150(%rsp), %r13d
leaq 0x1b0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
callq 0x22c0
movq %r15, %rdi
movl %r13d, %esi
movq %rbx, %rdx
callq 0x3966
movq %rbx, %rdi
callq 0x2120
movq %r14, %rdi
callq 0x2120
addq $0x28, %rbp
movq %r12, %rbx
jmp 0x2a72
leaq 0x68(%rsp), %rdi
callq 0x2120
pushq $0x1
popq %rdi
callq 0x21a0
movl 0x14(%rsp), %eax
incl %eax
cltd
idivl 0x10(%rsp)
movl %edx, 0x14(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x2120
cmpl $0x0, 0x44(%rsp)
je 0x2878
leaq 0x48(%rsp), %rdi
callq 0x2120
leaq 0x20(%rsp), %rdi
callq 0x2cf0
xorl %eax, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2cf0
jmp 0x2c5b
jmp 0x2c46
jmp 0x2c46
jmp 0x2bf5
jmp 0x2bf5
jmp 0x2bf5
jmp 0x2bf5
jmp 0x2bf0
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
jmp 0x2c56
jmp 0x2bf5
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x2cf0
jmp 0x2c4e
jmp 0x2bf5
jmp 0x2c46
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x2120
jmp 0x2ba9
jmp 0x2bf0
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
jmp 0x2c37
jmp 0x2bf5
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x2cf0
jmp 0x2c65
jmp 0x2c46
movq %rax, %rbx
jmp 0x2be4
movq %rax, %rbx
leaq 0x210(%rsp), %rdi
callq 0x2120
leaq 0xf8(%rsp), %rdi
jmp 0x2c37
jmp 0x2c4b
movq %rax, %rbx
jmp 0x2c3c
movq %rax, %rbx
jmp 0x2c65
jmp 0x2c0b
jmp 0x2c10
movq %rax, %rbx
leaq 0x1b0(%rsp), %rdi
jmp 0x2c20
movq %rax, %rbx
jmp 0x2c32
movq %rax, %rbx
jmp 0x2c25
movq %rax, %rbx
leaq 0x1d0(%rsp), %rdi
callq 0x2120
leaq 0x130(%rsp), %rdi
callq 0x2120
leaq 0x68(%rsp), %rdi
callq 0x2120
leaq 0xc0(%rsp), %rdi
jmp 0x2c56
movq %rax, %rbx
jmp 0x2c5b
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x2120
leaq 0x48(%rsp), %rdi
callq 0x2120
leaq 0x20(%rsp), %rdi
callq 0x2cf0
movq %rbx, %rdi
callq 0x2340
nop