C#:
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Configuration;
using Oxide.Core.Plugins;
using Rust;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("PlaytimeReward", "Flutes", "1.0.0")]
[Description("Extension of plugin CustomRewards")]
public class PlaytimeReward : RustPlugin
{
[PluginReference] Plugin PlaytimeTracker, CustomRewards;
private Configuration m_configuration;
public double PlaytimeTracker_GetPlayedTime(string type, ulong playerId)
{
double time = 0;
object timeObj = PlaytimeTracker.Call<object>("GetPlayTime", playerId.ToString());
if (timeObj != null)
time += (double)timeObj;
if (m_configuration.CheckAfkTime)
{
timeObj = PlaytimeTracker.Call<object>("GetAFKTime", playerId.ToString());
if (timeObj != null)
time += (double)timeObj;
}
return time;
}
public void PlaytimeTracker_TryGivePointsForPlayedTime(string type, ulong playerId)
{
var time = PlaytimeTracker_GetPlayedTime(type, playerId);
string stringVal = null;
var points = GetPointsWithString(playerId, out stringVal);
double oldTime = 0;
if (stringVal != null)
oldTime = double.Parse(stringVal);
if (time <= oldTime)
{
return;
}
stringVal = time.ToString();
if (time - oldTime < m_configuration.EveryXSeconds)
{
return;
}
int totalPoints = (int)((time / (double)m_configuration.EveryXSeconds) * (double)m_configuration.PointsEveryXSeconds);
if (totalPoints < points)
{
return;
}
var diff = totalPoints - points;
if (diff > 0)
{
GiveVotePointsWithString(totalPoints, playerId, time.ToString());
}
}
public int GetPointsWithString(ulong userId, out string stringVal)
{
stringVal = null;
if (CustomRewards == null)
{
Puts("Error : CustomRewards plugin not loaded !!");
return 0;
}
string[] result = CustomRewards.Call<string[]>("Get_Owner_WithString_API", m_configuration.CustomRewardsType, nameof(PlaytimeReward), userId);
if (result == null)
return 0;
int points = int.Parse(result[0]);
stringVal = result[1];
return points;
}
public void GiveVotePointsWithString(int points, ulong userId, string objString)
{
if (CustomRewards == null)
{
Puts("Error : CustomRewards plugin not loaded !!");
return;
}
CustomRewards.Call("Give_Owner_WithString_API", m_configuration.CustomRewardsType, nameof(PlaytimeReward), userId, points, objString);
}
private void RefreshPoints(ulong playerId)
{
if (PlaytimeTracker != null)
{
PlaytimeTracker_TryGivePointsForPlayedTime(m_configuration.CustomRewardsType, playerId);
}
}
#region CustomRewards Hooks
private void CustomRewards_OnBeforeShowUI(string type, ulong playerId)
{
if (type != m_configuration.CustomRewardsType)
return;
RefreshPoints(playerId);
}
#endregion
public class Configuration
{
public string CustomRewardsType = $"{nameof(PlaytimeReward)}";
[JsonProperty("Count Afk Time ?")]
public bool CheckAfkTime = true;
[JsonProperty("Added points")]
public int PointsEveryXSeconds = 1;
[JsonProperty("Add point every (in seconds)")]
public int EveryXSeconds = 3600;
public string ToJson() => JsonConvert.SerializeObject(this);
public Dictionary<string, object> ToDictionary() => JsonConvert.DeserializeObject<Dictionary<string, object>>(ToJson());
}
protected override void LoadDefaultConfig() => m_configuration = new Configuration();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
var config = Config.ReadObject<Configuration>();
if (config == null)
{
throw new JsonException();
}
m_configuration = config;
}
catch (Exception ex)
{
Puts($"Configuration file {Name}.json is invalid; using defaults error : {ex.Message}");
LoadDefaultConfig();
}
}
protected override void SaveConfig()
{
Puts($"Configuration changes saved to {Name}.json");
Config.WriteObject(m_configuration, true);
}
}
}