60 dbQueryGamesHistoryPlaces = "INSERT INTO rating_players (userid, gameid, place) \ |
62 dbQueryGamesHistoryPlaces = "INSERT INTO rating_players (userid, gameid, place) \ |
61 \ VALUES ((SELECT uid FROM users WHERE name = ?), ?, ?)" |
63 \ VALUES ((SELECT uid FROM users WHERE name = ?), ?, ?)" |
62 |
64 |
63 dbQueryReplayFilename = "SELECT filename FROM achievements WHERE id = ?" |
65 dbQueryReplayFilename = "SELECT filename FROM achievements WHERE id = ?" |
64 |
66 |
|
67 dbQueryBestTime = "SELECT MIN(value) FROM achievements WHERE location = ?" |
65 |
68 |
66 dbInteractionLoop dbConn = forever $ do |
69 dbInteractionLoop dbConn = forever $ do |
67 q <- liftM read getLine |
70 q <- liftM read getLine |
68 hPutStrLn stderr $ show q |
71 hPutStrLn stderr $ show q |
69 |
72 |
92 hFlush stdout |
95 hFlush stdout |
93 |
96 |
94 SendStats clients rooms -> |
97 SendStats clients rooms -> |
95 void $ execute dbConn dbQueryStats (clients, rooms) |
98 void $ execute dbConn dbQueryStats (clients, rooms) |
96 StoreAchievements p fileName teams g info -> |
99 StoreAchievements p fileName teams g info -> |
97 sequence_ $ parseStats dbConn p fileName teams g info |
100 parseStats dbConn p fileName teams g info |
98 |
101 |
99 |
102 |
100 --readTime = read . B.unpack . B.take 19 . B.drop 8 |
103 --readTime = read . B.unpack . B.take 19 . B.drop 8 |
101 readTime = B.take 19 . B.drop 8 |
104 readTime = B.take 19 . B.drop 8 |
102 |
105 |
105 -> Word16 |
108 -> Word16 |
106 -> B.ByteString |
109 -> B.ByteString |
107 -> [(B.ByteString, B.ByteString)] |
110 -> [(B.ByteString, B.ByteString)] |
108 -> GameDetails |
111 -> GameDetails |
109 -> [B.ByteString] |
112 -> [B.ByteString] |
110 -> [IO Int64] |
113 -> IO () |
111 parseStats dbConn p fileName teams (GameDetails script infRopes vamp infAttacks) = ps |
114 parseStats dbConn p fileName teams (GameDetails script infRopes vamp infAttacks) d = evalStateT (ps d) ("", maxBound) |
112 where |
115 where |
113 time = readTime fileName |
116 time = readTime fileName |
114 ps :: [B.ByteString] -> [IO Int64] |
117 ps :: [B.ByteString] -> StateT (B.ByteString, Int) IO () |
115 ps [] = [] |
118 ps [] = return () |
116 ps ("DRAW" : bs) = execute dbConn dbQueryGamesHistory (script, (fromIntegral p) :: Int, fileName, time, vamp, infRopes, infAttacks) |
119 ps ("DRAW" : bs) = do |
117 : places (map drawParams teams) |
120 io $ execute dbConn dbQueryGamesHistory (script, (fromIntegral p) :: Int, fileName, time, vamp, infRopes, infAttacks) |
118 : ps bs |
121 io $ places (map drawParams teams) |
119 ps ("WINNERS" : n : bs) = let winNum = readInt_ n in execute dbConn dbQueryGamesHistory (script, (fromIntegral p) :: Int, fileName, time, vamp, infRopes, infAttacks) |
122 ps bs |
120 : places (map (placeParams (take winNum bs)) teams) |
123 ps ("WINNERS" : n : bs) = do |
121 : ps (drop winNum bs) |
124 let winNum = readInt_ n |
122 ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) = execute dbConn dbQueryAchievement |
125 io $ execute dbConn dbQueryGamesHistory (script, (fromIntegral p) :: Int, fileName, time, vamp, infRopes, infAttacks) |
123 ( time |
126 io $ places (map (placeParams (take winNum bs)) teams) |
124 , typ |
127 ps (drop winNum bs) |
125 , fromMaybe "" (lookup teamname teams) |
128 ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) = do |
126 , (readInt_ value) :: Int |
129 let result = readInt_ value |
127 , fileName |
130 io $ execute dbConn dbQueryAchievement |
128 , location |
131 ( time |
129 , (fromIntegral p) :: Int |
132 , typ |
130 ) : ps bs |
133 , fromMaybe "" (lookup teamname teams) |
|
134 , result |
|
135 , fileName |
|
136 , location |
|
137 , (fromIntegral p) :: Int |
|
138 ) |
|
139 modify $ \st@(l, s) -> if result < s then (location, result) else st |
|
140 ps bs |
|
141 ps ("GHOST_POINTS" : n : bs) = do |
|
142 let pointsNum = readInt_ n |
|
143 (loc, time) <- get |
|
144 res <- io $ query dbConn dbQueryBestTime $ Only loc |
|
145 let bestTime = case res of |
|
146 [Only a] -> a |
|
147 _ -> maxBound :: Int |
|
148 when (time < bestTime) $ do |
|
149 -- store it |
|
150 return () |
|
151 ps (drop (2 * pointsNum) bs) |
131 ps (b:bs) = ps bs |
152 ps (b:bs) = ps bs |
|
153 |
132 drawParams t = (snd t, 0 :: Int) |
154 drawParams t = (snd t, 0 :: Int) |
133 placeParams winners t = (snd t, if (fst t) `elem` winners then 1 else 2 :: Int) |
155 placeParams winners t = (snd t, if (fst t) `elem` winners then 1 else 2 :: Int) |
134 places :: [(B.ByteString, Int)] -> IO Int64 |
156 places :: [(B.ByteString, Int)] -> IO Int64 |
135 places params = do |
157 places params = do |
136 res <- query_ dbConn dbQueryGameId |
158 res <- query_ dbConn dbQueryGameId |