Red Squirrel
No Lifer
In terms of cpu and memory, how efficient would this system be?
/*
##############################
Official Age of Valor Script
http://www.uovalor.com
Coded by Red Squirrel
redsquirrel@iceteks.com
##############################
*/
using Server;
using System;
using System.Collections.Generic;
namespace Server.Items
{
//to make an item/ability use this system simply add another entry in this list:
public enum DelayContextType
{
TestItem1,
TestItem2,
TestItem3,
Spell,
WeaponAbility,
Potion,
};
public class UseDelaySystem
{
/*
Functions:
AddContext(Mobile m, DelayContextType type, TimeSpan delay) //set a use delay
RemoveContext(Mobile m,DelayContextType type) //prematurely remove a delay (returns true if it existed already)
CheckContext(Mobile m, DelayContextType type) //check if a delay exists for specified abiliy (returns true if it exists)
*/
//----START SYSTEM----
public class DelayContextArray
{
private Dictionary<DelayContextType,DateTime> Contexts = new Dictionary<DelayContextType,DateTime>();
public DelayContextArray()
{
}
public bool Contains(DelayContextType dct)
{
DateTime context;
if(Contexts.TryGetValue(dct,out context))
{
//Console.WriteLine("context: {0}",context);//debug
if(context>DateTime.Now)return true;
else Contexts.Remove(dct);
}
//Console.WriteLine("-context: {0}",context);//debug
return false;
}
public bool Remove(DelayContextType dct)
{
DateTime context;
if(Contexts.TryGetValue(dct,out context))
{
if(context>DateTime.Now)
{
Contexts.Remove(dct);
return true;
}
}
return false;
}
public void Add(DelayContextType dct,TimeSpan delay)
{
DateTime context;
if(Contexts.TryGetValue(dct,out context))
{
//Console.WriteLine("contains {0}, removing... {1}",dct,context);//debug
Contexts.Remove(dct);
}
//Console.WriteLine("Adding...{0} {1}",dct,delay);//debug
Contexts[dct] = DateTime.Now+delay;
}
}
private static Dictionary<Mobile, DelayContextArray> UseDelayMain = new Dictionary<Mobile, DelayContextArray>();
//checks if m is still being delayed by "type" context (ex: need to wait again to use that ability type)
public static bool CheckContext(Mobile m, DelayContextType type)
{
DelayContextArray array;
if(UseDelayMain.TryGetValue(m,out array))
{
return array.Contains(type);
}
return false;
}
//removes context. Returns true if it did exist, or false if it did not
public static bool RemoveContext(Mobile m,DelayContextType type)
{
DelayContextArray array;
if(UseDelayMain.TryGetValue(m,out array))
{
if(array.Remove(type)) return true;
}
return false;
}
//tries to add a context with a delay
public static void AddContext(Mobile m, DelayContextType type,TimeSpan delay)
{
DelayContextArray array;
if(!UseDelayMain.TryGetValue(m,out array))
{
//Console.WriteLine("Cant find array, adding...");//debug
array = new DelayContextArray();
UseDelayMain[m] = array;
}
array.Add(type,delay);
}
}
}
I'd be calling up the 3 bottom functions directly at all times, kind of like this:
public override void OnDoubleClick( Mobile from )
{
if(UseDelaySystem.CheckContext(from,DelayContextType.TestItem2))
{
from.SendMessage("You must wait to use this again");
return;
}
from.SendMessage("You use UseDelayTestItem2");
UseDelaySystem.AddContext(from, DelayContextType.TestItem2,TimeSpan.FromSeconds(10));
}
In terms of scale, lets assume theres over 100k active mobiles in the program and my enum has maybe 30 entries. (currently I have 36k total, let alone active, but that grows over time)
I originally was going to use hash tables but decided to use dictionaries as I was told they're better, and I can also loop through them. (I'll be making a cleanup process to go through and delete all the entries that have expired but never been checked)
/*
##############################
Official Age of Valor Script
http://www.uovalor.com
Coded by Red Squirrel
redsquirrel@iceteks.com
##############################
*/
using Server;
using System;
using System.Collections.Generic;
namespace Server.Items
{
//to make an item/ability use this system simply add another entry in this list:
public enum DelayContextType
{
TestItem1,
TestItem2,
TestItem3,
Spell,
WeaponAbility,
Potion,
};
public class UseDelaySystem
{
/*
Functions:
AddContext(Mobile m, DelayContextType type, TimeSpan delay) //set a use delay
RemoveContext(Mobile m,DelayContextType type) //prematurely remove a delay (returns true if it existed already)
CheckContext(Mobile m, DelayContextType type) //check if a delay exists for specified abiliy (returns true if it exists)
*/
//----START SYSTEM----
public class DelayContextArray
{
private Dictionary<DelayContextType,DateTime> Contexts = new Dictionary<DelayContextType,DateTime>();
public DelayContextArray()
{
}
public bool Contains(DelayContextType dct)
{
DateTime context;
if(Contexts.TryGetValue(dct,out context))
{
//Console.WriteLine("context: {0}",context);//debug
if(context>DateTime.Now)return true;
else Contexts.Remove(dct);
}
//Console.WriteLine("-context: {0}",context);//debug
return false;
}
public bool Remove(DelayContextType dct)
{
DateTime context;
if(Contexts.TryGetValue(dct,out context))
{
if(context>DateTime.Now)
{
Contexts.Remove(dct);
return true;
}
}
return false;
}
public void Add(DelayContextType dct,TimeSpan delay)
{
DateTime context;
if(Contexts.TryGetValue(dct,out context))
{
//Console.WriteLine("contains {0}, removing... {1}",dct,context);//debug
Contexts.Remove(dct);
}
//Console.WriteLine("Adding...{0} {1}",dct,delay);//debug
Contexts[dct] = DateTime.Now+delay;
}
}
private static Dictionary<Mobile, DelayContextArray> UseDelayMain = new Dictionary<Mobile, DelayContextArray>();
//checks if m is still being delayed by "type" context (ex: need to wait again to use that ability type)
public static bool CheckContext(Mobile m, DelayContextType type)
{
DelayContextArray array;
if(UseDelayMain.TryGetValue(m,out array))
{
return array.Contains(type);
}
return false;
}
//removes context. Returns true if it did exist, or false if it did not
public static bool RemoveContext(Mobile m,DelayContextType type)
{
DelayContextArray array;
if(UseDelayMain.TryGetValue(m,out array))
{
if(array.Remove(type)) return true;
}
return false;
}
//tries to add a context with a delay
public static void AddContext(Mobile m, DelayContextType type,TimeSpan delay)
{
DelayContextArray array;
if(!UseDelayMain.TryGetValue(m,out array))
{
//Console.WriteLine("Cant find array, adding...");//debug
array = new DelayContextArray();
UseDelayMain[m] = array;
}
array.Add(type,delay);
}
}
}
I'd be calling up the 3 bottom functions directly at all times, kind of like this:
public override void OnDoubleClick( Mobile from )
{
if(UseDelaySystem.CheckContext(from,DelayContextType.TestItem2))
{
from.SendMessage("You must wait to use this again");
return;
}
from.SendMessage("You use UseDelayTestItem2");
UseDelaySystem.AddContext(from, DelayContextType.TestItem2,TimeSpan.FromSeconds(10));
}
In terms of scale, lets assume theres over 100k active mobiles in the program and my enum has maybe 30 entries. (currently I have 36k total, let alone active, but that grows over time)
I originally was going to use hash tables but decided to use dictionaries as I was told they're better, and I can also loop through them. (I'll be making a cleanup process to go through and delete all the entries that have expired but never been checked)