Advertisement
Guest User

Untitled

a guest
May 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 9.62 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3. using Newtonsoft.Json;
  4. using System.Reflection;
  5. using System.Text.RegularExpressions;
  6. using System.Data;
  7. using Oxide.Core.Plugins;
  8. using UnityEngine;
  9.  
  10. namespace Oxide.Plugins
  11. {
  12.     [Info("StatLogger", "Zanderwar", 1.0)]
  13.     [Description("A plugin that sends statistics to endpoints")]
  14.  
  15.     public class StatLogger : RustPlugin
  16.     {
  17.         /// <summary>
  18.         /// Enable Debug Mode... Will display console messages and responses
  19.         /// when data is sent to the endpoint
  20.         /// </summary>
  21.         protected bool debug = true;
  22.  
  23.         /// <summary>
  24.         /// When enabled only death data will be sent, and will be sent to baseEventUrl
  25.         /// </summary>
  26.         protected bool eventMode = false;
  27.  
  28.         /// <summary>
  29.         /// List of endpoints, but contain a baseUrl key
  30.         /// </summary>
  31.         public static Dictionary<string, string> endpoints = new Dictionary<string, string>
  32.         {
  33.             { "baseUrl", "http://ruststats.royal-soldiers.com/api/" },
  34.             { "baseEventsUrl", "http://ruststats.royal-soldiers.com/events-api/" },
  35.             { "currentlyOnline", "currently-online" },
  36.             { "playerKilled", "player-killed" },
  37.             { "playerJoined", "player-joined" },
  38.             { "playerDisconnected", "player-disconnected" },
  39.             { "playerSuicide", "player-suicide" },
  40.             { "playerChat", "player-chat" },
  41.             { "serverInfo", "server-info" }
  42.         };
  43.  
  44.         /// <summary>
  45.         /// Loads current online players into the website.
  46.         /// </summary>
  47.         void Loaded()
  48.         {
  49.             cmd.AddConsoleCommand("fakedistancekill", this, "fakeDistanceKill");
  50.             cmd.AddConsoleCommand("sendserverinfo", this, "sendServerInfo");
  51.  
  52.             if (this.eventMode) {
  53.                 Puts("You are running in event mode, only kill and player data will be sent to the server");
  54.                 return;
  55.             }
  56.  
  57.             this.sendServerInfo();
  58.  
  59.             timer.Repeat(60f, 0, () =>
  60.             {
  61.                 this.sendServerInfo();
  62.             });
  63.  
  64.  
  65.             var onlinePlayers = new Dictionary<int, Dictionary<string,string>>();
  66.             var count = 0;
  67.             foreach (BasePlayer player in BasePlayer.activePlayerList)
  68.             {
  69.                 onlinePlayers.Add(
  70.                     count,
  71.                     new Dictionary<string, string> {
  72.                         { "SteamID",  player.userID.ToString() },
  73.                         { "PlayerName", player.displayName.ToString() },
  74.                         { "Address", Regex.Replace(player.net.connection.ipaddress.ToString(), @":{1}[0-9]{1}\d*", "").ToString() }
  75.                     }
  76.                 );
  77.  
  78.                 count += 1;
  79.             }
  80.  
  81.             this.sendData(this.getEndpoint("currentlyOnline"), onlinePlayers, "Currently Online Players has been sent to the website");
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Sends data about a player that has just connected for use of Player List and Online Player List
  86.         /// </summary>
  87.         /// <param name="player"></param>
  88.         void OnPlayerInit(BasePlayer player)
  89.         {
  90.             var data = new Dictionary<string, string> {
  91.                 { "SteamID",  player.userID.ToString() },
  92.                 { "PlayerName", player.displayName.ToString() },
  93.                 { "Address", Regex.Replace(player.net.connection.ipaddress.ToString(), @":{1}[0-9]{1}\d*", "").ToString() }
  94.             };
  95.  
  96.             this.sendData(this.getEndpoint("playerJoined"), data, player.userID.ToString() + " has joined and the website has been notified");
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Player disconnected and informing the endpoint defined about it
  101.         /// </summary>
  102.         /// <param name="player"></param>
  103.         /// <param name="reason"></param>
  104.         void OnPlayerDisconnected(BasePlayer player, string reason)
  105.         {
  106.             var data = new Dictionary<string, string> {
  107.                 { "SteamID",  player.userID.ToString() }
  108.             };
  109.  
  110.             this.sendData(this.getEndpoint("playerDisconnected"), data, player.userID.ToString() + " has disconnected and the website has been notified");
  111.         }
  112.  
  113.         /// <summary>
  114.         /// Sends data about a player death
  115.         /// </summary>
  116.         /// <param name="victim"></param>
  117.         /// <param name="info"></param>
  118.         void OnEntityDeath(BaseCombatEntity victim, HitInfo info)
  119.         {
  120.             var target = victim?.ToPlayer();
  121.             var attacker = info?.Initiator?.ToPlayer();
  122.             var entity = info?.Initiator;
  123.  
  124.             if (victim?.ToPlayer() != null && info?.Initiator?.ToPlayer() != null)
  125.             {
  126.  
  127.                 if (target.userID == attacker.userID)
  128.                 {
  129.                     Puts("Self-Inflicted Death: " + target.displayName + "(" + info.damageTypes.GetMajorityDamageType() + ")");
  130.                     return;
  131.                 }
  132.  
  133.                 var weapon = info?.Weapon?.GetItem()?.info?.displayName?.english ?? "Unknown";
  134.                 var bone = info?.boneName ?? "Unknown";
  135.                 var attackerHealth = Convert.ToString(info.Initiator.Health()) ?? "Unknown";
  136.                 var distance = Convert.ToString(attacker.Distance(target.transform.position)) ?? "Unknown";
  137.  
  138.                 var data = new Dictionary<string, string> {
  139.                     { "VictimID",  target.userID.ToString() },
  140.                     { "KillerID",  attacker.userID.ToString() },
  141.                     { "KillerHP", attackerHealth },
  142.                     { "Weapon", weapon },
  143.                     { "Distance", distance },
  144.                     { "Bone", bone }
  145.                 };
  146.  
  147.                 this.sendData(this.getEndpoint("playerKilled"), data, "Sending Kill to Website");
  148.             }
  149.  
  150.  
  151.         }
  152.  
  153.         /// <summary>
  154.         /// Sends chat messages
  155.         /// </summary>
  156.         /// <param name="arg"></param>
  157.         void OnPlayerChat(ConsoleSystem.Arg arg)
  158.         {
  159.             BasePlayer player = (BasePlayer)arg.Connection.player;
  160.             string message = arg.GetString(0, "");
  161.  
  162.             var data = new Dictionary<string, string>
  163.             {
  164.                 { "SteamID",  player.userID.ToString() },
  165.                 { "Message",  message }
  166.             };
  167.  
  168.             this.sendData(this.getEndpoint("playerChat"), data, "Sending player chat");
  169.         }
  170.  
  171.         /// <summary>
  172.         /// DRY method to send data to the designated endpoint
  173.         /// </summary>
  174.         /// <param name="endpoint"></param>
  175.         /// <param name="dictionary"></param>
  176.         /// <param name="debugMessage"></param>
  177.         protected void sendData(string endpoint, object dictionary, string debugMessage = null)
  178.         {
  179.             string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);
  180.  
  181.             if (this.debug && !String.IsNullOrEmpty(debugMessage))
  182.             {
  183.                 Puts(debugMessage);
  184.             }
  185.  
  186.             webrequest.EnqueuePost(endpoint, "data=" + json, (code, response) => printResponse(response), this);
  187.         }
  188.  
  189.         /// <summary>
  190.         ///
  191.         /// </summary>
  192.         /// <param name="endpoint"></param>
  193.         /// <param name="json"></param>
  194.         /// <param name="debugMessage"></param>
  195.         protected void sendData(string endpoint, string json, string debugMessage = null)
  196.         {
  197.             if (this.debug && !String.IsNullOrEmpty(debugMessage))
  198.             {
  199.                 Puts(debugMessage);
  200.             }
  201.  
  202.             webrequest.EnqueuePost(endpoint, "data=" + json, (code, response) => printResponse(response), this);
  203.         }
  204.  
  205.         /// <summary>
  206.         /// Callback method to print the response to console
  207.         /// </summary>
  208.         /// <param name="response"></param>
  209.         protected void printResponse(string response)
  210.         {
  211.             if (!this.debug || String.IsNullOrEmpty(response))
  212.             {
  213.                 return;
  214.             }
  215.  
  216.             Puts(response);
  217.         }
  218.  
  219.         /// <summary>
  220.         ///
  221.         /// </summary>
  222.         protected void sendServerInfo()
  223.         {
  224.             if (this.eventMode) {
  225.                 return;
  226.             }
  227.  
  228.             string serverinfo = ConsoleSystem.Run(ConsoleSystem.Option.Server.Quiet(), "serverinfo");
  229.  
  230.             this.sendData(this.getEndpoint("serverInfo"), serverinfo, "Sending Server Info");
  231.         }
  232.  
  233.         /// <summary>
  234.         /// Get for the endpoint list, combines baseUrl with any other endpoints defined
  235.         /// </summary>
  236.         /// <param name="forWhat">What endpoint do you want to connect the baseUrl with</param>
  237.         /// <returns></returns>
  238.         protected string getEndpoint(string forWhat)
  239.         {
  240.             string baseUrl = (this.eventMode) ? StatLogger.endpoints["baseEventUrl"] : StatLogger.endpoints["baseUrl"];
  241.  
  242.             return baseUrl + StatLogger.endpoints[forWhat];
  243.         }
  244.  
  245.         /// <summary>
  246.         /// Fakes a distance kill
  247.         /// </summary>
  248.         /// <param name="arg"></param>
  249.         void fakeDistanceKill(ConsoleSystem.Arg arg)
  250.         {
  251.             var data = new Dictionary<string, string> {
  252.                 { "VictimID",  "76561197997942034" },
  253.                 { "KillerID",  "76561198302518503" },
  254.                 { "KillerHP", "100" },
  255.                 { "Weapon", "Semi-Automatic Pistol" },
  256.                 { "Distance", Convert.ToString(arg.GetInt(0,1)) },
  257.                 { "Bone", "head" }
  258.             };
  259.  
  260.             this.sendData(this.getEndpoint("playerKilled"), data, "Sending FAKE Kill to Website");
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement