Core

Argument handling and run function

class Core {
    public:
        Core();
        ~Core(){};

        int checkArgs(int ac, char **av);
        void run();

    private:
        int _port;
        std::string _ip;
};

Core class is use to verify command arguments, and set a port and an IP by default to connect with the server.

void Core::run()
{
    Game game;
    game.MainLoop();
}

The run() function is used to start the game and call the game loop.

int Core::checkArgs(int ac, char **av)
{
    if (ac < 2)
        return 0;
    if (ac > 5)
        throw MyError("Core", "Too many arguments.");
    if (std::string(av[1]).find("-help") != std::string::npos) {
        std::cout << "USAGE: ./client -p port -h machine\n\tport\tis the port number; 4242 by default\n\tmachine\tis the name of the machine; localhost by default" << std::endl;
        return -1;
    }

    for (int i = 0; i < ac; i++) {
        if (std::string(av[i]).find("-p") != std::string::npos) {
            if (av[i + 1] == NULL)
                throw MyError("Core", "You put the -p but no port after.");
            try {
                this->_port = std::stoi(av[i + 1]);
            } catch (std::exception &e) {
                throw MyError("Core", "Port is not a number.");
            }
        }
        if (std::string(av[i]).find("-h") != std::string::npos) {
            if (av[i + 1] == NULL)
                throw MyError("Core", "You put the -h but no IP adress after.");
            for (int j = 0; av[i + 1][j] != '\0'; j++) {
                if (av[i + 1][j] == '.')
                    continue;
                else if (av[i + 1][j] < '0' || av[i + 1][j] > '9')
                    throw MyError("Core", "IP adress is not a number.");
                else
                    this->_ip = av[i + 1];
            }
        }
    }
    return 0;
}

The checkArgs() function is used to check arguments of the binary. Start the client without argument take argument by default:

Core::Core()
{
    this->_port = 4242;
    this->_ip = "127.0.0.1";
}

Use -h can set the IP to connect at the server. Use -p can set the port to connect at the server. Use -help to see the help prompt.

Last updated