Files
cultivation-world-simulator/src/classes/magic_stone.py
2025-10-04 14:57:11 +08:00

46 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from typing import Union
class MagicStone(int):
"""
灵石实际上是一个int类代表持有的下品灵石的数量。
但是可以转换为中品、上品灵石。汇率为100:1
"""
def __init__(self, value: int):
self.value = value
def exchange(self) -> tuple[int, int, int]:
# 期望顺序:返回 (上品, 中品, 下品)
# 汇率100 下品 = 1 中品100 中品 = 1 上品
_middle_total, _lower = divmod(self.value, 100)
_upper, _middle = divmod(_middle_total, 100)
return _upper, _middle, _lower
def __str__(self) -> str:
_upper, _middle, _value = self.exchange()
return f"上品灵石:{_upper},中品灵石:{_middle},下品灵石:{_value}"
def __add__(self, other: Union['MagicStone', int]) -> 'MagicStone':
if isinstance(other, int):
return MagicStone(self.value + other)
return MagicStone(self.value + other.value)
def __iadd__(self, other: Union['MagicStone', int]) -> 'MagicStone':
if isinstance(other, int):
self.value += other
else:
self.value += other.value
return self
def __sub__(self, other: Union['MagicStone', int]) -> 'MagicStone':
if isinstance(other, int):
return MagicStone(self.value - other)
return MagicStone(self.value - other.value)
def __isub__(self, other: Union['MagicStone', int]) -> 'MagicStone':
if isinstance(other, int):
self.value -= other
else:
self.value -= other.value
return self