cfclient/F6/MetaTable.cs
2024-05-18 16:20:42 +08:00

118 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
namespace F6
{
public delegate bool TraverseCb<T>(T ele);
public interface IMetaTable
{
bool IsNoLoad()
{
return false;
}
void Load()
{
}
void PreInit1()
{
}
void ElementsInit(int round)
{
}
void PostInit1()
{
}
}
public class RawMetaTable<T>: IMetaTable
{
protected string fileName;
protected string primKey;
protected bool noLoad;
protected List<T> rawList = new List<T>();
public void Traverse(TraverseCb<T> cb)
{
foreach(var item in this.rawList)
{
if (!cb(item))
{
break;
}
}
}
public T RandElement()
{
if (this.rawList.Count > 0)
{
return this.rawList[new Random().Next(0, this.rawList.Count)];
}
return default(T);
}
public virtual void Load()
{
}
protected virtual void LoadPost()
{
}
}
public class IdMetaTable<T>: RawMetaTable<T>
{
protected Dictionary<Int64, T> idHash = new Dictionary<Int64, T> ();
public T GetById(Int64 id)
{
T v = default(T);
this.idHash.TryGetValue(id, out v);
return v;
}
protected override void LoadPost()
{
base.LoadPost();
}
}
public class NameMetaTable<T>: RawMetaTable<T>
{
protected Dictionary<string, T> nameHash = new Dictionary<string, T>();
public T GetByName(string key)
{
T v = default(T);
this.nameHash.TryGetValue(key, out v);
return v;
}
protected override void LoadPost()
{
base.LoadPost();
}
}
}