From 34bf5c0d9db195edf8b576d1273876966cf650b2 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 4 May 2021 16:11:56 +0100 Subject: Rename files to match code --- src/Bindings/AllToLua.pkg | 4 +- src/Bindings/LuaState.h | 2 +- src/CMakeLists.txt | 5 +- src/Entities/Player.cpp | 6 +- src/Entities/Player.h | 2 +- src/OSSupport/CMakeLists.txt | 1 + src/OSSupport/Stopwatch.h | 38 ++++++ src/Protocol/Palettes/Palette_1_13.h | 2 +- src/Protocol/Palettes/Palette_1_13_1.h | 2 +- src/Protocol/Palettes/Palette_1_14.h | 2 +- src/Protocol/Palettes/Palette_1_15.h | 2 +- src/Protocol/Palettes/Palette_1_16.h | 2 +- src/Protocol/Protocol_1_8.h | 2 +- src/Registries/CMakeLists.txt | 2 +- src/Registries/CustomStatistics.h | 125 +++++++++++++++++ src/Registries/Statistics.h | 125 ----------------- src/Statistics.cpp | 64 --------- src/Statistics.h | 43 ------ src/StatisticsManager.cpp | 64 +++++++++ src/StatisticsManager.h | 47 +++++++ src/Stopwatch.h | 38 ------ src/WorldStorage/CMakeLists.txt | 4 +- src/WorldStorage/NamespaceSerializer.h | 2 +- src/WorldStorage/StatSerializer.cpp | 214 ------------------------------ src/WorldStorage/StatSerializer.h | 30 ----- src/WorldStorage/StatisticsSerializer.cpp | 214 ++++++++++++++++++++++++++++++ src/WorldStorage/StatisticsSerializer.h | 30 +++++ 27 files changed, 538 insertions(+), 534 deletions(-) create mode 100644 src/OSSupport/Stopwatch.h create mode 100644 src/Registries/CustomStatistics.h delete mode 100644 src/Registries/Statistics.h delete mode 100644 src/Statistics.cpp delete mode 100644 src/Statistics.h create mode 100644 src/StatisticsManager.cpp create mode 100644 src/StatisticsManager.h delete mode 100644 src/Stopwatch.h delete mode 100644 src/WorldStorage/StatSerializer.cpp delete mode 100644 src/WorldStorage/StatSerializer.h create mode 100644 src/WorldStorage/StatisticsSerializer.cpp create mode 100644 src/WorldStorage/StatisticsSerializer.h (limited to 'src') diff --git a/src/Bindings/AllToLua.pkg b/src/Bindings/AllToLua.pkg index e6787b70d..1f9636cd0 100644 --- a/src/Bindings/AllToLua.pkg +++ b/src/Bindings/AllToLua.pkg @@ -68,7 +68,7 @@ $cfile "../CompositeChat.h" $cfile "../Map.h" $cfile "../MapManager.h" $cfile "../Scoreboard.h" -$cfile "../Statistics.h" +$cfile "../StatisticsManager.h" $cfile "../Protocol/MojangAPI.h" $cfile "../UUID.h" @@ -124,7 +124,7 @@ $cfile "../BlockEntities/MobSpawnerEntity.h" $cfile "../BlockEntities/FlowerPotEntity.h" // Registries: -$cfile "../Registries/Statistics.h" +$cfile "../Registries/CustomStatistics.h" diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index f615ef0a6..6f7a334b6 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -37,7 +37,7 @@ extern "C" #include "../Defines.h" #include "../FunctionRef.h" -#include "../Registries/Statistics.h" +#include "../Registries/CustomStatistics.h" #include "PluginManager.h" #include "LuaState_Typedefs.inc" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e9945fa54..b5dcead39 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,7 +58,7 @@ target_sources( Scoreboard.cpp Server.cpp SpawnPrepare.cpp - Statistics.cpp + StatisticsManager.cpp StringCompression.cpp StringUtils.cpp UUID.cpp @@ -138,8 +138,7 @@ target_sources( SetChunkData.h SettingsRepositoryInterface.h SpawnPrepare.h - Statistics.h - Stopwatch.h + StatisticsManager.h StringCompression.h StringUtils.h UUID.h diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 1e7c8df98..08adb1096 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -18,7 +18,7 @@ #include "../FastRandom.h" #include "../ClientHandle.h" -#include "../WorldStorage/StatSerializer.h" +#include "../WorldStorage/StatisticsSerializer.h" #include "../CompositeChat.h" #include "../Blocks/BlockHandler.h" @@ -1925,7 +1925,7 @@ bool cPlayer::LoadFromFile(const AString & a_FileName) { // Load the player stats. // We use the default world name (like bukkit) because stats are shared between dimensions / worlds. - StatSerializer::Load(m_Stats, m_DefaultWorldPath, GetUUID().ToLongString()); + StatisticsSerializer::Load(m_Stats, m_DefaultWorldPath, GetUUID().ToLongString()); } catch (...) { @@ -2059,7 +2059,7 @@ void cPlayer::SaveToDisk() // Save the player stats. // We use the default world name (like bukkit) because stats are shared between dimensions / worlds. // TODO: save together with player.dat, not in some other place. - StatSerializer::Save(m_Stats, m_DefaultWorldPath, GetUUID().ToLongString()); + StatisticsSerializer::Save(m_Stats, m_DefaultWorldPath, GetUUID().ToLongString()); } catch (...) { diff --git a/src/Entities/Player.h b/src/Entities/Player.h index e7b18f3b6..987bccbe5 100644 --- a/src/Entities/Player.h +++ b/src/Entities/Player.h @@ -7,7 +7,7 @@ #include "../World.h" #include "../Items/ItemHandler.h" -#include "../Statistics.h" +#include "../StatisticsManager.h" #include "../UUID.h" diff --git a/src/OSSupport/CMakeLists.txt b/src/OSSupport/CMakeLists.txt index 742bdcccf..3761913e1 100644 --- a/src/OSSupport/CMakeLists.txt +++ b/src/OSSupport/CMakeLists.txt @@ -36,6 +36,7 @@ target_sources( SleepResolutionBooster.h StackTrace.h StartAsService.h + Stopwatch.h TCPLinkImpl.h UDPEndpointImpl.h WinStackWalker.h diff --git a/src/OSSupport/Stopwatch.h b/src/OSSupport/Stopwatch.h new file mode 100644 index 000000000..7676b3856 --- /dev/null +++ b/src/OSSupport/Stopwatch.h @@ -0,0 +1,38 @@ + +// Stopwatch.h + +// Implements the cStopwatch class that measures and logs time between its creation and destruction + + + + + +#pragma once + + + + + +class cStopwatch +{ +public: + cStopwatch(const AString & a_Name): + m_Name(a_Name), + m_StartTime(std::chrono::high_resolution_clock::now()) + { + } + + ~cStopwatch() + { + auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - m_StartTime).count(); + LOG("Stopwatch: %s took %.03f sec", m_Name, static_cast(duration) / 1000); + } + +protected: + AString m_Name; + std::chrono::high_resolution_clock::time_point m_StartTime; +}; + + + + diff --git a/src/Protocol/Palettes/Palette_1_13.h b/src/Protocol/Palettes/Palette_1_13.h index 5b2a5ccb5..52aa70ea2 100644 --- a/src/Protocol/Palettes/Palette_1_13.h +++ b/src/Protocol/Palettes/Palette_1_13.h @@ -2,7 +2,7 @@ #include "BlockState.h" #include "Registries/Items.h" -#include "Registries/Statistics.h" +#include "Registries/CustomStatistics.h" namespace Palette_1_13 { diff --git a/src/Protocol/Palettes/Palette_1_13_1.h b/src/Protocol/Palettes/Palette_1_13_1.h index fda47e5c4..ca230743a 100644 --- a/src/Protocol/Palettes/Palette_1_13_1.h +++ b/src/Protocol/Palettes/Palette_1_13_1.h @@ -2,7 +2,7 @@ #include "BlockState.h" #include "Registries/Items.h" -#include "Registries/Statistics.h" +#include "Registries/CustomStatistics.h" namespace Palette_1_13_1 { diff --git a/src/Protocol/Palettes/Palette_1_14.h b/src/Protocol/Palettes/Palette_1_14.h index c92a19eba..3bc9b7980 100644 --- a/src/Protocol/Palettes/Palette_1_14.h +++ b/src/Protocol/Palettes/Palette_1_14.h @@ -2,7 +2,7 @@ #include "BlockState.h" #include "Registries/Items.h" -#include "Registries/Statistics.h" +#include "Registries/CustomStatistics.h" namespace Palette_1_14 { diff --git a/src/Protocol/Palettes/Palette_1_15.h b/src/Protocol/Palettes/Palette_1_15.h index 22c993de7..f71c38395 100644 --- a/src/Protocol/Palettes/Palette_1_15.h +++ b/src/Protocol/Palettes/Palette_1_15.h @@ -2,7 +2,7 @@ #include "BlockState.h" #include "Registries/Items.h" -#include "Registries/Statistics.h" +#include "Registries/CustomStatistics.h" namespace Palette_1_15 { diff --git a/src/Protocol/Palettes/Palette_1_16.h b/src/Protocol/Palettes/Palette_1_16.h index ae6cf9484..b6f85d2d6 100644 --- a/src/Protocol/Palettes/Palette_1_16.h +++ b/src/Protocol/Palettes/Palette_1_16.h @@ -2,7 +2,7 @@ #include "BlockState.h" #include "Registries/Items.h" -#include "Registries/Statistics.h" +#include "Registries/CustomStatistics.h" namespace Palette_1_16 { diff --git a/src/Protocol/Protocol_1_8.h b/src/Protocol/Protocol_1_8.h index faea84e57..b1157366b 100644 --- a/src/Protocol/Protocol_1_8.h +++ b/src/Protocol/Protocol_1_8.h @@ -15,7 +15,7 @@ Declares the 1.8 protocol classes: #include "Protocol.h" #include "../ByteBuffer.h" -#include "../Registries/Statistics.h" +#include "../Registries/CustomStatistics.h" #include "../mbedTLS++/AesCfb128Decryptor.h" #include "../mbedTLS++/AesCfb128Encryptor.h" diff --git a/src/Registries/CMakeLists.txt b/src/Registries/CMakeLists.txt index 31102881d..cc0a7b4df 100644 --- a/src/Registries/CMakeLists.txt +++ b/src/Registries/CMakeLists.txt @@ -5,6 +5,6 @@ target_sources( BlockStates.h BlockTypes.h + CustomStatistics.h Items.h - Statistics.h ) \ No newline at end of file diff --git a/src/Registries/CustomStatistics.h b/src/Registries/CustomStatistics.h new file mode 100644 index 000000000..c0edba191 --- /dev/null +++ b/src/Registries/CustomStatistics.h @@ -0,0 +1,125 @@ +#pragma once + +// tolua_begin +enum class CustomStatistic +{ + // tolua_end + + /* Achievements */ + AchOpenInventory, /* Taking Inventory */ + AchMineWood, /* Getting Wood */ + AchBuildWorkBench, /* Benchmarking */ + AchBuildPickaxe, /* Time to Mine! */ + AchBuildFurnace, /* Hot Topic */ + AchAcquireIron, /* Acquire Hardware */ + AchBuildHoe, /* Time to Farm! */ + AchMakeBread, /* Bake Bread */ + AchBakeCake, /* The Lie */ + AchBuildBetterPickaxe, /* Getting an Upgrade */ + AchCookFish, /* Delicious Fish */ + AchOnARail, /* On A Rail */ + AchBuildSword, /* Time to Strike! */ + AchKillEnemy, /* Monster Hunter */ + AchKillCow, /* Cow Tipper */ + AchFlyPig, /* When Pigs Fly */ + AchSnipeSkeleton, /* Sniper Duel */ + AchDiamonds, /* DIAMONDS! */ + AchPortal, /* We Need to Go Deeper */ + AchGhast, /* Return to Sender */ + AchBlazeRod, /* Into Fire */ + AchPotion, /* Local Brewery */ + AchTheEnd, /* The End? */ + AchTheEnd2, /* The End. */ + AchEnchantments, /* Enchanter */ + AchOverkill, /* Overkill */ + AchBookcase, /* Librarian */ + AchExploreAllBiomes, /* Adventuring Time */ + AchSpawnWither, /* The Beginning? */ + AchKillWither, /* The Beginning. */ + AchFullBeacon, /* Beaconator */ + AchBreedCow, /* Repopulation */ + AchDiamondsToYou, /* Diamonds to you! */ + + // tolua_begin + + /* Statistics */ + AnimalsBred, + AviateOneCm, + BellRing, + BoatOneCm, + CleanArmor, + CleanBanner, + CleanShulkerBox, + ClimbOneCm, + CrouchOneCm, + DamageAbsorbed, + DamageBlockedByShield, + DamageDealt, + DamageDealtAbsorbed, + DamageDealtResisted, + DamageResisted, + DamageTaken, + Deaths, + Drop, + EatCakeSlice, + EnchantItem, + FallOneCm, + FillCauldron, + FishCaught, + FlyOneCm, + HorseOneCm, + InspectDispenser, + InspectDropper, + InspectHopper, + InteractWithAnvil, + InteractWithBeacon, + InteractWithBlastFurnace, + InteractWithBrewingstand, + InteractWithCampfire, + InteractWithCartographyTable, + InteractWithCraftingTable, + InteractWithFurnace, + InteractWithGrindstone, + InteractWithLectern, + InteractWithLoom, + InteractWithSmithingTable, + InteractWithSmoker, + InteractWithStonecutter, + Jump, + LeaveGame, + MinecartOneCm, + MobKills, + OpenBarrel, + OpenChest, + OpenEnderchest, + OpenShulkerBox, + PigOneCm, + PlayNoteblock, + PlayOneMinute, + PlayRecord, + PlayerKills, + PotFlower, + RaidTrigger, + RaidWin, + SleepInBed, + SneakTime, + SprintOneCm, + StriderOneCm, + SwimOneCm, + TalkedToVillager, + TargetHit, + TimeSinceDeath, + TimeSinceRest, + TradedWithVillager, + TriggerTrappedChest, + TuneNoteblock, + UseCauldron, + WalkOnWaterOneCm, + WalkOneCm, + WalkUnderWaterOneCm, + + // Old ones just for compatibility + JunkFished, + TreasureFished, +}; +// tolua_end diff --git a/src/Registries/Statistics.h b/src/Registries/Statistics.h deleted file mode 100644 index c0edba191..000000000 --- a/src/Registries/Statistics.h +++ /dev/null @@ -1,125 +0,0 @@ -#pragma once - -// tolua_begin -enum class CustomStatistic -{ - // tolua_end - - /* Achievements */ - AchOpenInventory, /* Taking Inventory */ - AchMineWood, /* Getting Wood */ - AchBuildWorkBench, /* Benchmarking */ - AchBuildPickaxe, /* Time to Mine! */ - AchBuildFurnace, /* Hot Topic */ - AchAcquireIron, /* Acquire Hardware */ - AchBuildHoe, /* Time to Farm! */ - AchMakeBread, /* Bake Bread */ - AchBakeCake, /* The Lie */ - AchBuildBetterPickaxe, /* Getting an Upgrade */ - AchCookFish, /* Delicious Fish */ - AchOnARail, /* On A Rail */ - AchBuildSword, /* Time to Strike! */ - AchKillEnemy, /* Monster Hunter */ - AchKillCow, /* Cow Tipper */ - AchFlyPig, /* When Pigs Fly */ - AchSnipeSkeleton, /* Sniper Duel */ - AchDiamonds, /* DIAMONDS! */ - AchPortal, /* We Need to Go Deeper */ - AchGhast, /* Return to Sender */ - AchBlazeRod, /* Into Fire */ - AchPotion, /* Local Brewery */ - AchTheEnd, /* The End? */ - AchTheEnd2, /* The End. */ - AchEnchantments, /* Enchanter */ - AchOverkill, /* Overkill */ - AchBookcase, /* Librarian */ - AchExploreAllBiomes, /* Adventuring Time */ - AchSpawnWither, /* The Beginning? */ - AchKillWither, /* The Beginning. */ - AchFullBeacon, /* Beaconator */ - AchBreedCow, /* Repopulation */ - AchDiamondsToYou, /* Diamonds to you! */ - - // tolua_begin - - /* Statistics */ - AnimalsBred, - AviateOneCm, - BellRing, - BoatOneCm, - CleanArmor, - CleanBanner, - CleanShulkerBox, - ClimbOneCm, - CrouchOneCm, - DamageAbsorbed, - DamageBlockedByShield, - DamageDealt, - DamageDealtAbsorbed, - DamageDealtResisted, - DamageResisted, - DamageTaken, - Deaths, - Drop, - EatCakeSlice, - EnchantItem, - FallOneCm, - FillCauldron, - FishCaught, - FlyOneCm, - HorseOneCm, - InspectDispenser, - InspectDropper, - InspectHopper, - InteractWithAnvil, - InteractWithBeacon, - InteractWithBlastFurnace, - InteractWithBrewingstand, - InteractWithCampfire, - InteractWithCartographyTable, - InteractWithCraftingTable, - InteractWithFurnace, - InteractWithGrindstone, - InteractWithLectern, - InteractWithLoom, - InteractWithSmithingTable, - InteractWithSmoker, - InteractWithStonecutter, - Jump, - LeaveGame, - MinecartOneCm, - MobKills, - OpenBarrel, - OpenChest, - OpenEnderchest, - OpenShulkerBox, - PigOneCm, - PlayNoteblock, - PlayOneMinute, - PlayRecord, - PlayerKills, - PotFlower, - RaidTrigger, - RaidWin, - SleepInBed, - SneakTime, - SprintOneCm, - StriderOneCm, - SwimOneCm, - TalkedToVillager, - TargetHit, - TimeSinceDeath, - TimeSinceRest, - TradedWithVillager, - TriggerTrappedChest, - TuneNoteblock, - UseCauldron, - WalkOnWaterOneCm, - WalkOneCm, - WalkUnderWaterOneCm, - - // Old ones just for compatibility - JunkFished, - TreasureFished, -}; -// tolua_end diff --git a/src/Statistics.cpp b/src/Statistics.cpp deleted file mode 100644 index f037ec7b1..000000000 --- a/src/Statistics.cpp +++ /dev/null @@ -1,64 +0,0 @@ - -// Statistics.cpp - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#include "Statistics.h" - - - - - -bool StatisticsManager::SatisfiesPrerequisite(const CustomStatistic a_Stat) const -{ - switch (a_Stat) - { - case CustomStatistic::AchMineWood: return IsStatisticPresent(CustomStatistic::AchOpenInventory); - case CustomStatistic::AchBuildWorkBench: return IsStatisticPresent(CustomStatistic::AchMineWood); - case CustomStatistic::AchBuildHoe: return IsStatisticPresent(CustomStatistic::AchBuildWorkBench); - case CustomStatistic::AchBakeCake: return IsStatisticPresent(CustomStatistic::AchBuildHoe); - case CustomStatistic::AchMakeBread: return IsStatisticPresent(CustomStatistic::AchBuildHoe); - case CustomStatistic::AchBuildSword: return IsStatisticPresent(CustomStatistic::AchBuildWorkBench); - case CustomStatistic::AchKillCow: return IsStatisticPresent(CustomStatistic::AchBuildSword); - case CustomStatistic::AchFlyPig: return IsStatisticPresent(CustomStatistic::AchKillCow); - case CustomStatistic::AchBreedCow: return IsStatisticPresent(CustomStatistic::AchKillCow); - case CustomStatistic::AchKillEnemy: return IsStatisticPresent(CustomStatistic::AchBuildSword); - case CustomStatistic::AchSnipeSkeleton: return IsStatisticPresent(CustomStatistic::AchKillEnemy); - case CustomStatistic::AchBuildPickaxe: return IsStatisticPresent(CustomStatistic::AchBuildWorkBench); - case CustomStatistic::AchBuildBetterPickaxe: return IsStatisticPresent(CustomStatistic::AchBuildPickaxe); - case CustomStatistic::AchBuildFurnace: return IsStatisticPresent(CustomStatistic::AchBuildWorkBench); - case CustomStatistic::AchCookFish: return IsStatisticPresent(CustomStatistic::AchBuildFurnace); - case CustomStatistic::AchAcquireIron: return IsStatisticPresent(CustomStatistic::AchBuildFurnace); - case CustomStatistic::AchOnARail: return IsStatisticPresent(CustomStatistic::AchAcquireIron); - case CustomStatistic::AchDiamonds: return IsStatisticPresent(CustomStatistic::AchAcquireIron); - case CustomStatistic::AchPortal: return IsStatisticPresent(CustomStatistic::AchDiamonds); - case CustomStatistic::AchGhast: return IsStatisticPresent(CustomStatistic::AchPortal); - case CustomStatistic::AchBlazeRod: return IsStatisticPresent(CustomStatistic::AchPortal); - case CustomStatistic::AchPotion: return IsStatisticPresent(CustomStatistic::AchBlazeRod); - case CustomStatistic::AchTheEnd: return IsStatisticPresent(CustomStatistic::AchBlazeRod); - case CustomStatistic::AchTheEnd2: return IsStatisticPresent(CustomStatistic::AchTheEnd); - case CustomStatistic::AchEnchantments: return IsStatisticPresent(CustomStatistic::AchDiamonds); - case CustomStatistic::AchOverkill: return IsStatisticPresent(CustomStatistic::AchEnchantments); - case CustomStatistic::AchBookcase: return IsStatisticPresent(CustomStatistic::AchEnchantments); - case CustomStatistic::AchExploreAllBiomes: return IsStatisticPresent(CustomStatistic::AchTheEnd); - case CustomStatistic::AchSpawnWither: return IsStatisticPresent(CustomStatistic::AchTheEnd2); - case CustomStatistic::AchKillWither: return IsStatisticPresent(CustomStatistic::AchSpawnWither); - case CustomStatistic::AchFullBeacon: return IsStatisticPresent(CustomStatistic::AchKillWither); - case CustomStatistic::AchDiamondsToYou: return IsStatisticPresent(CustomStatistic::AchDiamonds); - default: UNREACHABLE("Unsupported achievement type"); - } -} - - - - - -bool StatisticsManager::IsStatisticPresent(const CustomStatistic a_Stat) const -{ - const auto Result = Custom.find(a_Stat); - if (Result != Custom.end()) - { - return Result->second > 0; - } - return false; -} diff --git a/src/Statistics.h b/src/Statistics.h deleted file mode 100644 index 38d2f0f7e..000000000 --- a/src/Statistics.h +++ /dev/null @@ -1,43 +0,0 @@ - -// Statistics.h - -#pragma once - -#include "Registries/Statistics.h" - -/* Hello fellow developer ! -In case you are trying to add new statistics to Cuberite you need to do a few things: ---------------------------------------------------------------------------- -1. add a new entry to the enum class Statistic in Registries\Statistics.h file -2. add this to serialization functions in WorldStorage\NamespaceSerializer.cpp - The String in the above is used for saving on disk! - so use the same string! - -In case you want to add a mapping of network IDs to the used stats -you will find a lua script in ../Tools/BlockTypePaletteGenerator/ExportStatMapping.lua -it will provide you with information how to use it. you need a registries.json -exported from the server https://wiki.vg/Data_Generators - - Greetings 12xx12 */ - - - - - -/** Class that manages the statistics and achievements of a single player. */ -struct StatisticsManager -{ - typedef unsigned StatValue; - - // TODO: Block tallies, entities killed, all the others - - std::unordered_map Custom; - - /** Returns whether the prerequisite for awarding an achievement are satisfied. */ - bool SatisfiesPrerequisite(CustomStatistic a_Stat) const; - -private: - - /** Returns if a statistic is both present and has nonzero value. */ - bool IsStatisticPresent(CustomStatistic a_Stat) const; -}; diff --git a/src/StatisticsManager.cpp b/src/StatisticsManager.cpp new file mode 100644 index 000000000..0b343cb76 --- /dev/null +++ b/src/StatisticsManager.cpp @@ -0,0 +1,64 @@ + +// Statistics.cpp + +#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules + +#include "StatisticsManager.h" + + + + + +bool StatisticsManager::SatisfiesPrerequisite(const CustomStatistic a_Stat) const +{ + switch (a_Stat) + { + case CustomStatistic::AchMineWood: return IsStatisticPresent(CustomStatistic::AchOpenInventory); + case CustomStatistic::AchBuildWorkBench: return IsStatisticPresent(CustomStatistic::AchMineWood); + case CustomStatistic::AchBuildHoe: return IsStatisticPresent(CustomStatistic::AchBuildWorkBench); + case CustomStatistic::AchBakeCake: return IsStatisticPresent(CustomStatistic::AchBuildHoe); + case CustomStatistic::AchMakeBread: return IsStatisticPresent(CustomStatistic::AchBuildHoe); + case CustomStatistic::AchBuildSword: return IsStatisticPresent(CustomStatistic::AchBuildWorkBench); + case CustomStatistic::AchKillCow: return IsStatisticPresent(CustomStatistic::AchBuildSword); + case CustomStatistic::AchFlyPig: return IsStatisticPresent(CustomStatistic::AchKillCow); + case CustomStatistic::AchBreedCow: return IsStatisticPresent(CustomStatistic::AchKillCow); + case CustomStatistic::AchKillEnemy: return IsStatisticPresent(CustomStatistic::AchBuildSword); + case CustomStatistic::AchSnipeSkeleton: return IsStatisticPresent(CustomStatistic::AchKillEnemy); + case CustomStatistic::AchBuildPickaxe: return IsStatisticPresent(CustomStatistic::AchBuildWorkBench); + case CustomStatistic::AchBuildBetterPickaxe: return IsStatisticPresent(CustomStatistic::AchBuildPickaxe); + case CustomStatistic::AchBuildFurnace: return IsStatisticPresent(CustomStatistic::AchBuildWorkBench); + case CustomStatistic::AchCookFish: return IsStatisticPresent(CustomStatistic::AchBuildFurnace); + case CustomStatistic::AchAcquireIron: return IsStatisticPresent(CustomStatistic::AchBuildFurnace); + case CustomStatistic::AchOnARail: return IsStatisticPresent(CustomStatistic::AchAcquireIron); + case CustomStatistic::AchDiamonds: return IsStatisticPresent(CustomStatistic::AchAcquireIron); + case CustomStatistic::AchPortal: return IsStatisticPresent(CustomStatistic::AchDiamonds); + case CustomStatistic::AchGhast: return IsStatisticPresent(CustomStatistic::AchPortal); + case CustomStatistic::AchBlazeRod: return IsStatisticPresent(CustomStatistic::AchPortal); + case CustomStatistic::AchPotion: return IsStatisticPresent(CustomStatistic::AchBlazeRod); + case CustomStatistic::AchTheEnd: return IsStatisticPresent(CustomStatistic::AchBlazeRod); + case CustomStatistic::AchTheEnd2: return IsStatisticPresent(CustomStatistic::AchTheEnd); + case CustomStatistic::AchEnchantments: return IsStatisticPresent(CustomStatistic::AchDiamonds); + case CustomStatistic::AchOverkill: return IsStatisticPresent(CustomStatistic::AchEnchantments); + case CustomStatistic::AchBookcase: return IsStatisticPresent(CustomStatistic::AchEnchantments); + case CustomStatistic::AchExploreAllBiomes: return IsStatisticPresent(CustomStatistic::AchTheEnd); + case CustomStatistic::AchSpawnWither: return IsStatisticPresent(CustomStatistic::AchTheEnd2); + case CustomStatistic::AchKillWither: return IsStatisticPresent(CustomStatistic::AchSpawnWither); + case CustomStatistic::AchFullBeacon: return IsStatisticPresent(CustomStatistic::AchKillWither); + case CustomStatistic::AchDiamondsToYou: return IsStatisticPresent(CustomStatistic::AchDiamonds); + default: UNREACHABLE("Unsupported achievement type"); + } +} + + + + + +bool StatisticsManager::IsStatisticPresent(const CustomStatistic a_Stat) const +{ + const auto Result = Custom.find(a_Stat); + if (Result != Custom.end()) + { + return Result->second > 0; + } + return false; +} diff --git a/src/StatisticsManager.h b/src/StatisticsManager.h new file mode 100644 index 000000000..288f4aabc --- /dev/null +++ b/src/StatisticsManager.h @@ -0,0 +1,47 @@ + +// Statistics.h + +/* Hello fellow developer ! +In case you are trying to add new statistics to Cuberite you need to do a few things: +--------------------------------------------------------------------------- +1. add a new entry to the enum class Statistic in Registries\Statistics.h file +2. add this to serialization functions in WorldStorage\NamespaceSerializer.cpp + The String in the above is used for saving on disk! + so use the same string! + +In case you want to add a mapping of network IDs to the used stats +you will find a lua script in ../Tools/BlockTypePaletteGenerator/ExportStatMapping.lua +it will provide you with information how to use it. you need a registries.json +exported from the server https://wiki.vg/Data_Generators + + Greetings 12xx12 */ + + + + + +#pragma once + +#include "Registries/CustomStatistics.h" + + + + + +/** Class that manages the statistics and achievements of a single player. */ +struct StatisticsManager +{ + typedef unsigned StatValue; + + // TODO: Block tallies, entities killed, all the others + + std::unordered_map Custom; + + /** Returns whether the prerequisite for awarding an achievement are satisfied. */ + bool SatisfiesPrerequisite(CustomStatistic a_Stat) const; + +private: + + /** Returns if a statistic is both present and has nonzero value. */ + bool IsStatisticPresent(CustomStatistic a_Stat) const; +}; diff --git a/src/Stopwatch.h b/src/Stopwatch.h deleted file mode 100644 index 7676b3856..000000000 --- a/src/Stopwatch.h +++ /dev/null @@ -1,38 +0,0 @@ - -// Stopwatch.h - -// Implements the cStopwatch class that measures and logs time between its creation and destruction - - - - - -#pragma once - - - - - -class cStopwatch -{ -public: - cStopwatch(const AString & a_Name): - m_Name(a_Name), - m_StartTime(std::chrono::high_resolution_clock::now()) - { - } - - ~cStopwatch() - { - auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - m_StartTime).count(); - LOG("Stopwatch: %s took %.03f sec", m_Name, static_cast(duration) / 1000); - } - -protected: - AString m_Name; - std::chrono::high_resolution_clock::time_point m_StartTime; -}; - - - - diff --git a/src/WorldStorage/CMakeLists.txt b/src/WorldStorage/CMakeLists.txt index af363c97f..13675c5e1 100644 --- a/src/WorldStorage/CMakeLists.txt +++ b/src/WorldStorage/CMakeLists.txt @@ -9,7 +9,7 @@ target_sources( NBTChunkSerializer.cpp SchematicFileSerializer.cpp ScoreboardSerializer.cpp - StatSerializer.cpp + StatisticsSerializer.cpp WSSAnvil.cpp WorldStorage.cpp @@ -21,7 +21,7 @@ target_sources( NBTChunkSerializer.h SchematicFileSerializer.h ScoreboardSerializer.h - StatSerializer.h + StatisticsSerializer.h WSSAnvil.h WorldStorage.h ) diff --git a/src/WorldStorage/NamespaceSerializer.h b/src/WorldStorage/NamespaceSerializer.h index c521db201..6ec6bc6f7 100644 --- a/src/WorldStorage/NamespaceSerializer.h +++ b/src/WorldStorage/NamespaceSerializer.h @@ -2,7 +2,7 @@ #include "Defines.h" #include "Mobs/MonsterTypes.h" -#include "Registries/Statistics.h" +#include "Registries/CustomStatistics.h" namespace NamespaceSerializer { diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp deleted file mode 100644 index 5432f6ab9..000000000 --- a/src/WorldStorage/StatSerializer.cpp +++ /dev/null @@ -1,214 +0,0 @@ - -// StatSerializer.cpp - - -#include "Globals.h" -#include "../Statistics.h" -#include "StatSerializer.h" -#include "NamespaceSerializer.h" - -#include - - - - - -namespace StatSerializer -{ - static auto MakeStatisticsDirectory(const std::string & WorldPath, std::string && FileName) - { - // Even though stats are shared between worlds, they are (usually) saved - // inside the folder of the default world. - - // Path to the world's statistics folder. - const auto Path = WorldPath + cFile::GetPathSeparator() + "stats"; - - // Ensure that the directory exists. - cFile::CreateFolder(Path); - - return Path + cFile::GetPathSeparator() + std::move(FileName) + ".json"; - } - - - - - - static void SaveStatToJSON(const StatisticsManager & Manager, Json::Value & a_Out) - { - if (Manager.Custom.empty()) - { - // Avoid saving "custom": null to disk: - return; - } - - auto & Custom = a_Out["custom"]; - for (const auto & [Statistic, Value] : Manager.Custom) - { - Custom[NamespaceSerializer::From(Statistic).data()] = Value; - } - } - - - - - - static void LoadLegacyFromJSON(StatisticsManager & Manager, const Json::Value & In) - { - // Upgrade mapping from pre-1.13 names. TODO: remove on 2020-09-18 - static const std::unordered_map LegacyMapping - { - { "achievement.openInventory", CustomStatistic::AchOpenInventory }, - { "achievement.mineWood", CustomStatistic::AchMineWood }, - { "achievement.buildWorkBench", CustomStatistic::AchBuildWorkBench }, - { "achievement.buildPickaxe", CustomStatistic::AchBuildPickaxe }, - { "achievement.buildFurnace", CustomStatistic::AchBuildFurnace }, - { "achievement.acquireIron", CustomStatistic::AchAcquireIron }, - { "achievement.buildHoe", CustomStatistic::AchBuildHoe }, - { "achievement.makeBread", CustomStatistic::AchMakeBread }, - { "achievement.bakeCake", CustomStatistic::AchBakeCake }, - { "achievement.buildBetterPickaxe", CustomStatistic::AchBuildBetterPickaxe }, - { "achievement.cookFish", CustomStatistic::AchCookFish }, - { "achievement.onARail", CustomStatistic::AchOnARail }, - { "achievement.buildSword", CustomStatistic::AchBuildSword }, - { "achievement.killEnemy", CustomStatistic::AchKillEnemy }, - { "achievement.killCow", CustomStatistic::AchKillCow }, - { "achievement.flyPig", CustomStatistic::AchFlyPig }, - { "achievement.snipeSkeleton", CustomStatistic::AchSnipeSkeleton }, - { "achievement.diamonds", CustomStatistic::AchDiamonds }, - { "achievement.portal", CustomStatistic::AchPortal }, - { "achievement.ghast", CustomStatistic::AchGhast }, - { "achievement.blazeRod", CustomStatistic::AchBlazeRod }, - { "achievement.potion", CustomStatistic::AchPotion }, - { "achievement.theEnd", CustomStatistic::AchTheEnd }, - { "achievement.theEnd2", CustomStatistic::AchTheEnd2 }, - { "achievement.enchantments", CustomStatistic::AchEnchantments }, - { "achievement.overkill", CustomStatistic::AchOverkill }, - { "achievement.bookcase", CustomStatistic::AchBookcase }, - { "achievement.exploreAllBiomes", CustomStatistic::AchExploreAllBiomes }, - { "achievement.spawnWither", CustomStatistic::AchSpawnWither }, - { "achievement.killWither", CustomStatistic::AchKillWither }, - { "achievement.fullBeacon", CustomStatistic::AchFullBeacon }, - { "achievement.breedCow", CustomStatistic::AchBreedCow }, - { "achievement.diamondsToYou", CustomStatistic::AchDiamondsToYou }, - { "stat.animalsBred", CustomStatistic::AnimalsBred }, - { "stat.boatOneCm", CustomStatistic::BoatOneCm }, - { "stat.climbOneCm", CustomStatistic::ClimbOneCm }, - { "stat.crouchOneCm", CustomStatistic::CrouchOneCm }, - { "stat.damageDealt", CustomStatistic::DamageDealt }, - { "stat.damageTaken", CustomStatistic::DamageTaken }, - { "stat.deaths", CustomStatistic::Deaths }, - { "stat.drop", CustomStatistic::Drop }, - { "stat.fallOneCm", CustomStatistic::FallOneCm }, - { "stat.fishCaught", CustomStatistic::FishCaught }, - { "stat.flyOneCm", CustomStatistic::FlyOneCm }, - { "stat.horseOneCm", CustomStatistic::HorseOneCm }, - { "stat.jump", CustomStatistic::Jump }, - { "stat.leaveGame", CustomStatistic::LeaveGame }, - { "stat.minecartOneCm", CustomStatistic::MinecartOneCm }, - { "stat.mobKills", CustomStatistic::MobKills }, - { "stat.pigOneCm", CustomStatistic::PigOneCm }, - { "stat.playerKills", CustomStatistic::PlayerKills }, - { "stat.playOneMinute", CustomStatistic::PlayOneMinute }, - { "stat.sprintOneCm", CustomStatistic::SprintOneCm }, - { "stat.swimOneCm", CustomStatistic::SwimOneCm }, - { "stat.talkedToVillager", CustomStatistic::TalkedToVillager }, - { "stat.timeSinceDeath", CustomStatistic::TimeSinceDeath }, - { "stat.tradedWithVillager", CustomStatistic::TradedWithVillager }, - { "stat.walkOneCm", CustomStatistic::WalkOneCm }, - { "stat.diveOneCm", CustomStatistic::WalkUnderWaterOneCm }, - { "stat.armorCleaned", CustomStatistic::CleanArmor }, - { "stat.bannerCleaned", CustomStatistic::CleanBanner }, - { "stat.cakeSlicesEaten", CustomStatistic::EatCakeSlice }, - { "stat.itemEnchanted", CustomStatistic::EnchantItem }, - { "stat.cauldronFilled", CustomStatistic::FillCauldron }, - { "stat.dispenserInspected", CustomStatistic::InspectDispenser }, - { "stat.dropperInspected", CustomStatistic::InspectDropper }, - { "stat.hopperInspected", CustomStatistic::InspectHopper }, - { "stat.beaconInteraction", CustomStatistic::InteractWithBeacon }, - { "stat.brewingstandInteraction", CustomStatistic::InteractWithBrewingstand }, - { "stat.craftingTableInteraction", CustomStatistic::InteractWithCraftingTable }, - { "stat.furnaceInteraction", CustomStatistic::InteractWithFurnace }, - { "stat.chestOpened", CustomStatistic::OpenChest }, - { "stat.enderchestOpened", CustomStatistic::OpenEnderchest }, - { "stat.noteblockPlayed", CustomStatistic::PlayNoteblock }, - { "stat.recordPlayed", CustomStatistic::PlayRecord }, - { "stat.flowerPotted", CustomStatistic::PotFlower }, - { "stat.trappedChestTriggered", CustomStatistic::TriggerTrappedChest }, - { "stat.noteblockTuned", CustomStatistic::TuneNoteblock }, - { "stat.cauldronUsed", CustomStatistic::UseCauldron }, - { "stat.aviateOneCm", CustomStatistic::AviateOneCm }, - { "stat.sleepInBed", CustomStatistic::SleepInBed }, - { "stat.sneakTime", CustomStatistic::SneakTime } - }; - - for (auto Entry = In.begin(); Entry != In.end(); ++Entry) - { - const auto & Key = Entry.key().asString(); - const auto FindResult = LegacyMapping.find(Key); - - if ((FindResult != LegacyMapping.end()) && Entry->isInt()) - { - Manager.Custom[FindResult->second] = Entry->asUInt(); - } - } - } - - - - - - static void LoadCustomStatFromJSON(StatisticsManager & Manager, const Json::Value & a_In) - { - for (auto it = a_In.begin(); it != a_In.end(); ++it) - { - const auto & Key = it.key().asString(); - const auto StatInfo = NamespaceSerializer::SplitNamespacedID(Key); - if (StatInfo.first == NamespaceSerializer::Namespace::Unknown) - { - // Ignore non-Vanilla, non-Cuberite namespaces for now: - continue; - } - - const auto & StatName = StatInfo.second; - try - { - Manager.Custom[NamespaceSerializer::ToCustomStatistic(StatName)] = it->asUInt(); - } - catch (const std::out_of_range &) - { - FLOGWARNING("Invalid statistic type \"{}\"", StatName); - } - catch (const Json::LogicError &) - { - FLOGWARNING("Invalid statistic value for type \"{}\"", StatName); - } - } - } - - - - - - void Load(StatisticsManager & Manager, const std::string & WorldPath, std::string && FileName) - { - Json::Value Root; - InputFileStream(MakeStatisticsDirectory(WorldPath, std::move(FileName))) >> Root; - - LoadLegacyFromJSON(Manager, Root); - LoadCustomStatFromJSON(Manager, Root["stats"]["custom"]); - } - - - - - - void Save(const StatisticsManager & Manager, const std::string & WorldPath, std::string && FileName) - { - Json::Value Root; - - SaveStatToJSON(Manager, Root["stats"]); - Root["DataVersion"] = NamespaceSerializer::DataVersion(); - - OutputFileStream(MakeStatisticsDirectory(WorldPath, std::move(FileName))) << Root; - } -} diff --git a/src/WorldStorage/StatSerializer.h b/src/WorldStorage/StatSerializer.h deleted file mode 100644 index a178db79c..000000000 --- a/src/WorldStorage/StatSerializer.h +++ /dev/null @@ -1,30 +0,0 @@ - -// StatSerializer.h - -// Declares the cStatSerializer class that is used for saving stats into JSON - - - - - -#pragma once - - - - - -struct StatisticsManager; -namespace Json { class Value; } - - - - - -namespace StatSerializer -{ - /* Try to load the player statistics. */ - void Load(StatisticsManager & Manager, const std::string & WorldPath, std::string && FileName); - - /* Try to save the player statistics. */ - void Save(const StatisticsManager & Manager, const std::string & WorldPath, std::string && FileName); -} diff --git a/src/WorldStorage/StatisticsSerializer.cpp b/src/WorldStorage/StatisticsSerializer.cpp new file mode 100644 index 000000000..34621d81c --- /dev/null +++ b/src/WorldStorage/StatisticsSerializer.cpp @@ -0,0 +1,214 @@ + +// StatisticsSerializer.cpp + + +#include "Globals.h" +#include "StatisticsManager.h" +#include "StatisticsSerializer.h" +#include "NamespaceSerializer.h" + +#include + + + + + +namespace StatisticsSerializer +{ + static auto MakeStatisticsDirectory(const std::string & WorldPath, std::string && FileName) + { + // Even though stats are shared between worlds, they are (usually) saved + // inside the folder of the default world. + + // Path to the world's statistics folder. + const auto Path = WorldPath + cFile::GetPathSeparator() + "stats"; + + // Ensure that the directory exists. + cFile::CreateFolder(Path); + + return Path + cFile::GetPathSeparator() + std::move(FileName) + ".json"; + } + + + + + + static void SaveStatToJSON(const StatisticsManager & Manager, Json::Value & a_Out) + { + if (Manager.Custom.empty()) + { + // Avoid saving "custom": null to disk: + return; + } + + auto & Custom = a_Out["custom"]; + for (const auto & [Statistic, Value] : Manager.Custom) + { + Custom[NamespaceSerializer::From(Statistic).data()] = Value; + } + } + + + + + + static void LoadLegacyFromJSON(StatisticsManager & Manager, const Json::Value & In) + { + // Upgrade mapping from pre-1.13 names. TODO: remove on 2020-09-18 + static const std::unordered_map LegacyMapping + { + { "achievement.openInventory", CustomStatistic::AchOpenInventory }, + { "achievement.mineWood", CustomStatistic::AchMineWood }, + { "achievement.buildWorkBench", CustomStatistic::AchBuildWorkBench }, + { "achievement.buildPickaxe", CustomStatistic::AchBuildPickaxe }, + { "achievement.buildFurnace", CustomStatistic::AchBuildFurnace }, + { "achievement.acquireIron", CustomStatistic::AchAcquireIron }, + { "achievement.buildHoe", CustomStatistic::AchBuildHoe }, + { "achievement.makeBread", CustomStatistic::AchMakeBread }, + { "achievement.bakeCake", CustomStatistic::AchBakeCake }, + { "achievement.buildBetterPickaxe", CustomStatistic::AchBuildBetterPickaxe }, + { "achievement.cookFish", CustomStatistic::AchCookFish }, + { "achievement.onARail", CustomStatistic::AchOnARail }, + { "achievement.buildSword", CustomStatistic::AchBuildSword }, + { "achievement.killEnemy", CustomStatistic::AchKillEnemy }, + { "achievement.killCow", CustomStatistic::AchKillCow }, + { "achievement.flyPig", CustomStatistic::AchFlyPig }, + { "achievement.snipeSkeleton", CustomStatistic::AchSnipeSkeleton }, + { "achievement.diamonds", CustomStatistic::AchDiamonds }, + { "achievement.portal", CustomStatistic::AchPortal }, + { "achievement.ghast", CustomStatistic::AchGhast }, + { "achievement.blazeRod", CustomStatistic::AchBlazeRod }, + { "achievement.potion", CustomStatistic::AchPotion }, + { "achievement.theEnd", CustomStatistic::AchTheEnd }, + { "achievement.theEnd2", CustomStatistic::AchTheEnd2 }, + { "achievement.enchantments", CustomStatistic::AchEnchantments }, + { "achievement.overkill", CustomStatistic::AchOverkill }, + { "achievement.bookcase", CustomStatistic::AchBookcase }, + { "achievement.exploreAllBiomes", CustomStatistic::AchExploreAllBiomes }, + { "achievement.spawnWither", CustomStatistic::AchSpawnWither }, + { "achievement.killWither", CustomStatistic::AchKillWither }, + { "achievement.fullBeacon", CustomStatistic::AchFullBeacon }, + { "achievement.breedCow", CustomStatistic::AchBreedCow }, + { "achievement.diamondsToYou", CustomStatistic::AchDiamondsToYou }, + { "stat.animalsBred", CustomStatistic::AnimalsBred }, + { "stat.boatOneCm", CustomStatistic::BoatOneCm }, + { "stat.climbOneCm", CustomStatistic::ClimbOneCm }, + { "stat.crouchOneCm", CustomStatistic::CrouchOneCm }, + { "stat.damageDealt", CustomStatistic::DamageDealt }, + { "stat.damageTaken", CustomStatistic::DamageTaken }, + { "stat.deaths", CustomStatistic::Deaths }, + { "stat.drop", CustomStatistic::Drop }, + { "stat.fallOneCm", CustomStatistic::FallOneCm }, + { "stat.fishCaught", CustomStatistic::FishCaught }, + { "stat.flyOneCm", CustomStatistic::FlyOneCm }, + { "stat.horseOneCm", CustomStatistic::HorseOneCm }, + { "stat.jump", CustomStatistic::Jump }, + { "stat.leaveGame", CustomStatistic::LeaveGame }, + { "stat.minecartOneCm", CustomStatistic::MinecartOneCm }, + { "stat.mobKills", CustomStatistic::MobKills }, + { "stat.pigOneCm", CustomStatistic::PigOneCm }, + { "stat.playerKills", CustomStatistic::PlayerKills }, + { "stat.playOneMinute", CustomStatistic::PlayOneMinute }, + { "stat.sprintOneCm", CustomStatistic::SprintOneCm }, + { "stat.swimOneCm", CustomStatistic::SwimOneCm }, + { "stat.talkedToVillager", CustomStatistic::TalkedToVillager }, + { "stat.timeSinceDeath", CustomStatistic::TimeSinceDeath }, + { "stat.tradedWithVillager", CustomStatistic::TradedWithVillager }, + { "stat.walkOneCm", CustomStatistic::WalkOneCm }, + { "stat.diveOneCm", CustomStatistic::WalkUnderWaterOneCm }, + { "stat.armorCleaned", CustomStatistic::CleanArmor }, + { "stat.bannerCleaned", CustomStatistic::CleanBanner }, + { "stat.cakeSlicesEaten", CustomStatistic::EatCakeSlice }, + { "stat.itemEnchanted", CustomStatistic::EnchantItem }, + { "stat.cauldronFilled", CustomStatistic::FillCauldron }, + { "stat.dispenserInspected", CustomStatistic::InspectDispenser }, + { "stat.dropperInspected", CustomStatistic::InspectDropper }, + { "stat.hopperInspected", CustomStatistic::InspectHopper }, + { "stat.beaconInteraction", CustomStatistic::InteractWithBeacon }, + { "stat.brewingstandInteraction", CustomStatistic::InteractWithBrewingstand }, + { "stat.craftingTableInteraction", CustomStatistic::InteractWithCraftingTable }, + { "stat.furnaceInteraction", CustomStatistic::InteractWithFurnace }, + { "stat.chestOpened", CustomStatistic::OpenChest }, + { "stat.enderchestOpened", CustomStatistic::OpenEnderchest }, + { "stat.noteblockPlayed", CustomStatistic::PlayNoteblock }, + { "stat.recordPlayed", CustomStatistic::PlayRecord }, + { "stat.flowerPotted", CustomStatistic::PotFlower }, + { "stat.trappedChestTriggered", CustomStatistic::TriggerTrappedChest }, + { "stat.noteblockTuned", CustomStatistic::TuneNoteblock }, + { "stat.cauldronUsed", CustomStatistic::UseCauldron }, + { "stat.aviateOneCm", CustomStatistic::AviateOneCm }, + { "stat.sleepInBed", CustomStatistic::SleepInBed }, + { "stat.sneakTime", CustomStatistic::SneakTime } + }; + + for (auto Entry = In.begin(); Entry != In.end(); ++Entry) + { + const auto & Key = Entry.key().asString(); + const auto FindResult = LegacyMapping.find(Key); + + if ((FindResult != LegacyMapping.end()) && Entry->isInt()) + { + Manager.Custom[FindResult->second] = Entry->asUInt(); + } + } + } + + + + + + static void LoadCustomStatFromJSON(StatisticsManager & Manager, const Json::Value & a_In) + { + for (auto it = a_In.begin(); it != a_In.end(); ++it) + { + const auto & Key = it.key().asString(); + const auto StatInfo = NamespaceSerializer::SplitNamespacedID(Key); + if (StatInfo.first == NamespaceSerializer::Namespace::Unknown) + { + // Ignore non-Vanilla, non-Cuberite namespaces for now: + continue; + } + + const auto & StatName = StatInfo.second; + try + { + Manager.Custom[NamespaceSerializer::ToCustomStatistic(StatName)] = it->asUInt(); + } + catch (const std::out_of_range &) + { + FLOGWARNING("Invalid statistic type \"{}\"", StatName); + } + catch (const Json::LogicError &) + { + FLOGWARNING("Invalid statistic value for type \"{}\"", StatName); + } + } + } + + + + + + void Load(StatisticsManager & Manager, const std::string & WorldPath, std::string && FileName) + { + Json::Value Root; + InputFileStream(MakeStatisticsDirectory(WorldPath, std::move(FileName))) >> Root; + + LoadLegacyFromJSON(Manager, Root); + LoadCustomStatFromJSON(Manager, Root["stats"]["custom"]); + } + + + + + + void Save(const StatisticsManager & Manager, const std::string & WorldPath, std::string && FileName) + { + Json::Value Root; + + SaveStatToJSON(Manager, Root["stats"]); + Root["DataVersion"] = NamespaceSerializer::DataVersion(); + + OutputFileStream(MakeStatisticsDirectory(WorldPath, std::move(FileName))) << Root; + } +} diff --git a/src/WorldStorage/StatisticsSerializer.h b/src/WorldStorage/StatisticsSerializer.h new file mode 100644 index 000000000..1ccc55dbd --- /dev/null +++ b/src/WorldStorage/StatisticsSerializer.h @@ -0,0 +1,30 @@ + +// StatisticsSerializer.h + +// Declares the cStatSerializer class that is used for saving stats into JSON + + + + + +#pragma once + + + + + +struct StatisticsManager; +namespace Json { class Value; } + + + + + +namespace StatisticsSerializer +{ + /* Try to load the player statistics. */ + void Load(StatisticsManager & Manager, const std::string & WorldPath, std::string && FileName); + + /* Try to save the player statistics. */ + void Save(const StatisticsManager & Manager, const std::string & WorldPath, std::string && FileName); +} -- cgit v1.2.3