Store TrophyRace records in database
authorunc0rr
Sat, 24 Aug 2013 00:13:15 +0400
changeset 9409 6564baf7dedf
parent 9407 860f959f92c9
child 9411 d95ac9992529
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	Fri Aug 23 16:02:08 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,9 +53,22 @@
 
         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
 
-        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
 
 dbConnectionLoop mySQLConnectionInfo =
     Control.Exception.handle (\(e :: IOException) -> hPutStrLn stderr $ show e) $ handleSqlError $
--- a/hedgewars/uScript.pas	Fri Aug 23 16:02:08 2013 +0400
+++ b/hedgewars/uScript.pas	Sat Aug 24 00:13:15 2013 +0400
@@ -1899,10 +1899,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	Fri Aug 23 16:02:08 2013 +0400
+++ b/hedgewars/uStats.pas	Sat Aug 24 00:13:15 2013 +0400
@@ -35,7 +35,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 @@
     ScriptCall('onAchievementsDeclaration');
 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	Fri Aug 23 16:02:08 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