// File: 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
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
movb 0x8(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x742f
jmp 0x7ae9
callq 0x4040
movq -0x118(%rbp), %rdi
movq %rax, -0x10(%rbp)
addq $0x90, %rdi
callq 0x9800
movq -0x118(%rbp), %rsi
movq %rax, -0x130(%rbp)
leaq -0x30(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x7bd0
movq -0x130(%rbp), %rdi
movq -0x128(%rbp), %rsi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
movl %eax, -0x11c(%rbp)
jmp 0x7488
leaq -0x30(%rbp), %rdi
callq 0x41f0
movl -0x11c(%rbp), %eax
cmpl $0x0, %eax
jge 0x750b
movq 0x20b25(%rip), %rdi     # 0x27fc8
leaq 0x20e5e(%rip), %rsi     # 0x28308
callq 0x52b0
movq %rax, -0x138(%rbp)
leaq -0x44(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x8790
movq -0x138(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x5300
movq %rax, %rdi
leaq 0x130fc(%rip), %rsi     # 0x1a5d9
callq 0x42d0
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5230
jmp 0x7ae9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x41f0
jmp 0x7bc4
movq -0x118(%rbp), %rdi
addq $0x90, %rdi
callq 0x9800
movq %rax, %rsi
movq (%rsi), %rax
movq 0x10(%rax), %rax
leaq -0x68(%rbp), %rdi
callq *%rax
jmp 0x7530
leaq -0x68(%rbp), %rdi
leaq 0x130ac(%rip), %rsi     # 0x1a5e7
callq 0x40d0
cmpl $0x0, %eax
je 0x75b0
leaq -0x88(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x4140
jmp 0x7557
movq -0x118(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x7ee0
jmp 0x756c
leaq -0x88(%rbp), %rdi
callq 0x41f0
jmp 0x75bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x75ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x75e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x41f0
jmp 0x75e5
movl $0x3, -0x8c(%rbp)
jmp 0x75c6
movl $0x0, -0x8c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x41f0
movl -0x8c(%rbp), %eax
testl %eax, %eax
je 0x75e0
jmp 0x75db
jmp 0x7ae9
jmp 0x76a4
leaq -0x68(%rbp), %rdi
callq 0x41f0
movl -0x3c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x7bc4
movq -0x38(%rbp), %rdi
callq 0x4120
movl (%rax), %eax
movl %eax, -0x90(%rbp)
movq 0x209b2(%rip), %rdi     # 0x27fc8
leaq 0x20ceb(%rip), %rsi     # 0x28308
callq 0x52b0
movq %rax, -0x140(%rbp)
jmp 0x762b
leaq -0x98(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x8790
jmp 0x763d
movq -0x140(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x5300
movq %rax, -0x148(%rbp)
jmp 0x7659
movq -0x148(%rbp), %rdi
leaq 0x12f72(%rip), %rsi     # 0x1a5d9
callq 0x42d0
jmp 0x766e
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5230
jmp 0x767a
movl $0x3, -0x8c(%rbp)
callq 0x44b0
jmp 0x7ae9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
callq 0x44b0
jmp 0x7bc4
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x8cf0
testb $0x1, %al
jne 0x7814
callq 0x5100
testb $0x1, %al
jne 0x76d2
jmp 0x7812
callq 0x51d0
movl %eax, -0x9c(%rbp)
movb $0x0, -0x9d(%rbp)
cmpl $0xe0, -0x9c(%rbp)
jne 0x7702
callq 0x51d0
movl %eax, -0x9c(%rbp)
movb $0x1, -0x9d(%rbp)
testb $0x1, -0x9d(%rbp)
je 0x7773
cmpl $0x4b, -0x9c(%rbp)
jne 0x772c
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x8d10
cmpl $0x4d, -0x9c(%rbp)
jne 0x774d
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x8e00
cmpl $0x50, -0x9c(%rbp)
jne 0x776e
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x8ef0
jmp 0x7810
cmpl $0x6a, -0x9c(%rbp)
je 0x7785
cmpl $0x68, -0x9c(%rbp)
jne 0x779d
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x8d10
cmpl $0x6c, -0x9c(%rbp)
jne 0x77be
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x8e00
cmpl $0x6b, -0x9c(%rbp)
jne 0x77df
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x8ef0
cmpl $0x2d, -0x9c(%rbp)
jne 0x77ed
jmp 0x7ae9
cmpl $0x7a, -0x9c(%rbp)
jne 0x780e
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x8f70
jmp 0x7810
jmp 0x7812
jmp 0x7826
callq 0x5100
testb $0x1, %al
jne 0x781f
jmp 0x7824
callq 0x51d0
jmp 0x7826
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
movq -0x118(%rbp), %rax
movl 0xa4(%rax), %esi
callq 0x9310
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x9180
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq -0x118(%rbp), %rsi
movq %rax, %rdi
addq $0x40, %rsi
callq 0x9200
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x9260
testb $0x1, %al
jne 0x78a4
jmp 0x7909
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq -0x118(%rbp), %rsi
movq %rax, %rdi
addq $0x30, %rsi
callq 0x8bb0
movq -0x118(%rbp), %rdi
addq $0x10, %rdi
callq 0x8c00
movq %rax, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x8c20
movq -0x118(%rbp), %rdi
addq $0x30, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x8cc0
leaq -0xb0(%rbp), %rdi
callq 0x8ca0
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x9330
movq -0x118(%rbp), %rdi
movl %eax, 0xa0(%rdi)
addq $0x20, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x9280
movb %al, %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x149(%rbp)
jne 0x796c
movq -0x118(%rbp), %rdi
addq $0x60, %rdi
callq 0x87b0
movq %rax, %rdi
callq 0x9280
movb %al, -0x149(%rbp)
movq -0x118(%rbp), %rdi
movb -0x149(%rbp), %al
andb $0x1, %al
movb %al, 0x8(%rdi)
movq (%rdi), %rax
callq *0x18(%rax)
callq 0x4040
movq %rax, -0xb8(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x9850
movq %rax, -0xd0(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x9820
movq %rax, -0xc8(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x98a0
movq %rax, -0xc0(%rbp)
cmpq $0x10, -0xc0(%rbp)
jle 0x79e2
xorl %eax, %eax
movq %rax, -0x158(%rbp)
jmp 0x79f5
movl $0x10, %eax
subq -0xc0(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movl %eax, -0xd4(%rbp)
leaq -0xdc(%rbp), %rdi
xorl %esi, %esi
movl $0x13, %edx
callq 0x8790
movq 0x205ac(%rip), %rdi     # 0x27fc8
leaq -0xdc(%rbp), %rsi
callq 0x5300
movq %rax, %rdi
leaq 0x208d6(%rip), %rsi     # 0x28308
callq 0x52b0
movq %rax, %rdi
leaq 0x12baa(%rip), %rsi     # 0x1a5eb
callq 0x42d0
leaq -0xe4(%rbp), %rdi
xorl %esi, %esi
movl $0x13, %edx
callq 0x8790
movq 0x20568(%rip), %rdi     # 0x27fc8
leaq -0xe4(%rbp), %rsi
callq 0x5300
movq %rax, %rdi
leaq 0x20892(%rip), %rsi     # 0x28308
callq 0x52b0
movq %rax, %rdi
leaq 0x12b6f(%rip), %rsi     # 0x1a5f4
callq 0x42d0
movq %rax, -0x160(%rbp)
cmpq $0x10, -0xc0(%rbp)
jle 0x7aab
movq -0xc0(%rbp), %rax
movq %rax, -0x168(%rbp)
jmp 0x7ab9
movl $0x10, %eax
movq %rax, -0x168(%rbp)
jmp 0x7ab9
movq -0x160(%rbp), %rdi
movq -0x168(%rbp), %rcx
movl $0x3e8, %eax            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rsi
callq 0x4520
movl -0xd4(%rbp), %edi
callq 0x5230
jmp 0x741a
movq -0x118(%rbp), %rdi
addq $0x90, %rdi
callq 0x9800
movq %rax, -0x178(%rbp)
leaq -0x109(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x4500
movq -0x170(%rbp), %rdx
leaq 0x12ac3(%rip), %rsi     # 0x1a5e7
leaq -0x108(%rbp), %rdi
callq 0x43f0
jmp 0x7b32
movq -0x178(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
leaq -0x108(%rbp), %rsi
callq *%rax
jmp 0x7b4b
leaq -0x108(%rbp), %rdi
callq 0x41f0
leaq -0x109(%rbp), %rdi
callq 0x4330
movl $0x32, %edi
callq 0x5230
movq -0x118(%rbp), %rdi
addq $0x90, %rdi
callq 0x9800
movq %rax, %rdi
movq (%rdi), %rax
callq *0x20(%rax)
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x7bb8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x41f0
leaq -0x109(%rbp), %rdi
callq 0x4330
movq -0x38(%rbp), %rdi
callq 0x44f0
nopl (%rax)