3 Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "util/serialize.h"
29 #include "util/string.h"
31 #include "util/numeric.h"
32 #include "inventorymanager.h" // deserializing InventoryLocations
34 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
36 // Get nearness factor for subject's action for this action
37 // Return value: 0 = impossible, >0 = factor
38 static float getSuspectNearness(bool is_guess, v3s16 suspect_p, int suspect_t,
39 v3s16 action_p, int action_t)
41 // Suspect cannot cause things in the past
42 if(action_t < suspect_t)
43 return 0; // 0 = cannot be
46 // Distance (1 node = -1 point)
47 f -= 1.0 * intToFloat(suspect_p, 1).getDistanceFrom(intToFloat(action_p, 1));
48 // Time (1 second = -1 point)
49 f -= 1.0 * (action_t - suspect_t);
50 // If is a guess, halve the points
59 class RollbackManager: public IRollbackManager
62 // IRollbackManager interface
64 void reportAction(const RollbackAction &action_)
66 // Ignore if not important
67 if(!action_.isImportant(m_gamedef))
69 RollbackAction action = action_;
70 action.unix_time = time(0);
72 action.actor = m_current_actor;
73 action.actor_is_guess = m_current_actor_is_guess;
74 // If actor is not known, find out suspect or cancel
75 if(action.actor.empty()){
77 if(!action.getPosition(&p))
79 // 60s default timeframe, 95 points shortcut
80 action.actor = getSuspect(p, 60, 95);
81 if(action.actor.empty())
83 action.actor_is_guess = true;
85 infostream<<"RollbackManager::reportAction():"
86 <<" time="<<action.unix_time
87 <<" actor=\""<<action.actor<<"\""
88 <<(action.actor_is_guess?" (guess)":"")
89 <<" action="<<action.toString()
93 std::string getActor()
95 return m_current_actor;
99 return m_current_actor_is_guess;
101 void setActor(const std::string &actor, bool is_guess)
103 m_current_actor = actor;
104 m_current_actor_is_guess = is_guess;
106 std::string getSuspect(v3s16 p, int max_time, float nearness_shortcut)
108 if(m_current_actor != "")
109 return m_current_actor;
110 int cur_time = time(0);
111 int first_time = cur_time - max_time;
112 RollbackAction likely_suspect;
113 float likely_suspect_nearness = 0;
114 for(std::list<RollbackAction>::const_reverse_iterator
115 i = m_action_latest_buffer.rbegin();
116 i != m_action_latest_buffer.rend(); i++)
118 if(i->unix_time < first_time)
122 // Find position of suspect or continue
124 if(!i->getPosition(&suspect_p))
126 float f = getSuspectNearness(i->actor_is_guess, suspect_p,
127 i->unix_time, p, cur_time);
128 if(f > likely_suspect_nearness){
129 likely_suspect_nearness = f;
131 if(likely_suspect_nearness >= nearness_shortcut)
135 // No likely suspect was found
136 if(likely_suspect_nearness == 0)
138 // Likely suspect was found
139 return likely_suspect.actor;
143 infostream<<"RollbackManager::flush()"<<std::endl;
144 std::ofstream of(m_filepath.c_str(), std::ios::app);
146 errorstream<<"RollbackManager::flush(): Could not open file "
147 <<"for appending: \""<<m_filepath<<"\""<<std::endl;
150 for(std::list<RollbackAction>::const_iterator
151 i = m_action_todisk_buffer.begin();
152 i != m_action_todisk_buffer.end(); i++)
154 // Do not save stuff that does not have an actor
159 of<<serializeJsonString(i->actor);
162 if(i->actor_is_guess){
164 of<<"actor_is_guess";
168 m_action_todisk_buffer.clear();
173 RollbackManager(const std::string &filepath, IGameDef *gamedef):
174 m_filepath(filepath),
176 m_current_actor_is_guess(false)
178 infostream<<"RollbackManager::RollbackManager("<<filepath<<")"
183 infostream<<"RollbackManager::~RollbackManager()"<<std::endl;
187 void addAction(const RollbackAction &action)
189 m_action_todisk_buffer.push_back(action);
190 m_action_latest_buffer.push_back(action);
192 // Flush to disk sometimes
193 if(m_action_todisk_buffer.size() >= 100)
197 bool readFile(std::list<RollbackAction> &dst)
199 // Load whole file to memory
200 std::ifstream f(m_filepath.c_str(), std::ios::in);
202 errorstream<<"RollbackManager::readFile(): Could not open "
203 <<"file for reading: \""<<m_filepath<<"\""<<std::endl;
207 if(f.eof() || !f.good())
210 std::getline(f, line);
214 std::istringstream is(line);
217 std::string action_time_raw;
218 std::getline(is, action_time_raw, ' ');
219 std::string action_actor;
221 action_actor = deSerializeJsonString(is);
222 }catch(SerializationError &e){
223 errorstream<<"RollbackManager: Error deserializing actor: "
224 <<e.what()<<std::endl;
227 RollbackAction action;
228 action.unix_time = stoi(action_time_raw);
229 action.actor = action_actor;
233 throw SerializationError("readFile(): second ' ' not found");
235 action.fromStream(is);
236 /*infostream<<"RollbackManager::readFile(): Action from disk: "
237 <<action.toString()<<std::endl;*/
238 dst.push_back(action);
240 catch(SerializationError &e){
241 errorstream<<"RollbackManager: Error on line: "<<line<<std::endl;
242 errorstream<<"RollbackManager: ^ error: "<<e.what()<<std::endl;
248 std::list<RollbackAction> getEntriesSince(int first_time)
250 infostream<<"RollbackManager::getEntriesSince("<<first_time<<")"<<std::endl;
251 // Collect enough data to this buffer
252 std::list<RollbackAction> action_buffer;
253 // Use the latest buffer if it is long enough
254 if(!m_action_latest_buffer.empty() &&
255 m_action_latest_buffer.begin()->unix_time <= first_time){
256 action_buffer = m_action_latest_buffer;
260 // Save all remaining stuff
262 // Load whole file to memory
263 bool good = readFile(action_buffer);
265 errorstream<<"RollbackManager::getEntriesSince(): Failed to"
266 <<" open file; using data in memory."<<std::endl;
267 action_buffer = m_action_latest_buffer;
270 return action_buffer;
273 std::string getLastNodeActor(v3s16 p, int range, int seconds,
274 v3s16 *act_p, int *act_seconds)
276 infostream<<"RollbackManager::getLastNodeActor("<<PP(p)
277 <<", "<<seconds<<")"<<std::endl;
279 int cur_time = time(0);
280 int first_time = cur_time - seconds;
282 std::list<RollbackAction> action_buffer = getEntriesSince(first_time);
284 std::list<RollbackAction> result;
286 for(std::list<RollbackAction>::const_reverse_iterator
287 i = action_buffer.rbegin();
288 i != action_buffer.rend(); i++)
290 if(i->unix_time < first_time)
293 // Find position of action or continue
295 if(!i->getPosition(&action_p))
302 if(abs(action_p.X - p.X) > range ||
303 abs(action_p.Y - p.Y) > range ||
304 abs(action_p.Z - p.Z) > range)
311 *act_seconds = cur_time - i->unix_time;
317 std::list<RollbackAction> getRevertActions(const std::string &actor_filter,
320 infostream<<"RollbackManager::getRevertActions("<<actor_filter
321 <<", "<<seconds<<")"<<std::endl;
323 int cur_time = time(0);
324 int first_time = cur_time - seconds;
326 std::list<RollbackAction> action_buffer = getEntriesSince(first_time);
328 std::list<RollbackAction> result;
330 for(std::list<RollbackAction>::const_reverse_iterator
331 i = action_buffer.rbegin();
332 i != action_buffer.rend(); i++)
334 if(i->unix_time < first_time)
336 if(i->actor != actor_filter)
338 const RollbackAction &action = *i;
339 /*infostream<<"RollbackManager::revertAction(): Should revert"
340 <<" time="<<action.unix_time
341 <<" actor=\""<<action.actor<<"\""
342 <<" action="<<action.toString()
344 result.push_back(action);
351 std::string m_filepath;
353 std::string m_current_actor;
354 bool m_current_actor_is_guess;
355 std::list<RollbackAction> m_action_todisk_buffer;
356 std::list<RollbackAction> m_action_latest_buffer;
359 IRollbackManager *createRollbackManager(const std::string &filepath, IGameDef *gamedef)
361 return new RollbackManager(filepath, gamedef);