// File: /workspace/github_cpp_makefile_star2_O1/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 0x38(%rdi), %rax
movq %rax, -0x58(%rbp)
cmpb $0x0, 0x8(%rbx)
jne 0x6342
callq 0x3050
movq %rax, %r12
movq 0x90(%rbx), %r15
leaq -0x98(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x63f8
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x18(%rax)
movl %eax, %r15d
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x5efb
callq 0x3260
testl %r15d, %r15d
leaq -0x50(%rbp), %r14
js 0x5f6f
movq 0x90(%rbx), %rsi
movq (%rsi), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq %r14, %rdi
leaq 0x6759(%rip), %rsi      # 0xc677
callq 0x30c0
movl %eax, %r13d
testl %eax, %eax
je 0x5fbe
leaq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0x78(%rbp), %r14
movq %r14, %rdi
callq 0x9652
movq %rbx, %rdi
movq %r14, %rsi
callq 0x664a
movq -0x78(%rbp), %rdi
xorl %r14d, %r14d
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x5fc4
callq 0x3260
jmp 0x5fc4
movq 0xb05a(%rip), %rdi      # 0x10fd0
leaq 0xb33b(%rip), %rsi      # 0x112b8
callq 0x3e37
movq $0x0, -0x50(%rbp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x3e7e
movl $0xd, %edx
movq %rax, %rdi
leaq 0x66c5(%rip), %rsi      # 0xc669
callq 0x3310
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x3dd0
movl $0x3, %r14d
jmp 0x6299
movl $0x3, %r14d
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x5fd6
callq 0x3260
testl %r13d, %r13d
je 0x6299
movq 0x20(%rbx), %rax
movl 0x68(%rax), %r14d
callq 0x3c94
testl %r14d, %r14d
je 0x6003
testb %al, %al
je 0x60c8
callq 0x3d5f
jmp 0x60c8
testb %al, %al
je 0x60c8
callq 0x3d5f
movl %eax, %r13d
movl %eax, %r15d
cmpl $0xe0, %eax
jne 0x6025
callq 0x3d5f
movl %eax, %r15d
cmpl $0xe0, %r13d
jne 0x604b
cmpl $0x50, %r15d
je 0x6071
cmpl $0x4d, %r15d
je 0x609d
cmpl $0x4b, %r15d
jne 0x60bb
movq 0x20(%rbx), %rdi
callq 0x6f78
jmp 0x60bb
movl %r15d, %eax
andl $-0x3, %eax
cmpl $0x68, %eax
jne 0x605f
movq 0x20(%rbx), %rdi
callq 0x6f78
cmpl $0x6b, %r15d
jg 0x6091
cmpl $0x2d, %r15d
je 0x60a8
cmpl $0x6b, %r15d
jne 0x60bb
movq 0x20(%rbx), %rax
cmpl $0x0, 0x98(%rax)
jne 0x60bb
movl $0x1, 0x98(%rax)
addl $0x28, 0x80(%rax)
jmp 0x60bb
cmpl $0x7a, %r15d
je 0x60b2
cmpl $0x6c, %r15d
jne 0x60bb
movq 0x20(%rbx), %rdi
callq 0x700a
jmp 0x60bb
movl $0x3, %r14d
xorl %eax, %eax
jmp 0x60c0
movq 0x20(%rbx), %rdi
callq 0x7098
xorl %r14d, %r14d
movb $0x1, %al
testb %al, %al
je 0x6299
movq 0x20(%rbx), %rdi
movl 0xa4(%rbx), %eax
addl %eax, 0x74(%rdi)
callq 0x7228
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 0x60fc
movl %eax, 0x48(%rbx)
movl 0x74(%r14), %eax
movl %eax, 0x4c(%rbx)
cmpl $0x4, 0x68(%r14)
jne 0x616d
movq 0x30(%rbx), %rax
movq %rax, 0x50(%r14)
leaq 0x58(%r14), %rdi
movq -0x58(%rbp), %rsi
callq 0x8026
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x60(%r14)
movl $0x0, 0x68(%r14)
movq 0x10(%rbx), %rsi
leaq -0x50(%rbp), %rdi
callq 0x6ef2
movaps -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x50(%rbp)
movq 0x38(%rbx), %rdi
movups %xmm0, 0x30(%rbx)
testq %rdi, %rdi
je 0x615f
callq 0x7fac
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x616d
callq 0x7fac
movq 0x20(%rbx), %rdx
movl 0x68(%rdx), %eax
xorl %ecx, %ecx
testl %eax, %eax
jne 0x6184
movl 0x78(%rdx), %ecx
movl $0x0, 0x78(%rdx)
movl %ecx, 0xa0(%rbx)
movb $0x1, %cl
cmpl $0x6, %eax
je 0x619c
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 %r12, %rcx
movq %rcx, %rax
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
imulq %rdx
movq %rdx, %r12
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %r12
addq %rax, %r12
movl $0x10, %r13d
subl %r12d, %r13d
xorl %r14d, %r14d
cmpq $0x1036640, %rcx        # imm = 0x1036640
cmovgel %r14d, %r13d
movabsq $0x1300000000, %rax     # imm = 0x1300000000
movq %rax, -0x50(%rbp)
movq 0xadd0(%rip), %r15      # 0x10fd0
movq %r15, %rdi
leaq -0x50(%rbp), %rsi
callq 0x3e7e
movq %rax, %rdi
leaq 0xb0a2(%rip), %rsi      # 0x112b8
callq 0x3e37
movl $0x8, %edx
movq %rax, %rdi
leaq 0x6451(%rip), %rsi      # 0xc67b
callq 0x3310
movabsq $0x1300000000, %rax     # imm = 0x1300000000
movq %rax, -0x50(%rbp)
movq %r15, %rdi
leaq -0x50(%rbp), %rsi
callq 0x3e7e
movq %rax, %rdi
leaq 0xb065(%rip), %rsi      # 0x112b8
callq 0x3e37
movq %rax, %r15
movl $0x5, %edx
movq %rax, %rdi
leaq 0x641a(%rip), %rsi      # 0xc684
callq 0x3310
cmpq $0x11, %r12
movl $0x10, %eax
cmovlq %rax, %r12
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divq %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0x3270
movl %r13d, %edi
callq 0x3dd0
testl %r14d, %r14d
je 0x5ea9
jmp 0x6342
movq %rdx, %r13
movq %rax, %r12
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x62c7
callq 0x3260
jmp 0x62c7
movq %rdx, %r13
movq %rax, %r12
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x62e1
callq 0x3260
jmp 0x62e1
movq %rdx, %r13
movq %rax, %r12
cmpl $0x1, %r13d
jne 0x63ef
movq %r12, %rdi
callq 0x3120
movq 0xacd6(%rip), %rdi      # 0x10fd0
leaq 0xafb7(%rip), %rsi      # 0x112b8
callq 0x3e37
movq $0x0, -0x50(%rbp)
movq %rax, %rdi
leaq -0x50(%rbp), %rsi
callq 0x3e7e
movl $0xd, %edx
movq %rax, %rdi
leaq 0x6340(%rip), %rsi      # 0xc669
callq 0x3310
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x3dd0
callq 0x3440
jmp 0x5fb3
movq 0x90(%rbx), %r14
leaq -0xa8(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0x631c(%rip), %rsi      # 0xc677
leaq 0x6318(%rip), %rdx      # 0xc67a
leaq -0xb8(%rbp), %r15
movq %r15, %rdi
callq 0x3bfa
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x18(%rax)
movq -0xb8(%rbp), %rdi
cmpq %r13, %rdi
je 0x638e
callq 0x3260
movl $0x32, %edi
callq 0x3dd0
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
jmp 0x63b9
movq %rax, %r12
callq 0x3440
jmp 0x63ef
movq %rax, %r12
movq -0xb8(%rbp), %rdi
cmpq %r13, %rdi
jne 0x63ea
jmp 0x63ef
movq %rax, %r12
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x63ef
callq 0x3260
movq %r12, %rdi
callq 0x3480
nop