]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/sensors.h
Admin, service and corporation sesnsors added for SUMX
[stg.git] / projects / stargazer / plugins / other / smux / sensors.h
1 #ifndef __SENSORS_H__
2 #define __SENSORS_H__
3
4 #include <map>
5
6 #include "stg/users.h"
7 #include "stg/tariffs.h"
8 #include "stg/user_property.h"
9
10 #include "stg/ObjectSyntax.h"
11
12 #include "value2os.h"
13 #include "types.h"
14
15 class Sensor {
16     public:
17         virtual bool GetValue(ObjectSyntax_t * objectSyntax) const = 0;
18 #ifdef DEBUG
19         virtual std::string ToString() const = 0;
20 #endif
21 };
22
23 typedef std::map<OID, Sensor *> Sensors;
24
25 class TotalUsersSensor : public Sensor {
26     public:
27         TotalUsersSensor(const USERS & u) : users(u) {}
28         virtual ~TotalUsersSensor() {}
29
30         bool GetValue(ObjectSyntax_t * objectSyntax) const
31         {
32         ValueToOS(users.GetUserNum(), objectSyntax);
33         return true;
34         }
35
36 #ifdef DEBUG
37         std::string ToString() const
38         { std::string res; x2str(users.GetUserNum(), res); return res; }
39 #endif
40
41     private:
42         const USERS & users;
43 };
44
45 class UsersSensor : public Sensor {
46     public:
47         UsersSensor(USERS & u) : users(u) {}
48         virtual ~UsersSensor() {}
49
50         bool GetValue(ObjectSyntax_t * objectSyntax) const;
51 #ifdef DEBUG
52         std::string ToString() const;
53 #endif
54
55     private:
56         USERS & users;
57
58         virtual bool UserPredicate(USER_PTR userPtr) const = 0;
59 };
60
61 class ConnectedUsersSensor : public UsersSensor {
62     public:
63         ConnectedUsersSensor(USERS & u) : UsersSensor(u) {}
64         virtual ~ConnectedUsersSensor() {}
65
66     private:
67         bool UserPredicate(USER_PTR userPtr) const
68         { return userPtr->GetConnected(); }
69 };
70
71 class AuthorizedUsersSensor : public UsersSensor {
72     public:
73         AuthorizedUsersSensor(USERS & u) : UsersSensor(u) {}
74         virtual ~AuthorizedUsersSensor() {}
75
76     private:
77         bool UserPredicate(USER_PTR userPtr) const
78         { return userPtr->GetAuthorized(); }
79 };
80
81 class AlwaysOnlineUsersSensor : public UsersSensor {
82     public:
83         AlwaysOnlineUsersSensor(USERS & u) : UsersSensor(u) {}
84         virtual ~AlwaysOnlineUsersSensor() {}
85
86     private:
87         bool UserPredicate(USER_PTR userPtr) const
88         { return userPtr->GetProperty().alwaysOnline; }
89 };
90
91 class NoCashUsersSensor : public UsersSensor {
92     public:
93         NoCashUsersSensor(USERS & u) : UsersSensor(u) {}
94         virtual ~NoCashUsersSensor() {}
95
96     private:
97         bool UserPredicate(USER_PTR userPtr) const
98         { return userPtr->GetProperty().cash < 0; }
99 };
100
101 class DisabledDetailStatsUsersSensor : public UsersSensor {
102     public:
103         DisabledDetailStatsUsersSensor(USERS & u) : UsersSensor(u) {}
104         virtual ~DisabledDetailStatsUsersSensor() {}
105
106     private:
107         bool UserPredicate(USER_PTR userPtr) const
108         { return userPtr->GetProperty().disabledDetailStat; }
109 };
110
111 class DisabledUsersSensor : public UsersSensor {
112     public:
113         DisabledUsersSensor(USERS & u) : UsersSensor(u) {}
114         virtual ~DisabledUsersSensor() {}
115
116     private:
117         bool UserPredicate(USER_PTR userPtr) const
118         { return userPtr->GetProperty().disabled; }
119 };
120
121 class PassiveUsersSensor : public UsersSensor {
122     public:
123         PassiveUsersSensor(USERS & u) : UsersSensor(u) {}
124         virtual ~PassiveUsersSensor() {}
125
126     private:
127         bool UserPredicate(USER_PTR userPtr) const
128         { return userPtr->GetProperty().passive; }
129 };
130
131 class CreditUsersSensor : public UsersSensor {
132     public:
133         CreditUsersSensor(USERS & u) : UsersSensor(u) {}
134         virtual ~CreditUsersSensor() {}
135
136     private:
137         bool UserPredicate(USER_PTR userPtr) const
138         { return userPtr->GetProperty().credit > 0; }
139 };
140
141 class FreeMbUsersSensor : public UsersSensor {
142     public:
143         FreeMbUsersSensor(USERS & u) : UsersSensor(u) {}
144         virtual ~FreeMbUsersSensor() {}
145
146     private:
147         bool UserPredicate(USER_PTR userPtr) const
148         { return userPtr->GetProperty().freeMb > 0; }
149 };
150
151 class TariffChangeUsersSensor : public UsersSensor {
152     public:
153         TariffChangeUsersSensor(USERS & u) : UsersSensor(u) {}
154         virtual ~TariffChangeUsersSensor() {}
155
156     private:
157         bool UserPredicate(USER_PTR userPtr) const
158         { return userPtr->GetProperty().nextTariff.ConstData().empty(); }
159 };
160
161 class TotalTariffsSensor : public Sensor {
162     public:
163         TotalTariffsSensor(const TARIFFS & t) : tariffs(t) {}
164         virtual ~TotalTariffsSensor() {}
165
166         bool GetValue(ObjectSyntax_t * objectSyntax) const
167         {
168         ValueToOS(tariffs.GetTariffsNum(), objectSyntax);
169         return true;
170         }
171
172 #ifdef DEBUG
173         std::string ToString() const
174         { std::string res; x2str(tariffs.GetTariffsNum(), res); return res; }
175 #endif
176
177     private:
178         const TARIFFS & tariffs;
179 };
180
181 class TotalAdminsSensor : public Sensor {
182     public:
183         TotalAdminsSensor(const ADMINS & a) : admins(a) {}
184         virtual ~TotalAdminsSensor() {}
185
186         bool GetValue(ObjectSyntax_t * objectSyntax) const
187         {
188         ValueToOS(admins.GetAdminsNum(), objectSyntax);
189         return true;
190         }
191
192 #ifdef DEBUG
193         std::string ToString() const
194         { std::string res; x2str(admins.GetAdminsNum(), res); return res; }
195 #endif
196
197     private:
198         const ADMINS & admins;
199 };
200
201 class TotalServicesSensor : public Sensor {
202     public:
203         TotalServicesSensor(const SERVICES & s) : services(s) {}
204         virtual ~TotalServicesSensor() {}
205
206         bool GetValue(ObjectSyntax_t * objectSyntax) const
207         {
208         ValueToOS(services.GetServicesNum(), objectSyntax);
209         return true;
210         }
211
212 #ifdef DEBUG
213         std::string ToString() const
214         { std::string res; x2str(services.GetServicesNum(), res); return res; }
215 #endif
216
217     private:
218         const SERVICES & services;
219 };
220
221 class TotalCorporationsSensor : public Sensor {
222     public:
223         TotalCorporationsSensor(const CORPORATIONS & c) : corporations(c) {}
224         virtual ~TotalCorporationsSensor() {}
225
226         bool GetValue(ObjectSyntax_t * objectSyntax) const
227         {
228         ValueToOS(corporations.GetCorporationsNum(), objectSyntax);
229         return true;
230         }
231
232 #ifdef DEBUG
233         std::string ToString() const
234         { std::string res; x2str(services.GetCorporationsNum(), res); return res; }
235 #endif
236
237     private:
238         const CORPORATIONS & corporations;
239 };
240
241 template <typename T>
242 class ConstSensor : public Sensor {
243     public:
244         ConstSensor(const T & v) : value(v) {}
245         virtual ~ConstSensor() {}
246
247         bool GetValue(ObjectSyntax * objectSyntax) const
248         { return ValueToOS(value, objectSyntax); }
249
250 #ifdef DEBUG
251         std::string ToString() const
252         { std::string res; x2str(value, res); return res; }
253 #endif
254
255     private:
256         T value;
257 };
258
259 template <>
260 inline
261 std::string ConstSensor<std::string>::ToString() const
262 {
263 return value;
264 }
265
266 #endif