-----------------------------------------------
-- World representation for a (very) simple MUD
-----------------------------------------------

module World where

-- The type of world descriptions
type World = [(PlaceId,Place)] 

-- The type of individual places in a world description
type Place = (Description, Moves)
type Moves = [(Direction,PlaceId)]

type PlaceId     = String 
type Description = String 
type Direction   = String 

newPlace :: Place
newPlace = ("Nothing to see and nowhere to go",[])

init :: World
init = []

startingPlace :: PlaceId
startingPlace = "start"

info :: World -> PlaceId -> Place
info w i = 
  case lookup i w of
    Nothing -> newPlace
    Just p -> p

update :: World -> PlaceId -> Place -> World
update w i p =
  let w' = filter (\(i',_) -> i /= i') w in 
  (i,p) : w'

---------------------------------------------------------------
-- Utility functions

move :: World -> PlaceId -> Direction -> Maybe PlaceId
move w i dir =
  case lookup i w of
    Nothing -> Nothing
    Just (_,m) -> case lookup dir m of
                    Nothing -> Nothing
                    Just i -> Just i

