author | unc0rr |
Tue, 24 Mar 2015 22:51:49 +0300 | |
changeset 10872 | 0ad3c53992d2 |
parent 10460 | 8dcea9087d75 |
child 10907 | 9b8e9813c6f8 |
permissions | -rw-r--r-- |
10460
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
1 |
{- |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
3 |
* Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com> |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
4 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
8 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
12 |
* GNU General Public License for more details. |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
13 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
17 |
\-} |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10017
diff
changeset
|
18 |
|
4906 | 19 |
{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-} |
2348 | 20 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
21 |
module Main where |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
22 |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
23 |
import Prelude hiding (catch) |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
24 |
import Control.Monad |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
25 |
import Control.Exception |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
26 |
import System.IO |
4932 | 27 |
import Data.Maybe |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
28 |
import Database.HDBC |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
29 |
import Database.HDBC.MySQL |
9409 | 30 |
import Data.List (lookup) |
31 |
import qualified Data.ByteString.Char8 as B |
|
9884 | 32 |
import Data.Word |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
33 |
-------------------------- |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
34 |
import CoreTypes |
9409 | 35 |
import Utils |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
36 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
37 |
|
2172 | 38 |
dbQueryAccount = |
10017 | 39 |
"SELECT users.pass, \ |
9435 | 40 |
\ (SELECT COUNT(users_roles.rid) FROM users_roles WHERE users.uid = users_roles.uid AND users_roles.rid = 3), \ |
41 |
\ (SELECT COUNT(users_roles.rid) FROM users_roles WHERE users.uid = users_roles.uid AND users_roles.rid = 13) \ |
|
42 |
\ FROM users WHERE users.name = ?" |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
43 |
|
2172 | 44 |
dbQueryStats = |
6040 | 45 |
"INSERT INTO gameserver_stats (players, rooms, last_update) VALUES (?, ?, UNIX_TIMESTAMP())" |
2172 | 46 |
|
9409 | 47 |
dbQueryAchievement = |
9868 | 48 |
"INSERT INTO achievements (time, typeid, userid, value, filename, location, protocol) \ |
9427 | 49 |
\ VALUES (?, (SELECT id FROM achievement_types WHERE name = ?), (SELECT uid FROM users WHERE name = ?), \ |
9868 | 50 |
\ ?, ?, ?, ?)" |
9409 | 51 |
|
9446 | 52 |
dbQueryReplayFilename = "SELECT filename FROM achievements WHERE id = ?" |
53 |
||
54 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
55 |
dbInteractionLoop dbConn = forever $ do |
4932 | 56 |
q <- liftM read getLine |
2869 | 57 |
hPutStrLn stderr $ show q |
4921 | 58 |
|
2869 | 59 |
case q of |
4921 | 60 |
CheckAccount clId clUid clNick _ -> do |
2869 | 61 |
statement <- prepare dbConn dbQueryAccount |
4932 | 62 |
execute statement [SqlByteString clNick] |
9435 | 63 |
result <- fetchRow statement |
2869 | 64 |
finish statement |
8924 | 65 |
let response = |
9437 | 66 |
if isJust result then let [pass, adm, contr] = fromJust result in |
2869 | 67 |
( |
4921 | 68 |
clId, |
2869 | 69 |
clUid, |
70 |
HasAccount |
|
9435 | 71 |
(fromSql pass) |
72 |
(fromSql adm == Just (1 :: Int)) |
|
73 |
(fromSql contr == Just (1 :: Int)) |
|
2869 | 74 |
) |
2919 | 75 |
else |
4921 | 76 |
(clId, clUid, Guest) |
4932 | 77 |
print response |
2869 | 78 |
hFlush stdout |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
79 |
|
9446 | 80 |
GetReplayName clId clUid fileId -> do |
81 |
statement <- prepare dbConn dbQueryReplayFilename |
|
82 |
execute statement [SqlByteString fileId] |
|
83 |
result <- fetchRow statement |
|
84 |
finish statement |
|
9450 | 85 |
let fn = if (isJust result) then fromJust . fromSql . head . fromJust $ result else "" |
9446 | 86 |
print (clId, clUid, ReplayName fn) |
87 |
hFlush stdout |
|
88 |
||
2869 | 89 |
SendStats clients rooms -> |
90 |
run dbConn dbQueryStats [SqlInt32 $ fromIntegral clients, SqlInt32 $ fromIntegral rooms] >> return () |
|
9409 | 91 |
--StoreAchievements (B.pack fileName) (map toPair teams) info |
10017 | 92 |
StoreAchievements p fileName teams info -> |
9868 | 93 |
mapM_ (run dbConn dbQueryAchievement) $ (parseStats p fileName teams) info |
2172 | 94 |
|
9446 | 95 |
|
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
96 |
readTime = read . B.unpack . B.take 19 . B.drop 8 |
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
97 |
|
9446 | 98 |
|
9868 | 99 |
parseStats :: Word16 -> B.ByteString -> [(B.ByteString, B.ByteString)] -> [B.ByteString] -> [[SqlValue]] |
100 |
parseStats p fileName teams = ps |
|
9409 | 101 |
where |
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
102 |
time = readTime fileName |
9421 | 103 |
ps [] = [] |
9409 | 104 |
ps ("DRAW" : bs) = ps bs |
105 |
ps ("WINNERS" : n : bs) = ps $ drop (readInt_ n) bs |
|
106 |
ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) = |
|
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
107 |
[ SqlUTCTime time |
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
108 |
, SqlByteString typ |
9409 | 109 |
, SqlByteString $ fromMaybe "" (lookup teamname teams) |
110 |
, SqlInt32 (readInt_ value) |
|
111 |
, SqlByteString fileName |
|
112 |
, SqlByteString location |
|
9884 | 113 |
, SqlInt32 $ fromIntegral p |
9409 | 114 |
] : ps bs |
9421 | 115 |
ps (b:bs) = ps bs |
116 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
117 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
118 |
dbConnectionLoop mySQLConnectionInfo = |
4906 | 119 |
Control.Exception.handle (\(e :: IOException) -> hPutStrLn stderr $ show e) $ handleSqlError $ |
2869 | 120 |
bracket |
121 |
(connectMySQL mySQLConnectionInfo) |
|
4932 | 122 |
disconnect |
123 |
dbInteractionLoop |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
124 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
125 |
|
4921 | 126 |
--processRequest :: DBQuery -> IO String |
127 |
--processRequest (CheckAccount clId clUid clNick clHost) = return $ show (clclId, clUid, Guest) |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
128 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
129 |
main = do |
2869 | 130 |
dbHost <- getLine |
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
131 |
dbName <- getLine |
2869 | 132 |
dbLogin <- getLine |
133 |
dbPassword <- getLine |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
134 |
|
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
135 |
let mySQLConnectInfo = defaultMySQLConnectInfo {mysqlHost = dbHost, mysqlDatabase = dbName, mysqlUser = dbLogin, mysqlPassword = dbPassword} |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
136 |
|
2869 | 137 |
dbConnectionLoop mySQLConnectInfo |