-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnection.cs
More file actions
130 lines (112 loc) · 5.14 KB
/
Copy pathConnection.cs
File metadata and controls
130 lines (112 loc) · 5.14 KB
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
using System;
using Archipelago.MultiClient.Net;
using Archipelago.MultiClient.Net.Enums;
using Archipelago.MultiClient.Net.MessageLog.Messages;
using Archipelago.MultiClient.Net.Models;
using BepInEx.Logging;
using Newtonsoft.Json.Linq;
public static class ConnectionHandler {
public static ArchipelagoSession Session;
public static LoginSuccessful Success;
public static ManualLogSource Logger;
public static int GravityReductions = 2;
public static int GoalHeightReductions = 0;
public static int WindTraps = 0;
public static void Connect(ManualLogSource logger, Action<bool> updateGravity) {
Logger = logger;
JArray list = JArray.Parse(ConfigHandler.ConnectionsList.Value);
int active = ConfigHandler.ActiveSlot.Value;
if (active < -1) {
Logger.LogWarning($"Illegal ActiveSlot value: {active}");
ProcessOfflineList();
return;
} else if (active >= list.Count) {
Logger.LogWarning($"ActiveSlot value out of range: {active}");
ProcessOfflineList();
return;
} else if (active == -1) {
Logger.LogInfo("Connecting disabled as per config");
ProcessOfflineList();
return;
}
JToken activeList = list[active];
string activeAddressPort = activeList.Value<string>("addressPort") ?? "localhost:38281";
string activeSlot = activeList.Value<string>("slot") ?? "Player1";
string activePassword = activeList.Value<string>("password") ?? "";
Session = ArchipelagoSessionFactory.CreateSession(activeAddressPort);
LoginResult result;
try {
result = Session.TryConnectAndLogin(
"Getting Over It", activeSlot, ItemsHandlingFlags.AllItems, password: activePassword
);
} catch (Exception e) {
result = new LoginFailure(e.GetBaseException().Message);
}
if (!result.Successful) {
LoginFailure failure = (LoginFailure)result;
string errorMessage = $"Failed to Connect to {activeAddressPort} as {activeSlot}:";
foreach (string error in failure.Errors) {
errorMessage += $"\n {error}";
}
foreach (ConnectionRefusedError error in failure.ErrorCodes) {
errorMessage += $"\n {error}";
}
logger.LogError(errorMessage);
} else {
Success = (LoginSuccessful)result;
logger.LogInfo("Connection successful");
logger.LogInfo("Seed: "+Session.RoomState.Seed);
ProcessReceived();
Session.Items.ItemReceived += (receivedItemsHelper) => {
try {
logger.LogInfo($"Received {receivedItemsHelper.DequeueItem().ItemName}");
ProcessReceived();
updateGravity(GravityControlPatch.creditsUp());
} catch (Exception e) {
logger.LogError("Exception from receiving item: "+e.GetBaseException().Message);
}
};
Session.MessageLog.OnMessageReceived += (LogMessage message) => {
logger.LogInfo(message.ToString());
};
}
}
public static void ProcessOfflineList() {
Logger.LogInfo("Processing offline inventory...");
JObject inv = JObject.Parse(ConfigHandler.OfflineItems.Value);
GravityReductions = inv.Value<int?>("Gravity Reduction") ?? 2;
GoalHeightReductions = inv.Value<int?>("Goal Height Reduction") ?? 0;
WindTraps = inv.Value<int?>("Wind Trap") ?? 0;
if (GravityReductions > 4) GravityReductions = 4;
if (GoalHeightReductions > 6) GoalHeightReductions = 6;
if (WindTraps > 6) WindTraps = 6;
Logger.LogInfo(
$"[OfflineList] Gravity Reductions: {GravityReductions}, Goal Height Reductions: {GoalHeightReductions}, Wind Traps: {WindTraps}"
);
}
public static void ProcessReceived() {
GravityReductions = 0;
GoalHeightReductions = 0;
WindTraps = 0;
foreach (ItemInfo item in Session.Items.AllItemsReceived) {
if (item.ItemName.Equals("Gravity Reduction")) GravityReductions++;
else if (item.ItemName.Equals("Goal Height Reduction")) GoalHeightReductions++;
else if (item.ItemName.Equals("Wind Trap")) WindTraps++;
}
if (GravityReductions > 4) GravityReductions = 4;
if (GoalHeightReductions > 6) GoalHeightReductions = 6;
if (WindTraps > 6) WindTraps = 6;
Logger.LogInfo(
$"[Received] Gravity Reductions: {GravityReductions}, Goal Height Reductions: {GoalHeightReductions}, Wind Traps: {WindTraps}"
);
}
public static void CheckLocation(string name) {
if (Success == null) return;
CheckLocation(Session.Locations.GetLocationIdFromName("Getting Over It", name), name);
}
public static void CheckLocation(long id, string name = null) {
if (Success == null) return;
Logger.LogInfo($"Checking location {(name == null ? id : name)}");
Session.Locations.CompleteLocationChecks([id]);
}
}