// File: /workspace/github_cpp_makefile_star2_O3/Leeingnyo[P]newPuyoC/game.cpp
// Function: VSRemoteGame::GameLoop()
void VSRemoteGame::GameLoop() {
    while (!gameover) {
        auto start = std::chrono::system_clock::now();

        if (socket->Send(this->Serialize()) < 0) {
            std::cout << Console::red << Console::GotoXY(0, 0) << "통신 에러";
            Console::Sleep(1000);
            break;
        }
        try {
            auto response = socket->Recv();
            if (response.compare("end")) {
                this->Deserialize(response);
            }
            else {
                break;
            }
        }
        catch (int error) {
            std::cout << Console::red << Console::GotoXY(0, 0) << "통신 에러";
            Console::Sleep(1000);
            break;
        }

        if (!my_board->IsBusy()) {
            if (kbhit()) {
                int input = getch();
                bool special = false;
                if (input == 224) {
                    input = getch();
                    special = true;
                }

                if (special) {
                    if (input == 75) {
                        my_board->MoveLeft();
                    }
                    if (input == 77) {
                        my_board->MoveRight();
                    }
                    if (input == 80) {
                        my_board->MoveDown();
                    }
                } else {
                    if (input == 'j' || input == 'h') {
                        my_board->MoveLeft();
                    }
                    if (input == 'l') {
                        my_board->MoveRight();
                    }
                    if (input == 'k') {
                        my_board->MoveDown();
                    }
                    if (input == '-') {
                        break;
                    }
                    if (input == 'z') { // rotate
                        my_board->RotateCCW();
                    }
                }
            }
        }
        else {
            if (kbhit()) getch();
        }
        // input
        
        my_board->TakeObstacles(obstacle_to_take);
        my_board->Update();
        my_board->UpdatePlayerInformation(my_info);
        if (my_board->IsNeedNext()) {
            my_board->SetNextBiPuyo(my_next_bipuyo);
            my_next_bipuyo = my_bipuyo_generator->GenerateBipuyo();
        }
        obstacle_to_send = my_board->SendObstacles();
        gameover = my_board->IsGameOver() || other_board->IsGameOver();
        // process

        Draw();

        auto end = std::chrono::system_clock::now();
        auto delta = (std::chrono::duration_cast<std::chrono::milliseconds>(end - start)).count();
        int tick = delta > 16 ? 0 : 16 - delta;
        std::cout << Console::GotoXY(0, 19) << Console::red << "        ";
        std::cout << Console::GotoXY(0, 19) << Console::red << "FPS: " << 1000 / (delta > 16 ? delta : 16);
        Console::Sleep(tick);
    }

    socket->Send("end");
    Console::Sleep(50); // 왜 필요할까
    socket->Close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
leaq -0x68(%rbp), %r15
leaq 0x38(%rdi), %rax
movq %rax, -0x58(%rbp)
cmpb $0x0, 0x8(%rbx)
jne 0x6277
callq 0x3050
movq %rax, %r13
movq 0x90(%rbx), %r14
leaq -0x98(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x63ca
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
callq *0x18(%rax)
movl %eax, %r14d
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x5ef5
callq 0x3260
testl %r14d, %r14d
leaq -0x78(%rbp), %r12
js 0x6233
movq 0x90(%rbx), %rsi
movq (%rsi), %rax
leaq -0x50(%rbp), %r14
movq %r14, %rdi
callq *0x10(%rax)
movq %r14, %rdi
leaq 0x6737(%rip), %rsi      # 0xc657
callq 0x30c0
movl %eax, %r14d
testl %eax, %eax
je 0x5f61
movq %r15, -0x78(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x95ca
movq %rbx, %rdi
movq %r12, %rsi
callq 0x661c
movq -0x78(%rbp), %rdi
xorl %r12d, %r12d
cmpq %r15, %rdi
je 0x5f67
callq 0x3260
jmp 0x5f67
movl $0x3, %r12d
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x5f79
callq 0x3260
testl %r14d, %r14d
je 0x6228
movq 0x20(%rbx), %rax
movl 0x68(%rax), %r14d
callq 0x3c8c
testl %r14d, %r14d
je 0x5fa6
testb %al, %al
je 0x6041
callq 0x3d57
jmp 0x6041
testb %al, %al
je 0x6041
callq 0x3d57
cmpl $0xe0, %eax
jne 0x5fd9
callq 0x3d57
cmpl $0x50, %eax
je 0x600c
cmpl $0x4d, %eax
je 0x5fff
cmpl $0x4b, %eax
jne 0x6041
movq 0x20(%rbx), %rdi
callq 0x6f66
jmp 0x6041
movl %eax, %r14d
andl $-0x3, %eax
cmpl $0x68, %eax
jne 0x5fed
movq 0x20(%rbx), %rdi
callq 0x6f66
cmpl $0x6b, %r14d
jle 0x600a
cmpl $0x7a, %r14d
je 0x602c
cmpl $0x6c, %r14d
jne 0x6041
movq 0x20(%rbx), %rdi
callq 0x6ff8
jmp 0x6041
jne 0x6037
movq 0x20(%rbx), %rax
cmpl $0x0, 0x98(%rax)
jne 0x6041
movl $0x1, 0x98(%rax)
addl $0x28, 0x80(%rax)
jmp 0x6041
movq 0x20(%rbx), %rdi
callq 0x7086
jmp 0x6041
cmpl $0x2d, %r14d
je 0x6277
movq 0x20(%rbx), %rdi
movl 0xa4(%rbx), %eax
addl %eax, 0x74(%rdi)
callq 0x71c0
movq 0x20(%rbx), %r14
movl 0x6c(%r14), %eax
addl %eax, 0x40(%rbx)
movl $0x0, 0x6c(%r14)
movl 0x70(%r14), %eax
movl %eax, 0x44(%rbx)
cmpl %eax, 0x48(%rbx)
jge 0x6075
movl %eax, 0x48(%rbx)
movl 0x74(%r14), %eax
movl %eax, 0x4c(%rbx)
movl 0x68(%r14), %eax
cmpl $0x4, %eax
jne 0x60f0
movq 0x30(%rbx), %rax
movq %rax, 0x50(%r14)
leaq 0x58(%r14), %rdi
movq -0x58(%rbp), %rsi
callq 0x7fc8
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x60(%r14)
movl $0x0, 0x68(%r14)
movq 0x10(%rbx), %rsi
leaq -0x50(%rbp), %rdi
callq 0x6ee8
movaps -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x50(%rbp)
movq 0x38(%rbx), %rdi
movups %xmm0, 0x30(%rbx)
testq %rdi, %rdi
je 0x60e8
callq 0x7f4e
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x60e8
callq 0x7f4e
movq 0x20(%rbx), %r14
movl 0x68(%r14), %eax
testl %eax, %eax
je 0x6107
movl $0x0, 0xa0(%rbx)
movb $0x1, %cl
cmpl $0x6, %eax
jne 0x6119
jmp 0x6124
movl 0x78(%r14), %eax
movl $0x0, 0x78(%r14)
movl %eax, 0xa0(%rbx)
movq 0x60(%rbx), %rax
cmpl $0x6, 0x68(%rax)
sete %cl
movb %cl, 0x8(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
callq 0x3050
movq %rax, %rcx
subq %r13, %rcx
movq %rcx, %rax
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
imulq %rdx
movq %rdx, %r13
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %r13
addq %rax, %r13
movl $0x10, %r14d
subl %r13d, %r14d
xorl %r12d, %r12d
cmpq $0x1036640, %rcx        # imm = 0x1036640
cmovgel %r12d, %r14d
movabsq $0x1300000000, %rax     # imm = 0x1300000000
movq %rax, -0x50(%rbp)
movq 0xae48(%rip), %rdi      # 0x10fd0
leaq -0x50(%rbp), %r15
movq %r15, %rsi
callq 0x3e76
movq %rax, %rdi
leaq 0xb11a(%rip), %rsi      # 0x112b8
callq 0x3e2f
movl $0x8, %edx
movq %rax, %rdi
leaq 0x64a9(%rip), %rsi      # 0xc65b
callq 0x3310
movabsq $0x1300000000, %rax     # imm = 0x1300000000
movq %rax, -0x50(%rbp)
movq 0xae04(%rip), %rdi      # 0x10fd0
movq %r15, %rsi
callq 0x3e76
movq %rax, %rdi
leaq 0xb0da(%rip), %rsi      # 0x112b8
callq 0x3e2f
movq %rax, %r15
movl $0x5, %edx
movq %rax, %rdi
leaq 0x646f(%rip), %rsi      # 0xc664
callq 0x3310
cmpq $0x11, %r13
movl $0x10, %eax
cmovlq %rax, %r13
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divq %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0x3270
movl %r14d, %edi
callq 0x3dc8
leaq -0x68(%rbp), %r15
testl %r12d, %r12d
je 0x5ea3
jmp 0x6277
movq 0xad96(%rip), %rdi      # 0x10fd0
leaq 0xb077(%rip), %rsi      # 0x112b8
callq 0x3e2f
leaq -0x50(%rbp), %rsi
movq $0x0, (%rsi)
movq %rax, %rdi
callq 0x3e76
leaq 0x63e9(%rip), %rsi      # 0xc649
movl $0xd, %edx
movq %rax, %rdi
callq 0x3310
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x3dc8
movq 0x90(%rbx), %r14
leaq -0xa8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x63c6(%rip), %rsi      # 0xc657
leaq 0x63c2(%rip), %rdx      # 0xc65a
leaq -0xb8(%rbp), %r15
movq %r15, %rdi
callq 0x3bec
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x18(%rax)
movq -0xb8(%rbp), %rdi
cmpq %r12, %rdi
je 0x62c4
callq 0x3260
movl $0x32, %edi
callq 0x3dc8
movq 0x90(%rbx), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq -0xb8(%rbp), %rdi
cmpq %r12, %rdi
jne 0x63bc
jmp 0x63c1
movq %r15, %rcx
movq %rdx, %r15
movq %rax, %r14
movq -0x78(%rbp), %rdi
cmpq %rcx, %rdi
je 0x6324
callq 0x3260
jmp 0x6324
movq %rdx, %r15
movq %rax, %r14
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x633e
callq 0x3260
jmp 0x633e
movq %rdx, %r15
movq %rax, %r14
cmpl $0x1, %r15d
jne 0x63c1
movq %r14, %rdi
callq 0x3120
movq 0xac7d(%rip), %rdi      # 0x10fd0
leaq 0xaf5e(%rip), %rsi      # 0x112b8
callq 0x3e2f
leaq -0x50(%rbp), %rsi
movq $0x0, (%rsi)
movq %rax, %rdi
callq 0x3e76
leaq 0x62d0(%rip), %rsi      # 0xc649
movl $0xd, %edx
movq %rax, %rdi
callq 0x3310
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x3dc8
callq 0x3440
jmp 0x6277
jmp 0x639c
movq %rax, %r14
callq 0x3440
jmp 0x63c1
movq %rax, %r14
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x63c1
callq 0x3260
movq %r14, %rdi
callq 0x3480
nop