From a5b5ab97dfd4d98938c0e6055eee5a937ba1a53e Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 7 Aug 2024 21:33:46 +0200 Subject: [PATCH] dice tool working with >2 peers --- external/solanaceae_tox_p2prng | 2 +- plugins/dice_tool.cpp | 130 ++++++++++++++++++++++++++------- plugins/dice_tool.hpp | 6 +- 3 files changed, 108 insertions(+), 30 deletions(-) diff --git a/external/solanaceae_tox_p2prng b/external/solanaceae_tox_p2prng index ecce043..3869731 160000 --- a/external/solanaceae_tox_p2prng +++ b/external/solanaceae_tox_p2prng @@ -1 +1 @@ -Subproject commit ecce043740066505a7b1c3672e3949bf3ffb9137 +Subproject commit 3869731e0dbcc4a9a6e06399ffaf11f3a771ce3e diff --git a/plugins/dice_tool.cpp b/plugins/dice_tool.cpp index a563292..36403bf 100644 --- a/plugins/dice_tool.cpp +++ b/plugins/dice_tool.cpp @@ -119,44 +119,120 @@ float DiceTool::render(float) { c_vec.emplace_back(_cr, cv); } - auto new_id = _p2prng.newGernationPeers(c_vec, ByteSpan{reinterpret_cast(&g_sides), sizeof(g_sides)}); - if (!new_id.empty()) { - auto& new_roll = _rolls.emplace_back(); - new_roll.id = new_id; - } + std::vector is {'D', 'I', 'C', 'E'}; + is.push_back(reinterpret_cast(&g_sides)[0]); + is.push_back(reinterpret_cast(&g_sides)[1]); + static_assert(sizeof(g_sides) == 2); + + auto new_id = _p2prng.newGernationPeers(c_vec, ByteSpan{is}); + //if (!new_id.empty()) { + //} } ImGui::SeparatorText("Rolls"); // list of past rolls and their state - //ImGui::CollapsingHeader("d"); - ImGui::Text("d6 [?] hmac 4/6"); - ImGui::Text("d6 [?] secret 1/3"); - ImGui::Text("d6 [1]"); + for (auto it = _rolls.crbegin(); it != _rolls.crend(); it++) { + const auto& roll = *it; + + std::string text{"d"}; + text += std::to_string(roll.sides); + text += " ["; + if (roll.state == P2PRNG::DONE) { + // dice start at 1 + text += std::to_string(int(roll.final_result)+1); + } else { + text += "?"; + } + text += "]"; + + if (roll.state == P2PRNG::INIT) { + text += " INIT"; + } else if (roll.state == P2PRNG::HMAC) { + text += " HMAC "; + text += std::to_string(roll.state_number_1); + text += "/"; + text += std::to_string(roll.state_number_2); + } else if (roll.state == P2PRNG::SECRET) { + text += " SECRET "; + text += std::to_string(roll.state_number_1); + text += "/"; + text += std::to_string(roll.state_number_2); + } + + ImGui::TextUnformatted(text.c_str()); + } } ImGui::End(); return 10.f; } -bool DiceTool::onEvent(const P2PRNG::Events::Init&) { - return false; -} - -bool DiceTool::onEvent(const P2PRNG::Events::HMAC&) { - return false; -} - -bool DiceTool::onEvent(const P2PRNG::Events::Secret&) { - return false; -} - -bool DiceTool::onEvent(const P2PRNG::Events::Done&) { - std::cout << "got a done!!!!!!!!!!!!!!!!!!\n"; - return false; -} - -bool DiceTool::onEvent(const P2PRNG::Events::ValError&) { +bool DiceTool::onEvent(const P2PRNG::Events::Init& e) { + if (e.initial_state.size != 4+2) { + return false; + } + + if (e.initial_state[0] != 'D' || e.initial_state[1] != 'I' || e.initial_state[2] != 'C' || e.initial_state[3] != 'E') { + return false; + } + + auto& new_roll = _rolls.emplace_back(); + new_roll.id = {e.id.cbegin(), e.id.cend()}; + new_roll.state = P2PRNG::State::INIT; + reinterpret_cast(&new_roll.sides)[0] = e.initial_state[4]; + reinterpret_cast(&new_roll.sides)[1] = e.initial_state[5]; + + return true; +} + +bool DiceTool::onEvent(const P2PRNG::Events::HMAC& e) { + auto roll_it = std::find_if(_rolls.begin(), _rolls.end(), [&e](const auto& a) -> bool { return ByteSpan{a.id} == e.id; }); + if (roll_it == _rolls.cend()) { + return false; + } + + roll_it->state = P2PRNG::State::HMAC; + roll_it->state_number_1 = e.have; + roll_it->state_number_2 = e.out_of; + + return true; +} + +bool DiceTool::onEvent(const P2PRNG::Events::Secret& e) { + auto roll_it = std::find_if(_rolls.begin(), _rolls.end(), [&e](const auto& a) -> bool { return ByteSpan{a.id} == e.id; }); + if (roll_it == _rolls.cend()) { + return false; + } + + roll_it->state = P2PRNG::State::SECRET; + roll_it->state_number_1 = e.have; + roll_it->state_number_2 = e.out_of; + + return true; +} + +bool DiceTool::onEvent(const P2PRNG::Events::Done& e) { + auto roll_it = std::find_if(_rolls.begin(), _rolls.end(), [&e](const auto& a) -> bool { return ByteSpan{a.id} == e.id; }); + if (roll_it == _rolls.cend()) { + return false; + } + + roll_it->state = P2PRNG::State::DONE; + roll_it->state_number_1 = 0; + roll_it->state_number_2 = 0; + roll_it->final_result = (e.result[0] | (e.result[1] << 8)) % roll_it->sides; + + std::cout << "done die roll " << roll_it->final_result << "\n"; + return true; +} + +bool DiceTool::onEvent(const P2PRNG::Events::ValError& e) { + auto roll_it = std::find_if(_rolls.cbegin(), _rolls.cend(), [&e](const auto& a) -> bool { return ByteSpan{a.id} == e.id; }); + if (roll_it == _rolls.cend()) { + return false; + } + return false; } diff --git a/plugins/dice_tool.hpp b/plugins/dice_tool.hpp index efb7535..c7f79af 100644 --- a/plugins/dice_tool.hpp +++ b/plugins/dice_tool.hpp @@ -12,9 +12,11 @@ class DiceTool : public P2PRNGEventI { struct Rolls { std::vector id; + uint16_t sides {}; + P2PRNG::State state {P2PRNG::State::UNKNOWN}; - uint16_t state_number_1{}; - uint16_t state_number_2{}; + uint16_t state_number_1 {}; + uint16_t state_number_2 {}; uint16_t final_result{}; };