Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarPlayerStorage.hpp
blob: 4033dae0d0a8142e0e71f5c168e65e9938f396eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once

#include "StarOrderedMap.hpp"
#include "StarUuid.hpp"
#include "StarPlayerFactory.hpp"
#include "StarThread.hpp"
#include "StarWorldStorage.hpp"
#include "StarStatistics.hpp"

namespace Star {

class PlayerStorage {
public:
  PlayerStorage(String const& storageDir);
  ~PlayerStorage();

  size_t playerCount() const;
  // Returns nothing if index is out of bounds.
  Maybe<Uuid> playerUuidAt(size_t index);
  // Returns nothing if name doesn't match a player.
  Maybe<Uuid> playerUuidByName(String const& name, Maybe<Uuid> except = {});
  // Returns nothing if name doesn't match a player.
  List<Uuid> playerUuidListByName(String const& name, Maybe<Uuid> except = {});

  // Also returns the diskStore Json if needed.
  Json savePlayer(PlayerPtr const& player);

  Maybe<Json> maybeGetPlayerData(Uuid const& uuid);
  Json getPlayerData(Uuid const& uuid);
  PlayerPtr loadPlayer(Uuid const& uuid);
  void deletePlayer(Uuid const& uuid);

  WorldChunks loadShipData(Uuid const& uuid);
  void applyShipUpdates(Uuid const& uuid, WorldChunks const& updates);

  // Move the given player to the top of the player ordering.
  void moveToFront(Uuid const& uuid);

  // Copy all the player relevant files for this uuid into .bak1 .bak2 etc
  // files for however many backups are configured
  void backupCycle(Uuid const& uuid);

  // Get / Set PlayerStorage global metadata
  void setMetadata(String key, Json value);
  Json getMetadata(String const& key);

private:
  String const& uuidFileName(Uuid const& uuid);
  void writeMetadata();

  mutable RecursiveMutex m_mutex;
  String m_storageDirectory;
  String m_backupDirectory;
  OrderedHashMap<Uuid, Json> m_savedPlayersCache;
  BiMap<Uuid, String> m_playerFileNames;
  JsonObject m_metadata;
};

}