Ejemplo n.º 1
0
        public bool NonQuery(string query, string[,] parameters)
        {
            try
            {
                cmd            = new MySqlCommand();
                cmd.Connection = conn;

                cmd.CommandText = query;

                for (int i = 0; i < parameters.Length / 2; i++)
                {
                    cmd.Parameters.AddWithValue(parameters[i, 0], parameters[i, 1]);
                }

                cmd.Prepare();

                cmd.ExecuteNonQuery();

                return(true);
            }
            catch (MySqlException e)
            {
                DarkLog.Error("DMPPlayerLogging: Error querying the logging database " + e);
                return(false);
            }
        }
Ejemplo n.º 2
0
        public Main()
        {
            LoadSettings();
            CommandHandler.RegisterCommand("reloadplayerlogging", ReloadSettings, "Reload the player logging settings");

            //Connect to database
            DBConnect connection = new DBConnect();

            //Check if table already exists
            string existsQuery = "show tables like @table;";

            string[,] existsParameters = { { "@table", settingsStore.tableName } };

            if (!connection.Query(existsQuery, existsParameters).HasRows)
            {
                //creates table
                string creationSQL = "CREATE TABLE `ksp`.`" + settingsStore.tableName + "` ( `session_id` INT NOT NULL AUTO_INCREMENT , `session_player_name` VARCHAR(32) NOT NULL , `session_start_time` TIMESTAMP NOT NULL , `session_duration` INT NOT NULL , `session_end_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`session_id`)) ENGINE = InnoDB;";
                string[,] creationParameters = { { } };

                if (connection.NonQuery(creationSQL, creationParameters))
                {
                    DarkLog.Debug("DMPPlayerLogging: Created database table.");
                }
                else
                {
                    DarkLog.Error("DMPPlayerLogging: Logging table creation failed.");
                }
            }

            connection.CloseConnection();
        }
Ejemplo n.º 3
0
        public override void OnClientDisconnect(ClientObject client)
        {
            //Connect to database
            DBConnect connection = new DBConnect();

            //Grab session data from list
            long connectTime     = connectedPlayers[client.playerName];
            long sessionDuration = DarkMultiPlayerCommon.Common.GetCurrentUnixTime() - connectTime;

            //Send session to the database
            string sessionSendSQL = "INSERT INTO `" + settingsStore.tableName + "` (`session_player_name`, `session_start_time`, `session_duration`) VALUES (@player_name, date_add('1970-01-01', INTERVAL @start SECOND), @duration)";

            string[,] parameters = { { "@player_name", client.playerName }, { "@start", connectTime.ToString() }, { "@duration", sessionDuration.ToString() } };

            if (connection.NonQuery(sessionSendSQL, parameters))
            {
                //remove player from list
                connectedPlayers.Remove(client.playerName);
                DarkLog.Debug("DMPPlayerLogging: Successfully sent " + client.playerName + "'s to the logging server.");
            }
            else
            {
                connectedPlayers.Remove(client.playerName);
                DarkLog.Error("DMPPlayerLogging: Couldn't send session data to logging database.");
            }
            connection.CloseConnection();
        }
Ejemplo n.º 4
0
        public QueryResult Query(string query, string[,] parameters)
        {
            try
            {
                cmd            = new MySqlCommand();
                cmd.Connection = conn;

                cmd.CommandText = query;

                for (int i = 0; i < parameters.Length / 2; i++)
                {
                    cmd.Parameters.AddWithValue(parameters[i, 0], parameters[i, 1]);
                }

                cmd.Prepare();

                MySqlDataReader reader = null;

                reader = cmd.ExecuteReader();

                QueryResult result = new QueryResult();
                result.HasRows = reader.HasRows;

                if (reader.HasRows)
                {
                    DataTable data = new DataTable();
                    data.Load(reader);
                    reader.Close();

                    result.Success = true;
                    result.Data    = data;
                    return(result);
                }
                else
                {
                    reader.Close();
                    result.Success = true;
                    return(result);
                }
            }
            catch (MySqlException e)
            {
                DarkLog.Error("DMPPlayerLogging: Error querying the logging database " + e);
                QueryResult result = new QueryResult();
                result.Success = false;
                return(result);
            }
        }
Ejemplo n.º 5
0
        public DBConnect()
        {
            string connectionString = "server=" + settingsStore.sqlIP + ";" +
                                      "uid=" + settingsStore.dbUsername + ";" +
                                      "pwd=" + settingsStore.dbPassword + ";" +
                                      "database=" + settingsStore.dbName + ";";

            try
            {
                conn = new MySqlConnection();
                conn.ConnectionString = connectionString;
                conn.Open();
                DarkLog.Debug("DMPPlayerLogging: Connection to logging database successful");
            }
            catch (MySqlException e)
            {
                DarkLog.Error("DMPPlayerLogging: Error connecting to logging database " + e);
            }
        }
Ejemplo n.º 6
0
        private void LoadSettings()
        {
            string tokenFileFullPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), TOKEN_FILE);

            if (!File.Exists(tokenFileFullPath))
            {
                using (StreamWriter sw = new StreamWriter(tokenFileFullPath))
                {
                    sw.WriteLine(Guid.NewGuid().ToString());
                }
            }
            using (StreamReader sr = new StreamReader(tokenFileFullPath))
            {
                settingsStore.serverHash = CalculateSHA256Hash(sr.ReadLine());
            }
            string settingsFileFullPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), SETTINGS_FILE);

            if (!File.Exists(settingsFileFullPath))
            {
                using (StreamWriter sw = new StreamWriter(settingsFileFullPath))
                {
                    sw.WriteLine("reporting = server.game.api.d-mp.org:9001");
                    sw.WriteLine("gameAddress = ");
                    sw.WriteLine("banner = ");
                    sw.WriteLine("homepage = ");
                    sw.WriteLine("admin = ");
                    sw.WriteLine("team = ");
                    sw.WriteLine("location = ");
                    sw.WriteLine("fixedIP = false");
                    sw.WriteLine("description = ");
                }
            }
            bool reloadSettings = false;

            using (StreamReader sr = new StreamReader(settingsFileFullPath))
            {
                bool   readingDescription = false;
                string currentLine;

                while ((currentLine = sr.ReadLine()) != null)
                {
                    if (!readingDescription)
                    {
                        try
                        {
                            string key   = currentLine.Substring(0, currentLine.IndexOf("=")).Trim();
                            string value = currentLine.Substring(currentLine.IndexOf("=") + 1).Trim();
                            switch (key)
                            {
                            case "reporting":
                            {
                                string    address = value.Substring(0, value.LastIndexOf(":"));
                                string    port    = value.Substring(value.LastIndexOf(":") + 1);
                                IPAddress reportingIP;
                                int       reportingPort = 0;
                                if (Int32.TryParse(port, out reportingPort))
                                {
                                    if (reportingPort > 0 && reportingPort < 65535)
                                    {
                                        //Try parsing the address directly before trying a DNS lookup
                                        if (!IPAddress.TryParse(address, out reportingIP))
                                        {
                                            IPHostEntry entry = Dns.GetHostEntry(address);
                                            reportingIP = entry.AddressList[0];
                                        }
                                        settingsStore.reportingEndpoint = new IPEndPoint(reportingIP, reportingPort);
                                    }
                                }
                            }
                            break;

                            case "gameAddress":
                                settingsStore.gameAddress = value;
                                break;

                            case "banner":
                                settingsStore.banner = value;
                                break;

                            case "homepage":
                                settingsStore.homepage = value;
                                break;

                            case "admin":
                                settingsStore.admin = value;
                                break;

                            case "team":
                                settingsStore.team = value;
                                break;

                            case "location":
                                settingsStore.location = value;
                                break;

                            case "fixedIP":
                                settingsStore.fixedIP = (value == "true");
                                break;

                            case "description":
                                readingDescription        = true;
                                settingsStore.description = value;
                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            DarkLog.Error("Error reading settings file, Exception " + e);
                        }
                    }
                    else
                    {
                        //Reading description
                        settingsStore.description += "\n" + currentLine;
                    }
                }
            }
            if (reloadSettings)
            {
                //Load with the default settings if anything is incorrect.
                File.Delete(settingsFileFullPath);
                LoadSettings();
            }
            loadedSettings = true;
        }
Ejemplo n.º 7
0
        private void LoadSettings()
        {
            string settingsFileFullPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), SETTINGS_FILE);

            if (!File.Exists(settingsFileFullPath))
            {
                using (StreamWriter sw = new StreamWriter(settingsFileFullPath))
                {
                    sw.WriteLine("sqlServer = localhost:3306");
                    sw.WriteLine("dbName = ksp");
                    sw.WriteLine("dbUsername = kspgs");
                    sw.WriteLine("dbPassword = abc123");
                    sw.WriteLine("tableName = tbl-player-sessions");
                }
            }
            using (StreamReader sr = new StreamReader(settingsFileFullPath))
            {
                string currentLine;

                while ((currentLine = sr.ReadLine()) != null)
                {
                    try
                    {
                        string key   = currentLine.Substring(0, currentLine.IndexOf("=")).Trim();
                        string value = currentLine.Substring(currentLine.IndexOf("=") + 1).Trim();
                        switch (key)
                        {
                        case "sqlServer":
                        {
                            string address = value.Substring(0, value.LastIndexOf(":"));
                            string port    = value.Substring(value.LastIndexOf(":") + 1);

                            settingsStore.sqlIP   = address;
                            settingsStore.sqlPort = port;
                        }
                        break;

                        case "dbName":
                            settingsStore.dbName = value;
                            break;

                        case "dbUsername":
                            settingsStore.dbUsername = value;
                            break;

                        case "dbPassword":
                            settingsStore.dbPassword = value;
                            break;

                        case "tableName":
                            settingsStore.tableName = value;
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        DarkLog.Error("Error reading settings file, Exception " + e);
                        File.Delete(settingsFileFullPath);
                        LoadSettings();
                    }
                }
            }
        }