//            // MPFW SERVER
//           // 
//  * *     // 
// * . *   //  implemented
//  *I*   //   |
//   *   //    v
//      //    (Net)Quake(World)-compatible game server
//     //                ^
//    //                 |
//   //                  eventually?
//  //
// //
//// github.com/safariminer
/// tilde.town/~safariminer
//

 	      //
 	     // Notes relative to netquake
 	    // - based on : https://www.gamers.org/dEngine/quake/QDP/qnp.html
 	   // - Netquake = UDP
 	  // - all numbers = big endian unless exception
 	 // - don't blindly follow the spec:
    //	  | game endpoints in quake don't have auth
   //     | do not use basic range, randomize ports throughout the available range
  //      | verify ip and port
 // 	  | even though the spec calls for it, don't display IP/port info in playerinfo control
//

#ifdef _DEBUG
#define MPFW_SERVER_VERSION std::string(std::string("Debug Build ") + std::string(__DATE__))
#else
#define MPFW_SERVER_VERSION "Release build"
#endif

#include <iostream> // basic io
#include <vector>  // dynamic arrays
#include <map>    // keyval maps
#include <asio.hpp>
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
#include <print>

#include "Config.h"
#include "NetQuake.h"
#include <thread>

#include <fstream>
#include <string>



bool _serverRunning = true;


int rcp = 0;

Config::Config *config = new Config::Config;


int main(int argc, char** argv) {
	
	
	std::print("MPFW Server\nBuild Version: {}\n--------------------\n", MPFW_SERVER_VERSION);

	std::vector<NetQuake::GameEndpoint> gameEndpoints;
	NetQuake::Internal::InternalRequestConsole* internalRequestConsole = new NetQuake::Internal::InternalRequestConsole();

	asio::io_context io_context;

	std::print("Starting control server thread...\n");
	
	std::thread controlServer(NetQuake::NetQuake_ControlServer, internalRequestConsole, config, &_serverRunning);
	
	controlServer.detach();
	std::print("Started control server thread\n");
	
	

	while (_serverRunning) {
		internalRequestConsole->Update(config, &gameEndpoints);
	}

	delete internalRequestConsole;
	return 0;
}