Files
libremetaverse/Programs/examples/TestClient/Commands/Groups/ActivateGroupCommand.cs
John Hurliman 81e6342d36 Removing LL prefix from all basic types
git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@1998 52acb1d6-8a22-11de-b505-999d5b087335
2008-07-25 05:15:05 +00:00

93 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using OpenMetaverse;
using OpenMetaverse.Packets;
using System.Text;
namespace OpenMetaverse.TestClient
{
/// <summary>
/// Changes Avatars currently active group
/// </summary>
public class ActivateGroupCommand : Command
{
ManualResetEvent GroupsEvent = new ManualResetEvent(false);
Dictionary<UUID, Group> groups = new Dictionary<UUID, Group>();
string activeGroup;
public ActivateGroupCommand(TestClient testClient)
{
Name = "activategroup";
Description = "Set a group as active. Usage: activategroup GroupName";
}
public override string Execute(string[] args, UUID fromAgentID)
{
if (args.Length < 1)
return Description;
groups.Clear();
activeGroup = string.Empty;
string groupName = String.Empty;
for (int i = 0; i < args.Length; i++)
groupName += args[i] + " ";
groupName = groupName.Trim();
GroupManager.CurrentGroupsCallback callback = new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
Client.Groups.OnCurrentGroups += callback;
Client.Groups.RequestCurrentGroups();
GroupsEvent.WaitOne(30000, false);
Client.Groups.OnCurrentGroups -= callback;
GroupsEvent.Reset();
if (groups.Count > 0)
{
foreach (Group currentGroup in groups.Values)
if (currentGroup.Name.ToLower() == groupName.ToLower())
{
NetworkManager.PacketCallback pcallback = new NetworkManager.PacketCallback(AgentDataUpdateHandler);
Client.Network.RegisterCallback(PacketType.AgentDataUpdate, pcallback);
Console.WriteLine("setting " + currentGroup.Name + " as active group");
Client.Groups.ActivateGroup(currentGroup.ID);
GroupsEvent.WaitOne(30000, false);
Client.Network.UnregisterCallback(PacketType.AgentDataUpdate, pcallback);
GroupsEvent.Reset();
/* A.Biondi
* TODO: Handle titles choosing.
*/
if (String.IsNullOrEmpty(activeGroup))
return Client.ToString() + " failed to activate the group " + groupName;
return "Active group is now " + activeGroup;
}
return Client.ToString() + " doesn't seem to be member of the group " + groupName;
}
return Client.ToString() + " doesn't seem member of any group";
}
void Groups_OnCurrentGroups(Dictionary<UUID, Group> cGroups)
{
groups = cGroups;
GroupsEvent.Set();
}
private void AgentDataUpdateHandler(Packet packet, Simulator sim)
{
AgentDataUpdatePacket p = (AgentDataUpdatePacket)packet;
if (p.AgentData.AgentID == Client.Self.AgentID)
{
activeGroup = Helpers.FieldToUTF8String(p.AgentData.GroupName) + " ( " + Helpers.FieldToUTF8String(p.AgentData.GroupTitle) + " )";
GroupsEvent.Set();
}
}
}
}