// File: /workspace/github_cpp_makefile_star2_O2/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 0x40(%rdi), %rax
movq %rax, -0x60(%rbp)
leaq 0x30(%rdi), %rax
movq %rax, -0x50(%rbp)
leaq -0xa0(%rbp), %r12
movq %rdi, -0x58(%rbp)
cmpb $0x0, 0x8(%rbx)
jne 0x5359
callq 0x3040
movq %rax, %r15
movq 0x90(%rbx), %r14
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5468
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
callq *0x18(%rax)
movl %eax, %r14d
movq %r12, %rdi
callq 0x3140
testl %r14d, %r14d
js 0x531d
movq 0x90(%rbx), %rsi
movq (%rsi), %rax
leaq -0x48(%rbp), %r13
movq %r13, %rdi
callq *0x10(%rax)
movq %r13, %rdi
leaq 0x557d(%rip), %rsi      # 0xa617
callq 0x30c0
movl %eax, %r14d
movq %r13, %rsi
pushq $0x3
popq %rax
movl %eax, %r13d
testl %r14d, %r14d
leaq -0xc0(%rbp), %r12
je 0x50d9
movq %r12, %rdi
callq 0x3370
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5614
movq %r12, %rdi
callq 0x3140
xorl %r13d, %r13d
leaq -0x48(%rbp), %rsi
movq %rsi, %rdi
callq 0x3140
testl %r14d, %r14d
je 0x530b
movq 0x20(%rbx), %rax
movl 0x68(%rax), %r14d
callq 0x3a7c
testl %r14d, %r14d
je 0x510e
testb %al, %al
je 0x51a6
callq 0x3b49
jmp 0x51a6
testb %al, %al
je 0x51a6
callq 0x3b49
cmpl $0xe0, %eax
jne 0x5141
callq 0x3b49
cmpl $0x50, %eax
je 0x517d
cmpl $0x4d, %eax
je 0x5172
cmpl $0x4b, %eax
jne 0x51a6
movq 0x20(%rbx), %rdi
callq 0x5d4e
jmp 0x51a6
movl %eax, %r14d
andl $-0x3, %eax
cmpl $0x68, %eax
jne 0x5155
movq 0x20(%rbx), %rdi
callq 0x5d4e
cmpl $0x6b, %r14d
je 0x517d
cmpl $0x6c, %r14d
je 0x5172
cmpl $0x7a, %r14d
je 0x519d
cmpl $0x2d, %r14d
jne 0x51a6
jmp 0x5359
movq 0x20(%rbx), %rdi
callq 0x5de0
jmp 0x51a6
movq 0x20(%rbx), %rax
cmpl $0x0, 0x98(%rax)
jne 0x51a6
movl $0x1, 0x98(%rax)
addl $0x28, 0x80(%rax)
jmp 0x51a6
movq 0x20(%rbx), %rdi
callq 0x5e6e
movq 0x20(%rbx), %rdi
movl 0xa4(%rbx), %eax
addl %eax, 0x74(%rdi)
callq 0x5f58
movq 0x20(%rbx), %rdi
movq -0x60(%rbp), %rsi
callq 0x5f96
movq 0x20(%rbx), %rdi
movl 0x68(%rdi), %eax
cmpl $0x4, %eax
jne 0x5208
movq -0x50(%rbp), %r14
movq %r14, %rsi
callq 0x5cd4
movq 0x10(%rbx), %rsi
leaq -0x48(%rbp), %r13
movq %r13, %rdi
callq 0x5d00
movq %r14, %rdi
movq %r13, %rsi
callq 0x81ec
leaq -0x40(%rbp), %rdi
callq 0x6896
movq 0x20(%rbx), %rdi
movl 0x68(%rdi), %eax
testl %eax, %eax
je 0x521c
andl $0x0, 0xa0(%rbx)
movb $0x1, %cl
cmpl $0x6, %eax
jne 0x5229
jmp 0x5234
movl 0x78(%rdi), %eax
andl $0x0, 0x78(%rdi)
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 0x3040
movq %rax, %rcx
subq %r15, %rcx
movq %rcx, %rax
cqto
movl $0xf4240, %esi          # imm = 0xF4240
idivq %rsi
movq %rax, %r14
pushq $0x10
popq %r15
subl %r14d, %r15d
xorl %r13d, %r13d
cmpq $0x1036640, %rcx        # imm = 0x1036640
cmovgel %r13d, %r15d
movabsq $0x1300000000, %rbx     # imm = 0x1300000000
movq %rbx, -0x48(%rbp)
movq 0xad43(%rip), %rdi      # 0xffc8
leaq -0x48(%rbp), %r12
movq %r12, %rsi
callq 0x3c46
movq %rax, %rdi
leaq 0xb015(%rip), %rsi      # 0x102b0
callq 0x3c12
movq %rax, %rdi
leaq 0x5371(%rip), %rsi      # 0xa61b
callq 0x3280
movq %rbx, -0x48(%rbp)
movq 0xad0e(%rip), %rdi      # 0xffc8
movq %r12, %rsi
callq 0x3c46
movq %rax, %rdi
leaq 0xafe4(%rip), %rsi      # 0x102b0
callq 0x3c12
movq %rax, %rdi
leaq 0x5349(%rip), %rsi      # 0xa624
callq 0x3280
movq %rax, %rdi
cmpq $0x11, %r14
jge 0x52ed
pushq $0x10
popq %r14
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divq %r14
movq %rax, %rsi
callq 0x3260
movl %r15d, %edi
callq 0x3bba
movq -0x58(%rbp), %rbx
testl %r13d, %r13d
leaq -0xa0(%rbp), %r12
je 0x5038
jmp 0x5359
movq 0xaca4(%rip), %rdi      # 0xffc8
leaq 0xaf85(%rip), %rsi      # 0x102b0
callq 0x3c12
leaq -0x48(%rbp), %rsi
andq $0x0, (%rsi)
movq %rax, %rdi
callq 0x3c46
leaq 0x52c2(%rip), %rsi      # 0xa609
movq %rax, %rdi
callq 0x3280
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x3bba
movq 0x90(%rbx), %r14
leaq 0x52b0(%rip), %rsi      # 0xa617
leaq -0x80(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x3100
movq (%r14), %rax
leaq -0x80(%rbp), %rsi
movq %r14, %rdi
callq *0x18(%rax)
leaq -0x80(%rbp), %rdi
callq 0x3140
pushq $0x32
popq %rdi
callq 0x3bba
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
leaq -0x80(%rbp), %rdi
jmp 0x545a
movq %rax, %r14
jmp 0x545f
movq %rdx, %r15
movq %rax, %r14
leaq -0xc0(%rbp), %rdi
callq 0x3140
jmp 0x53df
movq %rdx, %r15
movq %rax, %r14
leaq -0x48(%rbp), %rdi
callq 0x3140
jmp 0x53f0
movq %rdx, %r15
movq %rax, %r14
cmpl $0x1, %r15d
jne 0x545f
movq %r14, %rdi
callq 0x3110
movq 0xabc3(%rip), %rdi      # 0xffc8
leaq 0xaea4(%rip), %rsi      # 0x102b0
callq 0x3c12
leaq -0x48(%rbp), %rsi
andq $0x0, (%rsi)
movq %rax, %rdi
callq 0x3c46
leaq 0x51e1(%rip), %rsi      # 0xa609
movq %rax, %rdi
callq 0x3280
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x3bba
callq 0x3430
jmp 0x5359
jmp 0x5446
movq %rax, %r14
callq 0x3430
jmp 0x545f
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
callq 0x3140
movq %r14, %rdi
callq 0x3470
nop