-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultibot.cs
157 lines (133 loc) · 4.82 KB
/
Multibot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using OpenMetaverse;
namespace Multibot
{
public class LocalAvatar
{
public required Simulator Sim;
public required Avatar Agent;
}
class Multibot
{
private GridClient client;
private LoginParams loginParams;
private List<string> admins;
public Multibot(BotCreds credentials, List<string> admins)
{
client = new GridClient();
client.Settings.MULTIPLE_SIMS = true;
loginParams = new LoginParams(client, (LoginCredential)credentials, "", "")
{
AgreeToTos = true
};
if (credentials.LoginUrl != null) { loginParams.URI = credentials.LoginUrl; }
if (credentials.LoginLocation != null) { loginParams.LoginLocation = credentials.LoginLocation; }
this.admins = admins;
client.Self.IM += OnIm;
client.Self.ChatFromSimulator += OnChatFromSimulator;
}
private void OnChatFromSimulator(object? sender, ChatEventArgs args)
{
// Ignore chat from non-admins
if (!admins.Contains(args.OwnerID.ToString()))
{
Logger.Log($"Ignoring chat from {args.OwnerID}", Helpers.LogLevel.Debug);
return;
}
if (args.Message.StartsWith("!"))
{
ParseCommand(args.Message[1..], args.OwnerID);
}
}
private void OnIm(object? sender, InstantMessageEventArgs args)
{
// Ignore IMs from non-admins
if (!admins.Contains(args.IM.FromAgentID.ToString()))
{
Logger.Log($"Ignoring chat from {args.IM.FromAgentID}", Helpers.LogLevel.Debug);
return;
}
switch (args.IM.Dialog)
{
case InstantMessageDialog.RequestTeleport:
client.Self.TeleportLureRespond(args.IM.FromAgentID, args.IM.IMSessionID, true);
break;
case InstantMessageDialog.MessageFromAgent:
if (args.IM.Message.StartsWith("!"))
{
ParseCommand(args.IM.Message[1..], args.IM.FromAgentID);
}
break;
}
}
public void ParseCommand(string cmd, UUID agentId)
{
switch (cmd)
{
case "come":
var a = FindAvatarById(agentId);
if (a == null)
{
Logger.Log($"Unable to find agent {agentId} in current or neighboring regions.", Helpers.LogLevel.Warning);
return;
}
Utils.LongToUInts(a.Sim.Handle, out var x, out var y);
client.Self.AutoPilot(a.Agent.Position.X + x, a.Agent.Position.Y + y, a.Agent.Position.Z);
break;
default:
Logger.Log($"Unknown command {cmd}", Helpers.LogLevel.Info);
break;
}
}
private LocalAvatar? FindAvatarById(UUID id)
{
foreach (var sim in client.Network.Simulators)
{
var av = sim.ObjectsAvatars.Find(a => a.ID == id);
if (av != null)
{
return new LocalAvatar { Sim = sim, Agent = av };
}
}
return null;
}
public Task Start()
{
var tsc = new TaskCompletionSource<bool>();
if (client.Network.Connected)
{
tsc.SetResult(true);
return tsc.Task;
}
EventHandler<LoginProgressEventArgs>? onLoginProgress = null;
client.Network.LoginProgress += onLoginProgress = (sender, args) => {
switch (args.Status) {
case LoginStatus.Failed:
tsc.SetResult(true);
break;
case LoginStatus.Success:
tsc.SetResult(false);
break;
}
client.Network.LoginProgress -= onLoginProgress;
};
client.Network.BeginLogin(loginParams);
return tsc.Task;
}
public Task Stop()
{
var tsc = new TaskCompletionSource();
if (!client.Network.Connected)
{
tsc.SetResult();
return tsc.Task;
}
EventHandler<LoggedOutEventArgs>? onLogout = null;
client.Network.LoggedOut += onLogout = (sender, args) => {
tsc.SetResult();
client.Network.LoggedOut -= onLogout;
};
client.Network.BeginLogout();
return tsc.Task;
}
}
}