| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2016 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | http://www.blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | #include "replayhandler.hpp" |
| 25 | #include "nlohmann/json.hpp" |
| 26 | #include <sstream> |
| 27 | #include "sago/SagoMisc.hpp" |
| 28 | |
| 29 | using json = nlohmann::json; |
| 30 | |
| 31 | static std::tm GetLocalTime() { |
| 32 | std::time_t t = std::time(nullptr); |
| 33 | std::tm ret = *std::localtime(&t); |
| 34 | return ret; |
| 35 | } |
| 36 | |
| 37 | static std::string CreateFileName(const std::tm& t ) { |
| 38 | char buffer[200]; |
| 39 | snprintf(buffer, sizeof(buffer), "replays/blockattack_game_%i-%02i-%02iT%02i_%02i_%02i_AUTO.replay", t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); |
| 40 | std::string ret = buffer; |
| 41 | return ret; |
| 42 | } |
| 43 | |
| 44 | static void SaveReplayToFile(const SavedReplayStruct& sr, const std::string& filename) { |
| 45 | json j = json{ |
| 46 | {"savedReplay", sr} |
| 47 | }; |
| 48 | std::string s = j.dump(4); |
| 49 | sago::WriteFileContent(filename.c_str(), s); |
| 50 | } |
| 51 | |
| 52 | static void LoadReplayFromPhysFile(SavedReplayStruct& sr, const std::string& filename) { |
| 53 | std::string filecontent = sago::GetFileContent(filename.c_str()); |
| 54 | json j = json::parse(filecontent); |
| 55 | j.at("savedReplay").get_to(sr); |
| 56 | } |
| 57 | |
| 58 | void LoadReplay(const std::string& filename, BlockGameInfo& game1, BlockGameInfo& game2) { |
| 59 | SavedReplayStruct sr; |
| 60 | LoadReplayFromPhysFile(sr, filename); |
| 61 | if (sr.playerInfo.size() > 0) { |
| 62 | game1 = sr.playerInfo.at(0); |
| 63 | } |
| 64 | if (sr.playerInfo.size() > 1) { |
| 65 | game2 = sr.playerInfo.at(1); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void SaveReplay(const BlockGameInfo& game1) { |
| 70 | SavedReplayStruct sr; |
| 71 | sr.numberOfPlayers = 1; |
| 72 | sr.playerInfo.push_back(game1); |
| 73 | std::tm t = GetLocalTime(); |
| 74 | std::string filename = CreateFileName(t); |
| 75 | SaveReplayToFile(sr, filename); |
| 76 | } |
| 77 | |
| 78 | void SaveReplay(const BlockGameInfo& game1, const BlockGameInfo& game2) { |
| 79 | SavedReplayStruct sr; |
| 80 | sr.numberOfPlayers = 2; |
| 81 | sr.playerInfo.push_back(game1); |
| 82 | sr.playerInfo.push_back(game2); |
| 83 | std::tm t = GetLocalTime(); |
| 84 | std::string filename = CreateFileName(t); |
| 85 | SaveReplayToFile(sr, filename); |
| 86 | } |