author | unc0rr |
Wed, 13 May 2015 23:21:40 +0300 | |
branch | qmlfrontend |
changeset 10929 | 8ebf01f75d9f |
parent 10907 | 9b8e9813c6f8 |
child 10990 | c99385e4654d |
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 |
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
28 |
import Database.MySQL.Simple |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
29 |
import Database.MySQL.Simple.QueryResults |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
30 |
import Database.MySQL.Simple.Result |
9409 | 31 |
import Data.List (lookup) |
32 |
import qualified Data.ByteString.Char8 as B |
|
9884 | 33 |
import Data.Word |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
34 |
-------------------------- |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
35 |
import CoreTypes |
9409 | 36 |
import Utils |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
37 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
38 |
|
2172 | 39 |
dbQueryAccount = |
10017 | 40 |
"SELECT users.pass, \ |
9435 | 41 |
\ (SELECT COUNT(users_roles.rid) FROM users_roles WHERE users.uid = users_roles.uid AND users_roles.rid = 3), \ |
42 |
\ (SELECT COUNT(users_roles.rid) FROM users_roles WHERE users.uid = users_roles.uid AND users_roles.rid = 13) \ |
|
43 |
\ 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
|
44 |
|
2172 | 45 |
dbQueryStats = |
6040 | 46 |
"INSERT INTO gameserver_stats (players, rooms, last_update) VALUES (?, ?, UNIX_TIMESTAMP())" |
2172 | 47 |
|
9409 | 48 |
dbQueryAchievement = |
9868 | 49 |
"INSERT INTO achievements (time, typeid, userid, value, filename, location, protocol) \ |
9427 | 50 |
\ VALUES (?, (SELECT id FROM achievement_types WHERE name = ?), (SELECT uid FROM users WHERE name = ?), \ |
9868 | 51 |
\ ?, ?, ?, ?)" |
9409 | 52 |
|
9446 | 53 |
dbQueryReplayFilename = "SELECT filename FROM achievements WHERE id = ?" |
54 |
||
55 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
56 |
dbInteractionLoop dbConn = forever $ do |
4932 | 57 |
q <- liftM read getLine |
2869 | 58 |
hPutStrLn stderr $ show q |
4921 | 59 |
|
2869 | 60 |
case q of |
4921 | 61 |
CheckAccount clId clUid clNick _ -> do |
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
62 |
results <- query dbConn dbQueryAccount $ Only clNick |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
63 |
let response = case results of |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
64 |
[(pass, adm, contr)] -> |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
65 |
( |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
66 |
clId, |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
67 |
clUid, |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
68 |
HasAccount |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
69 |
(pass) |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
70 |
(adm == Just (1 :: Int)) |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
71 |
(contr == Just (1 :: Int)) |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
72 |
) |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
73 |
_ -> |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
74 |
(clId, clUid, Guest) |
4932 | 75 |
print response |
2869 | 76 |
hFlush stdout |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
77 |
|
9446 | 78 |
GetReplayName clId clUid fileId -> do |
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
79 |
results <- query dbConn dbQueryReplayFilename $ Only fileId |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
80 |
let fn = if null results then "" else fromOnly $ head results |
9446 | 81 |
print (clId, clUid, ReplayName fn) |
82 |
hFlush stdout |
|
83 |
||
2869 | 84 |
SendStats clients rooms -> |
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
85 |
void $ execute dbConn dbQueryStats (clients, rooms) |
9409 | 86 |
--StoreAchievements (B.pack fileName) (map toPair teams) info |
10017 | 87 |
StoreAchievements p fileName teams info -> |
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
88 |
void $ executeMany dbConn dbQueryAchievement $ (parseStats p fileName teams) info |
2172 | 89 |
|
9446 | 90 |
|
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
91 |
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
|
92 |
|
9446 | 93 |
|
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
94 |
parseStats :: |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
95 |
Word16 |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
96 |
-> B.ByteString |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
97 |
-> [(B.ByteString, B.ByteString)] |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
98 |
-> [B.ByteString] |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
99 |
-> [(B.ByteString, B.ByteString, B.ByteString, Int, B.ByteString, B.ByteString, Int)] |
9868 | 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) = |
|
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
107 |
( time |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
108 |
, typ |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
109 |
, fromMaybe "" (lookup teamname teams) |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
110 |
, readInt_ value |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
111 |
, fileName |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
112 |
, location |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
113 |
, fromIntegral p |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
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 = |
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
119 |
Control.Exception.handle (\(e :: SomeException) -> hPutStrLn stderr $ show e) $ |
2869 | 120 |
bracket |
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
121 |
(connect mySQLConnectionInfo) |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
122 |
close |
4932 | 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 |
|
10907
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
135 |
let mySQLConnectInfo = defaultConnectInfo { |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
136 |
connectHost = dbHost |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
137 |
, connectDatabase = dbName |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
138 |
, connectUser = dbLogin |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
139 |
, connectPassword = dbPassword |
9b8e9813c6f8
Switch to mysql-simple, as hdbc package seems to be abandoned and anyway never satisfied me
unc0rr
parents:
10460
diff
changeset
|
140 |
} |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
141 |
|
2869 | 142 |
dbConnectionLoop mySQLConnectInfo |