]> git.stg.codes - stg.git/blobdiff - projects/stargazer/tariff_impl.cpp
Merge branch 'stg-2.409'
[stg.git] / projects / stargazer / tariff_impl.cpp
index 70b755762bc131be0b1ac365b48b424f406b50d3..7dbae0da5def9330a4a6d01a957cf72b208d36e3 100644 (file)
 #include <ctime>
 #include <algorithm> // std::max
 
-#include "tariff.h"
+#include "stg/common.h"
+#include "tariff_impl.h"
 #include "stg_timer.h"
 
 //-----------------------------------------------------------------------------
-TARIFF & TARIFF::operator=(const TARIFF_DATA & td)
+TARIFF_IMPL & TARIFF_IMPL::operator=(const TARIFF_DATA & td)
 {
 tariffData = td;
 return *this;
 }
 //-----------------------------------------------------------------------------
-TARIFF & TARIFF::operator=(const TARIFF & t)
+TARIFF_IMPL & TARIFF_IMPL::operator=(const TARIFF_IMPL & t)
 {
 tariffData = t.tariffData;
 return *this;
 }
 //-----------------------------------------------------------------------------
-double TARIFF::GetPriceWithTraffType(uint64_t up,
+double TARIFF_IMPL::GetPriceWithTraffType(uint64_t up,
                                      uint64_t down,
                                      int dir,
                                      time_t t) const
@@ -55,7 +56,7 @@ double TARIFF::GetPriceWithTraffType(uint64_t up,
 return GetPriceWithoutFreeMb(dir, GetTraffByType(up, down) / (1024 * 1024), t);
 }
 //-----------------------------------------------------------------------------
-int64_t TARIFF::GetTraffByType(uint64_t up, uint64_t down) const
+int64_t TARIFF_IMPL::GetTraffByType(uint64_t up, uint64_t down) const
 {
 switch (tariffData.tariffConf.traffType)
     {
@@ -73,27 +74,17 @@ switch (tariffData.tariffConf.traffType)
     }
 }
 //-----------------------------------------------------------------------------
-int TARIFF::GetThreshold(int dir) const
+int TARIFF_IMPL::GetThreshold(int dir) const
 {
     return tariffData.dirPrice[dir].threshold;
 }
 //-----------------------------------------------------------------------------
-void TARIFF::PrintTariff() const
+void TARIFF_IMPL::Print() const
 {
-//printfd(__FILE__, "Traiff name: %s\n", tariffConf.name.c_str());
-//printfd(__FILE__, "Price: %8.3f   %8.3f   \n", dirPrice[0].GetPrice(0, 0), dirPrice[0].GetPrice(1, 0));
-//printfd(__FILE__, "Price: %8.3f   %8.3f   Thr:%d\n", dirPrice[1].GetPrice(0), dirPrice[1].GetPrice(1), dirPrice[1].GetThreshold());
-//printfd(__FILE__, "Price: %8.3f   %8.3f   Thr:%d\n", dirPrice[2].GetPrice(0), dirPrice[2].GetPrice(1), dirPrice[2].GetThreshold());
-//printfd(__FILE__, "Price: %8.3f   %8.3f   Thr:%d\n", dirPrice[3].GetPrice(0), dirPrice[3].GetPrice(1), dirPrice[3].GetThreshold());
-//printfd(__FILE__, "Free: %8.3f\n", tariffConf.free);
+printfd(__FILE__, "Traiff name: %s\n", tariffData.tariffConf.name.c_str());
 }
 //-----------------------------------------------------------------------------
-void TARIFF::GetTariffData(TARIFF_DATA * td) const
-{
-*td = tariffData;
-}
-//-----------------------------------------------------------------------------
-int TARIFF::Interval(int dir, time_t t) const
+int TARIFF_IMPL::Interval(int dir, time_t t) const
 {
 // Start of the day (and end of the night) in sec from 00:00:00
 int s1 = tariffData.dirPrice[dir].hDay * 3600 +
@@ -128,7 +119,7 @@ else
     }
 }
 //-----------------------------------------------------------------------------
-double TARIFF::GetPriceWithoutFreeMb(int dir, int mb, time_t t) const
+double TARIFF_IMPL::GetPriceWithoutFreeMb(int dir, int64_t mb, time_t t) const
 {
 int interval = Interval(dir, t);
 
@@ -152,45 +143,30 @@ else if (!sp && th)
     return tariffData.dirPrice[dir].priceNightA;
 else
     return tariffData.dirPrice[dir].priceDayA;
-
-/*if (tariffData.dirPrice[dir].noDiscount && tariffData.dirPrice[dir].singlePrice)
-    {
-    return tariffData.dirPrice[dir].priceDayA;
-    }
-else
+}
+//-----------------------------------------------------------------------------
+std::string TARIFF_IMPL::TariffChangeIsAllowed(const TARIFF & to, time_t currentTime) const
+{
+time_t timeout = GetChangePolicyTimeout();
+if (currentTime > timeout && timeout != 0)
+    return "";
+switch (GetChangePolicy())
     {
-    if (tariffData.dirPrice[dir].noDiscount)
-        {
-        // Without threshold
-        if (interval == TARIFF_DAY)
-            return tariffData.dirPrice[dir].priceDayA;
-        else
-            return tariffData.dirPrice[dir].priceNightA;
-        }
-
-    if (tariffData.dirPrice[dir].singlePrice)
-        {
-        // Without day/night
-        if (mb < tariffData.dirPrice[dir].threshold)
-            return tariffData.dirPrice[dir].priceDayA;
+    case TARIFF::ALLOW:
+        return "";
+    case TARIFF::TO_CHEAP:
+        if (to.GetFee() < GetFee())
+            return "";
         else
-            return tariffData.dirPrice[dir].priceDayB;
-        }
-
-    if (mb < tariffData.dirPrice[dir].threshold)
-        {
-        if (interval == TARIFF_DAY)
-            return tariffData.dirPrice[dir].priceDayA;
-        else
-            return tariffData.dirPrice[dir].priceNightA;
-        }
-    else
-        {
-        if (interval == TARIFF_DAY)
-            return tariffData.dirPrice[dir].priceDayB;
+            return "New tariff '" + to.GetName() + "' is more expensive than current tariff '" + GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
+    case TARIFF::TO_EXPENSIVE:
+        if (to.GetFee() >= GetFee())
+            return "";
         else
-            return tariffData.dirPrice[dir].priceNightB;
-        }
-    }*/
+            return "New tariff '" + to.GetName() + "' is more cheap than current tariff '" + GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
+    case TARIFF::DENY:
+        return "Current tariff '" + GetName() + "', new tariff '" + to.GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
+    }
+return "";
 }
 //-----------------------------------------------------------------------------