]> git.stg.codes - stg.git/blob - projects/stargazer/tariff_impl.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / tariff_impl.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Date: 07.11.2007
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26  $Revision: 1.11 $
27  $Date: 2010/10/07 16:57:21 $
28  $Author: faust $
29  */
30
31 #include "tariff_impl.h"
32
33 #include "stg_timer.h"
34 #include "stg/common.h"
35
36 #include <ctime>
37 #include <algorithm> // std::max
38
39 using STG::TariffImpl;
40
41 //-----------------------------------------------------------------------------
42 TariffImpl & TariffImpl::operator=(const TariffData & td)
43 {
44 tariffData = td;
45 return *this;
46 }
47 //-----------------------------------------------------------------------------
48 double TariffImpl::GetPriceWithTraffType(uint64_t up,
49                                      uint64_t down,
50                                      int dir,
51                                      time_t t) const
52 {
53 return GetPriceWithoutFreeMb(dir, GetTraffByType(up, down) / (1024 * 1024), t);
54 }
55 //-----------------------------------------------------------------------------
56 int64_t TariffImpl::GetTraffByType(uint64_t up, uint64_t down) const
57 {
58 switch (tariffData.tariffConf.traffType)
59     {
60     case TRAFF_UP:
61         return up;
62
63     case TRAFF_DOWN:
64         return down;
65
66     case TRAFF_MAX:
67         return std::max(up, down);
68
69     default:  //TRAFF_UP_DOWN:
70         return up + down;
71     }
72 }
73 //-----------------------------------------------------------------------------
74 int TariffImpl::GetThreshold(int dir) const
75 {
76     return tariffData.dirPrice[dir].threshold;
77 }
78 //-----------------------------------------------------------------------------
79 void TariffImpl::Print() const
80 {
81 printfd(__FILE__, "Traiff name: %s\n", tariffData.tariffConf.name.c_str());
82 }
83 //-----------------------------------------------------------------------------
84 int TariffImpl::Interval(int dir, time_t t) const
85 {
86 // Start of the day (and end of the night) in sec from 00:00:00
87 int s1 = tariffData.dirPrice[dir].hDay * 3600 +
88          tariffData.dirPrice[dir].mDay * 60;
89 // Start of the night (and end of the day) in sec from 00:00:00
90 int s2 = tariffData.dirPrice[dir].hNight * 3600 +
91          tariffData.dirPrice[dir].mNight * 60;
92
93 struct tm * lt;
94
95 lt = localtime(&t);
96
97 // Position of time t in sec from 00:00:00
98 // Ignoring seconds due to minute precision
99 int lts = lt->tm_hour * 3600 + lt->tm_min * 60;
100
101 if (s1 < s2)
102     {
103     // Normal situation (00:00:00 is a night)
104     if (lts > s1 && lts < s2)
105         return TARIFF_DAY;
106     else
107         return TARIFF_NIGHT;
108     }
109 else
110     {
111     // Not so common but possible situation (00:00:00 is a day)
112     if (lts < s1 && lts > s2)
113         return TARIFF_NIGHT;
114     else
115         return TARIFF_DAY;
116     }
117 }
118 //-----------------------------------------------------------------------------
119 double TariffImpl::GetPriceWithoutFreeMb(int dir, int64_t mb, time_t t) const
120 {
121 int interval = Interval(dir, t);
122
123 /*
124  * 0011 - NB
125  * *01* - NA
126  * 0**1 - DB
127  * **** - DA
128  */
129
130 bool nd = tariffData.dirPrice[dir].noDiscount;
131 bool sp = tariffData.dirPrice[dir].singlePrice;
132 bool th = (interval == TARIFF_NIGHT);
133 bool tr = (mb > tariffData.dirPrice[dir].threshold);
134
135 if (!nd && !sp && th && tr)
136     return tariffData.dirPrice[dir].priceNightB;
137 else if (!nd && tr)
138     return tariffData.dirPrice[dir].priceDayB;
139 else if (!sp && th)
140     return tariffData.dirPrice[dir].priceNightA;
141 else
142     return tariffData.dirPrice[dir].priceDayA;
143 }
144 //-----------------------------------------------------------------------------
145 std::string TariffImpl::TariffChangeIsAllowed(const Tariff & to, time_t currentTime) const
146 {
147 time_t timeout = GetChangePolicyTimeout();
148 if (currentTime > timeout && timeout != 0)
149     return "";
150 switch (GetChangePolicy())
151     {
152     case Tariff::ALLOW:
153         return "";
154     case Tariff::TO_CHEAP:
155         if (to.GetFee() < GetFee())
156             return "";
157         else
158             return "New tariff '" + to.GetName() + "' is more expensive than current tariff '" + GetName() + "'. The policy is '" + Tariff::toString(GetChangePolicy()) + "'.";
159     case Tariff::TO_EXPENSIVE:
160         if (to.GetFee() >= GetFee())
161             return "";
162         else
163             return "New tariff '" + to.GetName() + "' is more cheap than current tariff '" + GetName() + "'. The policy is '" + Tariff::toString(GetChangePolicy()) + "'.";
164     case Tariff::DENY:
165         return "Current tariff '" + GetName() + "', new tariff '" + to.GetName() + "'. The policy is '" + Tariff::toString(GetChangePolicy()) + "'.";
166     }
167 return "";
168 }
169 //-----------------------------------------------------------------------------