Files
LordMEandGitHub ced8db2d1f fixes bad index in playtime subsystem (#7353)
<!-- Write **BELOW** The Headers and **ABOVE** The comments else it may
not be viewable. -->
<!-- You can view Contributing.MD for a detailed description of the pull
request process. -->

## About The Pull Request
[2025-10-14T16:36:49] Runtime in code/controllers/subsystem/playtime.dm,
line 48: bad index
proc name: flush playtimes impl
(/datum/controller/subsystem/playtime/proc/flush_playtimes_impl)
src: Playtime (/datum/controller/subsystem/playtime)
call stack:
Playtime (/datum/controller/subsystem/playtime): flush playtimes impl()
Playtime (/datum/controller/subsystem/playtime): flush playtimes()
Playtime (/datum/controller/subsystem/playtime): fire(0)
Playtime (/datum/controller/subsystem/playtime): ignite(0)
Master (/datum/controller/master): RunQueue()
Master (/datum/controller/master): Loop(4)
Master (/datum/controller/master): StartProcessing(0)
<!-- Describe The Pull Request. Please be sure every change is
documented or this can delay review and even discourage maintainers from
merging your PR! -->

## Why It's Good For The Game

<!-- Argue for the merits of your changes and how they benefit the game,
especially if they are controversial and/or far reaching. If you can't
actually explain WHY what you are doing will improve the game, then it
probably isn't good for the game in the first place. -->

## Changelog

<!-- If your PR modifies aspects of the game that can be concretely
observed by players or admins you should add a changelog. If your change
does NOT meet this description, remove this section. Please note that
maintainers freely reserve the right to remove and add tags should they
deem it appropriate. You can attempt to finagle the system all you want,
but it's best to shoot for clear communication right off the bat. -->

🆑
fix: fixed a runtime in the playtime subsystem
/🆑

<!-- Both 🆑's are required for the changelog to work! You can put
your name to the right of the first 🆑 if you want to overwrite your
GitHub username as author ingame. -->
<!-- You can use multiple of the same prefix (they're only used for the
icon ingame) and delete the unneeded ones. Despite some of the tags,
changelogs should generally represent how a player might be affected by
the changes rather than a summary of the PR's contents. -->
2025-10-18 17:59:16 +01:00

93 lines
2.8 KiB
Plaintext

/**
* playtime tracking system
*
* yes, the code is messy and probably shouldn't be half-in-subsystem and half-elsewhere, buuut
* whatever.
*
* todo: this can probably be optimized to be better at yielding instead of using dumb CHECK_TICKS.
*/
SUBSYSTEM_DEF(playtime)
name = "Playtime"
wait = 10 MINUTES
subsystem_flags = SS_NO_TICK_CHECK | SS_NO_INIT
/datum/controller/subsystem/playtime/Shutdown()
flush_playtimes()
return ..()
/datum/controller/subsystem/playtime/fire(resumed)
for(var/client/C in GLOB.clients)
if(!C.initialized)
continue
queue_playtimes(C)
CHECK_TICK
flush_playtimes()
/datum/controller/subsystem/playtime/proc/flush_playtimes()
if(!SSdbcore.Connect())
return
// admin proccall guard override - there's no volatile args here
var/old_usr = usr
usr = null
. = flush_playtimes_impl()
usr = old_usr
/datum/controller/subsystem/playtime/proc/flush_playtimes_impl()
var/list/built = list()
for(var/client/C in GLOB.clients)
if(!C.initialized)
continue
var/playerid = C.player.player_id
for(var/roleid in C.persistent.playtime_queued)
var/minutes = C.persistent.playtime_queued[roleid]
built[++built.len] = list(
"roleid" = roleid,
"minutes" = minutes,
"player" = playerid
)
LAZYADDASSOC(C.persistent.playtime, roleid, minutes)
C.persistent.playtime_queued = list()
SSdbcore.MassInsertLegacy(DB_PREFIX_TABLE_NAME("playtime"), built, duplicate_key = "ON DUPLICATE KEY UPDATE minutes = minutes + VALUES(minutes)")
/**
* returns a list of playtime roles
*/
/datum/controller/subsystem/playtime/proc/playtime_for(mob/M)
if(isobserver(M))
var/mob/observer/dead/ghost = M
return list(ghost.started_as_observer? PLAYER_PLAYTIME_OBSERVER : PLAYER_PLAYTIME_DEAD)
else if(isnewplayer(M))
return list(PLAYER_PLAYTIME_LOBBY)
if(IS_DEAD(M))
. = list(PLAYER_PLAYTIME_DEAD)
else
. = list(PLAYER_PLAYTIME_LIVING)
var/best_effort_attempt_at_resolving_legacy_name_based_roles = M.mind?.assigned_role
var/datum/prototype/role/job/J = RSroles.legacy_job_by_title(best_effort_attempt_at_resolving_legacy_name_based_roles)
if(J)
. += PLAYER_PLAYTIME_ROLE(J.id)
/datum/controller/subsystem/playtime/proc/queue_playtimes(client/C)
set waitfor = FALSE
queue_playtimes_sync(C)
/datum/controller/subsystem/playtime/proc/queue_playtimes_sync(client/C)
if(isnull(C))
return
if(!C.initialized)
CRASH("how was this called on an uninitialized client?")
if(!SSdbcore.Connect())
return
var/list/playtimes = playtime_for(C.mob)
var/now = REALTIMEOFDAY
// deciseconds to minutes
var/since_last = round((now - C.persistent.playtime_last) * (1 / 10) * (1 / 60))
C.persistent.playtime_last = now
if(since_last < 0)
CRASH("how was since_last [since_last] < 0?")
if(!length(playtimes))
return
LAZYINITLIST(C.persistent.playtime_queued)
for(var/role in playtimes)
C.persistent.playtime_queued[role] += since_last