about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ap_state.cpp51
-rw-r--r--ap_state.h5
2 files changed, 34 insertions, 22 deletions
diff --git a/ap_state.cpp b/ap_state.cpp index 8400557..c901dce 100644 --- a/ap_state.cpp +++ b/ap_state.cpp
@@ -46,9 +46,7 @@ void APState::Connect(std::string server, std::string player,
46 std::lock_guard client_guard(client_mutex_); 46 std::lock_guard client_guard(client_mutex_);
47 47
48 if (apclient) { 48 if (apclient) {
49 apclient->reset(); 49 DestroyClient();
50 delete apclient;
51 apclient = nullptr;
52 } 50 }
53 51
54 apclient = new APClient(ap_get_uuid(""), "Lingo", server); 52 apclient = new APClient(ap_get_uuid(""), "Lingo", server);
@@ -61,10 +59,10 @@ void APState::Connect(std::string server, std::string player,
61 painting_shuffle_ = false; 59 painting_shuffle_ = false;
62 painting_mapping_.clear(); 60 painting_mapping_.clear();
63 61
64 bool connected = false; 62 connected_ = false;
65 bool has_connection_result = false; 63 has_connection_result_ = false;
66 64
67 apclient->set_room_info_handler([&]() { 65 apclient->set_room_info_handler([this, player, password]() {
68 tracker_frame_->SetStatusMessage( 66 tracker_frame_->SetStatusMessage(
69 "Connected to Archipelago server. Authenticating..."); 67 "Connected to Archipelago server. Authenticating...");
70 68
@@ -73,7 +71,7 @@ void APState::Connect(std::string server, std::string player,
73 }); 71 });
74 72
75 apclient->set_location_checked_handler( 73 apclient->set_location_checked_handler(
76 [&](const std::list<int64_t>& locations) { 74 [this](const std::list<int64_t>& locations) {
77 for (const int64_t location_id : locations) { 75 for (const int64_t location_id : locations) {
78 checked_locations_.insert(location_id); 76 checked_locations_.insert(location_id);
79 std::cout << "Location: " << location_id << std::endl; 77 std::cout << "Location: " << location_id << std::endl;
@@ -82,16 +80,16 @@ void APState::Connect(std::string server, std::string player,
82 RefreshTracker(); 80 RefreshTracker();
83 }); 81 });
84 82
85 apclient->set_slot_disconnected_handler([&]() { 83 apclient->set_slot_disconnected_handler([this]() {
86 tracker_frame_->SetStatusMessage("Disconnected from Archipelago."); 84 tracker_frame_->SetStatusMessage("Disconnected from Archipelago. Attempting to reconnect...");
87 }); 85 });
88 86
89 apclient->set_socket_disconnected_handler([&]() { 87 apclient->set_socket_disconnected_handler([this]() {
90 tracker_frame_->SetStatusMessage("Disconnected from Archipelago."); 88 tracker_frame_->SetStatusMessage("Disconnected from Archipelago. Attempting to reconnect...");
91 }); 89 });
92 90
93 apclient->set_items_received_handler( 91 apclient->set_items_received_handler(
94 [&](const std::list<APClient::NetworkItem>& items) { 92 [this](const std::list<APClient::NetworkItem>& items) {
95 for (const APClient::NetworkItem& item : items) { 93 for (const APClient::NetworkItem& item : items) {
96 // TODO: Progressive items. 94 // TODO: Progressive items.
97 95
@@ -102,7 +100,7 @@ void APState::Connect(std::string server, std::string player,
102 RefreshTracker(); 100 RefreshTracker();
103 }); 101 });
104 102
105 apclient->set_slot_connected_handler([&](const nlohmann::json& slot_data) { 103 apclient->set_slot_connected_handler([this](const nlohmann::json& slot_data) {
106 tracker_frame_->SetStatusMessage("Connected to Archipelago!"); 104 tracker_frame_->SetStatusMessage("Connected to Archipelago!");
107 105
108 door_shuffle_mode_ = slot_data["shuffle_doors"].get<DoorShuffleMode>(); 106 door_shuffle_mode_ = slot_data["shuffle_doors"].get<DoorShuffleMode>();
@@ -117,14 +115,14 @@ void APState::Connect(std::string server, std::string player,
117 } 115 }
118 } 116 }
119 117
120 connected = true; 118 connected_ = true;
121 has_connection_result = true; 119 has_connection_result_ = true;
122 }); 120 });
123 121
124 apclient->set_slot_refused_handler( 122 apclient->set_slot_refused_handler(
125 [&](const std::list<std::string>& errors) { 123 [this](const std::list<std::string>& errors) {
126 connected = false; 124 connected_ = false;
127 has_connection_result = true; 125 has_connection_result_ = true;
128 126
129 tracker_frame_->SetStatusMessage("Disconnected from Archipelago."); 127 tracker_frame_->SetStatusMessage("Disconnected from Archipelago.");
130 128
@@ -162,10 +160,12 @@ void APState::Connect(std::string server, std::string player,
162 int timeout = 5000; // 5 seconds 160 int timeout = 5000; // 5 seconds
163 int interval = 100; 161 int interval = 100;
164 int remaining_loops = timeout / interval; 162 int remaining_loops = timeout / interval;
165 while (!has_connection_result) { 163 while (!has_connection_result_) {
166 if (interval == 0) { 164 if (interval == 0) {
167 connected = false; 165 connected_ = false;
168 has_connection_result = true; 166 has_connection_result_ = true;
167
168 DestroyClient();
169 169
170 tracker_frame_->SetStatusMessage("Disconnected from Archipelago."); 170 tracker_frame_->SetStatusMessage("Disconnected from Archipelago.");
171 171
@@ -178,7 +178,7 @@ void APState::Connect(std::string server, std::string player,
178 interval--; 178 interval--;
179 } 179 }
180 180
181 if (connected) { 181 if (connected_) {
182 for (const MapArea& map_area : GetGameData().GetMapAreas()) { 182 for (const MapArea& map_area : GetGameData().GetMapAreas()) {
183 for (int section_id = 0; section_id < map_area.locations.size(); 183 for (int section_id = 0; section_id < map_area.locations.size();
184 section_id++) { 184 section_id++) {
@@ -261,6 +261,13 @@ int64_t APState::GetItemId(const std::string& item_name) {
261 return ap_id; 261 return ap_id;
262} 262}
263 263
264void APState::DestroyClient() {
265 client_active_ = false;
266 apclient->reset();
267 delete apclient;
268 apclient = nullptr;
269}
270
264APState& GetAPState() { 271APState& GetAPState() {
265 static APState* instance = new APState(); 272 static APState* instance = new APState();
266 return *instance; 273 return *instance;
diff --git a/ap_state.h b/ap_state.h index 8f27a23..ce7bafc 100644 --- a/ap_state.h +++ b/ap_state.h
@@ -43,11 +43,16 @@ class APState {
43 43
44 int64_t GetItemId(const std::string& item_name); 44 int64_t GetItemId(const std::string& item_name);
45 45
46 void DestroyClient();
47
46 TrackerFrame* tracker_frame_; 48 TrackerFrame* tracker_frame_;
47 49
48 bool client_active_ = false; 50 bool client_active_ = false;
49 std::mutex client_mutex_; 51 std::mutex client_mutex_;
50 52
53 bool connected_ = false;
54 bool has_connection_result_ = false;
55
51 std::set<int64_t> inventory_; 56 std::set<int64_t> inventory_;
52 std::set<int64_t> checked_locations_; 57 std::set<int64_t> checked_locations_;
53 58