diff options
author | Vittorio Romeo <vittorio.romeo@outlook.com> | 2021-11-04 19:42:21 +0000 |
---|---|---|
committer | Vittorio Romeo <vittorio.romeo@outlook.com> | 2021-11-04 19:42:21 +0000 |
commit | 58010b5cdeb122391018426e2561a876cbd2ac66 (patch) | |
tree | 1b66536be26c5c39aaffd2b5206eb9f4bc22df9e | |
parent | 0bad2a8e18baeaaba282196d6bd68bee5840f363 (diff) |
Improve replay filenames (add timestamp, shorten, display score in seconds)
-rw-r--r-- | include/SSVOpenHexagon/Online/Database.hpp | 7 | ||||
-rw-r--r-- | include/SSVOpenHexagon/Utils/Timestamp.hpp | 22 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/HexagonGame.cpp | 19 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/HexagonServer.cpp | 16 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/MenuGame.cpp | 15 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/Replay.cpp | 11 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Online/Database.cpp | 22 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Utils/Timestamp.cpp | 40 |
8 files changed, 99 insertions, 53 deletions
diff --git a/include/SSVOpenHexagon/Online/Database.hpp b/include/SSVOpenHexagon/Online/Database.hpp index 3ff7c9ce..d9638c23 100644 --- a/include/SSVOpenHexagon/Online/Database.hpp +++ b/include/SSVOpenHexagon/Online/Database.hpp @@ -16,13 +16,6 @@ namespace hg::Database { -using Clock = std::chrono::high_resolution_clock; -using TimePoint = std::chrono::time_point<Clock>; - -[[nodiscard]] std::uint64_t timestamp(const TimePoint tp); -[[nodiscard]] std::uint64_t nowTimestamp(); -[[nodiscard]] TimePoint toTimepoint(const std::uint64_t timestamp); - void addUser(const User& user); void removeUser(const std::uint32_t id); diff --git a/include/SSVOpenHexagon/Utils/Timestamp.hpp b/include/SSVOpenHexagon/Utils/Timestamp.hpp new file mode 100644 index 00000000..1c5cebeb --- /dev/null +++ b/include/SSVOpenHexagon/Utils/Timestamp.hpp @@ -0,0 +1,22 @@ +// Copyright (c) 2013-2020 Vittorio Romeo +// License: Academic Free License ("AFL") v. 3.0 +// AFL License page: https://opensource.org/licenses/AFL-3.0 + +#pragma once + +#include <chrono> +#include <cstdint> +#include <string> + +namespace hg::Utils { + +using Clock = std::chrono::high_resolution_clock; +using TimePoint = std::chrono::time_point<Clock>; + +[[nodiscard]] std::uint64_t timestamp(const TimePoint tp); +[[nodiscard]] std::uint64_t nowTimestamp(); +[[nodiscard]] TimePoint toTimepoint(const std::uint64_t timestamp); +[[nodiscard]] std::string formatTimepoint( + const TimePoint time, const std::string& format); + +} // namespace hg::Utils diff --git a/src/SSVOpenHexagon/Core/HexagonGame.cpp b/src/SSVOpenHexagon/Core/HexagonGame.cpp index 3b8fdf6f..c1bb1b72 100644 --- a/src/SSVOpenHexagon/Core/HexagonGame.cpp +++ b/src/SSVOpenHexagon/Core/HexagonGame.cpp @@ -921,16 +921,19 @@ void HexagonGame::death(bool mForce) status.hasDied = true; - const replay_file rf = death_createReplayFile(); - - // TODO (P2): for testing - if(onDeathReplayCreated) + if(!inReplay()) { - onDeathReplayCreated(rf); - } + const replay_file rf = death_createReplayFile(); + + // TODO (P2): for testing + if(onDeathReplayCreated) + { + onDeathReplayCreated(rf); + } - ssvu::lo("Replay") << "Attempting to send and save replay...\n"; - death_sendAndSaveReplay(rf); + ssvu::lo("Replay") << "Attempting to send and save replay...\n"; + death_sendAndSaveReplay(rf); + } death_saveScoreIfNeededAndShowPBEffects(); // Saves local best diff --git a/src/SSVOpenHexagon/Core/HexagonServer.cpp b/src/SSVOpenHexagon/Core/HexagonServer.cpp index a6c53cff..a6bc81cd 100644 --- a/src/SSVOpenHexagon/Core/HexagonServer.cpp +++ b/src/SSVOpenHexagon/Core/HexagonServer.cpp @@ -4,18 +4,22 @@ #include "SSVOpenHexagon/Core/HexagonServer.hpp" -#include "SSVOpenHexagon/Global/Config.hpp" -#include "SSVOpenHexagon/Utils/Match.hpp" -#include "SSVOpenHexagon/Global/Assets.hpp" #include "SSVOpenHexagon/Global/Assert.hpp" +#include "SSVOpenHexagon/Global/Assets.hpp" +#include "SSVOpenHexagon/Global/Config.hpp" #include "SSVOpenHexagon/Global/Version.hpp" + #include "SSVOpenHexagon/Core/HexagonGame.hpp" #include "SSVOpenHexagon/Core/Replay.hpp" + #include "SSVOpenHexagon/Utils/Concat.hpp" #include "SSVOpenHexagon/Utils/LevelValidator.hpp" +#include "SSVOpenHexagon/Utils/Match.hpp" #include "SSVOpenHexagon/Utils/Split.hpp" #include "SSVOpenHexagon/Utils/StringToCharVec.hpp" +#include "SSVOpenHexagon/Utils/Timestamp.hpp" #include "SSVOpenHexagon/Utils/VectorToSet.hpp" + #include "SSVOpenHexagon/Online/Shared.hpp" #include "SSVOpenHexagon/Online/Database.hpp" #include "SSVOpenHexagon/Online/Sodium.hpp" @@ -716,7 +720,7 @@ void HexagonServer::runIteration_FlushLogs() SSVOH_SLOG << "Replay valid, adding to database\n"; - Database::addScore(levelValidator, Database::nowTimestamp(), + Database::addScore(levelValidator, Utils::nowTimestamp(), c._loginData->_steamId, replayPlayedTime); return true; @@ -1002,12 +1006,12 @@ void HexagonServer::printCTSPDataVerbose( Database::removeAllLoginTokensForUser(user->id); - static_assert(std::is_same_v<Clock, Database::Clock>); + static_assert(std::is_same_v<Clock, Utils::Clock>); Database::addLoginToken( // Database::LoginToken{ .userId = user->id, - .timestamp = Database::nowTimestamp(), + .timestamp = Utils::nowTimestamp(), .token = loginToken // }); diff --git a/src/SSVOpenHexagon/Core/MenuGame.cpp b/src/SSVOpenHexagon/Core/MenuGame.cpp index 705295d4..34681cfb 100644 --- a/src/SSVOpenHexagon/Core/MenuGame.cpp +++ b/src/SSVOpenHexagon/Core/MenuGame.cpp @@ -42,6 +42,7 @@ #include "SSVOpenHexagon/Utils/Match.hpp" #include "SSVOpenHexagon/Utils/ScopeGuard.hpp" #include "SSVOpenHexagon/Utils/String.hpp" +#include "SSVOpenHexagon/Utils/Timestamp.hpp" #include "SSVOpenHexagon/Utils/UniquePtr.hpp" #include "SSVOpenHexagon/Utils/Utils.hpp" @@ -5552,15 +5553,7 @@ void MenuGame::drawLevelSelectionLeftSide( SSVOH_ASSERT( hexagonClient.getState() == HexagonClient::State::LoggedIn_Ready); - const auto serializeTimePoint = - [](const auto& time, const std::string& format) - { - std::time_t tt = std::chrono::system_clock::to_time_t(time); - std::tm tm = *std::gmtime(&tt); // GMT (UTC) - std::stringstream ss; - ss << std::put_time(&tm, format.c_str()); - return ss.str(); - }; + const auto drawEntry = [&](const int i, const std::string& userName, const std::uint64_t scoreTimestamp, @@ -5568,10 +5561,10 @@ void MenuGame::drawLevelSelectionLeftSide( { const float score = scoreValue; - const auto tp = Database::toTimepoint(scoreTimestamp); + const auto tp = Utils::toTimepoint(scoreTimestamp); const std::string timestampStr = - serializeTimePoint(tp, "%Y-%m-%d %H:%M:%S"); + Utils::formatTimepoint(tp, "%Y-%m-%d %H:%M:%S"); const std::string posStr = Utils::concat('#', i + 1); std::string scoreStr = ssvu::toStr(score) + 's'; diff --git a/src/SSVOpenHexagon/Core/Replay.cpp b/src/SSVOpenHexagon/Core/Replay.cpp index 313d9f11..64b06630 100644 --- a/src/SSVOpenHexagon/Core/Replay.cpp +++ b/src/SSVOpenHexagon/Core/Replay.cpp @@ -6,6 +6,7 @@ #include "SSVOpenHexagon/Global/Assert.hpp" #include "SSVOpenHexagon/Utils/Concat.hpp" +#include "SSVOpenHexagon/Utils/Timestamp.hpp" #include <SSVUtils/Core/Common/LikelyUnlikely.hpp> @@ -416,8 +417,14 @@ static constexpr std::size_t buf_size{2097152}; // 2MB [[nodiscard]] std::string replay_file::create_filename() const { - return Utils::concat(_version, '_', _player_name, '_', _level_id, '_', - _difficulty_mult, "x_", _played_score / 60.0, "s.ohreplay"); + const Utils::TimePoint tp = Utils::toTimepoint(Utils::nowTimestamp()); + + const std::string timestampStr = + Utils::formatTimepoint(tp, "%Y-%m-%d_%H-%M-%S"); + + return Utils::concat(_version, '_', timestampStr, '_', _player_name, '_', + _level_id, '_', _difficulty_mult, "x_", _played_score / 60.0, + "s.ohreplay"); } [[nodiscard]] bool compressed_replay_file::serialize_to_file( diff --git a/src/SSVOpenHexagon/Online/Database.cpp b/src/SSVOpenHexagon/Online/Database.cpp index 2ccdb3ba..5ef4133c 100644 --- a/src/SSVOpenHexagon/Online/Database.cpp +++ b/src/SSVOpenHexagon/Online/Database.cpp @@ -7,6 +7,7 @@ #include "SSVOpenHexagon/Global/Assert.hpp" #include "SSVOpenHexagon/Utils/Concat.hpp" #include "SSVOpenHexagon/Utils/ScopeGuard.hpp" +#include "SSVOpenHexagon/Utils/Timestamp.hpp" #include <SSVUtils/Core/Log/Log.hpp> @@ -80,23 +81,6 @@ inline auto& getStorage() } // namespace Impl -[[nodiscard]] std::uint64_t timestamp(const TimePoint tp) -{ - return std::chrono::duration_cast<std::chrono::seconds>( - tp.time_since_epoch()) - .count(); -} - -[[nodiscard]] std::uint64_t nowTimestamp() -{ - return timestamp(Clock::now()); -} - -[[nodiscard]] TimePoint toTimepoint(const std::uint64_t timestamp) -{ - return TimePoint{} + std::chrono::seconds(timestamp); -} - void addUser(const User& user) { const int id = Impl::getStorage().insert(user); @@ -221,9 +205,9 @@ constexpr int tokenValiditySeconds = 3600; [[nodiscard]] static bool isLoginTokenTimestampValid(const LoginToken& lt) { - const TimePoint now = Clock::now(); + const Utils::TimePoint now = Utils::Clock::now(); - return (now - toTimepoint(lt.timestamp)) < + return (now - Utils::toTimepoint(lt.timestamp)) < std::chrono::seconds(tokenValiditySeconds); } diff --git a/src/SSVOpenHexagon/Utils/Timestamp.cpp b/src/SSVOpenHexagon/Utils/Timestamp.cpp new file mode 100644 index 00000000..49428c63 --- /dev/null +++ b/src/SSVOpenHexagon/Utils/Timestamp.cpp @@ -0,0 +1,40 @@ +// Copyright (c) 2013-2020 Vittorio Romeo +// License: Academic Free License ("AFL") v. 3.0 +// AFL License page: https://opensource.org/licenses/AFL-3.0 + +#include "SSVOpenHexagon/Utils/Timestamp.hpp" + +#include <iomanip> +#include <sstream> + +namespace hg::Utils { + +[[nodiscard]] std::uint64_t timestamp(const TimePoint tp) +{ + return std::chrono::duration_cast<std::chrono::seconds>( + tp.time_since_epoch()) + .count(); +} + +[[nodiscard]] std::uint64_t nowTimestamp() +{ + return timestamp(Clock::now()); +} + +[[nodiscard]] TimePoint toTimepoint(const std::uint64_t timestamp) +{ + return TimePoint{} + std::chrono::seconds(timestamp); +} + +[[nodiscard]] std::string formatTimepoint( + const TimePoint time, const std::string& format) +{ + const std::time_t tt = std::chrono::system_clock::to_time_t(time); + const std::tm tm = *std::gmtime(&tt); // GMT (UTC) + + std::stringstream ss; + ss << std::put_time(&tm, format.c_str()); + return ss.str(); +} + +} // namespace hg::Utils |