PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 0ms app.codeStats 0ms

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