forked from vandernorth/NecroBot.GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventListener.cs
297 lines (260 loc) · 14.3 KB
/
EventListener.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using Microsoft.CSharp.RuntimeBinder;
using PoGo.NecroBot.Logic.Common;
using PoGo.NecroBot.Logic.Event;
using PoGo.NecroBot.Logic.Logging;
using PoGo.NecroBot.Logic.State;
using POGOProtos.Enums;
using POGOProtos.Inventory.Item;
using POGOProtos.Networking.Responses;
using System;
using System.Globalization;
namespace PoGo.NecroBot.GUI
{
public class EventListener
{
private PokeGUI gui;
public EventListener(PokeGUI gui)
{
this.gui = gui;
}
public void HandleEvent(ProfileEvent evt, ISession session)
{
Logger.Write(session.Translation.GetTranslation(TranslationString.EventProfileLogin,
evt.Profile.PlayerData.Username ?? ""));
}
public void HandleEvent(ErrorEvent evt, ISession session)
{
Logger.Write(evt.ToString(), LogLevel.Error);
}
public void HandleEvent(NoticeEvent evt, ISession session)
{
Logger.Write(evt.ToString());
}
public void HandleEvent(WarnEvent evt, ISession session)
{
Logger.Write(evt.ToString(), LogLevel.Warning);
if (evt.RequireInput)
{
Logger.Write(session.Translation.GetTranslation(TranslationString.RequireInputText));
Console.ReadKey();
}
}
public void HandleEvent(UseLuckyEggEvent evt, ISession session)
{
Logger.Write(session.Translation.GetTranslation(TranslationString.EventUsedLuckyEgg, evt.Count), LogLevel.Egg);
}
public void HandleEvent(PokemonEvolveEvent evt, ISession session)
{
Logger.Write(evt.Result == EvolvePokemonResponse.Types.Result.Success
? session.Translation.GetTranslation(TranslationString.EventPokemonEvolvedSuccess, evt.Id, evt.Exp)
: session.Translation.GetTranslation(TranslationString.EventPokemonEvolvedFailed, evt.Id, evt.Result,
evt.Id),
LogLevel.Evolve);
}
public void HandleEvent(TransferPokemonEvent evt, ISession session)
{
Logger.Write(
session.Translation.GetTranslation(TranslationString.EventPokemonTransferred, evt.Id.ToString().PadRight(20), evt.Cp,
evt.Perfection.ToString("0.00"), evt.BestCp, evt.BestPerfection.ToString("0.00"), evt.FamilyCandies),
LogLevel.Transfer);
}
public void HandleEvent(ItemRecycledEvent evt, ISession session)
{
Logger.Write(session.Translation.GetTranslation(TranslationString.EventItemRecycled, evt.Count, evt.Id),
LogLevel.Recycling);
}
public void HandleEvent(EggIncubatorStatusEvent evt, ISession session)
{
this.gui.updateIncubator(evt.IncubatorId, evt.KmWalked.ToString("F3"));
Logger.Write(evt.WasAddedNow
? session.Translation.GetTranslation(TranslationString.IncubatorPuttingEgg, evt.KmRemaining)
: session.Translation.GetTranslation(TranslationString.IncubatorStatusUpdate, evt.KmRemaining), LogLevel.Egg);
}
public void HandleEvent(EggHatchedEvent evt, ISession session)
{
Logger.Write(session.Translation.GetTranslation(TranslationString.IncubatorEggHatched, evt.PokemonId.ToString()), LogLevel.Egg);
}
public void HandleEvent(FortUsedEvent fortUsedEvent, ISession session)
{
var itemString = fortUsedEvent.InventoryFull
? session.Translation.GetTranslation(TranslationString.InvFullPokestopLooting)
: fortUsedEvent.Items;
Logger.Write(String.Format("Name: {0} \n - XP: {1} \n - Gems: {2} \n - Items: {3}", fortUsedEvent.Name, fortUsedEvent.Exp, fortUsedEvent.Gems, itemString), LogLevel.Pokestop);
this.gui.addPokestopVisited(new string[] { DateTime.Now.ToString("HH:mm:ss"),fortUsedEvent.Name, fortUsedEvent.Exp.ToString(), fortUsedEvent.Gems.ToString(), itemString, fortUsedEvent.Latitude.ToString("0.00000"), fortUsedEvent.Longitude.ToString("0.00000") });
}
public void HandleEvent(FortFailedEvent evt, ISession session)
{
Logger.Write(session.Translation.GetTranslation(TranslationString.EventFortFailed, evt.Name, evt.Try, evt.Max),
LogLevel.Pokestop, ConsoleColor.DarkRed);
}
public void HandleEvent(FortTargetEvent fortTargetEvent, ISession session)
{
int intTimeForArrival = (int)(fortTargetEvent.Distance / (session.LogicSettings.WalkingSpeedInKilometerPerHour * 0.2777));
this.gui.UIThread(() => this.gui.labelNext.TextLine1 = $"Next stop: {fortTargetEvent.Name} - {Math.Round(fortTargetEvent.Distance)}m - {intTimeForArrival} seconds");
Logger.Write($"Next stop: {fortTargetEvent.Name} - {Math.Round(fortTargetEvent.Distance)}m - {intTimeForArrival} seconds", LogLevel.Pokestop);
}
private void HandleEvent(PokemonCaptureEvent pokemonCaptureEvent, ISession session)
{
Func<ItemId, string> returnRealBallName = a =>
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (a)
{
case ItemId.ItemPokeBall:
return session.Translation.GetTranslation(TranslationString.Pokeball);
case ItemId.ItemGreatBall:
return session.Translation.GetTranslation(TranslationString.GreatPokeball);
case ItemId.ItemUltraBall:
return session.Translation.GetTranslation(TranslationString.UltraPokeball);
case ItemId.ItemMasterBall:
return session.Translation.GetTranslation(TranslationString.MasterPokeball);
default:
return session.Translation.GetTranslation(TranslationString.CommonWordUnknown);
}
};
var catchType = pokemonCaptureEvent.CatchType;
string strStatus;
switch (pokemonCaptureEvent.Status)
{
case CatchPokemonResponse.Types.CatchStatus.CatchError:
strStatus = session.Translation.GetTranslation(TranslationString.CatchStatusError);
break;
case CatchPokemonResponse.Types.CatchStatus.CatchEscape:
strStatus = session.Translation.GetTranslation(TranslationString.CatchStatusEscape);
break;
case CatchPokemonResponse.Types.CatchStatus.CatchFlee:
strStatus = session.Translation.GetTranslation(TranslationString.CatchStatusFlee);
break;
case CatchPokemonResponse.Types.CatchStatus.CatchMissed:
strStatus = session.Translation.GetTranslation(TranslationString.CatchStatusMissed);
break;
case CatchPokemonResponse.Types.CatchStatus.CatchSuccess:
strStatus = session.Translation.GetTranslation(TranslationString.CatchStatusSuccess);
break;
default:
strStatus = pokemonCaptureEvent.Status.ToString();
break;
}
var catchStatus = pokemonCaptureEvent.Attempt > 1
? session.Translation.GetTranslation(TranslationString.CatchStatusAttempt, strStatus, pokemonCaptureEvent.Attempt)
: session.Translation.GetTranslation(TranslationString.CatchStatus, strStatus);
var familyCandies = pokemonCaptureEvent.FamilyCandies > 0
? pokemonCaptureEvent.FamilyCandies.ToString()
: "";
string message;
if (pokemonCaptureEvent.Status == CatchPokemonResponse.Types.CatchStatus.CatchSuccess)
{
message = String.Format("({0}) | ({1}) {2} \n - Lvl: {3}\n - CP: ({4}/{5})\n - IV: {6}%\n - Chance: {7}%\n - from {8}m dist with a {9} ({10} left).\n - {11} EXP earned\n - {12}\n - lat: {13}, long: {14}", catchStatus, catchType, session.Translation.GetPokemonTranslation(pokemonCaptureEvent.Id),
pokemonCaptureEvent.Level, pokemonCaptureEvent.Cp, pokemonCaptureEvent.MaxCp, pokemonCaptureEvent.Perfection.ToString("0.00"), pokemonCaptureEvent.Probability,
pokemonCaptureEvent.Distance.ToString("F2"),
returnRealBallName(pokemonCaptureEvent.Pokeball), pokemonCaptureEvent.BallAmount,
pokemonCaptureEvent.Exp, familyCandies, pokemonCaptureEvent.Latitude.ToString("0.000000"), pokemonCaptureEvent.Longitude.ToString("0.000000"));
Logger.Write(message, LogLevel.Caught);
this.gui.addPokemonCaught(new string[] {
$"{DateTime.Now.ToString("HH:mm:ss")}",
catchStatus,
session.Translation.GetPokemonTranslation(pokemonCaptureEvent.Id),
pokemonCaptureEvent.Level.ToString(),
$"{pokemonCaptureEvent.Cp} / {pokemonCaptureEvent.MaxCp}",
pokemonCaptureEvent.Perfection.ToString("0.00") + "%",
pokemonCaptureEvent.Probability.ToString("0.00") + "%",
pokemonCaptureEvent.Distance.ToString("F2") + "m",
catchType,
$"{returnRealBallName(pokemonCaptureEvent.Pokeball)} ({pokemonCaptureEvent.BallAmount} left)",
pokemonCaptureEvent.Exp.ToString() + "xp",
familyCandies});
this.gui.sendEvent("Catch", "success", pokemonCaptureEvent.Id.ToString(), pokemonCaptureEvent.Cp);
}
else
{
message = session.Translation.GetTranslation(TranslationString.EventPokemonCaptureFailed, catchStatus, catchType, session.Translation.GetPokemonTranslation(pokemonCaptureEvent.Id),
pokemonCaptureEvent.Level, pokemonCaptureEvent.Cp, pokemonCaptureEvent.MaxCp, pokemonCaptureEvent.Perfection.ToString("0.00"), pokemonCaptureEvent.Probability,
pokemonCaptureEvent.Distance.ToString("F2"),
returnRealBallName(pokemonCaptureEvent.Pokeball), pokemonCaptureEvent.BallAmount,
pokemonCaptureEvent.Latitude.ToString("0.000000"), pokemonCaptureEvent.Longitude.ToString("0.000000"));
Logger.Write(message, LogLevel.Flee);
}
}
private void HandleEvent(NoPokeballEvent noPokeballEvent, ISession session)
{
Logger.Write(session.Translation.GetTranslation(TranslationString.EventNoPokeballs, noPokeballEvent.Id, noPokeballEvent.Cp),
LogLevel.Caught);
}
private void HandleEvent(UseBerryEvent useBerryEvent, ISession session)
{
string strBerry;
switch (useBerryEvent.BerryType)
{
case ItemId.ItemRazzBerry:
strBerry = session.Translation.GetTranslation(TranslationString.ItemRazzBerry);
break;
default:
strBerry = useBerryEvent.BerryType.ToString();
break;
}
Logger.Write(session.Translation.GetTranslation(TranslationString.EventUseBerry, strBerry, useBerryEvent.Count),
LogLevel.Berry);
}
private void HandleEvent(SnipeEvent snipeEvent, ISession session)
{
Logger.Write(snipeEvent.ToString(), LogLevel.Sniper);
}
private void HandleEvent(SnipeScanEvent snipeScanEvent, ISession session)
{
Logger.Write(snipeScanEvent.PokemonId == PokemonId.Missingno
? ((snipeScanEvent.Source != null) ? "(" + snipeScanEvent.Source + ") " : null) + session.Translation.GetTranslation(TranslationString.SnipeScan,
$"{snipeScanEvent.Bounds.Latitude},{snipeScanEvent.Bounds.Longitude}")
: ((snipeScanEvent.Source != null) ? "(" + snipeScanEvent.Source + ") " : null) + session.Translation.GetTranslation(TranslationString.SnipeScanEx, session.Translation.GetPokemonTranslation(snipeScanEvent.PokemonId),
snipeScanEvent.Iv > 0 ? snipeScanEvent.Iv.ToString(CultureInfo.InvariantCulture) : session.Translation.GetTranslation(TranslationString.CommonWordUnknown),
$"{snipeScanEvent.Bounds.Latitude},{snipeScanEvent.Bounds.Longitude}"), LogLevel.Sniper);
this.gui.setSniper(snipeScanEvent.Bounds.Latitude.ToString(), snipeScanEvent.Bounds.Longitude.ToString());
}
private void HandleEvent(EvolveCountEvent evolveCountEvent, ISession session)
{
this.gui.UIThread(() => this.gui.labelEvolvable.TextLine2 = evolveCountEvent.Evolves.ToString());
}
private void HandleEvent(UpdateEvent updateEvent, ISession session)
{
Logger.Write(updateEvent.ToString(), LogLevel.Update);
}
private void HandleEvent(SnipeModeEvent snipeModeEvent, ISession session)
{
string onOff = snipeModeEvent.Active ? "On" : "Off";
Logger.Write($"Sniper {onOff}", LogLevel.Sniper);
}
/*private void HandleEvent(DisplayHighestsPokemonEvent displayEvent, ISession session)
{
}*/
private void HandleEvent(UpdatePositionEvent posEvent, ISession session)
{
Logger.Write($"Position changed. lat={posEvent.Latitude.ToString("0.00000")}, lng={posEvent.Longitude.ToString("0.00000")}", LogLevel.Debug);
}
private void HandleEvent(PokeStopListEvent polEvent, ISession session)
{
Logger.Write($"PokeStopList has {polEvent.Forts.Count} items", LogLevel.Debug);
}
public void Listen(IEvent evt, ISession session)
{
dynamic eve = evt;
try
{
HandleEvent(eve, session);
}
// ReSharper disable once EmptyGeneralCatchClause
catch (RuntimeBinderException) {
Logger.Write($"No listener for event {evt.GetType().Name}", LogLevel.None);
}
catch (Exception ex)
{
try
{
Logger.Write($"Unhandeled exception ({ex.GetType().Name}) on event {evt.GetType().Name}: {ex.Message}", LogLevel.Error);
}
catch
{
//die here..
}
}
}
}
}