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

summaryrefslogtreecommitdiff
path: root/source/game/StarStatistics.hpp
blob: 30ae81085d4f61e950cd0c1622289d99c71c1db2 (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
60
61
62
63
64
65
#pragma once

#include "StarVersioningDatabase.hpp"
#include "StarStatisticsDatabase.hpp"
#include "StarLuaComponents.hpp"
#include "StarStatisticsService.hpp"

namespace Star {

STAR_CLASS(Statistics);

class Statistics {
public:
  Statistics(String const& storageDirectory, StatisticsServicePtr service = {});

  void writeStatistics();

  Json stat(String const& name, Json def = {}) const;
  Maybe<String> statType(String const& name) const;
  bool achievementUnlocked(String const& name) const;

  void recordEvent(String const& name, Json const& fields);
  bool reset();

  void update();

private:
  struct Stat {
    static Stat fromJson(Json const& json);
    Json toJson() const;

    String type;
    Json value;
  };

  void processEvent(String const& name, Json const& fields);

  // setStat and unlockAchievement must be kept private as some platforms'
  // services don't implement the API calls these correspond to.
  void setStat(String const& name, String const& type, Json const& value);
  void unlockAchievement(String const& name);
  bool checkAchievement(String const& achievementName);

  void readStatistics();
  void mergeServiceStatistics();

  LuaCallbacks makeStatisticsCallbacks();

  template <typename Result = LuaValue, typename... V>
  Maybe<Result> runStatScript(StringList const& scripts, Json const& config, String const& functionName, V&&... args);

  StatisticsServicePtr m_service;
  String m_storageDirectory;
  bool m_initialized;

  List<pair<String, Json>> m_pendingEvents;
  StringSet m_pendingAchievementChecks;

  StringMap<Stat> m_stats;
  StringSet m_achievements;

  LuaRootPtr m_luaRoot;
};

}