about summary refs log tree commit diff stats
path: root/ap_state.cpp
blob: 70f183e289f0be18220728bd3e789ceee85bf1b9 (plain) (blame)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "ap_state.h"

#define HAS_STD_FILESYSTEM
#define _WEBSOCKETPP_CPP11_STRICT_
#pragma comment(lib, "crypt32")

#include <hkutil/string.h>

#include <apclient.hpp>
#include <apuuid.hpp>
#include <chrono>
#include <exception>
#include <list>
#include <memory>
#include <mutex>
#include <set>
#include <thread>
#include <tuple>

#include "game_data.h"
#include "tracker_frame.h"
#include "tracker_state.h"

constexpr int AP_MAJOR = 0;
constexpr int AP_MINOR = 4;
constexpr int AP_REVISION = 0;

constexpr int ITEM_HANDLING = 7;  // <- all

namespace {

APClient* apclient = nullptr;

bool initialized = false;

TrackerFrame* tracker_frame;

bool client_active = false;
std::mutex client_mutex;

bool connected = false;
bool has_connection_result = false;

std::map<int64_t, int> inventory;
std::set<int64_t> checked_locations;

std::map<std::tuple<int, int>, int64_t> ap_id_by_location_id;
std::map<std::string, int64_t> ap_id_by_item_name;
std::map<LingoColor, int64_t> ap_id_by_color;
std::map<int64_t, std::string> progressive_item_by_ap_id;

DoorShuffleMode door_shuffle_mode = kNO_DOORS;
bool color_shuffle = false;
bool painting_shuffle = false;
int mastery_requirement = 21;

std::map<std::string, std::string> painting_mapping;

void RefreshTracker() {
  GetTrackerState().CalculateState();
  tracker_frame->UpdateIndicators();
}

int64_t GetItemId(const std::string& item_name) {
  int64_t ap_id = apclient->get_item_id(item_name);
  if (ap_id == APClient::INVALID_NAME_ID) {
    std::cout << "Could not find AP item ID for " << item_name << std::endl;
  }

  return ap_id;
}

void DestroyClient() {
  client_active = false;
  apclient->reset();
  delete apclient;
  apclient = nullptr;
}

}  // namespace

void AP_SetTrackerFrame(TrackerFrame* arg) { tracker_frame = arg; }

void AP_Connect(std::string server, std::string player, std::string password) {
  if (!initialized) {
    std::thread([]() {
      for (;;) {
        {
          std::lock_guard client_guard(client_mutex);
          if (apclient) {
            apclient->poll();
          }
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
      }
    }).detach();

    initialized = true;
  }

  tracker_frame->SetStatusMessage("Connecting to Archipelago server....");

  {
    std::lock_guard client_guard(client_mutex);

    if (apclient) {
      DestroyClient();
    }

    apclient = new APClient(ap_get_uuid(""), "Lingo", server);
  }

  inventory.clear();
  checked_locations.clear();
  door_shuffle_mode = kNO_DOORS;
  color_shuffle = false;
  painting_shuffle = false;
  painting_mapping.clear();
  mastery_requirement = 21;

  connected = false;
  has_connection_result = false;

  apclient->set_room_info_handler([player, password]() {
    inventory.clear();

    tracker_frame->SetStatusMessage(
        "Connected to Archipelago server. Authenticating...");

    apclient->ConnectSlot(player, password, ITEM_HANDLING, {"Tracker"},
                          {AP_MAJOR, AP_MINOR, AP_REVISION});
  });

  apclient->set_location_checked_handler(
      [](const std::list<int64_t>& locations) {
        for (const int64_t location_id : locations) {
          checked_locations.insert(location_id);
          std::cout << "Location: " << location_id << std::endl;
        }

        RefreshTracker();
      });

  apclient->set_slot_disconnected_handler([]() {
    tracker_frame->SetStatusMessage(
        "Disconnected from Archipelago. Attempting to reconnect...");
  });

  apclient->set_socket_disconnected_handler([]() {
    tracker_frame->SetStatusMessage(
        "Disconnected from Archipelago. Attempting to reconnect...");
  });

  apclient->set_items_received_handler(
      [](const std::list<APClient::NetworkItem>& items) {
        for (const APClient::NetworkItem& item : items) {
          inventory[item.item]++;
          std::cout << "Item: " << item.item << std::endl;
        }

        RefreshTracker();
      });

  apclient->set_slot_connected_handler([](const nlohmann::json& slot_data) {
    tracker_frame->SetStatusMessage("Connected to Archipelago!");

    door_shuffle_mode = slot_data["shuffle_doors"].get<DoorShuffleMode>();
    color_shuffle = slot_data["shuffle_colors"].get<bool>();
    painting_shuffle = slot_data["shuffle_paintings"].get<bool>();
    mastery_requirement = slot_data["mastery_achievements"].get<int>();

    if (painting_shuffle && slot_data.contains("painting_entrance_to_exit")) {
      painting_mapping.clear();

      for (const auto& mapping_it :
           slot_data["painting_entrance_to_exit"].items()) {
        painting_mapping[mapping_it.key()] = mapping_it.value();
      }
    }

    connected = true;
    has_connection_result = true;
  });

  apclient->set_slot_refused_handler([](const std::list<std::string>& errors) {
    connected = false;
    has_connection_result = true;

    tracker_frame->SetStatusMessage("Disconnected from Archipelago.");

    std::vector<std::string> error_messages;
    error_messages.push_back("Could not connect to Archipelago.");

    for (const std::string& error : errors) {
      if (error == "InvalidSlot") {
        error_messages.push_back("Invalid player name.");
      } else if (error == "InvalidGame") {
        error_messages.push_back("The specified player is not playing Lingo.");
      } else if (error == "IncompatibleVersion") {
        error_messages.push_back(
            "The Archipelago server is not the correct version for this "
            "client.");
      } else if (error == "InvalidPassword") {
        error_messages.push_back("Incorrect password.");
      } else if (error == "InvalidItemsHandling") {
        error_messages.push_back(
            "Invalid item handling flag. This is a bug with the tracker. "
            "Please report it to the lingo-ap-tracker GitHub.");
      } else {
        error_messages.push_back("Unknown error.");
      }
    }

    std::string full_message = hatkirby::implode(error_messages, " ");

    wxMessageBox(full_message, "Connection failed", wxOK | wxICON_ERROR);
  });

  client_active = true;

  int timeout = 5000;  // 5 seconds
  int interval = 100;
  int remaining_loops = timeout / interval;
  while (!has_connection_result) {
    if (interval == 0) {
      connected = false;
      has_connection_result = true;

      DestroyClient();

      tracker_frame->SetStatusMessage("Disconnected from Archipelago.");

      wxMessageBox("Timeout while connecting to Archipelago server.",
                   "Connection failed", wxOK | wxICON_ERROR);
    }

    std::this_thread::sleep_for(std::chrono::milliseconds(100));

    interval--;
  }

  if (connected) {
    for (const MapArea& map_area : GetGameData().GetMapAreas()) {
      for (int section_id = 0; section_id < map_area.locations.size();
           section_id++) {
        const Location& location = map_area.locations.at(section_id);

        int64_t ap_id = apclient->get_location_id(location.ap_location_name);
        if (ap_id == APClient::INVALID_NAME_ID) {
          std::cout << "Could not find AP location ID for "
                    << location.ap_location_name << std::endl;
        } else {
          ap_id_by_location_id[{map_area.id, section_id}] = ap_id;
        }
      }
    }

    for (const Door& door : GetGameData().GetDoors()) {
      if (!door.skip_item) {
        ap_id_by_item_name[door.item_name] = GetItemId(door.item_name);

        if (!door.group_name.empty() &&
            !ap_id_by_item_name.count(door.group_name)) {
          ap_id_by_item_name[door.group_name] = GetItemId(door.group_name);
        }

        for (const ProgressiveRequirement& prog_req : door.progressives) {
          ap_id_by_item_name[prog_req.item_name] =
              GetItemId(prog_req.item_name);
        }
      }
    }

    ap_id_by_color[LingoColor::kBlack] = GetItemId("Black");
    ap_id_by_color[LingoColor::kRed] = GetItemId("Red");
    ap_id_by_color[LingoColor::kBlue] = GetItemId("Blue");
    ap_id_by_color[LingoColor::kYellow] = GetItemId("Yellow");
    ap_id_by_color[LingoColor::kPurple] = GetItemId("Purple");
    ap_id_by_color[LingoColor::kOrange] = GetItemId("Orange");
    ap_id_by_color[LingoColor::kGreen] = GetItemId("Green");
    ap_id_by_color[LingoColor::kBrown] = GetItemId("Brown");
    ap_id_by_color[LingoColor::kGray] = GetItemId("Gray");

    RefreshTracker();
  } else {
    client_active = false;
  }
}

bool AP_HasCheckedGameLocation(int area_id, int section_id) {
  std::tuple<int, int> location_key = {area_id, section_id};

  if (ap_id_by_location_id.count(location_key)) {
    return checked_locations.count(ap_id_by_location_id.at(location_key));
  } else {
    return false;
  }
}

bool AP_HasColorItem(LingoColor color) {
  if (ap_id_by_color.count(color)) {
    return inventory.count(ap_id_by_color.at(color));
  } else {
    return false;
  }
}

bool AP_HasItem(const std::string& item, int quantity) {
  if (ap_id_by_item_name.count(item)) {
    int64_t ap_id = ap_id_by_item_name.at(item);
    return inventory.count(ap_id) && inventory.at(ap_id) >= quantity;
  } else {
    return false;
  }
}

DoorShuffleMode AP_GetDoorShuffleMode() { return door_shuffle_mode; }

bool AP_IsColorShuffle() { return color_shuffle; }

bool AP_IsPaintingShuffle() { return painting_shuffle; }

const std::map<std::string, std::string> AP_GetPaintingMapping() {
  return painting_mapping;
}

int AP_GetMasteryRequirement() { return mastery_requirement; }