Jump to content

Turf System?


Recommended Posts

Hello, what functions would I need to make a simple turf system? 

I know i'll need the

createColCuboid 

but how do i get gang names, players in gang, etc? as they don't have a function for it, as I only know

getTeamFromName

and

getPlayersinTeam

How can i get players inside a "gang" elementdata? 

do I do a loop like this? 

local players = getElementsWithinColShape( turfarea, "player" )
for i, v in ipairs(players) do
local gangname = getElementData(v, "gang") 
  if gangname ~= ?? then  -- idk how to get the gangname that's taking the colshape either. this just shows if gangname is different from turf owner
   setRadarAreaFlashing(turfarea, true)  -- capturing turf
    setTimer etc etc -- timer for capture etc
    ---

I have so much to know about making a simple turf system, If anyone could help about what functions to use, thankyou.

Edited by Bean666
Link to post

I'm currently designing an entire turf system for you in OOP, I will give it to you once its finished, free of charge. Just give me some time, thank you.

spacer.png

  • Like 1
Link to post
Posted (edited)
10 minutes ago, ShayF said:

I'm currently designing an entire turf system for you in OOP, I will give it to you once its finished, free of charge. Just give me some time, thank you.

spacer.png

that's actually pretty cool if you do, I would be glad to test it, and I will learn from it as well. thankyou!

Edited by Bean666
Link to post

@Bean666I've laid out a framework for you to expand on, this code has not been tested, but it should have everything you need to get started, i just kinda got lazy and didn't wanna do client side. This way you don't have to use element data, the data is always there for you to access, and it saves to a json file on the server side, so you'll never have trouble accessing, modifying that data and also players who play on your server wont be able to access that json db file..
I love using json for saving and loading large chunks of data into code.  If you plan to use this in an official project I suggest actually using MySQL/MariaDB which would be a totally different setup in the code but I'd be willing to switch that over for you if you wanted. Anyways, here you are. Just don't forget to add <oop>true</oop> to your meta file, because otherwise the oop formats in this file wont run.

local Turf = {}
Turf.__index = Turf

local claims = {}

local turfChatKey = 'G'

function Turf.getPlayerRank(serial)
	for i=1, #claims do
		for k=1, claims[i].members do
			if claims[i].members[k][1] == serial then
				return claims[i].members[k][3]
			end
		end
	end
	return false
end

function getPlayerTurf(serial)
	for i=1, #claims do
		for k=1, claims[i].members do
			if claims[i].members[k][1] == serial then
				return claims[i]
			end
		end
	end
	return false
end

function Turf.new(name)
	local self = setmetatable({}, Turf)
	self.name = name or 'default'
	
	self.bank = 0
	
	self.members = {}
	
	self.land = {}
	
	claims[#claims+1] = self
	return self
end

function Turf:addLand(x, y, radius)
	local position = Vector2(x, y) or nil
	if position then
		self.land[#self.land+1] = {}
		self.land[#self.land+1].position = position
		self.land[#self.land+1].radius = radius or 1
	end
end

function Turf:removeLand(x, y)
	if tonumber(x) and tonumber(y) then
		for i=1, #self.land do
			if self.land[i].x == x and self.land[i].y = y then
				self.land[i] = nil
			end
		end
	end
end

function Turf:addMember(player) -- player needs to be online
	if isElement(player) and getElementType(player) == 'player' then
		self.members[#self.members+1] = {getPlayerSerial(player), getPlayerName(player), 'member'}
	end
end

function Turf:removeMember(serial)
	for i=1, #self.members do
		if self.members[i][1] == serial then
			self.members[i] = nil
		end
	end
end

function Turf:playerAddMoney(player, amount) -- player needs to be online
	if isElement(player) and getElementType(player) == 'player' and tonumber(amount) then
		if getPlayerMoney(player) >= amount then
			setPlayerMoney(player, getPlayerMoney(player)-amount)
			self.bank = self.bank+amount
		end
	end
end

function Turf:playerRemoveMoney(player, amount) -- player needs to be online
	if isElement(player) and getElementType(player) == 'player' and tonumber(amount) then
		if self.bank >= amount then
			setPlayerMoney(player, getPlayerMoney(player)+amount)
			self.bank = self.bank-amount
		end
	end
end

addCommandHandler('turf', function(player, cmd, ...)
	local turf = getPlayerTurf(getPlayerSerial(player))
	if turf then
		for i=1, #turf.members do
			local outputUser = getPlayerFromSerial(turf.members[i][1])
			if outputUser then
				outputChatBox('['..turf.name..'] '..getPlayerName(player)..': '..table.concat(arg, ' '), outputUser, 255, 255, 255, true)
			end
		end
	end
end)

function Turf.save()
	local file = File('turfs.json')
	file:write(toJSON(claims))
	file:close()
end
addCommandHandler('turfsave', Turf.save)

function Turf.load()
	local file = fileOpen('turfs.json')
	if file then
		local buffer = file:read(file.size)
		claims = fromJSON(buffer)
	end
	file:close()
end

addEventHandler('onResourceStart', resourceRoot, function()
	Turf.load()
	for i, plr in pairs(getElementsByType('player')) do
		bindKey(plr, turfChatKey, 'down', 'chatbox', 'turf')
	end
end)

addEventHandler('onPlayerJoin', root, function()
	bindKey(source, turfChatKey, 'down', 'chatbox', 'turf')
end)
-- example
local myTurf = Turf.new('my turf name') -- name
myTurf:addLand(0, 1, 5) -- x, y, radius
myTurf:addMember(getRandomPlayer()) -- add player, player needs to be online
myTurf:removeMember('players serial') -- since i used serials you can do this with the player offline.

 

and when you add/modify any turfs just make sure you do /turfsave in console or in game to save the changes to the file. that way if you restart the resource or the server your last save is loaded. the command will not give an output, just save it and then check the file to see if theres any content, should work just fine.

Link to post
4 minutes ago, ShayF said:

@Bean666I've laid out a framework for you to expand on, this code has not been tested, but it should have everything you need to get started, i just kinda got lazy and didn't wanna do client side. This way you don't have to use element data, the data is always there for you to access, and it saves to a json file on the server side, so you'll never have trouble accessing, modifying that data and also players who play on your server wont be able to access that json db file..
I love using json for saving and loading large chunks of data into code.  If you plan to use this in an official project I suggest actually using MySQL/MariaDB which would be a totally different setup in the code but I'd be willing to switch that over for you if you wanted. Anyways, here you are. Just don't forget to add <oop>true</oop> to your meta file, because otherwise the oop formats in this file wont run.


local Turf = {}
Turf.__index = Turf

local claims = {}

local turfChatKey = 'G'

function Turf.getPlayerRank(serial)
	for i=1, #claims do
		for k=1, claims[i].members do
			if claims[i].members[k][1] == serial then
				return claims[i].members[k][3]
			end
		end
	end
	return false
end

function getPlayerTurf(serial)
	for i=1, #claims do
		for k=1, claims[i].members do
			if claims[i].members[k][1] == serial then
				return claims[i]
			end
		end
	end
	return false
end

function Turf.new(name)
	local self = setmetatable({}, Turf)
	self.name = name or 'default'
	
	self.bank = 0
	
	self.members = {}
	
	self.land = {}
	
	claims[#claims+1] = self
	return self
end

function Turf:addLand(x, y, radius)
	local position = Vector2(x, y) or nil
	if position then
		self.land[#self.land+1] = {}
		self.land[#self.land+1].position = position
		self.land[#self.land+1].radius = radius or 1
	end
end

function Turf:removeLand(x, y)
	if tonumber(x) and tonumber(y) then
		for i=1, #self.land do
			if self.land[i].x == x and self.land[i].y = y then
				self.land[i] = nil
			end
		end
	end
end

function Turf:addMember(player) -- player needs to be online
	if isElement(player) and getElementType(player) == 'player' then
		self.members[#self.members+1] = {getPlayerSerial(player), getPlayerName(player), 'member'}
	end
end

function Turf:removeMember(serial)
	for i=1, #self.members do
		if self.members[i][1] == serial then
			self.members[i] = nil
		end
	end
end

function Turf:playerAddMoney(player, amount) -- player needs to be online
	if isElement(player) and getElementType(player) == 'player' and tonumber(amount) then
		if getPlayerMoney(player) >= amount then
			setPlayerMoney(player, getPlayerMoney(player)-amount)
			self.bank = self.bank+amount
		end
	end
end

function Turf:playerRemoveMoney(player, amount) -- player needs to be online
	if isElement(player) and getElementType(player) == 'player' and tonumber(amount) then
		if self.bank >= amount then
			setPlayerMoney(player, getPlayerMoney(player)+amount)
			self.bank = self.bank-amount
		end
	end
end

addCommandHandler('turf', function(player, cmd, ...)
	local turf = getPlayerTurf(getPlayerSerial(player))
	if turf then
		for i=1, #turf.members do
			local outputUser = getPlayerFromSerial(turf.members[i][1])
			if outputUser then
				outputChatBox('['..turf.name..'] '..getPlayerName(player)..': '..table.concat(arg, ' '), outputUser, 255, 255, 255, true)
			end
		end
	end
end)

function Turf.save()
	local file = File('turfs.json')
	file:write(toJSON(claims))
	file:close()
end
addCommandHandler('turfsave', Turf.save)

function Turf.load()
	local file = fileOpen('turfs.json')
	if file then
		local buffer = file:read(file.size)
		claims = fromJSON(buffer)
	end
	file:close()
end

addEventHandler('onResourceStart', resourceRoot, function()
	Turf.load()
	for i, plr in pairs(getElementsByType('player')) do
		bindKey(plr, turfChatKey, 'down', 'chatbox', 'turf')
	end
end)

addEventHandler('onPlayerJoin', root, function()
	bindKey(source, turfChatKey, 'down', 'chatbox', 'turf')
end)

-- example
local myTurf = Turf.new('my turf name') -- name
myTurf:addLand(0, 1, 5) -- x, y, radius
myTurf:addMember(getRandomPlayer()) -- add player, player needs to be online
myTurf:removeMember('players serial') -- since i used serials you can do this with the player offline.

 

thank you i couldnt ask for more, but yes im using this for my project, whats the difference if i'll use mySQL tho?

Link to post
1 minute ago, Bean666 said:

thank you i couldnt ask for more, but yes im using this for my project, whats the difference if i'll use mySQL tho?

The only reason id personally use mysql is to get the data to a different server/website, so unless you're gonna do that then json should work just fine for ya.

Link to post
Posted (edited)
1 minute ago, ShayF said:

The only reason id personally use mysql is to get the data to a different server/website, so unless you're gonna do that then json should work just fine for ya.

then I should be fine with JSON i guess, my server's not that big and it's still ongoing so yeah i should be fine, thanks anyway <3.

i created a JSON file with the sample u gave, i dont need to put the json file in the meta.xml right? first time trying JSON.

Edited by Bean666
Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...