rollback.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <string>
  18. #include "irr_v3d.h"
  19. #include "rollback_interface.h"
  20. #include <list>
  21. #include <vector>
  22. #include "sqlite3.h"
  23. class IGameDef;
  24. struct ActionRow;
  25. struct Entity;
  26. class RollbackManager: public IRollbackManager
  27. {
  28. public:
  29. RollbackManager(const std::string & world_path, IGameDef * gamedef);
  30. ~RollbackManager();
  31. void reportAction(const RollbackAction & action_);
  32. std::string getActor();
  33. bool isActorGuess();
  34. void setActor(const std::string & actor, bool is_guess);
  35. std::string getSuspect(v3s16 p, float nearness_shortcut,
  36. float min_nearness);
  37. void flush();
  38. void addAction(const RollbackAction & action);
  39. std::list<RollbackAction> getEntriesSince(time_t first_time);
  40. std::list<RollbackAction> getNodeActors(v3s16 pos, int range,
  41. time_t seconds, int limit);
  42. std::list<RollbackAction> getRevertActions(
  43. const std::string & actor_filter, time_t seconds);
  44. private:
  45. void registerNewActor(const int id, const std::string & name);
  46. void registerNewNode(const int id, const std::string & name);
  47. int getActorId(const std::string & name);
  48. int getNodeId(const std::string & name);
  49. const char * getActorName(const int id);
  50. const char * getNodeName(const int id);
  51. bool createTables();
  52. bool initDatabase();
  53. bool registerRow(const ActionRow & row);
  54. const std::list<ActionRow> actionRowsFromSelect(sqlite3_stmt * stmt);
  55. ActionRow actionRowFromRollbackAction(const RollbackAction & action);
  56. const std::list<RollbackAction> rollbackActionsFromActionRows(
  57. const std::list<ActionRow> & rows);
  58. const std::list<ActionRow> getRowsSince(time_t firstTime,
  59. const std::string & actor);
  60. const std::list<ActionRow> getRowsSince_range(time_t firstTime, v3s16 p,
  61. int range, int limit);
  62. const std::list<RollbackAction> getActionsSince_range(time_t firstTime, v3s16 p,
  63. int range, int limit);
  64. const std::list<RollbackAction> getActionsSince(time_t firstTime,
  65. const std::string & actor = "");
  66. void migrate(const std::string & filepath);
  67. static float getSuspectNearness(bool is_guess, v3s16 suspect_p,
  68. time_t suspect_t, v3s16 action_p, time_t action_t);
  69. IGameDef *gamedef = nullptr;
  70. std::string current_actor;
  71. bool current_actor_is_guess = false;
  72. std::list<RollbackAction> action_todisk_buffer;
  73. std::list<RollbackAction> action_latest_buffer;
  74. std::string database_path;
  75. sqlite3 * db;
  76. sqlite3_stmt * stmt_insert;
  77. sqlite3_stmt * stmt_replace;
  78. sqlite3_stmt * stmt_select;
  79. sqlite3_stmt * stmt_select_range;
  80. sqlite3_stmt * stmt_select_withActor;
  81. sqlite3_stmt * stmt_knownActor_select;
  82. sqlite3_stmt * stmt_knownActor_insert;
  83. sqlite3_stmt * stmt_knownNode_select;
  84. sqlite3_stmt * stmt_knownNode_insert;
  85. std::vector<Entity> knownActors;
  86. std::vector<Entity> knownNodes;
  87. };