Example #1
0
async def evaluate(
    kol: "libkol.Session", expression: Expr, subs: Dict[str, int] = {}
) -> int:
    today = koldate.today()

    symbols = {
        "A": kol.ascensions,
        "D": kol.inebriety,
        "G": today.grimace_darkness,
        "H": 0,  # hobopower
        "J": 1 if today.jarlsberg else 0,
        "K": 0,  # smithsness,
        "L": kol.level,
        "M": today.moonlight,
        "N": 0,  # audience
        "R": await kol.get_reagent_potion_duration(),
        "W": kol.familiar_weight,
        "X": 1 if kol.gender == "f" else 0,
        "Y": kol.fury,
        "MUS": kol.get_stat(Stat.Muscle, buffed=True),
        "MYS": kol.get_stat(Stat.Mysticality, buffed=True),
        "MOX": kol.get_stat(Stat.Moxie, buffed=True),
        "ML": 0,  # Total +ML Modifier
        "MCD": 0,  # mind-control
        "HP": kol.max_hp,
        "BL": 0,  # basement level
    }

    for token, impl in state_functions.items():
        f = Function(token)
        expression = expression.replace(f, lambda sym: impl(kol, arg_decode(str(sym))))

    try:
        result = expression.evalf(subs={**symbols, **subs})
        if isinstance(result, int) or result.is_number:
            return result
        else:
            raise UnknownError(
                "Unknown symbols {} in {}".format(result.free_symbols, expression)
            )
    except Exception:
        raise UnknownError("Could not parse {}".format(expression))