Store TrophyRace records in database 0.9.19
authorunc0rr
Sat, 24 Aug 2013 00:13:15 +0400
branch0.9.19
changeset 9138 9e94a9bc8c7e
parent 9135 151c8e553de2
child 9140 a6bb1fde2939
Store TrophyRace records in database
gameServer/OfficialServer/extdbinterface.hs
hedgewars/uScript.pas
hedgewars/uStats.pas
share/hedgewars/Data/Maps/TrophyRace/map.lua
--- a/gameServer/OfficialServer/extdbinterface.hs	Tue Aug 06 00:26:07 2013 +0400
+++ b/gameServer/OfficialServer/extdbinterface.hs	Sat Aug 24 00:13:15 2013 +0400
@@ -9,8 +9,11 @@
 import Data.Maybe
 import Database.HDBC
 import Database.HDBC.MySQL
+import Data.List (lookup)
+import qualified Data.ByteString.Char8 as B
 --------------------------
 import CoreTypes
+import Utils
 
 
 dbQueryAccount =
@@ -19,6 +22,11 @@
 dbQueryStats =
     "INSERT INTO gameserver_stats (players, rooms, last_update) VALUES (?, ?, UNIX_TIMESTAMP())"
 
+dbQueryAchievement =
+    "INSERT INTO achievements (typeid, userid, value, filename, location) \
+    \ VALUES ((SELECT id FROM achievement_types WHERE name = ?), (SELECT uid FROM users WHERE name = ?), \
+    \ ?, ?, ?)"
+
 dbInteractionLoop dbConn = forever $ do
     q <- liftM read getLine
     hPutStrLn stderr $ show q
@@ -45,7 +53,27 @@
 
         SendStats clients rooms ->
                 run dbConn dbQueryStats [SqlInt32 $ fromIntegral clients, SqlInt32 $ fromIntegral rooms] >> return ()
+--StoreAchievements (B.pack fileName) (map toPair teams) info
+        StoreAchievements fileName teams info -> 
+            mapM_ (run dbConn dbQueryAchievement) $ (parseStats fileName teams) info
 
+<<<<<<< local
+        StoreAchievements {} -> return ()
+
+=======
+parseStats :: B.ByteString -> [(B.ByteString, B.ByteString)] -> [B.ByteString] -> [[SqlValue]]
+parseStats fileName teams = ps
+    where
+    ps ("DRAW" : bs) = ps bs
+    ps ("WINNERS" : n : bs) = ps $ drop (readInt_ n) bs
+    ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) =
+        [SqlByteString typ
+        , SqlByteString $ fromMaybe "" (lookup teamname teams)
+        , SqlInt32 (readInt_ value)
+        , SqlByteString fileName
+        , SqlByteString location
+        ] : ps bs
+>>>>>>> other
 
 dbConnectionLoop mySQLConnectionInfo =
     Control.Exception.handle (\(e :: IOException) -> hPutStrLn stderr $ show e) $ handleSqlError $
--- a/hedgewars/uScript.pas	Tue Aug 06 00:26:07 2013 +0400
+++ b/hedgewars/uScript.pas	Sat Aug 24 00:13:15 2013 +0400
@@ -1840,10 +1840,10 @@
 function lc_declareachievement(L : Plua_State) : LongInt; Cdecl;
 var gear: PGear;
 begin
-    if lua_gettop(L) <> 3 then
+    if lua_gettop(L) <> 4 then
         LuaError('Lua: Wrong number of parameters passed to DeclareAchievement!')
     else
-        declareAchievement(lua_tostring(L, 1), lua_tostring(L, 2), lua_tointeger(L, 3));
+        declareAchievement(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4));
     lc_declareachievement:= 0
 end;
 ///////////////////
--- a/hedgewars/uStats.pas	Tue Aug 06 00:26:07 2013 +0400
+++ b/hedgewars/uStats.pas	Sat Aug 24 00:13:15 2013 +0400
@@ -34,7 +34,7 @@
 procedure TurnReaction;
 procedure SendStats;
 procedure hedgehogFlight(Gear: PGear; time: Longword);
-procedure declareAchievement(id, teamname: shortstring; value: LongInt);
+procedure declareAchievement(id, teamname, location: shortstring; value: LongInt);
 
 implementation
 uses uSound, uLocale, uVariables, uUtils, uIO, uCaptions, uDebug, uMisc, uConsole, uScript;
@@ -311,12 +311,13 @@
 WriteLnToConsole('');
 end;
 
-procedure declareAchievement(id, teamname: shortstring; value: LongInt);
+procedure declareAchievement(id, teamname, location: shortstring; value: LongInt);
 begin
     if (length(id) = 0) or (length(teamname) = 0) then exit;
     WriteLnToConsole('ACHIEVEMENT');
     WriteLnToConsole(id);
     WriteLnToConsole(teamname);
+    WriteLnToConsole(location);
     WriteLnToConsole(inttostr(value));
 end;
  
--- a/share/hedgewars/Data/Maps/TrophyRace/map.lua	Tue Aug 06 00:26:07 2013 +0400
+++ b/share/hedgewars/Data/Maps/TrophyRace/map.lua	Sat Aug 24 00:13:15 2013 +0400
@@ -165,6 +165,6 @@
 
 function onAchievementsDeclaration()
     if besthog ~= nil then
-      DeclareAchievement("rope race", GetHogTeamName(besthog), besttime)
+      DeclareAchievement("rope race", GetHogTeamName(besthog), "TrophyRace", besttime)
     end
 end