HoverRace  2.0
Rulebook.h
Go to the documentation of this file.
1 
2 // Rulebook.h
3 //
4 // Copyright (c) 2010, 2013-2016 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "../../engine/Script/Handlers.h"
25 #include "../../engine/Script/RegistryRef.h"
26 #include "../../engine/Script/WrapperFactory.h"
27 #include "../../engine/Util/OS.h"
28 
29 #include "Rule.h"
30 
31 namespace HoverRace {
32  namespace Client {
33  namespace HoverScript {
34  class MetaPlayer;
35  class MetaSession;
36  class PlayerPeer;
37  class RulebookEnv;
38  class SessionPeer;
39  }
40  }
41  namespace Model {
42  class TrackEntry;
43  }
44  namespace Script {
45  class Core;
46  class Handlers;
47  }
48 }
49 
50 namespace HoverRace {
51 namespace Client {
52 
57 class Rulebook
58 {
59 public:
60  Rulebook(Script::Core &scripting,
61  const Util::OS::path_t &basePath);
62  Rulebook(const Rulebook&) = delete;
63  ~Rulebook() { }
64 
65  Rulebook &operator=(const Rulebook&) = delete;
66 
67 public:
68  const Util::OS::path_t &GetBasePath() const { return basePath; }
69 
70  void SetMetadata(const std::string &name, const std::string &title,
71  const std::string &description, int maxPlayers)
72  {
73  this->name = name;
74  this->title = title;
75  this->description = description;
76  this->maxPlayers = maxPlayers;
77  }
78 
79  const std::string &GetName() const { return name; }
80  const std::string &GetTitle() const { return title; }
81  const std::string &GetDescription() const { return description; }
82 
83 public:
84  struct metas_t
85  {
86  metas_t(Script::Core &scripting) :
87  player(scripting), session(scripting) { }
88  metas_t(const metas_t&) = default;
89  metas_t(metas_t&&) = default;
90 
91  metas_t &operator=(const metas_t&) = default;
92  metas_t &operator=(metas_t&&) = default;
93 
96  };
97 
98  const metas_t &GetMetas() const
99  {
100  if (!loaded) {
101  Load();
102  }
103  return metas;
104  }
105 
106  int GetMaxPlayers() const { return maxPlayers; }
107 
108 public:
109  // Temporary until we get real rule classes.
110  void AddRule(const std::string &name, const luabind::object &obj);
111 
112  luabind::object CreateDefaultRules() const;
113 
114  bool LoadMetadata();
115  void Load() const;
116 
117 public:
118  void SetOnLoad(const luabind::object &fn);
119 protected:
120  void OnLoad() const;
121 
122 public:
123  friend bool operator==(const Rulebook &lhs, const Rulebook &rhs);
124  friend bool operator<(const Rulebook &lhs, const Rulebook &rhs);
125 
126 private:
129  std::shared_ptr<HoverScript::RulebookEnv> env;
130  std::string defaultName;
131  std::string name;
132  std::string title;
133  std::string description;
135 
136  std::map<std::string, std::shared_ptr<Rule>> rules;
137 
139 
140  mutable metas_t metas;
141 
142  mutable bool loaded;
143 };
144 
145 inline bool operator==(const Rulebook &lhs, const Rulebook &rhs)
146 {
147  // Rulebook names are unique.
148  return lhs.name == rhs.name;
149 }
150 
151 inline bool operator<(const Rulebook &lhs, const Rulebook &rhs)
152 {
153  //TODO: Sort by manually-defined sort index, then by name.
154  return lhs.name < rhs.name;
155 }
156 
157 } // namespace Client
158 } // namespace HoverRace
const std::string & GetTitle() const
Definition: Rulebook.h:80
const std::string & GetName() const
Definition: Rulebook.h:79
boost::filesystem::path path_t
Definition: OS.h:57
int GetMaxPlayers() const
Definition: Rulebook.h:106
Definition: Rulebook.h:84
std::string title
Definition: Rulebook.h:132
Encapsulates a ref to the registry.
Definition: RegistryRef.h:50
void SetMetadata(const std::string &name, const std::string &title, const std::string &description, int maxPlayers)
Definition: Rulebook.h:70
std::string defaultName
Definition: Rulebook.h:130
std::shared_ptr< HoverScript::RulebookEnv > env
Definition: Rulebook.h:129
~Rulebook()
Definition: Rulebook.h:63
bool operator<(const Rulebook &lhs, const Rulebook &rhs)
Definition: Rulebook.h:151
metas_t(Script::Core &scripting)
Definition: Rulebook.h:86
Script::WrapperFactory< HoverScript::SessionPeer, HoverScript::MetaSession > session
Definition: Rulebook.h:95
std::map< std::string, std::shared_ptr< Rule > > rules
Definition: Rulebook.h:136
Script::RegistryRef onLoad
Definition: Rulebook.h:138
Script::WrapperFactory< HoverScript::PlayerPeer, HoverScript::MetaPlayer > player
Definition: Rulebook.h:94
Util::OS::path_t basePath
Definition: Rulebook.h:128
Script::Core & scripting
Definition: Rulebook.h:127
const metas_t & GetMetas() const
Definition: Rulebook.h:98
std::string description
Definition: Rulebook.h:133
const std::string & GetDescription() const
Definition: Rulebook.h:81
std::string name
Definition: Rulebook.h:131
int maxPlayers
Definition: Rulebook.h:134
const Util::OS::path_t & GetBasePath() const
Definition: Rulebook.h:68
Definition: Announcement.h:24
Defines the rules for a particular game session.
Definition: Rulebook.h:57
bool loaded
Definition: Rulebook.h:142
bool operator==(const Rulebook &lhs, const Rulebook &rhs)
Definition: Rulebook.h:145
A script environment.
Definition: Core.h:66
metas_t metas
Definition: Rulebook.h:140