Libsaki
Core library of Pancake Mahjong
table_event.h
1 #ifndef SAKI_TABLE_EVENT_H
2 #define SAKI_TABLE_EVENT_H
3 
4 #include "../form/form.h"
5 #include "../unit/action.h"
6 #include "../util/misc.h"
7 
8 #include <cstdint>
9 
10 
11 
12 namespace saki
13 {
14 
15 
16 
17 enum class RoundResult
18 {
19  // *** SYNC with string_enum.cpp ***
20  TSUMO, RON, HP, KSKP, SFRT, SKSR, SCRC, SCHR, NGSMG, ABORT, NUM_ROUNDRES
21 };
22 
24 {
25 public:
26  enum class Type
27  {
28  // *** SYNC with util/string_enum.cpp ***
29  TABLE_STARTED, FIRST_DEALER_CHOSEN, ROUND_STARTED, CLEANED,
30  DICED, DEALT, FLIPPED, DRAWN, DISCARDED,
31  RIICHI_CALLED, RIICHI_ESTABLISHED, BARKED, ROUND_ENDED,
32  POINTS_CHANGED, TABLE_ENDED, POPPED_UP
33  };
34 
35  struct Args
36  {
37  virtual ~Args() = default;
38  virtual std::unique_ptr<Args> clone() = 0;
39  };
40 
41  template<typename ArgsT>
43  {
44  explicit ArgsCloneable(const ArgsT &a) : args(a) {}
45 
46  std::unique_ptr<Args> clone() override
47  {
48  return std::make_unique<ArgsCloneable>(*this);
49  }
50 
51  ArgsT args;
52  };
53 
54  struct TableStarted
55  {
56  static const Type TYPE = Type::TABLE_STARTED;
57  uint32_t seed;
58  };
59 
61  {
62  static const Type TYPE = Type::FIRST_DEALER_CHOSEN;
63  Who who;
64  };
65 
66  struct RoundStarted
67  {
68  static const Type TYPE = Type::ROUND_STARTED;
69  int round;
70  int extraRound;
71  Who dealer;
72  bool allLast;
73  int deposit;
74  uint32_t seed;
75  };
76 
77  struct Cleaned
78  {
79  static const Type TYPE = Type::CLEANED;
80  };
81 
82  struct Diced
83  {
84  static const Type TYPE = Type::DICED;
85  int die1;
86  int die2;
87  };
88 
89  struct Dealt
90  {
91  static const Type TYPE = Type::DEALT;
92  };
93 
94  struct Flipped
95  {
96  static const Type TYPE = Type::FLIPPED;
97  };
98 
99  struct Drawn
100  {
101  static const Type TYPE = Type::DRAWN;
102  Who who;
103  };
104 
105  struct Discarded
106  {
107  static const Type TYPE = Type::DISCARDED;
108  bool spin;
109  };
110 
112  {
113  static const Type TYPE = Type::RIICHI_CALLED;
114  Who who;
115  };
116 
118  {
119  static const Type TYPE = Type::RIICHI_ESTABLISHED;
120  Who who;
121  };
122 
123  struct Barked
124  {
125  static const Type TYPE = Type::BARKED;
126  Who who;
128  bool spin;
129  };
130 
131  struct RoundEnded
132  {
133  static const Type TYPE = Type::ROUND_ENDED;
134  RoundResult result;
135  util::Stactor<Who, 4> openers;
136  Who gunner;
138  };
139 
141  {
142  static const Type TYPE = Type::POINTS_CHANGED;
143  };
144 
145  struct TableEnded
146  {
147  static const Type TYPE = Type::TABLE_ENDED;
148  std::array<Who, 4> ranks;
149  std::array<int, 4> scores;
150  };
151 
152  struct PoppedUp
153  {
154  static const Type TYPE = Type::POPPED_UP;
155  Who who;
156  };
157 
158  template<typename ArgsT>
159  TableEvent(ArgsT args)
160  : mType(ArgsT::TYPE)
161  , mArgs(std::make_unique<ArgsCloneable<ArgsT>>(args))
162  {
163  }
164 
165  ~TableEvent() = default;
166 
167  TableEvent(const TableEvent &copy)
168  : mType(copy.mType)
169  , mArgs(copy.mArgs->clone())
170  {
171  }
172 
173  Type type() const
174  {
175  return mType;
176  }
177 
178  template<typename ArgsT>
179  const ArgsT &as() const
180  {
181  assert(mType == ArgsT::TYPE);
182  return (static_cast<const ArgsCloneable<ArgsT> &>(*mArgs)).args;
183  }
184 
185 private:
186  Type mType;
187  std::unique_ptr<Args> mArgs;
188 };
189 
190 
191 
192 } // namespace saki
193 
194 
195 
196 #endif // SAKI_TABLE_EVENT_H
Definition: table_event.h:60
Definition: table_event.h:23
Definition: table_event.h:82
Definition: table_event.h:35
Definition: table_event.h:77
Definition: table_event.h:131
Definition: table_event.h:140
Definition: table_event.h:94
Definition: table_event.h:145
Definition: ai.cpp:18
Definition: table_event.h:66
Definition: table_event.h:105
Definition: table_event.h:42
Definition: table_event.h:99
M37 bark
M37 param is required to denote kakan.
Definition: table_event.h:127
Definition: table_event.h:89
Definition: table_event.h:123
Definition: table_event.h:117
Definition: table_event.h:152
Definition: meld.h:17
Stactor = statically allocated vector.
Definition: stactor.h:247
Definition: table_event.h:54
Definition: table_event.h:111
Definition: who.h:14