File size: 3,374 Bytes
e26cd2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import math


class Calculator:
    def get_price(self, data, level):
        return self.calculate_formula(data.priceFormula, level, data.priceBasic, data.priceFormulaK) if level else 0

    def get_profit(self, data, level):
        return (
            self.calculate_formula(data.profitFormula, level, data.profitBasic, data.priceFormulaK, data)
            if level
            else 0
        )

    def calculate_formula(self, formula, level, base_value, formula_coefficient, data=None):
        result = base_value
        if formula == "fnCompound":
            result = self.fn_compound(level, base_value, formula_coefficient)
        elif formula == "fnLogarithmic":
            result = self.fn_logarithmic(level, base_value)
        elif formula == "fnLinear":
            result = self.fn_linear(level, base_value)
        elif formula == "fnQuadratic":
            result = self.fn_quadratic(level, base_value)
        elif formula == "fnCubic":
            result = self.fn_cubic(level, base_value)
        elif formula == "fnExponential":
            result = self.fn_exponential(level, base_value, formula_coefficient)
        elif formula == "fnPayback":
            result = self.fn_payback(level, data)

        return self.smart_round(result)

    def smart_round(self, value):
        def round_to(value, factor=100):
            return round(value / factor) * factor

        if value < 50:
            return round(value)
        elif value < 100:
            return round_to(value, 5)
        elif value < 500:
            return round_to(value, 25)
        elif value < 1000:
            return round_to(value, 50)
        elif value < 5000:
            return round_to(value, 100)
        elif value < 10000:
            return round_to(value, 200)
        elif value < 100000:
            return round_to(value, 500)
        elif value < 500000:
            return round_to(value, 1000)
        elif value < 1000000:
            return round_to(value, 5000)
        elif value < 50000000:
            return round_to(value, 10000)
        elif value < 100000000:
            return round_to(value, 50000)
        else:
            return round_to(value, 100000)

    def fn_linear(self, level, base_value):
        return base_value * level

    def fn_quadratic(self, level, base_value):
        return base_value * level * level

    def fn_cubic(self, level, base_value):
        return base_value * level * level * level

    def fn_exponential(self, level, base_value, coefficient):
        return base_value * math.pow(coefficient / 10, level)

    def fn_logarithmic(self, level, base_value):
        return base_value * math.log2(level + 1)

    def fn_compound(self, level, base_value, coefficient):
        compound_rate = coefficient / 100
        return base_value * math.pow(1 + compound_rate, level - 1)

    def fn_payback(self, level, data):
        accumulated = [0]
        for current_level in range(1, level + 1):
            previous_accumulated = accumulated[current_level - 1]
            current_price = self.get_price(data, current_level)
            current_profit = data.profitBasic + data.profitFormulaK * (current_level - 1)
            smart_rounded_value = self.smart_round(previous_accumulated + current_price / current_profit)
            accumulated.append(smart_rounded_value)
        return accumulated[level]