Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. // Reference: Oxide.Core.MySql
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using Oxide.Core.Database;
  7. using Oxide.Core.Libraries.Covalence;
  8. using UnityEngine;
  9. using System.Collections;
  10. using Oxide.Core;
  11. using UnityEngine.Scripting;
  12. using System.Linq;
  13. using Oxide.Core.MySql;
  14.  
  15. namespace Oxide.Plugins
  16. {
  17. [Info("ServerListConnector", "531devv", "1.0.2", ResourceId = 1923)]
  18. [Description("xxxx")]
  19.  
  20. class ServerListConnector : CovalencePlugin
  21. {
  22. readonly Core.MySql.Libraries.MySql mySql = new Core.MySql.Libraries.MySql();
  23. Connection connection;
  24. DefaultConfig config;
  25.  
  26. class DefaultConfig
  27. {
  28. readonly List<string> exampleCommands = new List<string>();
  29. public readonly Dictionary<string, List<string>> Packages = new Dictionary<string, List<string>>();
  30.  
  31. public string DatabaseHost = "localhost";
  32. public int DatabasePort = 3306;
  33. public string DatabaseName = "oxide";
  34. public string DatabaseUser = "root";
  35. public string DatabasePassword = "changeme";
  36. public string ServerID = "111";
  37. }
  38.  
  39. protected override void LoadDefaultConfig()
  40. {
  41. PrintWarning("Creating a new configuration file");
  42. Config.Clear();
  43. config = new DefaultConfig();
  44. Config.WriteObject(config, true);
  45. SaveConfig();
  46. }
  47.  
  48. void Init()
  49. {
  50.  
  51. try
  52. {
  53. config = Config.ReadObject<DefaultConfig>();
  54. }
  55. catch
  56. {
  57. PrintWarning("Could not read config, creating new default config");
  58. LoadDefaultConfig();
  59. }
  60. }
  61.  
  62. void Loaded()
  63. {
  64. timer.Repeat(10f, 0, () =>
  65. {
  66. DatabaseUpdate();
  67. });
  68. }
  69.  
  70. void DatabaseUpdate()
  71. {
  72. var playerCount = players.Connected.Count();
  73. string InsertData = "UPDATE `servers_table` SET `players`=" + playerCount + ", `updated_at`=CURRENT_TIMESTAMP WHERE `id`=" + config.ServerID;
  74. connection = mySql.OpenDb(config.DatabaseHost, config.DatabasePort, config.DatabaseName, config.DatabaseUser, config.DatabasePassword, this);
  75. var sql = Core.Database.Sql.Builder.Append(InsertData);
  76. mySql.Insert(sql, connection);
  77. mySql.CloseDb(connection);
  78. }
  79.  
  80.  
  81. bool checkDatabase()
  82. {
  83. if (connection == null)
  84. {
  85. Puts("Can't connect to the database!");
  86. return false;
  87. }
  88. return true;
  89. }
  90.  
  91. T GetConfig<T>(string name, T value) => Config[name] == null ? value : (T)Convert.ChangeType(Config[name], typeof(T));
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement