wizard-wipeout/src-server/Server/Game/Update.hs

78 lines
2.1 KiB
Haskell
Raw Normal View History

2024-05-01 21:52:49 +00:00
module Server.Game.Update where
import qualified Control.Concurrent.STM as STM
import Control.Monad
import Control.Monad.Reader
import Control.Monad.State
2024-11-29 10:24:07 +00:00
import Data.List (foldl')
2024-05-01 21:52:49 +00:00
-- internal imports
import Library.Types
import Server.Types
updateSpawners :: Float -> Game ()
updateSpawners dt =
2024-11-03 03:40:46 +00:00
modify' (\sc@(StateContainer spawners _ _ _ _ _ _) ->
2024-05-01 21:52:49 +00:00
let newSpawners = map
(\spawner -> do
let newTTL = spawnerReloadTTL spawner - dt
if newTTL < 0 && spawnerState spawner == SpawnerEmpty
then spawner
{ spawnerReloadTTL = fromIntegral $ spawnerReloadTime spawner
, spawnerState = SpawnerFull
}
else spawner
{ spawnerReloadTTL = spawnerReloadTTL spawner - dt
}
)
spawners
in sc
{ scSpawners = newSpawners
}
)
updateWizards :: Float -> Game ()
updateWizards dt = do
playersVar <- gets scPlayers
players <- liftIO $ STM.atomically $ STM.readTMVar playersVar
let newPlayers = map
2024-11-29 10:24:07 +00:00
(\player@(Player _pId wizard@(Wizard _ _ health _ _ effects) readiness _lastPong) ->
let newEffects = foldl'
2024-05-01 21:52:49 +00:00
(\acc effect@(Effect _ ttl) ->
if ttl - dt < 0
then acc
else effect
{ effectTTL = ttl - dt
} : acc
)
[]
effects
2024-11-29 10:24:07 +00:00
effectDamage = foldl'
2024-05-01 21:52:49 +00:00
(\acc (Effect kind _) ->
if kind `elem` harmingAfflictions
then acc + 1
else acc
)
0
effects
in
if readiness
then
player
{ playerWizard = wizard
{ wizardEffects = newEffects
, wizardHealth = health - effectDamage
}
}
else
player
)
players
void $ liftIO $ STM.atomically $ STM.swapTMVar playersVar newPlayers