Skip to content

Instantly share code, notes, and snippets.

@Spaenny
Last active August 29, 2015 14:19
Show Gist options
  • Save Spaenny/3c2f8dcb969cfc78947f to your computer and use it in GitHub Desktop.
Save Spaenny/3c2f8dcb969cfc78947f to your computer and use it in GitHub Desktop.
An achievement system for Servers
if SERVER then
util.AddNetworkString( "Achievement" )
util.AddNetworkString( "OwnAchievement" )
hook.Add( "PlayerInitialSpawn", " Crating Achievement fep", function( ply )
if file.Exists( "achievementspe/"..ply:SteamID64()..".txt", "DATA" ) == true then
ply.achievements = util.JSONToTable( file.Read( "achievementspe/"..ply:SteamID64()..".txt" ) )
else
ply.achievements = ply.achievements or {}
ply.achievements.hello = 0
ply.achievements.prop = 0
ply.achievements.death = 0
ply.achievements.deathcount = 0
file.Write( "achievementspe/"..ply:SteamID64()..".txt", util.TableToJSON(ply.achievements))
end
if file.Exists( "achievementspe", "DATA" ) == false and file.IsDir("achievementspe", "DATA") == false then
file.CreateDir( "achievementspe", "DATA" )
end
end )
hook.Add( "PlayerSay", "Player Hello", function( ply, text, IsTeam )
local text = string.lower( text )
if ply.achievements.hello == 0 and string.sub( text, 1, 5 ) == "hello" then
net.Start( "Achievement" )
net.WriteUInt( 1, 2 )
net.WriteString( "Say Hello!" )
net.WriteEntity( ply )
net.Broadcast()
ply.achievements.hello = 1
file.Write( "achievementspe/"..ply:SteamID64()..".txt", util.TableToJSON(ply.achievements))
end
if string.match( text, "!resetmyachievements", 0) == "!resetmyachievements" then
ply.achievements = {}
file.Write( "achievementspe/"..ply:SteamID64()..".txt", util.TableToJSON(ply.achievements))
ply.achievements = {}
ply.achievements.hello = 0
ply.achievements.prop = 0
ply.achievements.death = 0
ply.achievements.deathcount = 0
file.Write( "achievementspe/"..ply:SteamID64()..".txt", util.TableToJSON(ply.achievements))
return ""
end
if string.match( text, "!myach", 0 ) == "!myach" then
net.Start( "OwnAchievement" )
net.WriteTable(ply.achievements)
net.Send( ply )
return ""
end
end )
hook.Add( "PlayerDeath", "Player Death Propkill", function( ply, ent, att )
if ply.achievements.prop == 0 and att:GetClass() == "prop_physics" and ply ~= att then
net.Start( "Achievement" )
net.WriteUInt( 1, 2 )
net.WriteString( "Welcome to Garrys Mod!" )
net.WriteEntity( ply )
net.Broadcast()
ply.achievements.prop = 1
file.Write( "achievementspe/"..ply:SteamID64()..".txt", util.TableToJSON(ply.achievements))
end
if ply.achievements.death == 0 and att:GetClass() == "worldspawn" then
net.Start( "Achievement" )
net.WriteUInt( 1, 2 )
net.WriteString( "Falldamage!" )
net.WriteEntity( ply )
net.Broadcast()
ply.achievements.death = 1
file.Write( "achievementspe/"..ply:SteamID64()..".txt", util.TableToJSON(ply.achievements))
end
ply.deathcounter = ply.deathcounter or 0
if ply.achievements.deathcount == 0 then
ply.deathcounter = ply.deathcounter + 1
if ply.deathcounter == 10 then
net.Start( "Achievement" )
net.WriteUInt( 1, 2 )
net.WriteString( "Die 10 times in a row!" )
net.WriteEntity( ply )
net.Broadcast()
ply.achievements.deathcount = 1
file.Write( "achievementspe/"..ply:SteamID64()..".txt", util.TableToJSON(ply.achievements))
end
end
end )
hook.Add( "PlayerDisconnected", "Player Disconnect Save", function( ply )
if file.Exists( "achievementspe", "DATA") == false and file.IsDir("achievementspe", "DATA") == false then
file.CreateDir( "achievementspe", "DATA" )
end
file.Write( "achievementspe/"..ply:SteamID64()..".txt", util.TableToJSON(ply.achievements))
end )
end
if CLIENT then
net.Receive( "Achievement", function()
--[[
Type
0 = no tag
1 = SERVER
2 = INFO
--]]
local t = net.ReadUInt( 2 )
local ach = net.ReadString()
local ply = net.ReadEntity()
local nick = ply:GetName()
if t == 0 then
chat.AddText( Color( 255, 255, 255 ), Color( 255, 255, 255 ), nick.." just received the achievement: ", Color( 20,200,100 ),'"'..ach..'"' )
elseif t == 1 then
chat.AddText( Color( 0, 120, 150 ), "[SERVER] ", Color( 255, 255, 255 ), nick.." just received the achievement: ", Color( 20,200,100 ),'"'..ach..'"' )
elseif t == 2 then
chat.AddText( Color( 255, 255, 255 ),"[INFO] ", Color( 255, 255, 255 ), nick.." just received the achievement: ", Color( 20,200,100 ),'"'..ach..'"' )
end
end )
net.Receive( "OwnAchievement", function()
local yourachievements = net.ReadTable()
local achievementnames = {}
achievementnames.hello = "Say Hello"
achievementnames.prop = "Welcome to Garry's Mod"
achievementnames.death = "Falldamage"
achievementnames.deathcount = "Die 10 times in a row"
local Frame = vgui.Create( "DFrame" )
Frame:SetPos( center )
Frame:SetSize( ScrW() * 0.208, ScrH() * 0.277 )
Frame:SetTitle( "Your Achievement Status" )
Frame:SetVisible( true )
Frame:ShowCloseButton( true )
Frame:MakePopup( true )
local DList = vgui.Create( "DListView", Frame )
DList:SetPos( 20, 30 )
DList:SetSize( ScrW() * 0.188, ScrH() * 0.237 )
DList:AddColumn( "Achievements" )
DList:AddColumn( "Scored" )
for k, v in pairs( yourachievements ) do
for n, rn in pairs( achievementnames ) do
if k == n then
k = rn
end
end
if v == 1 then
v = "achieved"
else
v = "not achieved"
end
DList:AddLine( k, v )
end
end )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment