-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexplevel.csx
135 lines (118 loc) · 4.15 KB
/
explevel.csx
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
public static class ExplevelTracker
{
public static int Level { get; private set; }
public static int CurrentExp { get; private set; }
public static int NextLevelExp { get; private set; }
public static void Refresh()
{
UO.Log("Refreshing explevel info...");
UO.Say(".explevel");
var gump = UO.WaitForGump(false);
Parse(gump);
UO.GumpResponse().Cancel();
}
private const string expGainMessageSuffix = " zkusenosti";
private const string expGainMessagePrefix = "Ziskal jsi ";
private static readonly GumpTypeId explevelGumpId = (GumpTypeId)0x96000553;
public static void Enable()
{
UO.CommandHandler.Invoke("explevel-run");
}
public static void Disable()
{
UO.CommandHandler.Terminate("explevel-run");
}
public static void Run()
{
var journal = UO.CreateEventJournal();
journal
.When<SpeechReceivedEvent>(
e => e.Speech.Message.StartsWith(expGainMessagePrefix),
e => ParseExpGainMessage(e.Speech.Message))
.When<GumpReceivedEvent>(
e => e.Gump.GumpTypeId.Equals(explevelGumpId),
e => Parse(e.Gump))
.Incomming();
}
// Example: Ziskal jsi 44 zkusenosti.
public static void ParseExpGainMessage(string message)
{
if (message.StartsWith(expGainMessagePrefix))
{
if (Level == 0 && NextLevelExp == 0 && CurrentExp == 0)
{
Refresh();
return;
}
var expGainMessageParts = message.Split(' ');
if (expGainMessageParts.Length != 4)
{
UO.Log($"Not a exp gain message: {message}");
return;
}
int expGain = int.Parse(expGainMessageParts[2]);
if (expGain + CurrentExp > NextLevelExp)
{
Refresh();
}
else
{
CurrentExp += expGain;
}
}
else
{
UO.Log($"Message is not an exp gain message.");
UO.Log(message);
}
}
public static void Parse(Gump gump)
{
if (gump == null && !gump.GumpTypeId.Equals(explevelGumpId))
{
UO.Log("The gump is not .explevel gump");
return;
}
if (gump.TextLines.Length < 2)
{
UO.Log($"The gump is incorrect. Expecting 2 or more text lines but the gump has {gump.TextLines.Length}");
return;
}
string experienceValueText = gump.TextLines[2];
if (string.IsNullOrEmpty(experienceValueText) || !experienceValueText.Contains("/"))
{
UO.Log($"The gump is incorrect. Expecting exp value on line 2, but {experienceValueText} found.");
return;
}
string[] experienceValueParts = experienceValueText.Split('/');
if (experienceValueParts.Length != 2)
{
UO.Log($"Cannot parse experience text is ${experienceValueText}");
return;
}
if (!int.TryParse(experienceValueParts[0].Trim(), out int currentExp))
{
UO.Log($"Cannot parse experience text is ${experienceValueText}");
return;
}
CurrentExp = currentExp;
if (!int.TryParse(experienceValueParts[1].Trim(), out int nextLevelExp))
{
UO.Log($"Cannot parse experience text is ${experienceValueText}");
return;
}
NextLevelExp = nextLevelExp;
string levelValueText = gump.TextLines[4];
if (!int.TryParse(levelValueText, out int level))
{
UO.Log($"Cannot parse level text is ${levelValueText}");
return;
}
Level = level;
}
}
UO.RegisterBackgroundCommand("explevel-run", ExplevelTracker.Run);
UO.RegisterCommand("explevel-enable", ExplevelTracker.Enable);
UO.RegisterCommand("explevel-disable", ExplevelTracker.Disable);
UO.RegisterCommand(",explevel", () => UO.Say(".explevel"));
UO.RegisterCommand(",explevel-refresh", ExplevelTracker.Refresh);