HoverRace  2.0
SessionPeer.h
Go to the documentation of this file.
1 
2 // SessionPeer.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 <luabind/luabind.hpp>
25 #include <luabind/object.hpp>
26 
27 #include "../../../engine/Util/WorldCoordinates.h"
28 #include "../../../engine/Script/Peer.h"
29 
30 namespace HoverRace {
31  namespace Client {
32  namespace HoverScript {
33  class MetaPlayer;
34  class MetaSession;
35  class TrackPeer;
36  }
37  class ClientSession;
38  }
39  namespace Script {
40  class Core;
41  }
42  namespace Util {
43  class Clock;
44  }
45 }
46 
47 namespace HoverRace {
48 namespace Client {
49 namespace HoverScript {
50 
55 class SessionPeer : public Script::Peer
56 {
58 
59 protected:
60  class PlayerRef
61  {
62  public:
63  PlayerRef(std::shared_ptr<MetaPlayer> meta);
64 
65  public:
66  std::shared_ptr<MetaPlayer> meta;
67  private:
68  boost::signals2::scoped_connection startedConn;
69  boost::signals2::scoped_connection finishedConn;
70  };
71 
72 public:
73  SessionPeer(Script::Core &scripting, ClientSession *session);
74  virtual ~SessionPeer();
75 
76 public:
77  static void Register(Script::Core &scripting);
78 
79 public:
80  void SetMeta(MetaSession *meta) { this->meta = meta; }
81 
82 public:
83  template<class Fn>
84  void ForEachPlayer(Fn fn)
85  {
86  for (auto &ref : playerRefs) {
87  fn(ref->meta);
88  }
89  }
90  std::shared_ptr<MetaPlayer> &GetPlayer(unsigned idx)
91  {
92  return playerRefs[idx]->meta;
93  }
94 
95  void OnSessionStart(ClientSession *session);
96  void OnSessionEnd();
97 
98 protected:
99  void VerifySession() const;
100 
101 public:
102  int LGetNumPlayers() const;
103  MR_SimulationTime LGetTime() const;
104  std::shared_ptr<Util::Clock> LGetClock() const;
105  std::shared_ptr<Util::Clock> LGetCountdown() const;
106  void LCountdownToNextPhase(const std::string &s) const;
107  std::shared_ptr<TrackPeer> LGetTrack() const;
108 
109 private:
112  luabind::object rules;
113  luabind::object players;
114  std::vector<std::unique_ptr<PlayerRef>> playerRefs;
115 };
116 
117 } // namespace HoverScript
118 } // namespace Client
119 } // namespace HoverRace
void ForEachPlayer(Fn fn)
Definition: SessionPeer.h:84
Scripting peer for a game session.
Definition: SessionPeer.h:55
luabind::object rules
Definition: SessionPeer.h:112
MR_Int32 MR_SimulationTime
Definition: WorldCoordinates.h:106
boost::signals2::scoped_connection finishedConn
Definition: SessionPeer.h:69
void SetMeta(MetaSession *meta)
Definition: SessionPeer.h:80
MetaSession * meta
Definition: SessionPeer.h:111
Base class for script peers.
Definition: Peer.h:47
boost::signals2::scoped_connection startedConn
Definition: SessionPeer.h:68
Native base class for scripted sessions.
Definition: MetaSession.h:43
int idx
Definition: SdlDisplay.cpp:254
std::shared_ptr< MetaPlayer > meta
Definition: SessionPeer.h:66
Definition: ClientSession.h:58
Definition: Announcement.h:24
luabind::object players
Definition: SessionPeer.h:113
std::shared_ptr< MetaPlayer > & GetPlayer(unsigned idx)
Definition: SessionPeer.h:90
std::vector< std::unique_ptr< PlayerRef > > playerRefs
Definition: SessionPeer.h:114
ClientSession * session
Definition: SessionPeer.h:110
A script environment.
Definition: Core.h:66