Refactor, Beatify and EndRound hook fix

This commit is contained in:
David Vogel 2018-08-03 11:22:18 +02:00
parent 0877e8a03d
commit 777ffcacbd
13 changed files with 210 additions and 205 deletions

View File

@ -1,4 +1,4 @@
if (SERVER) then if SERVER then
include("d3stats/init.lua") include("d3stats/init.lua")
else else
include("d3stats/cl_init.lua") include("d3stats/cl_init.lua")

View File

@ -1,27 +1,27 @@
-- Delete HUD and redo if already existent. This will reset the displayed values until the next update from the server -- Delete HUD and redo if already existent. This will reset the displayed values until the next update from the server
if d3stats.D3StatsOverlay then if D3stats.D3statsOverlay then
d3stats.D3StatsOverlay:Remove() D3stats.D3statsOverlay:Remove()
d3stats.Overlay_Init() D3stats.Overlay_Init()
end end
-- HUD on the left top of the screen -- HUD on the left top of the screen
function d3stats.Overlay_Init() function D3stats.Overlay_Init()
d3stats.D3StatsOverlay = vgui.Create( "D3StatsOverlay" ) D3stats.D3statsOverlay = vgui.Create("D3statsOverlay")
d3stats.D3StatsOverlay:SetPos( d3stats.Overlay_X, d3stats.Overlay_Y ) D3stats.D3statsOverlay:SetPos(D3stats.Overlay_X, D3stats.Overlay_Y)
d3stats.D3StatsOverlay:SetSize( 350, 100 ) D3stats.D3statsOverlay:SetSize(350, 100)
end end
-- Call this function from cl_targetid.lua in the gamemode. Works for ZS, needs adjustments for other gamemodes. -- Call this function from cl_targetid.lua in the gamemode. Works for ZS, needs adjustments for other gamemodes.
local colTemp = Color(255, 255, 255) local colTemp = Color(255, 255, 255)
function d3stats.DrawTargetID( ent, fade, x, y ) function D3stats.DrawTargetID(ent, fade, x, y)
colTemp.a = fade * 255 colTemp.a = fade * 255
--util.ColorCopy(COLOR_FRIENDLY, colTemp) --util.ColorCopy(COLOR_FRIENDLY, colTemp)
local Level = ent:GetNWInt( "D3Stats_Level", 0 ) local Level = ent:GetNWInt("D3stats_Level", 0)
if Level > 0 and d3stats.Levels[Level] then if Level > 0 and D3stats.Levels[Level] then
draw.SimpleTextBlur("Level " .. tostring(Level) .. " \"" .. d3stats.Levels[Level].Name .. "\"", d3stats.Font_TargetID, x, y, colTemp, TEXT_ALIGN_CENTER) draw.SimpleTextBlur("Level " .. tostring(Level) .. " \"" .. D3stats.Levels[Level].Name .. "\"", D3stats.Font_TargetID, x, y, colTemp, TEXT_ALIGN_CENTER)
y = y + draw.GetFontHeight(d3stats.Font_TargetID) + 4 y = y + draw.GetFontHeight(D3stats.Font_TargetID) + 4
end end
return x, y return x, y

View File

@ -1,16 +1,16 @@
d3stats = d3stats or {} D3stats = D3stats or {}
-- Includes -- Includes
include( "sh_settings.lua" ) include("sh_settings.lua")
include( "sh_level.lua" ) include("sh_level.lua")
include( "sh_concommand.lua" ) include("sh_concommand.lua")
include( "cl_network.lua" ) include("cl_network.lua")
include( "cl_hud.lua" ) include("cl_hud.lua")
include( "vgui/overlay.lua" ) include("vgui/overlay.lua")
-- Initialisation -- Initialization
hook.Add( "Initialize", "D3Stats_Init", function () hook.Add("Initialize", "D3stats_Init", function ()
d3stats.Overlay_Init() D3stats.Overlay_Init()
end ) end)

View File

@ -1,9 +1,9 @@
net.Receive( "D3Stats_UpdateXP", function() net.Receive("D3stats_UpdateXP", function()
local XP = net.ReadUInt( 32 ) local XP = net.ReadUInt(32)
--print( "XP update: " .. tostring(XP)) --print("XP update: " .. tostring(XP))
if d3stats and d3stats.D3StatsOverlay then if D3stats and D3stats.D3statsOverlay then
d3stats.D3StatsOverlay:StatsUpdate( XP, d3stats.CalculateLevel( XP ) ) D3stats.D3statsOverlay:StatsUpdate(XP, D3stats.CalculateLevel(XP))
end end
end ) end)

View File

@ -5,50 +5,58 @@ Any "zombie survival" gamemode specific code goes in here
]] ]]
-- XP rewards for players getting points -- XP rewards for players getting points
hook.Add( "PlayerPointsAdded", "D3Stats_ZS_PlayerPointsAdded", function ( ply, points ) hook.Add("PlayerPointsAdded", "D3stats_ZS_PlayerPointsAdded", function (ply, points)
if points <= d3stats.PlayerPointsAdded_Limit then if points <= D3stats.PlayerPointsAdded_Limit then
ply:D3Stats_AddXP( points ) ply:D3stats_AddXP(points)
end end
end ) end)
-- XP rewards for zombies killing humans -- XP rewards for zombies killing humans
hook.Add( "PostZombieKilledHuman", "D3Stats_ZS_PostZombieKilledHuman", function ( ply, attacker, dmginfo, headshot, wassuicide ) hook.Add("PostZombieKilledHuman", "D3stats_ZS_PostZombieKilledHuman", function (ply, attacker, dmginfo, headshot, wassuicide)
local reward = d3stats.ZombieKilledHuman_Static + d3stats.ZombieKilledHuman_Fraction * ply:Frags() local reward = D3stats.ZombieKilledHuman_Static + D3stats.ZombieKilledHuman_Fraction * ply:Frags()
reward = math.Clamp( reward, d3stats.ZombieKilledHuman_Min, d3stats.ZombieKilledHuman_Max ) reward = math.Clamp(reward, D3stats.ZombieKilledHuman_Min, D3stats.ZombieKilledHuman_Max)
attacker:D3Stats_AddXP( reward ) attacker:D3stats_AddXP(reward)
end ) end)
-- Message on levelchange -- Message on levelchange
hook.Add( "D3Stats_LevelChanged", "D3Stats_ZS_LevelChanged", function ( ply, oldLevel, Level ) hook.Add("D3stats_LevelChanged", "D3stats_ZS_LevelChanged", function (ply, oldLevel, Level)
if Level > oldLevel then if Level > oldLevel then
ply:CenterNotify( Color( 0, 255, 255 ), string.format( d3stats.Message.Level_Ascended, Level, d3stats.Levels[Level].Name ) ) ply:CenterNotify(Color(0, 255, 255), string.format(D3stats.Message.Level_Ascended, Level, D3stats.Levels[Level].Name))
else else
ply:CenterNotify( Color( 0, 255, 255 ), string.format( d3stats.Message.Level_Changed, Level, d3stats.Levels[Level].Name ) ) ply:CenterNotify(Color(0, 255, 255), string.format(D3stats.Message.Level_Changed, Level, D3stats.Levels[Level].Name))
end end
end ) end)
-- Handle "Use_Hammer" permission. TODO: Only prevent that the person can nail things -- Handle "Use_Hammer" permission. TODO: Only prevent that the person can nail things
hook.Add( "PlayerSwitchWeapon", "D3Stats_ZS_EquipHammer", function( ply, oldWeapon, newWeapon ) hook.Add("PlayerSwitchWeapon", "D3stats_ZS_EquipHammer", function(ply, oldWeapon, newWeapon)
local Class = newWeapon:GetClass() local Class = newWeapon:GetClass()
if Class == "weapon_zs_hammer" or Class == "weapon_zs_electrohammer" then if Class == "weapon_zs_hammer" or Class == "weapon_zs_electrohammer" then
if not ply:D3Stats_HasPermission( "Use_Hammer" ) then if not ply:D3stats_HasPermission("Use_Hammer") then
ply:CenterNotify( Color( 255, 0, 0 ), string.format( d3stats.Message.Disallow_Hold_Hammer, d3stats.GetPermissionLevel( "Use_Hammer" ) ) ) ply:CenterNotify(Color(255, 0, 0), string.format(D3stats.Message.Disallow_Hold_Hammer, D3stats.GetPermissionLevel("Use_Hammer")))
return true return true
end end
end end
end ) end)
-- Handle EndRound events for statistics -- Handle EndRound events for statistics
hook.Add( "EndRound", "D3Stats_ZS_EndRound", function( team ) hook.Add("EndRound", "D3stats_ZS_EndRound", function(team)
local won = ( TEAM_SURVIVORS == team ) and true or false if D3stats.RoundEnded then return end
D3stats.RoundEnded = true
d3stats.Map_End( won ) local won = (TEAM_SURVIVORS == team) and true or false
end )
-- Display map statistic message when the player joined D3stats.Map_End(won)
hook.Add( "PlayerReady", "D3Stats_Map_PlayerReady", function ( ply ) end)
d3stats.Map_Message( false, ply )
end ) -- Reset stuff on a new game
hook.Add("DoRestartGame", "D3stats_ZS_DoRestartGame", function()
D3stats.RoundEnded = nil
end)
-- Display map statistics message when a player joins
hook.Add("PlayerReady", "D3stats_Map_PlayerReady", function (ply)
D3stats.Map_Message(false, ply)
end)

View File

@ -5,49 +5,49 @@ by David Vogel (Dadido3)
]] ]]
d3stats = d3stats or {} D3stats = D3stats or {}
AddCSLuaFile( "sh_settings.lua" ) AddCSLuaFile("sh_settings.lua")
AddCSLuaFile( "sh_level.lua" ) AddCSLuaFile("sh_level.lua")
AddCSLuaFile( "sh_concommand.lua" ) AddCSLuaFile("sh_concommand.lua")
AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile("cl_init.lua")
AddCSLuaFile( "cl_network.lua" ) AddCSLuaFile("cl_network.lua")
AddCSLuaFile( "cl_hud.lua" ) AddCSLuaFile("cl_hud.lua")
AddCSLuaFile( "vgui/overlay.lua" ) AddCSLuaFile("vgui/overlay.lua")
include( "sh_settings.lua" ) include("sh_settings.lua")
include( "sh_level.lua" ) include("sh_level.lua")
include( "sh_concommand.lua" ) include("sh_concommand.lua")
include( "gamemodes/sv_zombiesurvival.lua" ) include("gamemodes/sv_zombiesurvival.lua")
include( "sv_storage.lua" ) include("sv_storage.lua")
include( "sv_map.lua" ) include("sv_map.lua")
include( "sv_network.lua" ) include("sv_network.lua")
hook.Add( "PlayerInitialSpawn", "D3Stats_PlayerSpawn", function ( ply ) hook.Add("PlayerInitialSpawn", "D3stats_PlayerSpawn", function (ply)
-- Send its own XP -- Send its own XP
ply:D3Stats_Net_UpdateXP() ply:D3stats_Net_UpdateXP()
-- Store level as network and local player variable -- Store level as network and local player variable
ply.D3Stats_Level = d3stats.CalculateLevel( ply:D3Stats_GetXP() ) ply.D3stats_Level = D3stats.CalculateLevel(ply:D3stats_GetXP())
ply:SetNWInt( "D3Stats_Level", ply.D3Stats_Level ) ply:SetNWInt("D3stats_Level", ply.D3stats_Level)
end ) end)
-- Initialisation -- Initialisation
hook.Add( "Initialize", "D3Stats_Init", function () hook.Add("Initialize", "D3stats_Init", function ()
d3stats.Storage.Initialize() D3stats.Storage.Initialize()
resource.AddFile("resource/fonts/ghoulfriaoe.ttf") resource.AddFile("resource/fonts/ghoulfriaoe.ttf")
resource.AddFile("resource/fonts/hauntaoe.ttf") resource.AddFile("resource/fonts/hauntaoe.ttf")
resource.AddFile("resource/fonts/nightaoe.ttf") resource.AddFile("resource/fonts/nightaoe.ttf")
end ) end)
local meta = FindMetaTable( "Player" ) local meta = FindMetaTable("Player")
if not meta then return end if not meta then return end
function meta:D3Stats_GetXP() function meta:D3stats_GetXP()
local XP = tonumber( self:GetPData( "D3Stats_XP", "0" ) ) local XP = tonumber(self:GetPData("D3stats_XP", "0"))
if not XP then if not XP then
XP = 0 XP = 0
@ -56,43 +56,43 @@ function meta:D3Stats_GetXP()
return XP return XP
end end
function meta:D3Stats_SetXP( XP ) function meta:D3stats_SetXP(XP)
if not XP then if not XP then
XP = 0 XP = 0
end end
self:SetPData( "D3Stats_XP", tostring( XP ) ) self:SetPData("D3stats_XP", tostring(XP))
self:D3Stats_Net_UpdateXP() self:D3stats_Net_UpdateXP()
-- Update network variable and send message on level change -- Update network variable and send message on level change
local Level = d3stats.CalculateLevel( XP ) local Level = D3stats.CalculateLevel(XP)
if self.D3Stats_Level and self.D3Stats_Level ~= Level then if self.D3stats_Level and self.D3stats_Level ~= Level then
self:SetNWInt( "D3Stats_Level", Level ) self:SetNWInt("D3stats_Level", Level)
hook.Call( "D3Stats_LevelChanged" , nil, self, self.D3Stats_Level, Level ) hook.Call("D3stats_LevelChanged" , nil, self, self.D3stats_Level, Level)
end end
self.D3Stats_Level = Level self.D3stats_Level = Level
end end
function meta:D3Stats_AddXP( XP ) function meta:D3stats_AddXP(XP)
if not XP then if not XP then
XP = 0 XP = 0
end end
self:D3Stats_SetXP( self:D3Stats_GetXP() + XP ) self:D3stats_SetXP(self:D3stats_GetXP() + XP)
end end
function meta:D3Stats_GetLevel() function meta:D3stats_GetLevel()
return self.D3Stats_Level return self.D3stats_Level
end end
-- Check if the players level has the permission -- Check if the players level has the permission
function meta:D3Stats_HasPermission( Permission ) function meta:D3stats_HasPermission(Permission)
-- If there are too few players with this permission, allow it -- If there are too few players with this permission, allow it anyway
if d3stats.Permissions[Permission] and d3stats.Permissions[Permission].AllowIfLessThan and d3stats.Permissions[Permission].AllowIfLessThan > d3stats.CountPermissionPlayers( Permission, d3stats.Permissions[Permission].Team ) then if D3stats.Permissions[Permission] and D3stats.Permissions[Permission].AllowIfLessThan and D3stats.Permissions[Permission].AllowIfLessThan > D3stats.CountPermissionPlayers(Permission, D3stats.Permissions[Permission].Team) then
return true return true
end end
return d3stats.LevelCheckPermission( self:D3Stats_GetLevel(), Permission ) return D3stats.LevelCheckPermission(self:D3stats_GetLevel(), Permission)
end end

View File

@ -1,7 +1,7 @@
if SERVER then if SERVER then
concommand.Add( "d3stats_clearall", function( ply, cmd, args ) concommand.Add("D3stats_clearall", function(ply, cmd, args, argsString)
print( "What the hell are you doing?" ) print("What the hell are you doing? Not implemented yet!")
end ) end)
end end

View File

@ -1,8 +1,8 @@
-- Calculate the level from the given XP -- Calculate the level from the given XP
function d3stats.CalculateLevel( XP ) function D3stats.CalculateLevel(XP)
local Level = 1 local Level = 1
for key, value in pairs( d3stats.Levels ) do for key, value in pairs(D3stats.Levels) do
if value.XP_needed <= XP then if value.XP_needed <= XP then
Level = key Level = key
else else
@ -14,15 +14,15 @@ function d3stats.CalculateLevel( XP )
end end
-- Check if the level has the given permission -- Check if the level has the given permission
function d3stats.LevelCheckPermission( Level, Permission ) function D3stats.LevelCheckPermission(Level, Permission)
local Granted = false local Granted = false
-- If the permission is not on the list, allow it -- If the permission is not on the list, allow it
if not d3stats.Permissions[Permission] then if not D3stats.Permissions[Permission] then
return true return true
end end
for key, value in pairs( d3stats.Levels ) do for key, value in pairs(D3stats.Levels) do
if key <= Level then if key <= Level then
if value.Permissions and value.Permissions[Permission] then if value.Permissions and value.Permissions[Permission] then
Granted = value.Permissions[Permission] Granted = value.Permissions[Permission]
@ -36,14 +36,14 @@ function d3stats.LevelCheckPermission( Level, Permission )
end end
-- Returns what level is needed for the given permission -- Returns what level is needed for the given permission
function d3stats.GetPermissionLevel( Permission ) function D3stats.GetPermissionLevel(Permission)
local Level local Level
if not d3stats.Permissions[Permission] then if not D3stats.Permissions[Permission] then
return 1 return 1
end end
for key, value in pairs( d3stats.Levels ) do for key, value in pairs(D3stats.Levels) do
if value.Permissions and value.Permissions[Permission] then if value.Permissions and value.Permissions[Permission] then
return key return key
end end
@ -54,13 +54,13 @@ end
-- Count the amount of online players who have the permission -- Count the amount of online players who have the permission
if SERVER then if SERVER then
function d3stats.CountPermissionPlayers( Permission, Team ) function D3stats.CountPermissionPlayers(Permission, Team)
local Counter = 0 local Counter = 0
local players = player.GetAll() local players = player.GetAll()
for key, ply in pairs( players ) do for key, ply in pairs(players) do
if Team == nil or ply:Team() == Team then if Team == nil or ply:Team() == Team then
if d3stats.LevelCheckPermission( ply:D3Stats_GetLevel(), Permission ) == true then if D3stats.LevelCheckPermission(ply:D3stats_GetLevel(), Permission) == true then
Counter = Counter + 1 Counter = Counter + 1
end end
end end

View File

@ -7,14 +7,14 @@ Settings and level definitions are stored in here
-- Permissions -- Permissions
-- - Everything not in this list will be allowed by default -- - Everything not in this list will be allowed by default
-- - AllowIfLessThan: If the amount of players who have the permission is lower than this number, allow it anyway -- - AllowIfLessThan: If the amount of players who have the permission is lower than this number, allow it anyway
-- - Team: Reduces count to the specified team. In ZS: TEAM_SURVIVOR = 4, -- - Team: AllowIfLessThan only counts the specified team. In ZS: TEAM_SURVIVOR = 4,
d3stats.Permissions = { D3stats.Permissions = {
--["Buy_Hammer"] = { AllowIfLessThan = 4, Team = 4 }, --["Buy_Hammer"] = {AllowIfLessThan = 4, Team = 4},
--["Use_Hammer"] = { AllowIfLessThan = 4, Team = 4 }, --["Use_Hammer"] = {AllowIfLessThan = 4, Team = 4},
} }
-- Levels, please sort by XP -- Levels, please sort by XP
d3stats.Levels = { D3stats.Levels = {
{ XP_needed = 500, Name = "Kleiner" }, { XP_needed = 500, Name = "Kleiner" },
{ XP_needed = 1370, Name = "Survivor" }, { XP_needed = 1370, Name = "Survivor" },
{ XP_needed = 2612, Name = "Helper" }, { XP_needed = 2612, Name = "Helper" },
@ -50,30 +50,30 @@ d3stats.Levels = {
} }
-- Human reward settings -- Human reward settings
d3stats.PlayerPointsAdded_Limit = 200 -- Ignore all "PlayerPointsAdded" callbacks above this XP value D3stats.PlayerPointsAdded_Limit = 200 -- Ignore all "PlayerPointsAdded" callbacks above this XP value
-- Zombie reward is calculated as follows: Reward = math.clamp( Static + Fraction * Human_Points, Min, Max ) -- Zombie reward is calculated as follows: Reward = math.clamp(Static + Fraction * Human_Points, Min, Max)
d3stats.ZombieKilledHuman_Fraction = 1.0 -- Amount of XP a zombie gets of the killed humans points D3stats.ZombieKilledHuman_Fraction = 1.0 -- Amount of XP a zombie gets of the killed humans points
d3stats.ZombieKilledHuman_Static = 100 -- Amount of XP a zombie gets for killing a human D3stats.ZombieKilledHuman_Static = 100 -- Amount of XP a zombie gets for killing a human
d3stats.ZombieKilledHuman_Max = 1000 -- Upper XP reward clamp D3stats.ZombieKilledHuman_Max = 1000 -- Upper XP reward clamp
d3stats.ZombieKilledHuman_Min = 0 -- Lower XP reward clamp D3stats.ZombieKilledHuman_Min = 0 -- Lower XP reward clamp
-- Messages TODO: Multilanguage -- Messages TODO: Multilanguage
d3stats.Message = {} D3stats.Message = {}
d3stats.Message.Level_Ascended = "You ascended to level %i \"%s\"" D3stats.Message.Level_Ascended = "You ascended to level %i \"%s\""
d3stats.Message.Level_Changed = "Your level changed to level %i \"%s\"" D3stats.Message.Level_Changed = "Your level changed to level %i \"%s\""
d3stats.Message.Disallow_Hold_Hammer = "You can't use the hammer until you have reached level %i" D3stats.Message.Disallow_Hold_Hammer = "You can't use the hammer until you have reached level %i"
d3stats.Message.MapStats_Zero = "We are playing %s." -- Message to players who just joined D3stats.Message.MapStats_Zero = "We are playing %s." -- Message to players who just joined
d3stats.Message.MapStats = "We are playing %s. Humans won %i of %i times (%.1f%%)" -- Message to players who just joined (With statistics) D3stats.Message.MapStats = "We are playing %s. Humans won %i of %i times (%.1f%%)" -- Message to players who just joined (With statistics)
d3stats.Message.MapStats_End = "%s has been won %i of %i times (%.1f%%)" -- Message to all players at the end of the round (With statistics) D3stats.Message.MapStats_End = "%s has been won %i of %i times (%.1f%%)" -- Message to all players at the end of the round (With statistics)
if CLIENT then if CLIENT then
-- Overlay positions -- Overlay positions
d3stats.Overlay_X = 0 D3stats.Overlay_X = 0
d3stats.Overlay_Y = 80 * math.Clamp(ScrH() / 1080, 0.6, 1) -- This needs to be redone D3stats.Overlay_Y = 80 * math.Clamp(ScrH() / 1080, 0.6, 1) -- This needs to be redone
-- Fonts -- Fonts
surface.CreateFont( "D3Stats_OverlayFont_XP", { surface.CreateFont("D3stats_OverlayFont_XP", {
font = "Ghoulish Fright AOE", font = "Ghoulish Fright AOE",
extended = false, extended = false,
size = 22, size = 22,
@ -89,8 +89,8 @@ if CLIENT then
shadow = false, shadow = false,
additive = false, additive = false,
outline = false, outline = false,
} ) })
surface.CreateFont( "D3Stats_OverlayFont_Level", { surface.CreateFont("D3stats_OverlayFont_Level", {
font = "Haunt AOE", font = "Haunt AOE",
extended = false, extended = false,
size = 26, size = 26,
@ -106,9 +106,9 @@ if CLIENT then
shadow = false, shadow = false,
additive = false, additive = false,
outline = true, outline = true,
} ) })
d3stats.Font_Overlay_XP = "D3Stats_OverlayFont_XP" D3stats.Font_Overlay_XP = "D3stats_OverlayFont_XP"
d3stats.Font_Overlay_Level = "D3Stats_OverlayFont_Level" D3stats.Font_Overlay_Level = "D3stats_OverlayFont_Level"
d3stats.Font_TargetID = "ZSHUDFontTiny" -- Use ZS Font D3stats.Font_TargetID = "ZSHUDFontTiny" -- Use ZS Font
end end

View File

@ -1,35 +1,35 @@
function d3stats.Map_Message( roundend, ply ) function D3stats.Map_Message(roundend, ply)
local map = game.GetMap() local map = game.GetMap()
local count, wins, avg_players = d3stats.Storage.Map_Get( map ) local count, wins, avg_players = D3stats.Storage.Map_Get(map)
local Message local Message
if count > 0 then if count > 0 then
if roundend == true then if roundend == true then
Message = string.format( d3stats.Message.MapStats_End, map, wins, count, wins / count * 100 ) Message = string.format(D3stats.Message.MapStats_End, map, wins, count, wins / count * 100)
else else
Message = string.format( d3stats.Message.MapStats, map, wins, count, wins / count * 100 ) Message = string.format(D3stats.Message.MapStats, map, wins, count, wins / count * 100)
end end
else else
Message = string.format( d3stats.Message.MapStats_Zero, map ) Message = string.format(D3stats.Message.MapStats_Zero, map)
end end
if ply then if ply then
ply:ChatPrint( Message ) ply:ChatPrint(Message)
else else
PrintMessage( HUD_PRINTTALK, Message ) PrintMessage(HUD_PRINTTALK, Message)
end end
end end
function d3stats.Map_End( won ) function D3stats.Map_End(won)
local map = game.GetMap() local map = game.GetMap()
local players = #player.GetAll() local players = #player.GetAll()
if players > 0 then if players > 0 then
d3stats.Storage.Map_AddOutcome( map, won, players ) D3stats.Storage.Map_AddOutcome(map, won, players)
d3stats.Map_Message( true ) D3stats.Map_Message(true)
end end
end end
--d3stats.Map_End( false ) --D3stats.Map_End(false)
--d3stats.Map_Message( false, player.GetByID(1) ) --D3stats.Map_Message(false, player.GetByID(1))

View File

@ -1,11 +1,11 @@
util.AddNetworkString( "D3Stats_UpdateXP" ) util.AddNetworkString("D3stats_UpdateXP")
local meta = FindMetaTable( "Player" ) local meta = FindMetaTable("Player")
if not meta then return end if not meta then return end
-- Send the XP of this (self) player to itself -- Send the XP of this (self) player to itself
function meta:D3Stats_Net_UpdateXP() function meta:D3stats_Net_UpdateXP()
net.Start( "D3Stats_UpdateXP" ) net.Start("D3stats_UpdateXP")
net.WriteUInt( self:D3Stats_GetXP(), 32 ) net.WriteUInt(self:D3stats_GetXP(), 32)
net.Send( self ) net.Send(self)
end end

View File

@ -4,46 +4,46 @@ Storage stuff
]] ]]
d3stats.Storage = {} D3stats.Storage = {}
function d3stats.Storage.Initialize() function D3stats.Storage.Initialize()
-- Storage for map data -- Storage for map data
if not sql.TableExists( "d3stats_maps") then if not sql.TableExists("D3stats_maps") then
local result = sql.Query( "CREATE TABLE d3stats_maps ( name varchar(255) PRIMARY KEY, count INT, wins INT, avg_players REAL )" ) local result = sql.Query("CREATE TABLE D3stats_maps (name varchar(255) PRIMARY KEY, count INT, wins INT, avg_players REAL)")
end end
end end
function d3stats.Storage.Map_AddOutcome( name, won, players ) function D3stats.Storage.Map_AddOutcome(name, won, players)
local count = 1 local count = 1
local wins = 0 local wins = 0
local avg_players = players local avg_players = players
if won == true then wins = 1 end if won == true then wins = 1 end
local result = sql.QueryRow( "SELECT count, wins, avg_players FROM d3stats_maps WHERE name = '" .. name .. "'" ) local result = sql.QueryRow("SELECT count, wins, avg_players FROM D3stats_maps WHERE name = '" .. name .. "'")
if result then if result then
if result ~= false then if result ~= false then
count = count + tonumber( result.count ) count = count + tonumber(result.count)
wins = tonumber( result.wins ) + 1 * ( won and 1 or 0 ) wins = tonumber(result.wins) + 1 * (won and 1 or 0)
avg_players = ( players + tonumber( result.avg_players ) ) / 2 avg_players = (players + tonumber(result.avg_players)) / 2
else else
Print(sql.LastError()) Print(sql.LastError())
end end
end end
local result = sql.Query( "INSERT OR REPLACE INTO d3stats_maps ( name, count, wins, avg_players ) VALUES ( '" .. name .. "', " .. tostring(count) .. ", " .. tostring(wins) .. ", " .. tostring(avg_players) .. " );" ) local result = sql.Query("INSERT OR REPLACE INTO D3stats_maps (name, count, wins, avg_players) VALUES ('" .. name .. "', " .. tostring(count) .. ", " .. tostring(wins) .. ", " .. tostring(avg_players) .. ");")
end end
function d3stats.Storage.Map_Get( name ) function D3stats.Storage.Map_Get(name)
local count = 0 local count = 0
local wins = 0 local wins = 0
local avg_players = 0 local avg_players = 0
local result = sql.QueryRow( "SELECT count, wins, avg_players FROM d3stats_maps WHERE name = '" .. name .. "'" ) local result = sql.QueryRow("SELECT count, wins, avg_players FROM D3stats_maps WHERE name = '" .. name .. "'")
if result then if result then
if result ~= false then if result ~= false then
count = tonumber( result.count ) count = tonumber(result.count)
wins = tonumber( result.wins ) wins = tonumber(result.wins)
avg_players = tonumber( result.avg_players ) avg_players = tonumber(result.avg_players)
else else
Print(sql.LastError()) Print(sql.LastError())
end end

View File

@ -1,62 +1,59 @@
local PANEL = {} local PANEL = {}
function PANEL:Init() function PANEL:Init()
self.Progress = vgui.Create("DProgress", self)
self.Progress:SetPos(10, 10)
self.Progress:SetSize(200, 20)
self.Progress = vgui.Create( "DProgress", self ) self.Label_XP = vgui.Create("DLabel", self)
self.Progress:SetPos( 10, 10 ) self.Label_XP:SetTextColor(color_black)
self.Progress:SetSize( 200, 20 ) self.Label_XP:SetFont(D3stats.Font_Overlay_XP)
self.Label_XP:SetSize(200, 20)
self.Label_XP:SetPos(10, 10) -- Set the position of the label
self.Label_XP:SetContentAlignment(5)
self.Label_XP = vgui.Create( "DLabel", self ) self.Label_Level = vgui.Create("DLabel", self)
self.Label_XP:SetTextColor( color_black ) self.Label_Level:SetFont(D3stats.Font_Overlay_Level)
self.Label_XP:SetFont( d3stats.Font_Overlay_XP ) self.Label_Level:SetPos(10, 35) -- Set the position of the label
self.Label_XP:SetSize( 200, 20 ) self.Label_Level:SetSize(200, 20)
self.Label_XP:SetPos( 10, 10 ) -- Set the position of the label self.Label_Level:SetContentAlignment(5)
self.Label_XP:SetContentAlignment( 5 )
self.Label_Level = vgui.Create( "DLabel", self )
self.Label_Level:SetFont( d3stats.Font_Overlay_Level )
self.Label_Level:SetPos( 10, 35 ) -- Set the position of the label
self.Label_Level:SetSize( 200, 20 )
self.Label_Level:SetContentAlignment( 5 )
self:StatsUpdate( 0, 1 )
--self.Label:SetDark( 1 ) -- Set the colour of the text inside the label to a darker one
--timer.Create( "D3StatsOverlay_Timer", 0.1, 0, function() self.Progress:SetFraction( math.random() ) end )
self:StatsUpdate(0, 1)
--self.Label:SetDark(1) -- Set the colour of the text inside the label to a darker one
--timer.Create("D3statsOverlay_Timer", 0.1, 0, function() self.Progress:SetFraction(math.random()) end)
end end
function PANEL:Paint( aWide, aTall ) function PANEL:Paint(aWide, aTall)
-- Nothing for now -- Nothing for now
end end
function PANEL:StatsUpdate( XP, Level ) function PANEL:StatsUpdate(XP, Level)
local Text_XP local Text_XP
local Text_Level local Text_Level
local Fraction local Fraction
if d3stats.Levels[Level+1] then if D3stats.Levels[Level+1] then
Text_XP = "XP: " .. tostring( XP ) .. " / " .. d3stats.Levels[Level+1].XP_needed Text_XP = "XP: " .. tostring(XP) .. " / " .. D3stats.Levels[Level+1].XP_needed
Fraction = ( XP - d3stats.Levels[Level].XP_needed ) / ( d3stats.Levels[Level+1].XP_needed - d3stats.Levels[Level].XP_needed ) Fraction = (XP - D3stats.Levels[Level].XP_needed) / (D3stats.Levels[Level+1].XP_needed - D3stats.Levels[Level].XP_needed)
else else
Text_XP = "XP: " .. tostring( XP ) Text_XP = "XP: " .. tostring(XP)
Fraction = 1 Fraction = 1
end end
Text_Level = "Level: " .. tostring( Level ) .. " \"" .. d3stats.Levels[Level].Name .. "\"" Text_Level = "Level: " .. tostring(Level) .. " \"" .. D3stats.Levels[Level].Name .. "\""
self.Label_XP:SetText( Text_XP ) -- Set the text of the label self.Label_XP:SetText(Text_XP) -- Set the text of the label
--self.Label_XP:SizeToContents() -- Size the label to fit the text in it --self.Label_XP:SizeToContents() -- Size the label to fit the text in it
self.Label_Level:SetText( Text_Level ) -- Set the text of the label self.Label_Level:SetText(Text_Level) -- Set the text of the label
--self.Label_Level:SizeToContents() -- Size the label to fit the text in it --self.Label_Level:SizeToContents() -- Size the label to fit the text in it
self.Progress:SetFraction( Fraction ) self.Progress:SetFraction(Fraction)
end end
function PANEL:SetText( aText ) self.Text = tostring( aText ) end function PANEL:SetText(aText) self.Text = tostring(aText) end
function PANEL:GetText() return self.Text or "" end function PANEL:GetText() return self.Text or "" end
vgui.Register( "D3StatsOverlay", PANEL, "DPanel" ) vgui.Register("D3statsOverlay", PANEL, "DPanel")