openplanet-nl / issues

Issue tracker for Openplanet.
10 stars 0 forks source link

Time.as dependency: ParseStr function enhancement #54

Closed chipsTM closed 2 years ago

chipsTM commented 2 years ago

As a plugin developer I would like to make available a function to convert a relative time string and convert to a numerical score similar to that which is provided by the nadeo apis

Examples: Time::ParseStr("6:01.103") => 361103 Time::ParseStr("0:51.158") => 51158 Time::ParseStr("12:56:09.493") => 46569493

My quick and dirty function for reference:

uint ParseStr(string time) {
    auto indexms = time.IndexOf(".");
    auto ms = Text::ParseUInt(time.SubStr(indexms+1));
    auto lhs = time.SubStr(0, indexms).Split(":");
    lhs.Reverse();

    uint total = ms;
    for (uint i = 0; i < lhs.Length; i++) {
        total += Text::ParseUInt(lhs[i]) * (60 ** i) * 1000;
    }
    return total;
}
codecat commented 2 years ago

Thanks! I used your code as a base and added this to Time.as:

    uint64 ParseRelativeTime(const string &in time)
    {
        uint64 ret = 0;

        int indexms = time.IndexOf(".");
        if (indexms != -1) {
            ret += Text::ParseUInt64(time.SubStr(indexms + 1));
        }

        array<string> lhs;
        if (indexms != -1) {
            lhs = time.SubStr(0, indexms).Split(":");
        } else {
            lhs = time.Split(":");
        }

        for (int i = int(lhs.Length) - 1; i >= 0; i--) {
            int m = int(lhs.Length) - 1 - i;
            ret += Text::ParseUInt64(lhs[i]) * (60 ** m) * 1000;
        }

        return ret;
    }