module GossipTypes
(
Agent
, AgentId
, AgentName
, Call
, GroupCall
, (☎)
, toCalls
, showAgent
, printCall
, printCalls
, printMakeCall
, printGroupCall
, printGroupCalls
, printMakeGroupCall
, printNoCalls
, printAllCalls
)
where
import Control.Monad
import Data.Graph.Inductive (Gr, LEdge, LNode, prettyPrint, Graph (noNodes))
import Data.List
import System.Console.ANSI
import Util
type Agent = LNode Char
type AgentId = Int
type AgentName = Char
type Call = (Agent, Agent)
type GroupCall = (Agent, [Agent])
(☎) :: Agent -> Agent -> Call
☎ :: Agent -> Agent -> Call
(☎) Agent
f Agent
t = (Agent
f,Agent
t)
infix 0 ☎
toCalls :: GroupCall -> [Call]
toCalls :: GroupCall -> [Call]
toCalls g :: GroupCall
g@(Agent
f, [Agent]
to) = [(Agent
f,Agent
t) | Agent
t <- [Agent]
to] [Call] -> [Call] -> [Call]
forall a. [a] -> [a] -> [a]
++ [(Agent
t1,Agent
t2) | Agent
t1 <- [Agent]
to, Agent
t2 <- [Agent]
to, Agent
t1 Agent -> Agent -> Bool
forall a. Eq a => a -> a -> Bool
/= Agent
t2]
showAgent :: Agent -> String
showAgent :: Agent -> String
showAgent (Node
_, Char
lab) = [Char
lab]
printNoCalls :: IO ()
printNoCalls :: IO ()
printNoCalls = do
String -> IO ()
putStr String
"["
Color -> String -> IO ()
putStrFgc Color
Red String
"No calls to display"
String -> IO ()
putStrLn String
"]"
printCall :: Call -> IO ()
printCall :: Call -> IO ()
printCall ((Node
_, Char
i), (Node
_, Char
j)) = do
String -> IO ()
putStr (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
" (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char -> String
forall a. Show a => a -> String
show Char
i String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"," String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char -> String
forall a. Show a => a -> String
show Char
j String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
") "
printMakeCall :: Call -> IO ()
printMakeCall :: Call -> IO ()
printMakeCall ((Node
_,Char
m), (Node
_,Char
n)) = do
String -> IO ()
putStr String
"\nMaking call between "
Color -> String -> IO ()
putStrFgc Color
Green (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Char -> String
forall a. Show a => a -> String
show Char
m
String -> IO ()
putStr String
" and "
Color -> String -> IO ()
putStrLnFgc Color
Green (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Char -> String
forall a. Show a => a -> String
show Char
n
printCalls :: [Call] -> IO ()
printCalls :: [Call] -> IO ()
printCalls [] = IO ()
printNoCalls
printCalls [Call]
c = (Call -> IO ()) -> [Call] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Call -> IO ()
printCall [Call]
c
printGroupCall :: GroupCall -> IO ()
printGroupCall :: GroupCall -> IO ()
printGroupCall GroupCall
g = do
Color -> String -> IO ()
putStrFgc Color
Green String
" Groupcall with calls: "
(Call -> IO ()) -> [Call] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Call -> IO ()
printCall (GroupCall -> [Call]
toCalls GroupCall
g)
String -> IO ()
putStr String
"\n"
printMakeGroupCall :: GroupCall -> IO ()
printMakeGroupCall :: GroupCall -> IO ()
printMakeGroupCall ((Node
_,Char
f), [Agent]
to) = do
String -> IO ()
putStr String
"\nMaking group call initialised by "
Color -> String -> IO ()
putStrFgc Color
Green [Char
f]
String -> IO ()
putStr String
" to: "
Color -> String -> IO ()
putStrLnFgc Color
Green (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Char -> String -> String
forall a. a -> [a] -> [a]
intersperse Char
' ' (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ (Agent -> Char) -> [Agent] -> String
forall a b. (a -> b) -> [a] -> [b]
map Agent -> Char
forall a b. (a, b) -> b
snd [Agent]
to
printGroupCalls :: [GroupCall] -> IO ()
printGroupCalls :: [GroupCall] -> IO ()
printGroupCalls [] = IO ()
printNoCalls
printGroupCalls [GroupCall]
g = (GroupCall -> IO ()) -> [GroupCall] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ GroupCall -> IO ()
printGroupCall [GroupCall]
g
printAllCalls :: ([Call], [GroupCall]) -> IO ()
printAllCalls :: ([Call], [GroupCall]) -> IO ()
printAllCalls ([Call]
c, [GroupCall]
g) = do
Color -> String -> IO ()
putStrLnFgc Color
Yellow String
"Direct calls:"
[Call] -> IO ()
printCalls [Call]
c
String -> IO ()
putStr String
"\n"
Color -> String -> IO ()
putStrLnFgc Color
Yellow String
"Group calls:"
[GroupCall] -> IO ()
printGroupCalls [GroupCall]
g