Commit 21673926 authored by jan.koester's avatar jan.koester
Browse files

test

parent 9ee9616f
Loading
Loading
Loading
Loading
+47 −128
Original line number Diff line number Diff line
@@ -84,29 +84,17 @@ namespace blogi {
            dbpp::SQL uuidTypeSql;
            const dbpp::SQL uuidType = Args->database[0]->getUUIDType(uuidTypeSql);


            sql << "CREATE TABLE IF NOT EXISTS gameserver_protocols("
                <<   "id " << uuidType << " PRIMARY KEY,"
                <<   "pname character varying(255) NOT NULL"
                << ");";
            Args->database[0]->exec(sql,res);

            sql.clear();
            res.clear();
            sql << "CREATE TABLE IF NOT EXISTS gameserver("
                <<   "id " << uuidType << " PRIMARY KEY,"
                <<   "protocol " << uuidType << ","
                <<   "protocol character varying(64) NOT NULL DEFAULT 'hlds',"
                <<   "addr character varying(255) NOT NULL,"
                <<   "port integer,"
                <<   "FOREIGN KEY (protocol) REFERENCES gameserver_protocols (id)"
                <<   "port integer"
                << ");";

            Args->database[0]->exec(sql,res);

            sql << "SELECT COALESCE(p.pname,'hlds'),g.addr,g.port "
                << "FROM gameserver g "
                << "LEFT JOIN gameserver_protocols p ON g.protocol=p.id";

            sql.clear();
            res.clear();
            sql << "SELECT protocol,addr,port FROM gameserver";
            int count = Args->database[0]->exec(sql,res);

            for (int i = 0; i < count; i++) {
@@ -141,9 +129,6 @@ namespace blogi {
            std::string formAddr, formPort, formProtocol;
            bool hasServerForm = false;
            std::string delServerId;
            std::string formProtoName;
            bool hasProtoForm = false;
            std::string delProtoId;

            for(const auto &cur : form.urlData()){
                if(cur.key=="server_addr"){
@@ -157,38 +142,9 @@ namespace blogi {
                    hasServerForm = true;
                }else if(cur.key=="delete_server_id"){
                    delServerId = cur.value;
                }else if(cur.key=="protocol_name"){
                    formProtoName = cur.value;
                    hasProtoForm = true;
                }else if(cur.key=="delete_protocol_id"){
                    delProtoId = cur.value;
                }
            }

            // Delete protocol
            if(!delProtoId.empty()){
                dbpp::SQL sql;
                dbpp::DBResult res;
                std::vector<char> buf;
                sql << "DELETE FROM gameserver_protocols WHERE id='" << sql.escaped(buf, delProtoId.c_str()) << "'";
                Args->database[tid]->exec(sql,res);
                saved = true;
            }

            // Add protocol
            if(hasProtoForm && !formProtoName.empty()){
                dbpp::SQL sql;
                dbpp::DBResult res;
                std::vector<char> buf;
                uuid::uuid protoId;
                protoId.generate();
                sql << "INSERT INTO gameserver_protocols (id,pname) VALUES ('"
                    << protoId.c_str() << "','"
                    << sql.escaped(buf, formProtoName.c_str()) << "')";
                Args->database[tid]->exec(sql,res);
                saved = true;
            }

            // Delete server
            if(!delServerId.empty()){
                dbpp::SQL sql;
@@ -199,8 +155,8 @@ namespace blogi {
                saved = true;
            }

            // Add server
            if(hasServerForm && !formAddr.empty() && !formPort.empty()){
            // Add server — protocol is the enum string directly
            if(hasServerForm && !formAddr.empty() && !formPort.empty() && !formProtocol.empty()){
                dbpp::SQL sql;
                dbpp::DBResult res;
                std::vector<char> buf;
@@ -209,8 +165,8 @@ namespace blogi {
                sql << "INSERT INTO gameserver (id,protocol,addr,port) VALUES ('"
                    << servId.c_str() << "','"
                    << sql.escaped(buf, formProtocol.c_str()) << "','"
                    << sql.escaped(buf, formAddr.c_str()) << "','"
                    << sql.escaped(buf, formPort.c_str()) << "')";
                    << sql.escaped(buf, formAddr.c_str()) << "',"
                    << atoi(formPort.c_str()) << ")";
                Args->database[tid]->exec(sql,res);
                saved = true;
            }
@@ -223,65 +179,12 @@ namespace blogi {
                json_object_object_add(jroot, "message_type", json_object_new_string("success"));
            }

            // --- Protocols section ---
            json_object *jprotocols = json_object_new_array();
            {
                dbpp::SQL sql;
                dbpp::DBResult res;
                sql << "SELECT id,pname FROM gameserver_protocols";
                int count = Args->database[tid]->exec(sql,res);
                for(int i = 0; i < count; ++i){
                    json_object *jp = json_object_new_object();
                    json_object_object_add(jp, "id", json_object_new_string(res[i][0]));
                    json_object_object_add(jp, "pname", json_object_new_string(res[i][1]));
                    json_object_array_add(jprotocols, jp);
                }
            }
            json_object_object_add(jroot, "protocols", jprotocols);

            // Add protocol form
            json_object *jprotoform = json_object_new_object();
            json_object_object_add(jprotoform, "method", json_object_new_string("POST"));
            json_object_object_add(jprotoform, "action", json_object_new_string(
                Args->config->buildurl("settings/gamestatus").c_str()));
            {
                json_object *jfields = json_object_new_array();
                json_object *jf = json_object_new_object();
                json_object_object_add(jf, "name", json_object_new_string("protocol_name"));
                json_object_object_add(jf, "label", json_object_new_string("Protocol Name"));
                json_object_object_add(jf, "type", json_object_new_string("text"));
                json_object_object_add(jf, "value", json_object_new_string(""));
                json_object_array_add(jfields, jf);
                json_object_object_add(jprotoform, "fields", jfields);
            }
            json_object_object_add(jprotoform, "submit", json_object_new_string(tr(_lang,"Add Protocol").c_str()));
            json_object_object_add(jroot, "protocol_form", jprotoform);

            // Delete protocol form
            json_object *jdelprotoform = json_object_new_object();
            json_object_object_add(jdelprotoform, "method", json_object_new_string("POST"));
            json_object_object_add(jdelprotoform, "action", json_object_new_string(
                Args->config->buildurl("settings/gamestatus").c_str()));
            {
                json_object *jfields = json_object_new_array();
                json_object *jf = json_object_new_object();
                json_object_object_add(jf, "name", json_object_new_string("delete_protocol_id"));
                json_object_object_add(jf, "label", json_object_new_string("Protocol ID to delete"));
                json_object_object_add(jf, "type", json_object_new_string("text"));
                json_object_object_add(jf, "value", json_object_new_string(""));
                json_object_array_add(jfields, jf);
                json_object_object_add(jdelprotoform, "fields", jfields);
            }
            json_object_object_add(jdelprotoform, "submit", json_object_new_string(tr(_lang,"Delete Protocol").c_str()));
            json_object_object_add(jroot, "delete_protocol_form", jdelprotoform);

            // --- Servers section ---
            // --- Server list ---
            json_object *jservers = json_object_new_array();
            {
                dbpp::SQL sql;
                dbpp::DBResult res;
                sql << "SELECT g.id,COALESCE(p.pname,'unknown'),g.addr,g.port FROM gameserver g "
                    << "LEFT JOIN gameserver_protocols p ON g.protocol=p.id";
                sql << "SELECT id,protocol,addr,port FROM gameserver";
                int count = Args->database[tid]->exec(sql,res);
                for(int i = 0; i < count; ++i){
                    json_object *js = json_object_new_object();
@@ -294,41 +197,57 @@ namespace blogi {
            }
            json_object_object_add(jroot, "items", jservers);

            // Add server form
            // --- Add server form ---
            json_object *jform   = json_object_new_object();
            json_object *jfields = json_object_new_array();
            json_object_object_add(jform, "method", json_object_new_string("POST"));
            json_object_object_add(jform, "action", json_object_new_string(
                Args->config->buildurl("settings/gamestatus").c_str()));

            json_object *jfields = json_object_new_array();

            auto addField = [&](const char *name, const char *label, const char *type, const std::string &value){
            auto addTextField = [&](const char *name, const char *label){
                json_object *jf = json_object_new_object();
                json_object_object_add(jf, "name",  json_object_new_string(name));
                json_object_object_add(jf, "label", json_object_new_string(label));
                json_object_object_add(jf, "type", json_object_new_string(type));
                json_object_object_add(jf, "value", json_object_new_string(value.c_str()));
                json_object_object_add(jf, "type",  json_object_new_string("text"));
                json_object_object_add(jf, "value", json_object_new_string(""));
                json_object_array_add(jfields, jf);
            };

            addField("server_addr", "Server Address", "text", "");
            addField("server_port", "Server Port", "text", "");
            addField("server_protocol", "Protocol ID", "text", "");
            addTextField("server_addr", tr(_lang,"Server Address").c_str());
            addTextField("server_port", tr(_lang,"Server Port").c_str());

            // Protocol select — options hardcoded from the module's supported protocols
            {
                static const char* protocols[] = { "hlds", "bf1942", "cod4", nullptr };
                json_object *jpf  = json_object_new_object();
                json_object *jopts = json_object_new_array();
                json_object_object_add(jpf, "name",  json_object_new_string("server_protocol"));
                json_object_object_add(jpf, "label", json_object_new_string(tr(_lang,"Protocol").c_str()));
                json_object_object_add(jpf, "type",  json_object_new_string("select"));
                for(int p = 0; protocols[p]; ++p){
                    json_object *jopt = json_object_new_object();
                    json_object_object_add(jopt, "value", json_object_new_string(protocols[p]));
                    json_object_object_add(jopt, "label", json_object_new_string(protocols[p]));
                    json_object_array_add(jopts, jopt);
                }
                json_object_object_add(jpf, "options", jopts);
                json_object_array_add(jfields, jpf);
            }

            json_object_object_add(jform, "fields", jfields);
            json_object_object_add(jform, "submit", json_object_new_string(tr(_lang,"Add Server").c_str()));
            json_object_object_add(jroot, "form", jform);

            // Delete server form
            // --- Delete server form ---
            json_object *jdelform   = json_object_new_object();
            json_object *jdelfields = json_object_new_array();
            json_object_object_add(jdelform, "method", json_object_new_string("POST"));
            json_object_object_add(jdelform, "action", json_object_new_string(
                Args->config->buildurl("settings/gamestatus").c_str()));
            json_object *jdelfields = json_object_new_array();
            {
                json_object *jf = json_object_new_object();
                json_object_object_add(jf, "name",  json_object_new_string("delete_server_id"));
                json_object_object_add(jf, "label", json_object_new_string("Server ID to delete"));
                json_object_object_add(jf, "label", json_object_new_string(tr(_lang,"Server ID").c_str()));
                json_object_object_add(jf, "type",  json_object_new_string("text"));
                json_object_object_add(jf, "value", json_object_new_string(""));
                json_object_array_add(jdelfields, jf);