#include "server/GameServer.h"
#include <iostream>
#include <csignal>

GameServer* g_server = nullptr;

void signalHandler(int signum) {
    std::cout << "\n收到信号 " << signum << "，正在关闭服务器..." << std::endl;
    if (g_server) {
        g_server->stop();
    }
    exit(signum);
}

int main(int argc, char* argv[]) {
    int port = 8888;
    std::string dbPath = "data/game.db";
    
    if (argc > 1) {
        port = std::atoi(argv[1]);
    }
    if (argc > 2) {
        dbPath = argv[2];
    }
    
    std::cout << "==================================" << std::endl;
    std::cout << "  在线RPG游戏 服务器" << std::endl;
    std::cout << "==================================" << std::endl;
    std::cout << "端口：" << port << std::endl;
    std::cout << "数据库：" << dbPath << std::endl;
    std::cout << "==================================" << std::endl;
    
    // 注册信号处理
    signal(SIGINT, signalHandler);
    signal(SIGTERM, signalHandler);
    
    // 创建并启动服务器
    GameServer server(port, dbPath);
    g_server = &server;
    
    if (!server.start()) {
        std::cerr << "启动服务器失败" << std::endl;
        return 1;
    }
    
    // 运行服务器主循环
    server.run();
    
    return 0;
}
