aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules
diff options
context:
space:
mode:
authorTeravus Ovares2008-02-05 05:26:18 +0000
committerTeravus Ovares2008-02-05 05:26:18 +0000
commitca5aadfbff63e03b7ca081e753204d8da040c9bb (patch)
treee2d19e01678c27cff57bc26257cec7385c3df274 /OpenSim/Region/Environment/Modules
parentAdded copyright statements. (diff)
downloadopensim-SC-ca5aadfbff63e03b7ca081e753204d8da040c9bb.zip
opensim-SC-ca5aadfbff63e03b7ca081e753204d8da040c9bb.tar.gz
opensim-SC-ca5aadfbff63e03b7ca081e753204d8da040c9bb.tar.bz2
opensim-SC-ca5aadfbff63e03b7ca081e753204d8da040c9bb.tar.xz
* Refactored the sound calls to SceneObjectPart
* Fixed a few bugs * Wrote an example module to make certain event systems more mature.
Diffstat (limited to 'OpenSim/Region/Environment/Modules')
-rw-r--r--OpenSim/Region/Environment/Modules/BetaGridLikeMoneyModule.cs324
1 files changed, 324 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/BetaGridLikeMoneyModule.cs b/OpenSim/Region/Environment/Modules/BetaGridLikeMoneyModule.cs
new file mode 100644
index 0000000..81478a1
--- /dev/null
+++ b/OpenSim/Region/Environment/Modules/BetaGridLikeMoneyModule.cs
@@ -0,0 +1,324 @@
1/*
2* Copyright (c) Contributors, http://opensimulator.org/
3* See CONTRIBUTORS.TXT for a full list of copyright holders.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above copyright
10* notice, this list of conditions and the following disclaimer in the
11* documentation and/or other materials provided with the distribution.
12* * Neither the name of the OpenSim Project nor the
13* names of its contributors may be used to endorse or promote products
14* derived from this software without specific prior written permission.
15*
16* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY
17* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*
27*/
28
29using Nini.Config;
30using System;
31using System.Collections;
32using System.Collections.Generic;
33using OpenSim.Framework;
34using OpenSim.Framework.Console;
35using OpenSim.Region.Environment.Interfaces;
36using OpenSim.Region.Environment.Scenes;
37using libsecondlife;
38using MoneyTransferArgs = OpenSim.Region.Environment.Scenes.EventManager.MoneyTransferArgs;
39
40namespace OpenSim.Region.Environment.Modules
41{
42 public class BetaGridLikeMoneyModule: IRegionModule
43 {
44
45 private LogBase m_log;
46
47 private Dictionary<ulong,Scene> m_scenel = new Dictionary<ulong,Scene>();
48
49 private IConfigSource m_gConfig;
50
51 private bool m_keepMoneyAcrossLogins = true;
52
53 private int m_minFundsBeforeRefresh = 100;
54
55 private int m_stipend = 1000;
56
57 private bool m_enabled = true;
58
59
60
61 private Dictionary<LLUUID, int> m_KnownClientFunds = new Dictionary<LLUUID, int>();
62
63
64
65 private bool gridmode = false;
66
67 public void Initialise(Scene scene, IConfigSource config)
68 {
69 m_log = MainLog.Instance;
70
71 m_gConfig = config;
72 ReadConfigAndPopulate();
73
74 if (m_enabled)
75 {
76 lock (m_scenel)
77 {
78
79 if (m_scenel.ContainsKey(scene.RegionInfo.RegionHandle))
80 {
81 m_scenel[scene.RegionInfo.RegionHandle] = scene;
82 }
83 else
84 {
85 m_scenel.Add(scene.RegionInfo.RegionHandle, scene);
86 }
87 }
88
89 scene.EventManager.OnNewClient += OnNewClient;
90 scene.EventManager.OnMoneyTransfer += MoneyTransferAction;
91 scene.EventManager.OnClientClosed += ClientClosed;
92 }
93 }
94 private void ReadConfigAndPopulate()
95 {
96 IConfig startupConfig = m_gConfig.Configs["Startup"];
97 gridmode = startupConfig.GetBoolean("gridmode", false);
98 m_enabled = (startupConfig.GetString("moneymodule", "BetaGridLikeMoneyModule") == "BetaGridLikeMoneyModule");
99
100 }
101
102 private void OnNewClient(IClientAPI client)
103 {
104 // Here we check if we're in grid mode
105 // I imagine that the 'check balance'
106 // function for the client should be here or shortly after
107
108 if (gridmode)
109 {
110 CheckExistAndRefreshFunds(client.AgentId);
111 }
112 else
113 {
114 CheckExistAndRefreshFunds(client.AgentId);
115 }
116
117 // Subscribe to Money messages
118 client.OnMoneyBalanceRequest += SendMoneyBalance;
119 client.OnLogout += ClientClosed;
120
121 }
122
123 public void ClientClosed(LLUUID AgentID)
124 {
125 lock (m_KnownClientFunds)
126 {
127 if (!m_keepMoneyAcrossLogins)
128 m_KnownClientFunds.Remove(AgentID);
129 }
130
131 }
132
133 private void MoneyTransferAction (Object osender, MoneyTransferArgs e)
134 {
135 IClientAPI sender = null;
136 IClientAPI receiver = null;
137
138 sender = LocateClientObject(e.sender);
139 if (sender != null)
140 {
141
142
143 receiver = LocateClientObject(e.reciever);
144 bool transactionresult = doMoneyTranfer(e.sender, e.reciever, e.amount);
145
146 if (e.sender != e.reciever)
147 {
148 if (sender != null)
149 {
150 sender.SendMoneyBalance(LLUUID.Random(), transactionresult, Helpers.StringToField(e.description), GetFundsForAgentID(e.sender));
151 }
152 }
153
154 if (receiver != null)
155 {
156 receiver.SendMoneyBalance(LLUUID.Random(), transactionresult, Helpers.StringToField(e.description), GetFundsForAgentID(e.reciever));
157 }
158
159
160 }
161 else
162 {
163 MainLog.Instance.Warn("MONEY", "Potential Fraud Warning, got money transfer request for avatar that isn't in this simulator - Details; Sender:" + e.sender.ToString() + " Reciver: " + e.reciever.ToString() + " Amount: " + e.amount.ToString());
164 }
165
166
167
168 }
169
170 private bool doMoneyTranfer(LLUUID Sender, LLUUID Receiver, int amount)
171 {
172 bool result = false;
173 if (amount >= 0)
174 {
175 lock (m_KnownClientFunds)
176 {
177 // If we don't know about the sender, then the sender can't
178 // actually be here and therefore this is likely fraud or outdated.
179 if (m_KnownClientFunds.ContainsKey(Sender))
180 {
181 // Does the sender have enough funds to give?
182 if (m_KnownClientFunds[Sender] >= amount)
183 {
184 // Subtract the funds from the senders account
185 m_KnownClientFunds[Sender] -= amount;
186
187 // do we know about the receiver?
188 if (!m_KnownClientFunds.ContainsKey(Receiver))
189 {
190 // Make a record for them so they get the updated balance when they login
191 CheckExistAndRefreshFunds(Receiver);
192 }
193
194 //Add the amount to the Receiver's funds
195 m_KnownClientFunds[Receiver] += amount;
196 result = true;
197 }
198 else
199 {
200 // These below are redundant to make this clearer to read
201 result = false;
202 }
203 }
204 else
205 {
206 result = false;
207 }
208 }
209 }
210 return result;
211 }
212
213 private IClientAPI LocateClientObject(LLUUID AgentID)
214 {
215 ScenePresence tPresence = null;
216 IClientAPI rclient = null;
217
218 lock (m_scenel)
219 {
220 foreach (Scene _scene in m_scenel.Values)
221 {
222 tPresence = _scene.GetScenePresence(AgentID);
223 if (tPresence != null)
224 {
225 if (!tPresence.IsChildAgent)
226 {
227 rclient = tPresence.ControllingClient;
228 }
229 }
230 if (rclient != null)
231 {
232 return rclient;
233 }
234 }
235
236 }
237 return null;
238 }
239
240 public void ClientClosed(IClientAPI client)
241 {
242 ClientClosed(client.AgentId);
243 }
244
245 public void SendMoneyBalance(IClientAPI client, LLUUID agentID, LLUUID SessionID, LLUUID TransactionID)
246 {
247 if (client.AgentId == agentID && client.SessionId == SessionID)
248 {
249
250 int returnfunds = 0;
251
252 try
253 {
254 returnfunds = GetFundsForAgentID(agentID);
255 }
256 catch (System.Exception e)
257 {
258 client.SendAlertMessage(e.Message + " ");
259 }
260
261 client.SendMoneyBalance(TransactionID, true, new byte[0], returnfunds);
262 }
263 else
264 {
265 client.SendAlertMessage("Unable to send your money balance to you!");
266 }
267 }
268
269 private void CheckExistAndRefreshFunds(LLUUID agentID)
270 {
271 lock (m_KnownClientFunds)
272 {
273 if (!m_KnownClientFunds.ContainsKey(agentID))
274 {
275 m_KnownClientFunds.Add(agentID, m_stipend);
276 }
277 else
278 {
279 if (m_KnownClientFunds[agentID] <= m_minFundsBeforeRefresh)
280 {
281 m_KnownClientFunds[agentID] = m_stipend;
282 }
283 }
284 }
285 }
286 private int GetFundsForAgentID(LLUUID AgentID)
287 {
288 int returnfunds = 0;
289 lock (m_KnownClientFunds)
290 {
291 if (m_KnownClientFunds.ContainsKey(AgentID))
292 {
293 returnfunds = m_KnownClientFunds[AgentID];
294 }
295 else
296 {
297 throw new Exception("Unable to get funds.");
298 }
299 }
300 return returnfunds;
301 }
302
303 public void PostInitialise()
304 {
305 }
306
307
308
309 public void Close()
310 {
311 }
312
313 public string Name
314 {
315 get { return "BetaGridLikeMoneyModule"; }
316 }
317
318 public bool IsSharedModule
319 {
320 get { return true; }
321 }
322 }
323
324}