about summary refs log tree commit diff stats
path: root/src/ap_state.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ap_state.cpp')
-rw-r--r--src/ap_state.cpp93
1 files changed, 54 insertions, 39 deletions
diff --git a/src/ap_state.cpp b/src/ap_state.cpp index 29e649f..d01290b 100644 --- a/src/ap_state.cpp +++ b/src/ap_state.cpp
@@ -243,7 +243,7 @@ struct APState {
243 checked_paintings.count(painting_mapping.at(painting_id))); 243 checked_paintings.count(painting_mapping.at(painting_id)));
244 } 244 }
245 245
246 std::string GetItemName(int id) { return apclient->get_item_name(id); } 246 std::string GetItemName(int id) { return apclient->get_item_name(id, "Lingo"); }
247 247
248 void RevealPaintings() { 248 void RevealPaintings() {
249 std::lock_guard state_guard(state_mutex); 249 std::lock_guard state_guard(state_mutex);
@@ -369,7 +369,7 @@ struct APState {
369 } 369 }
370 } 370 }
371 371
372 RefreshTracker(false); 372 RefreshTracker(StateUpdate{.cleared_locations = true});
373 } 373 }
374 374
375 void OnSlotDisconnected() { 375 void OnSlotDisconnected() {
@@ -391,51 +391,53 @@ struct APState {
391 } 391 }
392 392
393 void OnItemsReceived(const std::list<APClient::NetworkItem>& items) { 393 void OnItemsReceived(const std::list<APClient::NetworkItem>& items) {
394 std::vector<ItemState> item_states;
395
394 { 396 {
395 std::lock_guard state_guard(state_mutex); 397 std::lock_guard state_guard(state_mutex);
396 398
399 std::map<int64_t, int> index_by_item;
400
397 for (const APClient::NetworkItem& item : items) { 401 for (const APClient::NetworkItem& item : items) {
398 inventory[item.item]++; 402 inventory[item.item]++;
399 TrackerLog(fmt::format("Item: {}", item.item)); 403 TrackerLog(fmt::format("Item: {}", item.item));
404
405 index_by_item[item.item] = item.index;
406 }
407
408 for (const auto& [item_id, item_index] : index_by_item) {
409 item_states.push_back(ItemState{.name = GetItemName(item_id),
410 .amount = inventory[item_id],
411 .index = item_index});
400 } 412 }
401 } 413 }
402 414
403 RefreshTracker(false); 415 RefreshTracker(StateUpdate{.items = item_states});
404 } 416 }
405 417
406 void OnRetrieved(const std::map<std::string, nlohmann::json>& data) { 418 void OnRetrieved(const std::map<std::string, nlohmann::json>& data) {
419 StateUpdate state_update;
420
407 { 421 {
408 std::lock_guard state_guard(state_mutex); 422 std::lock_guard state_guard(state_mutex);
409 423
410 for (const auto& [key, value] : data) { 424 for (const auto& [key, value] : data) {
411 HandleDataStorage(key, value); 425 HandleDataStorage(key, value, state_update);
412 } 426 }
413 } 427 }
414 428
415 RefreshTracker(false); 429 RefreshTracker(state_update);
416 } 430 }
417 431
418 void OnSetReply(const std::string& key, const nlohmann::json& value) { 432 void OnSetReply(const std::string& key, const nlohmann::json& value) {
419 bool should_refresh = false; 433 StateUpdate state_update;
420 bool should_redraw_position = false; 434
421 { 435 {
422 std::lock_guard state_guard(state_mutex); 436 std::lock_guard state_guard(state_mutex);
423 HandleDataStorage(key, value); 437 HandleDataStorage(key, value, state_update);
424
425 if (key.ends_with("PlayerPos"))
426 {
427 should_redraw_position = true;
428 } else {
429 should_refresh = true;
430 }
431 } 438 }
432 439
433 if (should_refresh) 440 RefreshTracker(state_update);
434 {
435 RefreshTracker(false);
436 } else if (should_redraw_position) {
437 tracker_frame->RedrawPosition();
438 }
439 } 441 }
440 442
441 void OnSlotConnected(std::string player, std::string server, 443 void OnSlotConnected(std::string player, std::string server,
@@ -531,7 +533,7 @@ struct APState {
531 } 533 }
532 534
533 ResetReachabilityRequirements(); 535 ResetReachabilityRequirements();
534 RefreshTracker(true); 536 RefreshTracker(std::nullopt);
535 } 537 }
536 538
537 void OnSlotRefused(const std::list<std::string>& errors) { 539 void OnSlotRefused(const std::list<std::string>& errors) {
@@ -574,11 +576,15 @@ struct APState {
574 } 576 }
575 577
576 // Assumes state mutex is locked. 578 // Assumes state mutex is locked.
577 void HandleDataStorage(const std::string& key, const nlohmann::json& value) { 579 void HandleDataStorage(const std::string& key, const nlohmann::json& value, StateUpdate& state_update) {
578 if (value.is_boolean()) { 580 if (value.is_boolean()) {
579 data_storage[key] = value.get<bool>(); 581 data_storage[key] = value.get<bool>();
580 TrackerLog(fmt::format("Data storage {} retrieved as {}", key, 582 TrackerLog(fmt::format("Data storage {} retrieved as {}", key,
581 (value.get<bool>() ? "true" : "false"))); 583 (value.get<bool>() ? "true" : "false")));
584
585 if (key.find("Achievement|") != std::string::npos) {
586 state_update.achievements = true;
587 }
582 } else if (value.is_number()) { 588 } else if (value.is_number()) {
583 data_storage[key] = value.get<int>(); 589 data_storage[key] = value.get<int>();
584 TrackerLog(fmt::format("Data storage {} retrieved as {}", key, 590 TrackerLog(fmt::format("Data storage {} retrieved as {}", key,
@@ -587,6 +593,7 @@ struct APState {
587 if (key.ends_with("PlayerPos")) { 593 if (key.ends_with("PlayerPos")) {
588 auto map_value = value.get<std::map<std::string, int>>(); 594 auto map_value = value.get<std::map<std::string, int>>();
589 player_pos = std::tuple<int, int>(map_value["x"], map_value["z"]); 595 player_pos = std::tuple<int, int>(map_value["x"], map_value["z"]);
596 state_update.player_position = true;
590 } else { 597 } else {
591 data_storage[key] = value.get<std::map<std::string, int>>(); 598 data_storage[key] = value.get<std::map<std::string, int>>();
592 } 599 }
@@ -595,6 +602,7 @@ struct APState {
595 } else if (value.is_null()) { 602 } else if (value.is_null()) {
596 if (key.ends_with("PlayerPos")) { 603 if (key.ends_with("PlayerPos")) {
597 player_pos = std::nullopt; 604 player_pos = std::nullopt;
605 state_update.player_position = true;
598 } else { 606 } else {
599 data_storage.erase(key); 607 data_storage.erase(key);
600 } 608 }
@@ -606,6 +614,8 @@ struct APState {
606 if (key.ends_with("Paintings")) { 614 if (key.ends_with("Paintings")) {
607 data_storage[key] = 615 data_storage[key] =
608 std::set<std::string>(list_value.begin(), list_value.end()); 616 std::set<std::string>(list_value.begin(), list_value.end());
617 state_update.paintings =
618 std::vector<std::string>(list_value.begin(), list_value.end());
609 } else { 619 } else {
610 data_storage[key] = list_value; 620 data_storage[key] = list_value;
611 } 621 }
@@ -616,29 +626,34 @@ struct APState {
616 } 626 }
617 627
618 // State mutex should NOT be locked. 628 // State mutex should NOT be locked.
619 void RefreshTracker(bool reset) { 629 // nullopt state_update indicates a reset.
630 void RefreshTracker(std::optional<StateUpdate> state_update) {
620 TrackerLog("Refreshing display..."); 631 TrackerLog("Refreshing display...");
621 632
622 std::string prev_msg; 633 if (!state_update || !state_update->items.empty() ||
623 { 634 !state_update->paintings.empty()) {
624 std::lock_guard state_guard(state_mutex); 635 std::string prev_msg;
636 {
637 std::lock_guard state_guard(state_mutex);
625 638
626 prev_msg = status_message; 639 prev_msg = status_message;
627 SetStatusMessage(fmt::format("{} Recalculating...", status_message)); 640 SetStatusMessage(fmt::format("{} Recalculating...", status_message));
628 } 641 }
629 642
630 RecalculateReachability(); 643 RecalculateReachability();
631 644
632 if (reset) { 645 {
633 tracker_frame->ResetIndicators(); 646 std::lock_guard state_guard(state_mutex);
634 } else {
635 tracker_frame->UpdateIndicators();
636 }
637 647
638 { 648 SetStatusMessage(prev_msg);
639 std::lock_guard state_guard(state_mutex); 649 }
650 }
651
640 652
641 SetStatusMessage(prev_msg); 653 if (!state_update) {
654 tracker_frame->ResetIndicators();
655 } else {
656 tracker_frame->UpdateIndicators(*state_update);
642 } 657 }
643 } 658 }
644 659