View difference between Paste ID: MJivXq3U and W7TD7UVB
SHOW: | | - or go back to the newest paste.
1
os.loadAPI("gcapi")
2
3
print("NOTICE: The computer is not storing data of any kind so the player using it is not breaking the rules of the server. Regarding the tracking of players the program doesn't track staff players.")
4
5-
gb = peripheral.find("openperipheral_glassesbridge")
5+
gb = peripheral.wrap("left")
6-
sensor = peripheral.find("openperipheral_sensor")
6+
sensor = peripheral.wrap("back")
7
--------------------------------------------
8
-->               Variables              <--
9
--------------------------------------------
10
local playersRange = {} --{[player]=range}
11
local lastPosition = {}
12
local config = {}
13
--------------------------------------------
14
-->               Tracking               <--
15
--------------------------------------------
16
function updatePositions()
17
	while true do
18
		displayClosePlayers()
19
		sleep(1)
20
	end
21
end
22
function getPosPlayers()
23
	local dataPlayers = gcapi.getPosPlayers()
24
	if sensor then
25
		local xo = config.sensor_position.x
26
		local yo = config.sensor_position.y
27
		local zo = config.sensor_position.z
28
		for i=1,#dataPlayers do
29
			local player_data = dataPlayers[i]
30
			local player_name = player_data["name"]
31
			local sensor_data = sensor.getPlayerData(player_name)
32
			if sensor_data then
33
				dataPlayers[i].x = math.floor(xo + sensor_data.position.x)
34
				dataPlayers[i].y = math.floor(yo + sensor_data.position.y)
35
				dataPlayers[i].z = math.floor(zo + sensor_data.position.z)
36
				
37
				dataPlayers[i].world = "world"
38
				dataPlayers[i].updated_at = "Updated now"
39
			end
40
		end
41
	end
42
	return dataPlayers
43
end
44
function getClosePlayers(user, range)
45
	local playerPositions = getPosPlayers()
46
	local closePlayers = {}
47
	
48
	local player_data_1 = nil
49
	for _,player_data in pairs(playerPositions) do
50
		if  player_data.name == user then
51
			player_data_1 = player_data
52
		end
53
	end
54
	if not player_data_1 then
55
		return nil, false, "Player not found"
56
	end
57
	
58
	local xo = player_data_1.x
59
	local yo = player_data_1.y
60
	local zo = player_data_1.z
61
	
62
	for _,player_data in pairs(playerPositions) do
63
		local name = player_data.name
64
		if  name ~= user then
65
			local distance_v = {["x"] = (player_data.x - xo),["y"] = (player_data.y - yo),["z"] = (player_data.z - zo)}
66
			
67
			local distance = (distance_v.y^2+distance_v.x^2 + distance_v.z^2)^0.5
68
			if distance <= range then
69
				table.insert(closePlayers, {["name"]= name, ["distance"] = math.floor(distance)})
70
			end
71
		end
72
	end
73
	table.sort(closePlayers, function(a,b) return a.distance<b.distance end)
74
	
75
	return closePlayers, true, nil
76
	
77
end
78
--------------------------------------------------------
79
-->                    Setup File                    <--
80
--------------------------------------------------------
81
function readConfigFile(cfg)
82
	local list = gcapi.getListFromFile("/config.cfg")
83
	for key,value in pairs(list) do
84
		cfg[key] = value
85
	end
86
	return cfg
87
end
88
function saveConfigFile(cfg)
89
	gcapi.saveListToFile("/config.cfg", cfg)
90
end
91
--------------------------------------------------------
92
-->            	      Manual Setup                   <--
93
--------------------------------------------------------
94
config = readConfigFile(config)
95
if sensor and config.sensor_position == nil then
96
	local sensor_position = {}
97
	print("Where is the sensor?")
98
	write("X: ")
99
	sensor_position.x = read():gsub(' ','')
100
	write("Y: ")
101
	sensor_position.y = read():gsub(' ','')
102
	write("Z: ")
103
	sensor_position.z = read():gsub(' ','')
104
	config.sensor_position = sensor_position
105
	saveConfigFile(config)
106
end
107
config = readConfigFile(config)
108
109
--------------------------------------------
110
-->                Glasses               <--
111
--------------------------------------------
112
function displayClosePlayers()
113
	for _,user in pairs(gb.getUsers()) do
114
		local range = playersRange[user]
115
		if not range then
116
			range = 600
117
		end
118
		local closePlayers, ok, err = getClosePlayers(user, range)
119
		if ok then
120
			local glasses = gb.getUserSurface(user)
121
			glasses.clear()
122
			for i=1, #closePlayers, 1 do
123
				local playerData = closePlayers[i]
124
				if playerData then
125
					local player = playerData["name"]
126
					local distance = playerData["distance"]
127
					glasses.addText(10,10+10*i, player .. " - " .. distance .. "[m]")
128
				end
129
			end
130
			
131
		end
132
	end
133
end
134
--------------------------------------------
135
-->                Commands              <--
136
--------------------------------------------
137
function listener()
138
	while true do
139
		local tEvent = {os.pullEvent()}
140
		if tEvent[1] == "chat_command" then
141
			co = tostring(tEvent[2])
142
			player = tostring(tEvent[3])
143
			print(player..": "..co)
144
			if tonumber(co) then
145
				playersRange[player] = tonumber(co)
146
			end
147
		end
148
	end
149
end
150
--------------------------------------------
151
-->              Main program            <--
152
--------------------------------------------
153
parallel.waitForAny(listener, updatePositions)