gameServer/ServerState.hs
author sheepluva
Fri, 06 Dec 2013 23:53:35 +0100
changeset 9760 395ca7fe6362
parent 8452 170afc3ac39f
child 9973 7589978c9912
permissions -rw-r--r--
It seems that at the current state it is necessary to protect sending stats/ending game from multiple execution, as that can happen if you e.g. fail a mission more than once in the same tick (e.g. destroying two essential crates at the same time) Otherwise you can get a blank / stuck frontend (e.g. when using deagle to shoot the two last crates at the same time)! the best approach might be to never call the function that sends stats and ends game from any event handler directly, but instead have a flag 'isFailed' that is set to true when any of the possible fails happen and to check that flag every tick to send stats and end game if true

module ServerState
    (
    module RoomsAndClients,
    clientRoomA,
    ServerState(..),
    client's,
    allClientsS,
    allRoomsS,
    roomClientsS,
    sameProtoClientsS,
    io
    ) where

import Control.Monad.State.Strict
import Data.Set as Set(Set)
import Data.Word
----------------------
import RoomsAndClients
import CoreTypes

data ServerState = ServerState {
        clientIndex :: !(Maybe ClientIndex),
        serverInfo :: !ServerInfo,
        removedClients :: !(Set.Set ClientIndex),
        roomsClients :: !MRnC
    }


clientRoomA :: StateT ServerState IO RoomIndex
clientRoomA = do
    (Just ci) <- gets clientIndex
    rnc <- gets roomsClients
    io $ clientRoomM rnc ci

client's :: (ClientInfo -> a) -> StateT ServerState IO a
client's f = do
    (Just ci) <- gets clientIndex
    rnc <- gets roomsClients
    io $ client'sM rnc f ci

allClientsS :: StateT ServerState IO [ClientInfo]
allClientsS = gets roomsClients >>= liftIO . clientsM

allRoomsS :: StateT ServerState IO [RoomInfo]
allRoomsS = gets roomsClients >>= liftIO . roomsM

roomClientsS :: RoomIndex -> StateT ServerState IO [ClientInfo]
roomClientsS ri = do
    rnc <- gets roomsClients
    io $ roomClientsM rnc ri

sameProtoClientsS :: Word16 -> StateT ServerState IO [ClientInfo]
sameProtoClientsS p = liftM f allClientsS
    where
        f = filter (\c -> clientProto c == p)

io :: IO a -> StateT ServerState IO a
io = liftIO