Skip to content

fix: don't reset player minutes on worldtime change (#631) - #1253

Open
LopezDev7 wants to merge 2 commits into
openmultiplayer:masterfrom
LopezDev7:fix/631-preserve-minutes-on-worldtime
Open

LopezDev7 wants to merge 2 commits into
openmultiplayer:masterfrom
LopezDev7:fix/631-preserve-minutes-on-worldtime

Conversation

@LopezDev7

Copy link
Copy Markdown

SetWorldTime was zeroing out the minutes of players that had their own time set with SetPlayerTime. change the world time and they'd snap to :00, real annoying

SA:MP doesn't do that, it only changes the hour and keeps the player's minute. went through the 0.3.7 client source and rpc 94 only sends the hour, the minute is never touched. so the client just applies the new hour and keeps the minute. did the same thing here, only override the hour.

rpc stays as is (still just the hour) so sa:mp clients are fine.

Fixes #631

@GuiBoneVowt

Copy link
Copy Markdown

Gave this a test on top of current master (72bf89c), Windows build.

First the repro from #631, with a quick test script using NPCs, so no client needed: SetPlayerTime every step and SetWorldTime every 5th step. On master GetPlayerTime goes back to :00 on every 5th step, with this PR the minutes stay right. Wrap-around works too (23:45 then SetWorldTime(25) gives 01:45).

The fix assumes the client keeps the minute, so I checked that in game as well. SetPlayerTime(10, 30) with the clock off, then SetWorldTime(15), then clock on a couple of seconds later: the client shows 15:30. master says 15:00 there and this PR says 15:30, so it matches now.

One thing I noticed, not caused by this PR: if the clock is already on (TogglePlayerClock), the client ignores SetWorldTime completely and stays at 10:30, so GetPlayerTime is off either way in that case (15:00 on master, 15:30 here). Skipping players with hasClock() in that loop might cover it, but that's probably for another PR.

What I used in game

Local server, connected with the 0.3.7-R5 client, once on master and once with this PR.

#include <open.mp>

forward ClockOn(playerid);

main() {}

public OnGameModeInit()
{
    AddPlayerClass(0, 1958.33, 1343.12, 15.36, 269.15, WEAPON_FIST, 0, WEAPON_FIST, 0, WEAPON_FIST, 0);
    return 1;
}

ShowServerTime(playerid)
{
    new hour, minute, msg[64];
    GetPlayerTime(playerid, hour, minute);
    format(msg, sizeof msg, "GetPlayerTime: %02d:%02d", hour, minute);
    SendClientMessage(playerid, -1, msg);
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    // clock already on: client stays at 10:30
    if (!strcmp(cmdtext, "/time1", true))
    {
        TogglePlayerClock(playerid, true);
        SetPlayerTime(playerid, 10, 30);
        SetWorldTime(15);
        ShowServerTime(playerid);
        return 1;
    }
    // clock off: client ends up at 15:30 once the clock comes back
    if (!strcmp(cmdtext, "/time2", true))
    {
        TogglePlayerClock(playerid, false);
        SetPlayerTime(playerid, 10, 30);
        SetWorldTime(15);
        SetTimerEx("ClockOn", 2000, false, "i", playerid);
        return 1;
    }
    return 0;
}

public ClockOn(playerid)
{
    TogglePlayerClock(playerid, true);
    ShowServerTime(playerid);
}

Clients with the clock on (TogglePlayerClock) ignore SetWorldTime entirely, so updating their stored time desynced GetPlayerTime from what they actually see. Skip them instead.

Suggested by GuiBoneVowt in the openmultiplayer#1253 review.
@LopezDev7

Copy link
Copy Markdown
Author

Thanks for the really thorough testing @GuiBoneVowt, much appreciated — especially checking the client behaviour in game.

Good catch on the clocked players case. Since their clients ignore SetWorldTime completely, updating their stored time was just desyncing GetPlayerTime from what they actually see. Pushed 79b0632, which skips players with hasClock() in that loop, as you suggested. Kept it in this PR since it's a small change in the same code path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SetPlayerTime bug

2 participants