· 8 years ago · Jul 20, 2018, 03:24 PM
1local PLSpeedTable = {}
2
3local PLAYERSTATS = "slb_players_v3" -- new table, again!
4local IDX_PLAYERSTATS = "IDX_SLBSTATS3"
5
6if ( !sql.TableExists( PLAYERSTATS ) ) then
7 sql.Query( "CREATE TABLE IF NOT EXISTS "..PLAYERSTATS.." ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, steamid INTEGER, won, lost INTEGER, topspeed FLOAT, mapname TEXT );" )
8 sql.Query( "CREATE INDEX "..IDX_PLAYERSTATS.." ON "..PLAYERSTATS.." ( target DESC )" )
9end
10
11local function ClearSteamID( sid )
12 sid = string.lower( sid )
13 if ( string.find( sid, "steam" ) ) then
14 sid = string.gsub( sid, "steam", "" )
15 sid = string.gsub( sid, ":", "" )
16 sid = string.gsub( sid, "_", "" )
17 sid = tonumber( sid )
18 else
19 sid = 0
20 end
21 return sid
22end
23
24function stats_load( ply )
25 local sid = ClearSteamID( ply:SteamID() )
26 local mapname = game.GetMap()
27 local result = sql.Query( "SELECT topspeed, mapname, won, lost FROM "..PLAYERSTATS.." WHERE steamid = "..sid..";" )
28 if ( result == nil ) then
29 sql.Query( "INSERT INTO "..PLAYERSTATS.." ( steamid, topspeed, mapname, won, lost ) VALUES ( "..sid..", 0, '"..mapname.."', 0, 0 );" )
30 ply:PrintMessage( HUD_PRINTTALK, "[Sledbuild] Welcome to Sledbuild!,all your stats are getting saved!" )
31 else
32 for k, v in pairs( result ) do
33 if ( v['mapname'] && v['topspeed'] && v['mapname'] == game.GetMap() ) then
34 PLSpeedTable[sid].gts = tonumber( v['topspeed'] )
35 ply:SetNWString( "_GTS", tostring( PLSpeedTable[sid].gts / 10 ) )
36 if ( v['won'] && v['lost'] ) then
37 ply:PrintMessage( HUD_PRINTTALK, "[Sledbuild] Total Races Won: "..v['won']..", Lost: "..v['lost'] )
38 end
39 break
40 end
41 end
42 end
43end
44
45function stats_save( ply )
46 local sid = ClearSteamID( ply:SteamID() )
47 local mapname = game.GetMap()
48 local topspeed = PLSpeedTable[ClearSteamID(ply:SteamID())].gts
49 local won, lost = ply:Frags(), ply:Deaths()
50
51 local result = sql.Query( "SELECT won, lost FROM "..PLAYERSTATS.." WHERE steamid = "..sid..";" )
52 for k, v in pairs( result ) do
53 if ( v['won'] && v['lost'] ) then
54 won = won + tonumber( v['won'] )
55 lost = lost + tonumber( v['lost'] )
56 break
57 end
58 end
59 sql.Query( "UPDATE "..PLAYERSTATS.." SET topspeed = "..topspeed..", won = "..won..", lost = "..lost.." WHERE steamid = "..sid..";" )
60end
61
62-- // -- Player Stats Shit here -- // --
63
64local lastspeedpublish = CurTime()
65local PUBLISHPAUSE = 0.66
66
67PLSpeedTable = PLSpeedTable or {}
68
69function PST_UpdateNW()
70 if lastspeedpublish + PUBLISHPAUSE < CurTime() then
71 lastspeedpublish = CurTime()
72 for _, pl in pairs( player.GetAll() ) do
73 local sid = ClearSteamID(pl:SteamID())
74 pl:SetNWInt( "_SPEED", PLSpeedTable[sid].speed )
75 pl:SetNWInt( "_CTS", PLSpeedTable[sid].cts )
76 pl:SetNWInt( "_GTS", PLSpeedTable[sid].gts )
77 end
78 end
79end
80hook.Add( "Think", "PST_UpdateNW", PST_UpdateNW )
81
82function GenerateSubmitKey( ply ) -- some fancy stuff here :P
83 //local key = tostring( math.floor( math.sin( CurTime() * ply:UniqueID() ) * ( math.random( 50000 ) + 1337 ) ) ) -- no floating point pl0x
84 //key = string.sub( ply:Name(), -math.random(3) )..key..string.reverse( string.sub( ply:SteamID(), -math.random(5) ) )
85 return tostring( math.floor( CurTime() ) + 50 ).."_LOLHAX" //key -- NOW BEAT THAT MOFO xD
86end
87
88function PST_DelayedKeySend( ply )
89 local sid = ClearSteamID(ply:SteamID())
90 PLSpeedTable[sid].key = GenerateSubmitKey( ply )
91
92 local rp = RecipientFilter()
93 rp:AddPlayer( ply )
94 umsg.Start( "recspeedkey", rp )
95 umsg.String( PLSpeedTable[sid].key )
96 umsg.End()
97end
98
99function PST_PlayerInitialSpawn( ply )
100 local sid = ClearSteamID(ply:SteamID())
101 PLSpeedTable[sid] = { cts = 0, speed = 0, gts = 0, key = "" }
102 stats_load( ply )
103 //timer.Simple(1, PST_DelayedKeySend, ply )
104end
105//hook.Add( "PlayerInitialSpawn", "PST_PlayerInitialSpawn", PST_PlayerInitialSpawn )
106//added it to the initialspawn hook @ init.lua
107
108function PST_PlayerDisconnected( ply )
109 if ( PLSpeedTable[ClearSteamID(ply:SteamID())] ) then
110 stats_save( ply )
111 PLSpeedTable[ClearSteamID(ply:SteamID())] = nil
112 end
113end
114hook.Add( "PlayerDisconnected", "PST_PlayerDisconnected", PST_PlayerDisconnected )
115
116local consolespam = false
117
118function PST_CCSubmitSpeeds( ply, cmd, args )
119 if ply && ply:IsPlayer() && ply:Alive() then
120 if ( #args == 2 ) then
121 local sid = ClearSteamID( ply:SteamID() )
122 //if ( args[1] == PLSpeedTable[sid].key ) then
123 PLSpeedTable[sid].cts = tonumber( args[1] )
124 PLSpeedTable[sid].speed = tonumber( args[2] )
125 if PLSpeedTable[sid].cts > PLSpeedTable[sid].gts then
126 PLSpeedTable[sid].gts = PLSpeedTable[sid].cts
127 end
128 return
129 //end
130 end
131 end
132 if ply && ply:IsPlayer() && consolespam then
133 Msg( "Player "..ply:Name().." couldn't get updated ("..string.Implode("|",args)..")\n" )
134 end
135end
136concommand.Add( "CC_SubmitSpeeds" , PST_CCSubmitSpeeds )
137
138function enabledebug( ply, cmd, arg )
139 consolespam = !consolespam
140end
141concommand.Add( "con_debug", enabledebug )