equal
deleted
inserted
replaced
53 |
53 |
54 dbQueryGamesHistory = |
54 dbQueryGamesHistory = |
55 "INSERT INTO rating_games (script, protocol, filename, time) \ |
55 "INSERT INTO rating_games (script, protocol, filename, time) \ |
56 \ VALUES (?, ?, ?, ?)" |
56 \ VALUES (?, ?, ?, ?)" |
57 |
57 |
|
58 dbQueryGameId = "SELECT LAST_INSERT_ID()" |
|
59 |
58 dbQueryGamesHistoryPlaces = "INSERT INTO rating_players (userid, gameid, place) \ |
60 dbQueryGamesHistoryPlaces = "INSERT INTO rating_players (userid, gameid, place) \ |
59 \ VALUES ((SELECT uid FROM users WHERE name = ?), LAST_INSERT_ID(), ?)" |
61 \ VALUES ((SELECT uid FROM users WHERE name = ?), ?, ?)" |
60 |
62 |
61 dbQueryReplayFilename = "SELECT filename FROM achievements WHERE id = ?" |
63 dbQueryReplayFilename = "SELECT filename FROM achievements WHERE id = ?" |
62 |
64 |
63 |
65 |
64 dbInteractionLoop dbConn = forever $ do |
66 dbInteractionLoop dbConn = forever $ do |
110 where |
112 where |
111 time = readTime fileName |
113 time = readTime fileName |
112 ps :: [B.ByteString] -> [IO Int64] |
114 ps :: [B.ByteString] -> [IO Int64] |
113 ps [] = [] |
115 ps [] = [] |
114 ps ("DRAW" : bs) = execute dbConn dbQueryGamesHistory (script, (fromIntegral p) :: Int, fileName, time) |
116 ps ("DRAW" : bs) = execute dbConn dbQueryGamesHistory (script, (fromIntegral p) :: Int, fileName, time) |
115 : executeMany dbConn dbQueryGamesHistoryPlaces (map drawParams teams) |
117 : places (map drawParams teams) |
116 : ps bs |
118 : ps bs |
117 ps ("WINNERS" : n : bs) = let winNum = readInt_ n in execute dbConn dbQueryGamesHistory (script, (fromIntegral p) :: Int, fileName, time) |
119 ps ("WINNERS" : n : bs) = let winNum = readInt_ n in execute dbConn dbQueryGamesHistory (script, (fromIntegral p) :: Int, fileName, time) |
118 : executeMany dbConn dbQueryGamesHistoryPlaces (map (placeParams (take winNum bs)) teams) |
120 : places (map (placeParams (take winNum bs)) teams) |
119 : ps (drop winNum bs) |
121 : ps (drop winNum bs) |
120 ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) = execute dbConn dbQueryAchievement |
122 ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) = execute dbConn dbQueryAchievement |
121 ( time |
123 ( time |
122 , typ |
124 , typ |
123 , fromMaybe "" (lookup teamname teams) |
125 , fromMaybe "" (lookup teamname teams) |
127 , (fromIntegral p) :: Int |
129 , (fromIntegral p) :: Int |
128 ) : ps bs |
130 ) : ps bs |
129 ps (b:bs) = ps bs |
131 ps (b:bs) = ps bs |
130 drawParams t = (snd t, 0 :: Int) |
132 drawParams t = (snd t, 0 :: Int) |
131 placeParams winners t = (snd t, if (fst t) `elem` winners then 1 else 2 :: Int) |
133 placeParams winners t = (snd t, if (fst t) `elem` winners then 1 else 2 :: Int) |
|
134 places :: [(B.ByteString, Int)] -> IO Int64 |
|
135 places params = do |
|
136 res <- query_ dbConn dbQueryGameId |
|
137 let gameId = case res of |
|
138 [Only a] -> a |
|
139 _ -> 0 |
|
140 mapM_ (execute dbConn dbQueryGamesHistoryPlaces . midInsert gameId) params |
|
141 return 0 |
|
142 midInsert :: Int -> (a, b) -> (a, Int, b) |
|
143 midInsert g (a, b) = (a, g, b) |
132 |
144 |
133 dbConnectionLoop mySQLConnectionInfo = |
145 dbConnectionLoop mySQLConnectionInfo = |
134 Control.Exception.handle (\(e :: SomeException) -> hPutStrLn stderr $ show e) $ |
146 Control.Exception.handle (\(e :: SomeException) -> hPutStrLn stderr $ show e) $ |
135 bracket |
147 bracket |
136 (connect mySQLConnectionInfo) |
148 (connect mySQLConnectionInfo) |