/Aurora/Modules/Avatar/Currency/Config.cs

https://bitbucket.org/VirtualReality/software-testing · C# · 273 lines · 235 code · 31 blank · 7 comment · 30 complexity · 212396736390c51277612ff73c43c6f9 MD5 · raw file

  1. using Aurora.Framework;
  2. using Aurora.Framework.ConsoleFramework;
  3. using Aurora.Framework.Modules;
  4. using Nini.Config;
  5. using OpenMetaverse;
  6. using OpenMetaverse.StructuredData;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. namespace Simple.Currency
  11. {
  12. public class SimpleCurrencyConfig : IDataTransferable
  13. {
  14. #region declarations
  15. private uint m_priceUpload = 0;
  16. private uint m_priceGroupCreate = 0;
  17. private int m_stipend = 0;
  18. private string m_upgradeMembershipUri = "";
  19. private string m_errorURI = "";
  20. private bool m_CanBuyCurrencyInworld = true;
  21. private bool m_GiveStipends = false;
  22. private string m_stipendsEveryType = "month";
  23. private bool m_stipendsPremiumOnly = false;
  24. private int m_StipendsEvery = 1;
  25. private uint m_clientPort = 8002;
  26. #endregion
  27. #region functions
  28. public SimpleCurrencyConfig(IConfig economyConfig)
  29. {
  30. foreach (PropertyInfo propertyInfo in GetType().GetProperties())
  31. {
  32. try
  33. {
  34. if (propertyInfo.PropertyType.IsAssignableFrom(typeof (float)))
  35. propertyInfo.SetValue(this,
  36. economyConfig.GetFloat(propertyInfo.Name,
  37. float.Parse(
  38. propertyInfo.GetValue(this, new object[0])
  39. .ToString())), new object[0]);
  40. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (int)))
  41. propertyInfo.SetValue(this,
  42. economyConfig.GetInt(propertyInfo.Name,
  43. int.Parse(
  44. propertyInfo.GetValue(this, new object[0])
  45. .ToString())), new object[0]);
  46. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (bool)))
  47. propertyInfo.SetValue(this,
  48. economyConfig.GetBoolean(propertyInfo.Name,
  49. bool.Parse(
  50. propertyInfo.GetValue(this, new object[0])
  51. .ToString())), new object[0]);
  52. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (string)))
  53. propertyInfo.SetValue(this,
  54. economyConfig.GetString(propertyInfo.Name,
  55. propertyInfo.GetValue(this, new object[0])
  56. .ToString()), new object[0]);
  57. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (UUID)))
  58. propertyInfo.SetValue(this,
  59. new UUID(economyConfig.GetString(propertyInfo.Name,
  60. propertyInfo.GetValue(this, new object[0])
  61. .ToString())), new object[0]);
  62. }
  63. catch (Exception)
  64. {
  65. MainConsole.Instance.Warn("[SimpleCurrency]: Exception reading economy config: " + propertyInfo.Name);
  66. }
  67. }
  68. }
  69. public SimpleCurrencyConfig()
  70. {
  71. }
  72. public SimpleCurrencyConfig(OSDMap values)
  73. {
  74. FromOSD(values);
  75. }
  76. public override OSDMap ToOSD()
  77. {
  78. OSDMap returnvalue = new OSDMap();
  79. foreach (PropertyInfo propertyInfo in GetType().GetProperties())
  80. {
  81. try
  82. {
  83. if (propertyInfo.PropertyType.IsAssignableFrom(typeof (float)))
  84. returnvalue.Add(propertyInfo.Name, (float) propertyInfo.GetValue(this, new object[0]));
  85. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (int)))
  86. returnvalue.Add(propertyInfo.Name, (int) propertyInfo.GetValue(this, new object[0]));
  87. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (bool)))
  88. returnvalue.Add(propertyInfo.Name, (bool) propertyInfo.GetValue(this, new object[0]));
  89. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (string)))
  90. returnvalue.Add(propertyInfo.Name, (string) propertyInfo.GetValue(this, new object[0]));
  91. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (UUID)))
  92. returnvalue.Add(propertyInfo.Name, (UUID) propertyInfo.GetValue(this, new object[0]));
  93. }
  94. catch (Exception ex)
  95. {
  96. MainConsole.Instance.Warn("[SimpleCurrency]: Exception toOSD() config: " + ex.ToString());
  97. }
  98. }
  99. return returnvalue;
  100. }
  101. public override sealed void FromOSD(OSDMap values)
  102. {
  103. foreach (PropertyInfo propertyInfo in GetType().GetProperties())
  104. {
  105. if (values.ContainsKey(propertyInfo.Name))
  106. {
  107. try
  108. {
  109. if (propertyInfo.PropertyType.IsAssignableFrom(typeof (float)))
  110. propertyInfo.SetValue(this, float.Parse(values[propertyInfo.Name].AsString()), new object[0]);
  111. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (int)))
  112. propertyInfo.SetValue(this, values[propertyInfo.Name].AsInteger(), new object[0]);
  113. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (bool)))
  114. propertyInfo.SetValue(this, values[propertyInfo.Name].AsBoolean(), new object[0]);
  115. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (string)))
  116. propertyInfo.SetValue(this, values[propertyInfo.Name].AsString(), new object[0]);
  117. else if (propertyInfo.PropertyType.IsAssignableFrom(typeof (UUID)))
  118. propertyInfo.SetValue(this, values[propertyInfo.Name].AsUUID(), new object[0]);
  119. }
  120. catch (Exception ex)
  121. {
  122. MainConsole.Instance.Warn("[SimpleCurrency]: Exception reading fromOSD() config: " +
  123. ex.ToString());
  124. }
  125. }
  126. }
  127. }
  128. #endregion
  129. #region properties
  130. public string ErrorURI
  131. {
  132. get { return m_errorURI; }
  133. set { m_errorURI = value; }
  134. }
  135. public string UpgradeMembershipUri
  136. {
  137. get { return m_upgradeMembershipUri; }
  138. set { m_upgradeMembershipUri = value; }
  139. }
  140. public int Stipend
  141. {
  142. get { return m_stipend; }
  143. set { m_stipend = value; }
  144. }
  145. public bool GiveStipends
  146. {
  147. get { return m_GiveStipends; }
  148. set { m_GiveStipends = value; }
  149. }
  150. public string StipendsEveryType
  151. {
  152. get { return m_stipendsEveryType; }
  153. set { m_stipendsEveryType = value; }
  154. }
  155. public int StipendsEvery
  156. {
  157. get { return m_StipendsEvery; }
  158. set { m_StipendsEvery = value; }
  159. }
  160. public int PriceGroupCreate
  161. {
  162. get { return (int) m_priceGroupCreate; }
  163. set { m_priceGroupCreate = (uint) value; }
  164. }
  165. public int PriceUpload
  166. {
  167. get { return (int) m_priceUpload; }
  168. set { m_priceUpload = (uint) value; }
  169. }
  170. public bool StipendsPremiumOnly
  171. {
  172. get { return m_stipendsPremiumOnly; }
  173. set { m_stipendsPremiumOnly = value; }
  174. }
  175. public int ClientPort
  176. {
  177. get { return (int) m_clientPort; }
  178. set { m_clientPort = (uint) value; }
  179. }
  180. public bool CanBuyCurrencyInworld
  181. {
  182. get { return m_CanBuyCurrencyInworld; }
  183. set { m_CanBuyCurrencyInworld = value; }
  184. }
  185. #endregion
  186. }
  187. public class UserCurrency : IDataTransferable
  188. {
  189. public UUID PrincipalID;
  190. public uint Amount;
  191. public uint LandInUse;
  192. public uint Tier;
  193. public bool IsGroup;
  194. public uint StipendsBalance;
  195. /// <summary>
  196. /// </summary>
  197. /// <param name="osdMap"></param>
  198. public UserCurrency(OSDMap osdMap)
  199. {
  200. if (osdMap != null)
  201. FromOSD(osdMap);
  202. }
  203. public UserCurrency()
  204. {
  205. }
  206. /// <summary></summary>
  207. /// <param name="osdMap"></param>
  208. public override sealed void FromOSD(OSDMap osdMap)
  209. {
  210. UUID.TryParse(osdMap["PrincipalID"].AsString(), out PrincipalID);
  211. uint.TryParse(osdMap["Amount"].AsString(), out Amount);
  212. uint.TryParse(osdMap["LandInUse"].AsString(), out LandInUse);
  213. uint.TryParse(osdMap["Tier"].AsString(), out Tier);
  214. bool.TryParse(osdMap["IsGroup"].AsString(), out IsGroup);
  215. uint.TryParse(osdMap["StipendsBalance"].AsString(), out StipendsBalance);
  216. }
  217. public bool FromArray(List<string> queryResults)
  218. {
  219. return UUID.TryParse(queryResults[0], out PrincipalID) &&
  220. uint.TryParse(queryResults[1], out Amount) &&
  221. uint.TryParse(queryResults[2], out LandInUse) &&
  222. uint.TryParse(queryResults[3], out Tier) &&
  223. bool.TryParse(queryResults[4], out IsGroup) &&
  224. uint.TryParse(queryResults[5], out StipendsBalance);
  225. }
  226. /// <summary></summary>
  227. /// <returns></returns>
  228. public override OSDMap ToOSD()
  229. {
  230. return
  231. new OSDMap
  232. {
  233. {"PrincipalID", PrincipalID},
  234. {"Amount", Amount},
  235. {"LandInUse", LandInUse},
  236. {"Tier", Tier},
  237. {"IsGroup", IsGroup},
  238. {"StipendsBalance", StipendsBalance}
  239. };
  240. }
  241. }
  242. }