diff options
author | Connie Price <contact@connieprice.co.uk> | 2022-01-30 04:31:16 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-30 04:31:16 +0000 |
commit | 0810a806367250c041c9732d2054e587beced1fd (patch) | |
tree | b97c9b2fec3191a480bd8cbcdbc7bd8bc04ecab0 /Northstar.Custom/mod/scripts/vscripts | |
parent | cfdee009ffc1a6379c6e5982b80d28694436ac35 (diff) | |
download | NorthstarMods-0810a806367250c041c9732d2054e587beced1fd.tar.gz NorthstarMods-0810a806367250c041c9732d2054e587beced1fd.zip |
Queued GunGame Meter (#166)
* Changed the gungame earn meter to queue up rewards, to reduce the chances of rapidly gained rewards from being handled incorrectly.
* Stop queuing up the meter clearing, immediately clear.
* Removed unnecessary prints.
* Fixed players not having individual queues.
Diffstat (limited to 'Northstar.Custom/mod/scripts/vscripts')
-rw-r--r-- | Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/_gg_earn_meter.gnut | 100 | ||||
-rw-r--r-- | Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/sh_gg_earn_meter.gnut | 4 |
2 files changed, 84 insertions, 20 deletions
diff --git a/Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/_gg_earn_meter.gnut b/Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/_gg_earn_meter.gnut index 2da60753..fbcf2b03 100644 --- a/Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/_gg_earn_meter.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/_gg_earn_meter.gnut @@ -1,44 +1,104 @@ +global function Sv_GGEarnMeter_Init global function Sv_GGEarnMeter_SetPercentage global function Sv_GGEarnMeter_AddPercentage global function AddCallback_GGEarnMeterFull +enum RewardType +{ + SET, + ADD +} + +struct RewardStruct +{ + float amount = 0.0 + int type = RewardType.ADD +} + struct { - array< bool functionref( entity ) > onGGEarnMeterFullCallbacks + array< bool functionref( entity ) > onGGEarnMeterFullCallbacks, + table< entity, array< RewardStruct > > rewardQueue, // Because we have delayed reward handling, let's queue things so that everything gets handled eventually. + table< entity, bool > checkingQueue } file -void function Sv_GGEarnMeter_SetPercentage( entity player, float percentage ) +void function Sv_GGEarnMeter_Init() { - player.SetPlayerNetFloat( "gunGameLevelPercentage", clamp(percentage, 0.0, 1.0) ) - - thread CheckPercentage( player ) + AddCallback_OnClientConnected( Sv_GGEarnMeter_OnClientConnected ) } -void function Sv_GGEarnMeter_AddPercentage( entity player, float percentage ) +void function Sv_GGEarnMeter_OnClientConnected( entity player ) { - Sv_GGEarnMeter_SetPercentage(player, player.GetPlayerNetFloat( "gunGameLevelPercentage") + percentage) + file.rewardQueue[ player ] <- [] + file.checkingQueue[ player ] <- false } -void function AddCallback_GGEarnMeterFull( bool functionref( entity ) callbackFunc ) +void function AddReward( entity player, float amount, int type ) { - Assert( !file.onGGEarnMeterFullCallbacks.contains( callbackFunc ), "Already added " + FunctionToString( callbackFunc ) + " with AddCallback_GGEarnMeterFull" ) + RewardStruct reward + reward.amount = amount + reward.type = type - file.onGGEarnMeterFullCallbacks.append( callbackFunc ) + file.rewardQueue[player].append( reward ) + + TryCheckQueue( player ) } -void function CheckPercentage( entity player ) +void function TryCheckQueue( entity player ) { - float percentage = player.GetPlayerNetFloat( "gunGameLevelPercentage") + if ( !file.checkingQueue[player] ) { + thread CheckQueue( player ) + } +} + +void function CheckQueue( entity player ) +{ + player.EndSignal( "OnDestroy" ) // If the player disconnects or stops existing for some reason, stop doing shit with it so the server doesn't break. + + file.checkingQueue[player] = true + + while ( file.rewardQueue[player].len() > 0 && IsValid(player) ) + { + RewardStruct reward = file.rewardQueue[player].remove(0) + + float newValue = reward.amount + if ( reward.type == RewardType.ADD ) + newValue += player.GetPlayerNetFloat( "gunGameLevelPercentage") + newValue = clamp( newValue, 0.0, 1.0 ) - if ( percentage < 1.0 ) - return + player.SetPlayerNetFloat( "gunGameLevelPercentage", newValue ) - wait 0.2 + wait 0.2 - bool lastWeapon = false - foreach ( callbackFunc in file.onGGEarnMeterFullCallbacks ) - lastWeapon = callbackFunc( player ) + if ( (newValue * 100) < 99 ) // To try and catch fun rounding issues. + continue - if ( !lastWeapon ) - Sv_GGEarnMeter_SetPercentage(player, 0.0) + bool keepFull = false + foreach ( callbackFunc in file.onGGEarnMeterFullCallbacks ) + keepFull = callbackFunc( player ) + + if ( !keepFull ) + player.SetPlayerNetFloat( "gunGameLevelPercentage", 0.0 ) + + wait 0.2 + } + + file.checkingQueue[player] = false +} + +void function Sv_GGEarnMeter_SetPercentage( entity player, float percentage ) +{ + AddReward( player, percentage, RewardType.SET ) +} + +void function Sv_GGEarnMeter_AddPercentage( entity player, float percentage ) +{ + AddReward( player, percentage, RewardType.ADD ) +} + +void function AddCallback_GGEarnMeterFull( bool functionref( entity ) callbackFunc ) +{ + Assert( !file.onGGEarnMeterFullCallbacks.contains( callbackFunc ), "Already added " + FunctionToString( callbackFunc ) + " with AddCallback_GGEarnMeterFull" ) + + file.onGGEarnMeterFullCallbacks.append( callbackFunc ) }
\ No newline at end of file diff --git a/Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/sh_gg_earn_meter.gnut b/Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/sh_gg_earn_meter.gnut index 2eba4eee..6ba7975f 100644 --- a/Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/sh_gg_earn_meter.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/gamemodes/gg_earn_meter/sh_gg_earn_meter.gnut @@ -15,6 +15,10 @@ void function Sh_GGEarnMeter_Init(string gamemode) file.callbackAdded = true AddCallback_OnRegisteringCustomNetworkVars( RegisterSharedNetwork ) } + + #if SERVER + Sv_GGEarnMeter_Init() + #endif } void function RegisterSharedNetwork() |