1804
|
1 |
module CoreTypes where
|
|
2 |
|
|
3 |
import System.IO
|
|
4 |
import Control.Concurrent.Chan
|
|
5 |
import Control.Concurrent.STM
|
|
6 |
import Data.Word
|
|
7 |
import qualified Data.Map as Map
|
|
8 |
import qualified Data.IntMap as IntMap
|
|
9 |
import qualified Data.IntSet as IntSet
|
|
10 |
import Data.Sequence(Seq, empty)
|
|
11 |
import Network
|
|
12 |
|
1833
|
13 |
|
1804
|
14 |
data ClientInfo =
|
|
15 |
ClientInfo
|
|
16 |
{
|
|
17 |
clientUID :: Int,
|
|
18 |
sendChan :: Chan [String],
|
|
19 |
clientHandle :: Handle,
|
|
20 |
host :: String,
|
|
21 |
nick :: String,
|
|
22 |
clientProto :: Word16,
|
|
23 |
roomID :: Int,
|
|
24 |
isMaster :: Bool,
|
|
25 |
isReady :: Bool,
|
|
26 |
forceQuit :: Bool,
|
|
27 |
partRoom :: Bool
|
|
28 |
}
|
|
29 |
|
|
30 |
instance Show ClientInfo where
|
|
31 |
show ci = show $ clientUID ci
|
|
32 |
|
|
33 |
instance Eq ClientInfo where
|
|
34 |
a1 == a2 = clientHandle a1 == clientHandle a2
|
|
35 |
|
|
36 |
data HedgehogInfo =
|
|
37 |
HedgehogInfo String String
|
|
38 |
|
|
39 |
data TeamInfo =
|
|
40 |
TeamInfo
|
|
41 |
{
|
|
42 |
teamowner :: String,
|
|
43 |
teamname :: String,
|
|
44 |
teamcolor :: String,
|
|
45 |
teamgrave :: String,
|
|
46 |
teamfort :: String,
|
|
47 |
teamvoicepack :: String,
|
|
48 |
difficulty :: Int,
|
|
49 |
hhnum :: Int,
|
|
50 |
hedgehogs :: [HedgehogInfo]
|
|
51 |
}
|
|
52 |
|
|
53 |
data RoomInfo =
|
|
54 |
RoomInfo
|
|
55 |
{
|
|
56 |
roomUID :: Int,
|
|
57 |
name :: String,
|
|
58 |
password :: String,
|
|
59 |
roomProto :: Word16,
|
|
60 |
teams :: [TeamInfo],
|
|
61 |
gameinprogress :: Bool,
|
|
62 |
playersIn :: !Int,
|
|
63 |
readyPlayers :: Int,
|
|
64 |
playersIDs :: IntSet.IntSet,
|
|
65 |
isRestrictedJoins :: Bool,
|
|
66 |
isRestrictedTeams :: Bool,
|
|
67 |
roundMsgs :: Seq String,
|
|
68 |
leftTeams :: [String],
|
|
69 |
teamsAtStart :: [TeamInfo],
|
|
70 |
params :: Map.Map String [String]
|
|
71 |
}
|
|
72 |
|
|
73 |
instance Show RoomInfo where
|
|
74 |
show ri = (show $ roomUID ri)
|
|
75 |
++ ", players ids: " ++ (show $ IntSet.size $ playersIDs ri)
|
|
76 |
++ ", players: " ++ (show $ playersIn ri)
|
1824
|
77 |
++ ", ready: " ++ (show $ readyPlayers ri)
|
1804
|
78 |
|
|
79 |
instance Eq RoomInfo where
|
|
80 |
a1 == a2 = roomUID a1 == roomUID a2
|
|
81 |
|
|
82 |
newRoom = (
|
|
83 |
RoomInfo
|
|
84 |
0
|
|
85 |
""
|
|
86 |
""
|
|
87 |
0
|
|
88 |
[]
|
|
89 |
False
|
|
90 |
0
|
|
91 |
0
|
|
92 |
IntSet.empty
|
|
93 |
False
|
|
94 |
False
|
|
95 |
Data.Sequence.empty
|
|
96 |
[]
|
|
97 |
[]
|
|
98 |
(Map.singleton "MAP" ["+rnd+"])
|
|
99 |
)
|
|
100 |
|
|
101 |
data StatisticsInfo =
|
|
102 |
StatisticsInfo
|
|
103 |
{
|
|
104 |
playersNumber :: Int,
|
|
105 |
roomsNumber :: Int
|
|
106 |
}
|
|
107 |
|
|
108 |
data ServerInfo =
|
|
109 |
ServerInfo
|
|
110 |
{
|
|
111 |
isDedicated :: Bool,
|
|
112 |
serverMessage :: String,
|
|
113 |
listenPort :: PortNumber,
|
|
114 |
loginsNumber :: Int,
|
|
115 |
nextRoomID :: Int,
|
1832
|
116 |
dbHost :: String,
|
|
117 |
dbLogin :: String,
|
|
118 |
dbPassword :: String,
|
1833
|
119 |
stats :: TMVar StatisticsInfo,
|
|
120 |
dbQueries :: Chan DBQuery
|
1804
|
121 |
}
|
|
122 |
|
|
123 |
instance Show ServerInfo where
|
|
124 |
show si = "Logins: " ++ (show $ loginsNumber si)
|
|
125 |
|
|
126 |
newServerInfo = (
|
|
127 |
ServerInfo
|
|
128 |
True
|
|
129 |
"<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>"
|
|
130 |
46631
|
|
131 |
0
|
|
132 |
0
|
1832
|
133 |
""
|
|
134 |
""
|
|
135 |
""
|
1804
|
136 |
)
|
|
137 |
|
|
138 |
data CoreMessage =
|
|
139 |
Accept ClientInfo
|
|
140 |
| ClientMessage (Int, [String])
|
|
141 |
-- | CoreMessage String
|
|
142 |
-- | TimerTick
|
|
143 |
|
1833
|
144 |
data DBQuery =
|
|
145 |
HasRegistered String
|
|
146 |
| CheckPassword String
|
|
147 |
|
1804
|
148 |
|
|
149 |
type Clients = IntMap.IntMap ClientInfo
|
|
150 |
type Rooms = IntMap.IntMap RoomInfo
|
|
151 |
|
|
152 |
--type ClientsTransform = [ClientInfo] -> [ClientInfo]
|
|
153 |
--type RoomsTransform = [RoomInfo] -> [RoomInfo]
|
|
154 |
--type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [ClientInfo]
|
|
155 |
--type Answer = ServerInfo -> (HandlesSelector, [String])
|
|
156 |
|
|
157 |
type ClientsSelector = Clients -> Rooms -> [Int]
|