I was making a groups system, and I've made a lot of the communication stuff, but it runs poorly, and I have no clue about making it better.
What happens: Players can create groups. Anyone in a group can invite/remove players from the group, the owner cannot be removed. If the owner leaves the group is removed.
Edited code:
cl_groups.lua
[code]No longer exists[/code]
sv_groups.lua
[code]util.AddNetworkString("MakeGroup")
util.AddNetworkString("LeaveGroup")
util.AddNetworkString("AddPlayer")
util.AddNetworkString("RemovePlayer")
net.Receive("RemovePlayer", function(len, client)
local ply = net.ReadEntity()
if gr ~= 0 then
local r = GroupSystem.GetRank(ply)
if r == "Member" then
GroupSystem.GroupTable[gr][ply] = nil
else
return
end
end
GroupSystem.SendInfo()
end)
GroupSystem.RemoveGroup = function(group)
GroupSystem.GroupTable[group] = nil
GroupSystem.SendInfo()
end
GroupSystem.SetGroup = function(ply, group)
local gr = GroupSystem.GetGroup(ply)
if gr ~= 0 then
local r = GroupSystem.GetRank(ply)
if r == "Member" then
GroupSystem.GroupTable[gr][ply] = nil
else
GroupSystem.RemoveGroup(gr)
end
end
if GroupSystem.GroupTable[group] then
GroupSystem.GroupTable[group][ply] = {}
GroupSystem.GroupTable[group][ply].Rank = "Member"
else
GroupSystem.GroupTable[group] = {}
GroupSystem.GroupTable[group][ply] = {}
GroupSystem.GroupTable[group][ply].Rank = "Owner"
end
GroupSystem.SendInfo()
end
GroupSystem.GetNextNumber = function()
local k = 0
while true do
k = k + 1
if not GroupSystem.GroupTable[k] then break end
end
return k
end
function GroupSystem.SendInfo(ply)
nettable.commit(GroupSystem.GroupTable)
end
hook.Add("PlayerDisconnected", "CleanGroups", function(ply)
local gr = GroupSystem.GetGroup(ply)
if gr == 0 then return end
local r = GroupSystem.GetRank(ply)
if r == "Member" then
GroupSystem.GroupTable[gr][ply] = nil
GroupSystem.SendInfo()
else
GroupSystem.RemoveGroup(gr)
end
end)
hook.Add("PlayerInitialSpawn", "GiveGroups", function(ply)
GroupSystem.SendInfo(ply)
end)
net.Receive("MakeGroup", function(len, client)
GroupSystem.SetGroup(client, GroupSystem.GetNextNumber())
end)
net.Receive("LeaveGroup", function(len, client)
--GroupSystem.SetGroup(client, GroupSystem.GetNextNumber())
local gr = GroupSystem.GetGroup(client)
if gr == 0 then return end
local r = GroupSystem.GetRank(client)
print(r)
if r == "Member" then
GroupSystem.GroupTable[gr][client] = nil
GroupSystem.SendInfo()
else
GroupSystem.RemoveGroup(gr)
end
end)[/code]
sh_groups.lua
[code]GroupSystem = {}
GroupSystem.GroupTable = nettable.get("GroupSystem")
GroupSystem.GetGroup = function(ply)
for k, v in pairs(GroupSystem.GroupTable) do
for l, b in pairs(v) do
if l == ply then return k end
end
end
return 0
end
GroupSystem.GetMembers = function(group)
return GroupSystem.GroupTable[group]
end
GroupSystem.GetRank = function(ply)
for k, v in pairs(GroupSystem.GroupTable) do
for l, b in pairs(v) do
if l:Name() == ply:Name() then return b.Rank end
end
end
return "N/A"
end
GroupSystem.GetBoss = function(ply)
local g = GroupSystem.GetGroup(ply)
for k, v in pairs(GroupSystem.GroupTable[g]) do
if v.Rank == "Owner" then
return k
end
end
return "N/A"
end
GroupSystem.ShareGroup = function(ply1, ply2)
local g1 = GroupSystem.GetGroup(ply1)
local g2 = GroupSystem.GetGroup(ply2)
if g1 == g2 then return true end
return false
end[/code]
Refactored a lot. Still not fully satisfied with my clientside drawing setup, but it will have to do.
It looks like you're re-sending the entire list of groups every time there is a change. This is bad if the number of groups becomes large. You only really want to send the parts that change.
[QUOTE=!cake;47412826]It looks like you're re-sending the entire list of groups every time there is a change. This is bad if the number of groups becomes large. You only really want to send the parts that change.[/QUOTE]
Yeah, that was part of my problem. I couldn't think of a good way to figure out what had changed, and send only the changes.
You might find my library useful. [url]https://github.com/wyozi/gmod-nettable[/url]
[QUOTE=Wyozi;47412900]You might find my library useful. [url]https://github.com/wyozi/gmod-nettable[/url][/QUOTE]
I'm looking at the examples, and it looks like nettable.commit(table) commits the table from the server, but I'm not entirely sure how the code at the top to set it up works.
[QUOTE=Ghost_Sailor;47413604]I'm looking at the examples, and it looks like nettable.commit(table) commits the table from the server, but I'm not entirely sure how the code at the top to set it up works.[/QUOTE]
You use nettable.get to create the table on both server and clientside (doesn't need to be in the same file as long as the id is same). Then you can edit the table freely on the serverside, and when you're done call nettable.commit(id) to send the updates to the table to all players (by default).
I know the examples and README is kind of messy. If you need more help, add me on steam [url]http://steamcommunity.com/id/wyozi/[/url]
Refactored the code, using the nettable library. It looks and runs a lot better. Any glaring errors/problems anyone sees? Edited into the first post.
Why are you defining the functions like that in sh_groups.lua?
[QUOTE=James xX;47415099]Why are you defining the functions like that in sh_groups.lua?[/QUOTE]
I've generally done things that have a large subset of functions like that, so there's less chance of overwriting a function elsewhere, and also because I've always done it like that.
Sorry, you need to Log In to post a reply to this thread.