forked from R3R3R3/PMUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
178 lines (150 loc) · 5.18 KB
/
Copy pathutils.lua
File metadata and controls
178 lines (150 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
-- Common utilities
--[[
prepare(db, query, ...)
A PreparedStatement-like query execution helper for luasql-postgres.
Automatically quotes and escapes strings passed as arguments. Parameters are
denoted with the question-mark character ('?').
Reports count mismatches in SQL query parameters and function args and provides
pretty good debugging context.
Acceptable arguments are of types: string, nil, number.
Example usage:
prepare(db, "INSERT INTO tab VALUES (?, ?)", "Fred", 22)
Doesn't support ? characters embedded in strings in query. Use:
prepare(db, "INSERT INTO tab2 VALUES (?)", "lol?")
--]]
function pmutils.prepare(db, query, ...)
local join_table = {}
local argc = select('#', ...)
local escaped
local val
local valtype
local fin = false
local i = 0
for split_query in string.gmatch(query, "[^%?]+") do
if fin then
error("prepare(): Too few function arguments (context: \""
.. table.concat(join_table) .. "\")", 3)
end
i = i + 1
table.insert(join_table, split_query)
if i > argc then
fin = true
else
val = select(i, ...)
valtype = type(val)
if valtype == "string" then
escaped = "'" .. db:escape(val) .. "'"
elseif valtype == "number" then
escaped = db:escape(tostring(val))
elseif valtype == "boolean" then
escaped = (val and "TRUE") or "FALSE"
elseif valtype == "nil" then
escaped = "NULL"
else
error("prepare(): Arg " .. tostring(i)
.. " is not of type: string, number, nil (context: \""
.. table.concat(join_table) .. "\")")
end
table.insert(join_table, escaped)
end
end
if i ~= (argc + 1) then
error("prepare(): Arg count doesn't equal SQL parameter count (context: \""
.. table.concat(join_table) .. "\")")
end
return db:execute(table.concat(join_table))
end
-- Used for retrieving the keys and values of a table,
-- Returns two lists
function pmutils.table_keyvals(tab)
local keyset = {}
local valset = {}
local n = 0
for k, v in pairs(tab) do
n = n + 1
keyset[n] = k
valset[n] = v
end
return keyset, valset
end
-- Search for an element in a table
function pmutils.search(element, tab)
for _, v in ipairs(tab) do
if v == element then
return true
end
end
return false
end
function pmutils.different_pos(pos1,pos2)
if pos1.x ~= pos2.x then return true
elseif pos1.z ~= pos2.z then return true
elseif pos1.y ~= pos2.y then return true
else return false
end
end
-- Stringifies a vector V, frequently used as a table key
--[[ USE dump(tab) TO STRINGIFY A TABLE ]]--
function ptos(x, y, z)
return tostring(x) .. ", " .. tostring(y) .. ", " .. tostring(z)
end
function vtos(v)
return tostring(v.x) .. ", " .. tostring(v.y) .. ", " .. tostring(v.z)
end
function round(x)
return x >= 0
and math.floor(x + 0.5)
or math.ceil(x - 0.5)
end
function pprintv(v)
local nv = vector.apply(v, round)
return "(" .. vtos(nv) .. ")"
end
local playerMoveCallbacks = {}
local playerMoveInterval = 0.5
local playerMoveHistoryLength = 20
function pmutils.register_player_move(callbackFunction, interval, historyLength)
table.insert( playerMoveCallbacks, callbackFunction)
if interval and interval < playerMoveInterval then playerMoveInterval = interval end
if historyLength and historyLength > playerMoveHistoryLength then playerMoveHistoryLength = historyLength end
end
local playerMoveHistory = {}
local timer = 0
-- This can probably be made more eficient
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer >= playerMoveInterval then
for _,player in ipairs(minetest.get_connected_players()) do
local playerHistory = playerMoveHistory[player:get_player_name()]
if not playerHistory then
playerHistory = {}
table.insert(playerHistory, 1, player:get_pos())
end
local lastPos = playerHistory[1]
if(lastPos and pmutils.different_pos(lastPos, player:get_pos())) then
table.insert(playerHistory, 1, player:get_pos())
if table.getn(playerHistory) > playerMoveHistoryLength then table.remove(playerHistory) end
for _,callback in pairs(playerMoveCallbacks) do callback(player, playerHistory) end
end
playerMoveHistory[player:get_player_name()] = playerHistory
end
timer = 0
end
end)
--------------------------------------------------------------------------------
--
-- Why does Minetest not give us a way to do this...
--
--------------------------------------------------------------------------------
function pmutils.grant_privilege(pname, privname)
local privs = core.get_player_privs(pname)
privs[privname] = true
core.run_priv_callbacks(pname, privname, "", "grant")
core.set_player_privs(pname, privs)
end
function pmutils.revoke_privilege(pname, privname)
local privs = core.get_player_privs(pname)
privs[privname] = nil
core.run_priv_callbacks(pname, privname, "", "revoke")
core.set_player_privs(pname, privs)
end