Skip to content

Instantly share code, notes, and snippets.

@jdtech3
Last active September 3, 2020 00:01
Show Gist options
  • Save jdtech3/d05445d0b65db4020926a2927154585c to your computer and use it in GitHub Desktop.
Save jdtech3/d05445d0b65db4020926a2927154585c to your computer and use it in GitHub Desktop.
[Politics and War] Python rewrite of Sheepy's PHP infra calculator
# [what] JDTech's Python rewrite of Sheepy's PHP infra calculator
# [license] MIT
# [updated] 2020-09-02
def infra_price(amt: float) -> float:
"""Calculates infra price at a certain infra level
Args:
amt (float): Infra level
Returns:
float: Price of one (1) infra at specified infra level
"""
return (abs(amt - 10)**2.2 / 710) + 300
def infra_cost(_from: float = 0.0, _to: float = 100.0, udp: bool = False, cce: bool = False) -> float:
"""Calculates the total cost of buying infra to specified level from specified starting level
Args:
_from (float): starting infra
_to (float): ending infra
udp (bool): urbanization domestic policy?
cce (bool): center for civil engineering project?
Returns:
(float): Total cost of infra
"""
value: float = 0 # total cost of infra
# Rounding
_from, _to = round(_from, 2), round(_to, 2)
# Calc difference
difference = _to - _from
# Input validation
if difference > 10000: # Too big
raise ValueError("Difference between starting and ending infra is greater than 10000")
elif difference == 0: # 0 infra bought
return 0.0
elif difference < 0: # Selling infra
return difference * 150
# Actual calculation time (with some spicy recursion)
if difference > 100:
thing = 100 if difference % 100 == 0 else difference % 100
chunk_cost = round(infra_price(_from), 2) * thing
value += chunk_cost + infra_cost(_from + thing, _to)
else:
value += round(infra_price(_from), 2) * difference
# Add urbanization and CCE discounts
discounts = (0.05 * value if udp else 0) + (0.05 * value if cce else 0)
return value - discounts
if __name__ == '__main__':
while True:
start = float(input("Starting infra: "))
end = float(input("Ending infra: "))
urban = input("Urbanization? (y/n), default n: ") == 'y'
cce = input("CCE project? (y/n), default n: ") == 'y'
cost = infra_cost(start, end, udp=urban, cce=cce)
print(f'${cost:,.2f}\n---')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment