Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Reference: Oxide.Core.MySql
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Oxide.Core.Database;
- using Oxide.Core.Libraries.Covalence;
- using UnityEngine;
- using System.Collections;
- using Oxide.Core;
- using UnityEngine.Scripting;
- using System.Linq;
- using Oxide.Core.MySql;
- namespace Oxide.Plugins
- {
- [Info("ServerListConnector", "531devv", "1.0.2", ResourceId = 1923)]
- [Description("xxxx")]
- class ServerListConnector : CovalencePlugin
- {
- readonly Core.MySql.Libraries.MySql mySql = new Core.MySql.Libraries.MySql();
- Connection connection;
- DefaultConfig config;
- class DefaultConfig
- {
- readonly List<string> exampleCommands = new List<string>();
- public readonly Dictionary<string, List<string>> Packages = new Dictionary<string, List<string>>();
- public string DatabaseHost = "localhost";
- public int DatabasePort = 3306;
- public string DatabaseName = "oxide";
- public string DatabaseUser = "root";
- public string DatabasePassword = "changeme";
- public string ServerID = "111";
- }
- protected override void LoadDefaultConfig()
- {
- PrintWarning("Creating a new configuration file");
- Config.Clear();
- config = new DefaultConfig();
- Config.WriteObject(config, true);
- SaveConfig();
- }
- void Init()
- {
- try
- {
- config = Config.ReadObject<DefaultConfig>();
- }
- catch
- {
- PrintWarning("Could not read config, creating new default config");
- LoadDefaultConfig();
- }
- }
- void Loaded()
- {
- timer.Repeat(10f, 0, () =>
- {
- DatabaseUpdate();
- });
- }
- void DatabaseUpdate()
- {
- var playerCount = players.Connected.Count();
- string InsertData = "UPDATE `servers_table` SET `players`=" + playerCount + ", `updated_at`=CURRENT_TIMESTAMP WHERE `id`=" + config.ServerID;
- connection = mySql.OpenDb(config.DatabaseHost, config.DatabasePort, config.DatabaseName, config.DatabaseUser, config.DatabasePassword, this);
- var sql = Core.Database.Sql.Builder.Append(InsertData);
- mySql.Insert(sql, connection);
- mySql.CloseDb(connection);
- }
- bool checkDatabase()
- {
- if (connection == null)
- {
- Puts("Can't connect to the database!");
- return false;
- }
- return true;
- }
- T GetConfig<T>(string name, T value) => Config[name] == null ? value : (T)Convert.ChangeType(Config[name], typeof(T));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement