// File: 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
movq %rsp, %rbp
subq $0x4e0, %rsp            # imm = 0x4E0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x3410
movl %eax, -0x14(%rbp)
movb $0x0, -0x15(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4460
movq 0x783b(%rip), %rdi      # 0xafd8
leaq 0x4860(%rip), %rsi      # 0x8004
callq 0x32d0
movq %rax, -0x478(%rbp)
jmp 0x37b2
movq -0x478(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x32d0
movq %rax, -0x480(%rbp)
jmp 0x37cf
movq -0x480(%rbp), %rdi
movq 0x77eb(%rip), %rsi      # 0xafc8
callq 0x3310
jmp 0x37e4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0x14(%rbp), %edx
leaq -0x40(%rbp), %rdi
callq 0x6b20
jmp 0x37fa
jmp 0x37fc
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpq $0x10, %rax
jae 0x3857
movq -0xd0(%rbp), %rax
movl -0xc4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x3814
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4309
jmp 0x3859
jmp 0x385b
movl -0x40(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x40(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xc0(%rbp,%rax,8), %rcx
movq %rcx, -0xc0(%rbp,%rax,8)
movl -0x40(%rbp), %eax
movl %eax, -0xd4(%rbp)
movb -0x15(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x38a9
jmp 0x3bf0
leaq -0x170(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4480
jmp 0x38bb
movl -0x40(%rbp), %edx
leaq -0x158(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x6ef0
jmp 0x38d3
movups -0xe8(%rbp), %xmm0
movaps %xmm0, -0x50(%rbp)
movups -0xf8(%rbp), %xmm0
movaps %xmm0, -0x60(%rbp)
movups -0x108(%rbp), %xmm0
movaps %xmm0, -0x70(%rbp)
movups -0x118(%rbp), %xmm0
movaps %xmm0, -0x80(%rbp)
movups -0x158(%rbp), %xmm0
movups -0x148(%rbp), %xmm1
movups -0x138(%rbp), %xmm2
movups -0x128(%rbp), %xmm3
movaps %xmm3, -0x90(%rbp)
movaps %xmm2, -0xa0(%rbp)
movaps %xmm1, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x4570
movl -0xd4(%rbp), %edi
incl %edi
xorl %eax, %eax
movl %eax, %r8d
leaq -0xc0(%rbp), %rsi
movq %r8, %rdx
movq %r8, %rcx
callq 0x32b0
jmp 0x3964
movl -0x40(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xc0(%rbp,%rax,8), %rax
movq %rax, -0x488(%rbp)
movl -0x40(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x488(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x3b83
movl -0x40(%rbp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x3100
movl %eax, -0x48c(%rbp)
jmp 0x39c3
movl -0x48c(%rbp), %eax
movl %eax, -0x174(%rbp)
movl -0x174(%rbp), %edx
leaq -0x198(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x6b60
jmp 0x39e7
leaq -0x1e0(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x3150
jmp 0x39fc
movl -0x174(%rbp), %edx
leaq -0x1c0(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x6e00
jmp 0x3a17
leaq -0x1e0(%rbp), %rdi
callq 0x31d0
leaq -0x30(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x45d0
jmp 0x3a35
movq 0x759c(%rip), %rdi      # 0xafd8
leaq 0x45d8(%rip), %rsi      # 0x801b
callq 0x32d0
movq %rax, -0x498(%rbp)
jmp 0x3a51
movq -0x498(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x3280
movq %rax, -0x4a0(%rbp)
jmp 0x3a6d
movq -0x4a0(%rbp), %rdi
movq 0x754d(%rip), %rsi      # 0xafc8
callq 0x3310
jmp 0x3a82
leaq -0x1f8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4480
jmp 0x3a94
movl -0xd4(%rbp), %esi
leaq -0x1f8(%rbp), %rdi
callq 0x6e70
movl %eax, -0x4a4(%rbp)
jmp 0x3aae
movl -0x4a4(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x4570
movl -0x174(%rbp), %esi
leaq 0x4559(%rip), %rdx      # 0x802c
leaq -0x40(%rbp), %rdi
callq 0x6d00
jmp 0x3ade
leaq -0x1c0(%rbp), %rdi
callq 0x4420
leaq -0x198(%rbp), %rdi
callq 0x31d0
jmp 0x3b83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x4570
jmp 0x4309
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x3b72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x31d0
jmp 0x3b72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x3b66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x4570
leaq -0x1c0(%rbp), %rdi
callq 0x4420
leaq -0x198(%rbp), %rdi
callq 0x31d0
jmp 0x4309
movq 0x744e(%rip), %rdi      # 0xafd8
leaq 0x44c2(%rip), %rsi      # 0x8053
callq 0x32d0
movq %rax, -0x4b0(%rbp)
jmp 0x3b9f
leaq -0x30(%rbp), %rdi
callq 0x4640
movq -0x4b0(%rbp), %rdi
movq %rax, %rsi
callq 0x30c0
movq %rax, -0x4b8(%rbp)
jmp 0x3bc0
movq -0x4b8(%rbp), %rdi
movq 0x73fa(%rip), %rsi      # 0xafc8
callq 0x3310
jmp 0x3bd5
leaq -0x30(%rbp), %rdi
callq 0x4640
movslq -0x14(%rbp), %rcx
cmpq %rcx, %rax
jb 0x3beb
movb $0x1, -0x15(%rbp)
jmp 0x389b
movl $0x1, %edi
callq 0x3210
jmp 0x3bfc
leaq -0x218(%rbp), %rdi
callq 0x5ce0
jmp 0x3c0a
leaq -0x218(%rbp), %rdi
callq 0x31f0
movl %eax, %esi
leaq -0x238(%rbp), %rdi
callq 0x4670
jmp 0x3c26
leaq -0x250(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4480
jmp 0x3c38
movl -0x40(%rbp), %eax
movl %eax, -0x254(%rbp)
movups -0x238(%rbp), %xmm0
movups -0x228(%rbp), %xmm1
movaps %xmm1, -0x270(%rbp)
movaps %xmm0, -0x280(%rbp)
movl -0x254(%rbp), %esi
leaq -0x250(%rbp), %rdi
leaq -0x280(%rbp), %rdx
callq 0x3660
jmp 0x3c78
leaq -0x280(%rbp), %rdi
callq 0x46a0
leaq -0x250(%rbp), %rdi
callq 0x4570
movl $0x0, -0x284(%rbp)
jmp 0x3c9c
movslq -0x284(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x46b0
movl 0x20(%rax), %eax
movl %eax, -0x288(%rbp)
leaq -0x30(%rbp), %rax
movq %rax, -0x290(%rbp)
movq -0x290(%rbp), %rdi
callq 0x4320
movq %rax, -0x298(%rbp)
movq -0x290(%rbp), %rdi
callq 0x4350
movq %rax, -0x2a0(%rbp)
leaq -0x298(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
callq 0x4380
testb $0x1, %al
jne 0x3d02
jmp 0x3ded
leaq -0x298(%rbp), %rdi
callq 0x43c0
movq %rax, %rsi
leaq -0x2c8(%rbp), %rdi
callq 0x43e0
jmp 0x3d1f
movl -0x2a8(%rbp), %eax
cmpl -0x288(%rbp), %eax
jne 0x3db6
movl -0x2a8(%rbp), %esi
leaq 0x4325(%rip), %rdx      # 0x8063
leaq -0x40(%rbp), %rdi
callq 0x6d00
jmp 0x3d49
jmp 0x3dd0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x42fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x42f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x46a0
leaq -0x250(%rbp), %rdi
callq 0x4570
jmp 0x42f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x4420
jmp 0x42f1
movl -0x2a8(%rbp), %esi
leaq 0x42a5(%rip), %rdx      # 0x8068
leaq -0x40(%rbp), %rdi
callq 0x6d00
jmp 0x3dce
jmp 0x3dd0
leaq -0x2c8(%rbp), %rdi
callq 0x4420
leaq -0x298(%rbp), %rdi
callq 0x4440
jmp 0x3ce6
movl -0x288(%rbp), %edx
leaq -0x2e8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x6b60
jmp 0x3e05
leaq -0x308(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x3150
jmp 0x3e1a
leaq -0x328(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
callq 0x3150
jmp 0x3e2f
leaq -0x238(%rbp), %rdi
leaq -0x308(%rbp), %rsi
leaq -0x328(%rbp), %rdx
callq 0x59c0
movl %eax, -0x4bc(%rbp)
jmp 0x3e51
leaq -0x328(%rbp), %rdi
callq 0x31d0
leaq -0x308(%rbp), %rdi
callq 0x31d0
movl -0x4bc(%rbp), %eax
cmpl $0x0, %eax
je 0x411e
movq 0x7159(%rip), %rdi      # 0xafd8
leaq 0x41e7(%rip), %rsi      # 0x806d
callq 0x32d0
movq %rax, -0x4c8(%rbp)
jmp 0x3e94
movslq -0x284(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x46b0
movq -0x4c8(%rbp), %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, -0x4d0(%rbp)
jmp 0x3ebc
movq -0x4d0(%rbp), %rdi
leaq 0x41ae(%rip), %rsi      # 0x8078
callq 0x32d0
movq %rax, -0x4d8(%rbp)
jmp 0x3ed8
movq -0x4d8(%rbp), %rdi
movq 0x70e2(%rip), %rsi      # 0xafc8
callq 0x3310
jmp 0x3eed
movslq -0x284(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x46b0
movq %rax, %rdx
leaq 0x4178(%rip), %rsi      # 0x807f
leaq -0x388(%rbp), %rdi
callq 0x4770
jmp 0x3f15
leaq 0x416c(%rip), %rdx      # 0x8088
leaq -0x368(%rbp), %rdi
leaq -0x388(%rbp), %rsi
callq 0x4720
jmp 0x3f31
leaq -0x348(%rbp), %rdi
leaq -0x368(%rbp), %rsi
leaq -0x218(%rbp), %rdx
callq 0x46d0
jmp 0x3f4d
leaq -0x368(%rbp), %rdi
callq 0x31d0
leaq -0x388(%rbp), %rdi
callq 0x31d0
leaq -0x30(%rbp), %rax
movq %rax, -0x390(%rbp)
movq -0x390(%rbp), %rdi
callq 0x4320
movq %rax, -0x398(%rbp)
movq -0x390(%rbp), %rdi
callq 0x4350
movq %rax, -0x3a0(%rbp)
leaq -0x398(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x4380
testb $0x1, %al
jne 0x3fb2
jmp 0x40f2
leaq -0x398(%rbp), %rdi
callq 0x43c0
movq %rax, %rsi
leaq -0x3c8(%rbp), %rdi
callq 0x43e0
jmp 0x3fcf
movl -0x3a8(%rbp), %eax
movl %eax, -0x4dc(%rbp)
leaq -0x3e8(%rbp), %rdi
leaq -0x348(%rbp), %rsi
callq 0x3150
jmp 0x3ff0
movl -0x4dc(%rbp), %esi
leaq -0x40(%rbp), %rdi
leaq -0x3e8(%rbp), %rdx
callq 0x6c40
jmp 0x4008
leaq -0x3e8(%rbp), %rdi
callq 0x31d0
leaq -0x3c8(%rbp), %rdi
callq 0x4420
leaq -0x398(%rbp), %rdi
callq 0x4440
jmp 0x3f96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x42a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4068
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x328(%rbp), %rdi
callq 0x31d0
leaq -0x308(%rbp), %rdi
callq 0x31d0
jmp 0x42a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x409f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x368(%rbp), %rdi
callq 0x31d0
leaq -0x388(%rbp), %rdi
callq 0x31d0
jmp 0x42a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x410d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x40e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x3e8(%rbp), %rdi
callq 0x31d0
leaq -0x3c8(%rbp), %rdi
callq 0x4420
jmp 0x410d
movl $0xa, -0x3ec(%rbp)
leaq -0x348(%rbp), %rdi
callq 0x31d0
jmp 0x4286
leaq -0x348(%rbp), %rdi
callq 0x31d0
jmp 0x42a5
leaq -0x410(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0x5b30
jmp 0x4133
leaq -0x30(%rbp), %rax
movq %rax, -0x418(%rbp)
movq -0x418(%rbp), %rdi
callq 0x4320
movq %rax, -0x420(%rbp)
movq -0x418(%rbp), %rdi
callq 0x4350
movq %rax, -0x428(%rbp)
leaq -0x420(%rbp), %rdi
leaq -0x428(%rbp), %rsi
callq 0x4380
testb $0x1, %al
jne 0x4180
jmp 0x4241
leaq -0x420(%rbp), %rdi
callq 0x43c0
movq %rax, %rsi
leaq -0x450(%rbp), %rdi
callq 0x43e0
jmp 0x419d
movl -0x430(%rbp), %eax
movl %eax, -0x4e0(%rbp)
leaq -0x470(%rbp), %rdi
leaq -0x410(%rbp), %rsi
callq 0x3150
jmp 0x41be
movl -0x4e0(%rbp), %esi
leaq -0x40(%rbp), %rdi
leaq -0x470(%rbp), %rdx
callq 0x6c40
jmp 0x41d6
leaq -0x470(%rbp), %rdi
callq 0x31d0
leaq -0x450(%rbp), %rdi
callq 0x4420
leaq -0x420(%rbp), %rdi
callq 0x4440
jmp 0x4164
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4262
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4233
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x470(%rbp), %rdi
callq 0x31d0
leaq -0x450(%rbp), %rdi
callq 0x4420
jmp 0x4262
movl -0x284(%rbp), %eax
addl $0x1, %eax
cltd
idivl -0x14(%rbp)
movl %edx, -0x284(%rbp)
leaq -0x410(%rbp), %rdi
callq 0x31d0
jmp 0x4270
leaq -0x410(%rbp), %rdi
callq 0x31d0
jmp 0x42a5
movl $0x1, %edi
callq 0x3210
jmp 0x427c
movl $0x0, -0x3ec(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x31d0
movl -0x3ec(%rbp), %eax
testl %eax, %eax
je 0x42a0
jmp 0x429e
jmp 0x42b3
jmp 0x3c9a
leaq -0x2e8(%rbp), %rdi
callq 0x31d0
jmp 0x42f1
movl $0x0, -0x4(%rbp)
movl $0x1, -0x3ec(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x46a0
leaq -0x218(%rbp), %rdi
callq 0x31d0
leaq -0x30(%rbp), %rdi
callq 0x4570
movl -0x4(%rbp), %eax
addq $0x4e0, %rsp            # imm = 0x4E0
popq %rbp
retq
leaq -0x238(%rbp), %rdi
callq 0x46a0
leaq -0x218(%rbp), %rdi
callq 0x31d0
leaq -0x30(%rbp), %rdi
callq 0x4570
movq -0x38(%rbp), %rdi
callq 0x3460
nopl (%rax,%rax)