mirror of
https://github.com/lifestorm/wnsrc.git
synced 2025-12-16 21:33:46 +03:00
Upload
This commit is contained in:
57
addons/vyhub-gmod/lua/vyhub/server/sv_advert.lua
Normal file
57
addons/vyhub-gmod/lua/vyhub/server/sv_advert.lua
Normal file
@@ -0,0 +1,57 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
VyHub.Advert = VyHub.Advert or {}
|
||||
VyHub.adverts = VyHub.adverts or {}
|
||||
|
||||
local current_advert = 0
|
||||
|
||||
function VyHub.Advert:refresh()
|
||||
VyHub.API:get("/advert/", nil, { active = "true", serverbundle_id = VyHub.server.serverbundle.id }, function(code, result)
|
||||
VyHub.adverts = result
|
||||
end)
|
||||
end
|
||||
|
||||
function VyHub.Advert:show(advert)
|
||||
if advert then
|
||||
local lines = string.Explode('\n', advert.content)
|
||||
local color = VyHub.Util:hex2rgb(advert.color)
|
||||
|
||||
local prefix = VyHub.Config.advert_prefix
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
VyHub.Util:print_chat_all(line, prefix, color)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function VyHub.Advert:next()
|
||||
current_advert = current_advert + 1;
|
||||
|
||||
local advert = VyHub.adverts[current_advert];
|
||||
|
||||
if advert then
|
||||
VyHub.Advert:show(advert)
|
||||
else
|
||||
current_advert = 0
|
||||
end
|
||||
end
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_advert_vyhub_ready", function ()
|
||||
VyHub.Advert:refresh()
|
||||
|
||||
timer.Create("vyhub_advert_next", VyHub.Config.advert_interval, 0, function()
|
||||
VyHub.Advert:next()
|
||||
end)
|
||||
|
||||
timer.Create("vyhub_advert_refresh", 300, 0, function ()
|
||||
VyHub.Advert:refresh()
|
||||
end)
|
||||
end)
|
||||
170
addons/vyhub-gmod/lua/vyhub/server/sv_api.lua
Normal file
170
addons/vyhub-gmod/lua/vyhub/server/sv_api.lua
Normal file
@@ -0,0 +1,170 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
|
||||
VyHub.API = VyHub.API or {}
|
||||
|
||||
local content_type = "application/json; charset=utf-8"
|
||||
local json = VyHub.Lib.json
|
||||
|
||||
function VyHub.API:request(method, url, path_params, query, headers, request_body, type, success, failed, no_error_for)
|
||||
no_error_for = no_error_for or {}
|
||||
|
||||
if path_params != nil then
|
||||
url = f(url, unpack(path_params))
|
||||
end
|
||||
|
||||
if istable(request_body) then
|
||||
request_body = json.encode(request_body)
|
||||
end
|
||||
|
||||
success_func = function(code, body, headers)
|
||||
local result = body
|
||||
|
||||
if headers["Content-Type"] and headers["Content-Type"] == 'application/json' then
|
||||
result = json.decode(body)
|
||||
end
|
||||
|
||||
if code >= 200 and code < 300 then
|
||||
VyHub:msg(f("HTTP %s %s (%s): %s", method, url, json.encode(query), code), "debug")
|
||||
|
||||
if success != nil then
|
||||
-- VyHub:msg(f("Response: %s", body), "debug")
|
||||
|
||||
success(code, result, headers)
|
||||
end
|
||||
else
|
||||
if not table.HasValue(no_error_for, code) then
|
||||
local err_msg = f("HTTP %s %s: %s", method, url, code)
|
||||
|
||||
if query then
|
||||
err_msg = err_msg .. f("\nQuery: %s", json.encode(query))
|
||||
end
|
||||
if request_body then
|
||||
err_msg = err_msg .. f("\nBody: %s", request_body)
|
||||
end
|
||||
|
||||
VyHub:msg(err_msg, "error")
|
||||
|
||||
if code < 500 and string.find( body:lower(), "html>" ) == nil then
|
||||
VyHub:msg(f("Response: %s", body), "error")
|
||||
end
|
||||
end
|
||||
|
||||
if failed != nil then
|
||||
local err_text = json.encode(result)
|
||||
|
||||
if istable(result) and result.detail != nil and result.detail.msg != nil then
|
||||
err_text = f("%s (%s)", result.detail.msg, result.detail.code)
|
||||
end
|
||||
|
||||
failed(code, result, headers, err_text)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
failed_func = function(reason)
|
||||
local err_msg = f("HTTP %s %s: %s", method, url, code)
|
||||
|
||||
if query then
|
||||
err_msg = err_msg .. f("\nQuery: %s", json.encode(query))
|
||||
end
|
||||
if request_body then
|
||||
err_msg = err_msg .. f("\nBody: %s", request_body)
|
||||
end
|
||||
|
||||
VyHub:msg(err_msg, "error")
|
||||
|
||||
if failed != nil then
|
||||
failed(0, reason, {})
|
||||
end
|
||||
end
|
||||
|
||||
HTTP({
|
||||
method = method,
|
||||
url = url,
|
||||
parameters = query,
|
||||
headers = headers,
|
||||
body = request_body,
|
||||
type = type,
|
||||
success = success_func,
|
||||
failed = failed_func,
|
||||
})
|
||||
end
|
||||
|
||||
function VyHub.API:get(endpoint, path_params, query, success, failed, no_error_for)
|
||||
local url = f("%s%s", VyHub.API.url, endpoint)
|
||||
|
||||
VyHub.API:request("GET", url, path_params, query, self.headers, nil, content_type, success, failed, no_error_for)
|
||||
end
|
||||
|
||||
function VyHub.API:delete(endpoint, path_params, success, failed)
|
||||
local url = f("%s%s", VyHub.API.url, endpoint)
|
||||
|
||||
self:request("DELETE", url, path_params, nil, self.headers, nil, content_type, success, failed)
|
||||
end
|
||||
|
||||
function VyHub.API:post(endpoint, path_params, body, success, failed, query)
|
||||
local url = f("%s%s", VyHub.API.url, endpoint)
|
||||
|
||||
self:request("POST", url, path_params, query, self.headers, body, content_type, success, failed)
|
||||
end
|
||||
|
||||
function VyHub.API:patch(endpoint, path_params, body, success, failed)
|
||||
local url = f("%s%s", VyHub.API.url, endpoint)
|
||||
|
||||
self:request("PATCH", url, path_params, nil, self.headers, body, content_type, success, failed)
|
||||
end
|
||||
|
||||
function VyHub.API:put(endpoint, path_params, body, success, failed)
|
||||
local url = f("%s%s", VyHub.API.url, endpoint)
|
||||
|
||||
self:request("PUT", url, path_params, nil, self.headers, body, content_type, success, failed)
|
||||
end
|
||||
|
||||
hook.Add("vyhub_loading_finish", "vyhub_api_vyhub_loading_finish", function()
|
||||
if VyHub.Util:invalid_str({VyHub.Config.api_url, VyHub.Config.api_key, VyHub.Config.server_id}) then
|
||||
VyHub:msg("API URL, Server ID or API Key not set! Please follow the manual.", "error")
|
||||
|
||||
timer.Simple(60, function ()
|
||||
hook.Run("vyhub_loading_finish")
|
||||
end)
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
VyHub.API.url = VyHub.Config.api_url
|
||||
VyHub.API.headers = {
|
||||
Authorization = f("Bearer %s", VyHub.Config.api_key)
|
||||
}
|
||||
|
||||
if string.EndsWith(VyHub.API.url, "/") then
|
||||
VyHub.API.url = string.sub(VyHub.API.url, 1, -2)
|
||||
end
|
||||
|
||||
VyHub:msg(f("API URL is %s", VyHub.API.url))
|
||||
|
||||
VyHub.API:get("/openapi.json", nil, nil, function(code, result, headers)
|
||||
VyHub:msg(f("Connection to API %s version %s successful!", result.info.title, result.info.version), "success")
|
||||
|
||||
hook.Run("vyhub_api_ready")
|
||||
end, function()
|
||||
VyHub:msg("Connection to API failed! Trying to use cache.", "error")
|
||||
|
||||
hook.Run("vyhub_api_failed")
|
||||
end)
|
||||
end)
|
||||
|
||||
concommand.Add("vh_reinit", function (ply)
|
||||
if not VyHub.Util:is_server(ply) then return end
|
||||
|
||||
hook.Run("vyhub_loading_finish")
|
||||
end)
|
||||
1018
addons/vyhub-gmod/lua/vyhub/server/sv_ban.lua
Normal file
1018
addons/vyhub-gmod/lua/vyhub/server/sv_ban.lua
Normal file
File diff suppressed because it is too large
Load Diff
58
addons/vyhub-gmod/lua/vyhub/server/sv_cache.lua
Normal file
58
addons/vyhub-gmod/lua/vyhub/server/sv_cache.lua
Normal file
@@ -0,0 +1,58 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
local json = VyHub.Lib.json
|
||||
|
||||
VyHub.Cache = VyHub.Cache or {}
|
||||
|
||||
function VyHub.Cache:save(key, value)
|
||||
local data = {
|
||||
timestamp = os.time(),
|
||||
data = value
|
||||
}
|
||||
|
||||
local filename = f("vyhub/%s.json", key)
|
||||
local json = json.encode(data)
|
||||
|
||||
VyHub:msg("Write " .. filename .. ": " .. json, "debuga")
|
||||
|
||||
file.Write(filename, json)
|
||||
end
|
||||
|
||||
function VyHub.Cache:get(key, max_age)
|
||||
local path = f("vyhub/%s.json", key)
|
||||
|
||||
if not file.Exists(path, "data") then
|
||||
return nil
|
||||
end
|
||||
|
||||
local data_str = file.Read(path, "data")
|
||||
|
||||
if not string.Trim(data_str) then
|
||||
return nil
|
||||
end
|
||||
|
||||
local success, data = pcall(json.decode, data_str)
|
||||
|
||||
if not success then
|
||||
return nil
|
||||
end
|
||||
|
||||
if istable(data) and data.timestamp and data.data then
|
||||
if max_age != nil and os.time() - data.timestamp > max_age then
|
||||
return nil
|
||||
end
|
||||
|
||||
return data.data
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
75
addons/vyhub-gmod/lua/vyhub/server/sv_commands.lua
Normal file
75
addons/vyhub-gmod/lua/vyhub/server/sv_commands.lua
Normal file
@@ -0,0 +1,75 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_commands_vyhub_ready", function ()
|
||||
VyHub:get_frontend_url(function (url)
|
||||
-- !shop
|
||||
local function open_shop(ply, args)
|
||||
if IsValid(ply) then
|
||||
VyHub.Util:open_url(ply, f('%s/shop', url))
|
||||
end
|
||||
end
|
||||
|
||||
for _, cmd in ipairs(VyHub.Config.commands_shop) do
|
||||
VyHub.Util:register_chat_command(cmd, open_shop)
|
||||
end
|
||||
|
||||
-- !bans
|
||||
local function open_bans(ply, args)
|
||||
if IsValid(ply) then
|
||||
VyHub.Util:open_url(ply, f('%s/bans', url))
|
||||
end
|
||||
end
|
||||
|
||||
for _, cmd in ipairs(VyHub.Config.commands_bans) do
|
||||
VyHub.Util:register_chat_command(cmd, open_bans)
|
||||
end
|
||||
|
||||
-- !warnings
|
||||
local function open_warnings(ply, args)
|
||||
if IsValid(ply) then
|
||||
VyHub.Util:open_url(ply, f('%s/warnings', url))
|
||||
end
|
||||
end
|
||||
|
||||
for _, cmd in ipairs(VyHub.Config.commands_warnings) do
|
||||
VyHub.Util:register_chat_command(cmd, open_warnings)
|
||||
end
|
||||
|
||||
-- !news
|
||||
local function open_news(ply, args)
|
||||
if IsValid(ply) then
|
||||
VyHub.Util:open_url(ply, f('%s/', url))
|
||||
end
|
||||
end
|
||||
|
||||
for _, cmd in ipairs(VyHub.Config.commands_news) do
|
||||
VyHub.Util:register_chat_command(cmd, open_news)
|
||||
end
|
||||
|
||||
-- !user
|
||||
local function open_profile(ply, args)
|
||||
if IsValid(ply) and args[1] then
|
||||
other_ply = VyHub.Util:get_player_by_nick(args[1])
|
||||
|
||||
if IsValid(other_ply) then
|
||||
VyHub.Util:open_url(ply, f('%s/profile/steam/%s', url, other_ply:SteamID64()))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, cmd in ipairs(VyHub.Config.commands_profile) do
|
||||
VyHub.Util:register_chat_command(cmd, open_profile)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
88
addons/vyhub-gmod/lua/vyhub/server/sv_config.lua
Normal file
88
addons/vyhub-gmod/lua/vyhub/server/sv_config.lua
Normal file
@@ -0,0 +1,88 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
|
||||
function VyHub.Config:load_cache_config()
|
||||
local ccfg = VyHub.Cache:get("config")
|
||||
|
||||
if ccfg != nil and #table.GetKeys(ccfg) > 0 then
|
||||
VyHub.Config = table.Merge(VyHub.Config, ccfg)
|
||||
VyHub:msg(f("Loaded cache config values: %s", table.concat(table.GetKeys(ccfg), ', ')))
|
||||
end
|
||||
end
|
||||
|
||||
concommand.Add("vh_setup", function (ply, _, args)
|
||||
if not VyHub.Util:is_server(ply) then return end
|
||||
|
||||
if not args[1] or not args[2] or not args[3] then return end
|
||||
|
||||
local ccfg = VyHub.Cache:get("config")
|
||||
|
||||
if not istable(ccfg) then
|
||||
ccfg = {}
|
||||
end
|
||||
|
||||
ccfg["api_key"] = args[1]
|
||||
ccfg["api_url"] = args[2]
|
||||
ccfg["server_id"] = args[3]
|
||||
|
||||
VyHub.Cache:save("config", ccfg)
|
||||
|
||||
for key, value in pairs(ccfg) do
|
||||
VyHub.Config[key] = value
|
||||
end
|
||||
|
||||
VyHub:msg(f("Successfully set initial config, please wait up to one minute for VyHub to initialize (%s, %s, %s)", args[1], args[2], args[3]))
|
||||
end)
|
||||
|
||||
concommand.Add("vh_config", function (ply, _, args)
|
||||
if not VyHub.Util:is_server(ply) then return end
|
||||
|
||||
local ccfg = VyHub.Cache:get("config")
|
||||
|
||||
if not args[1] or not args[2] then
|
||||
if istable(ccfg) then
|
||||
VyHub:msg("Additional config options:")
|
||||
PrintTable(ccfg)
|
||||
else
|
||||
VyHub:msg("No additional config options set.")
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
local key = args[1]
|
||||
local value = args[2]
|
||||
|
||||
if not istable(ccfg) then
|
||||
ccfg = {}
|
||||
end
|
||||
|
||||
if value == "false" then
|
||||
value = false
|
||||
elseif value == "true" then
|
||||
value = true
|
||||
end
|
||||
|
||||
ccfg[key] = value
|
||||
VyHub.Cache:save("config", ccfg)
|
||||
|
||||
VyHub.Config[key] = value
|
||||
|
||||
VyHub:msg(f("Successfully set config value %s.", key))
|
||||
end)
|
||||
|
||||
concommand.Add("vh_config_reset", function (ply)
|
||||
if not VyHub.Util:is_server(ply) then return end
|
||||
|
||||
VyHub.Cache:save("config", {})
|
||||
|
||||
VyHub:msg(f("Successfully cleared additional config.", key))
|
||||
end)
|
||||
95
addons/vyhub-gmod/lua/vyhub/server/sv_dashboard.lua
Normal file
95
addons/vyhub-gmod/lua/vyhub/server/sv_dashboard.lua
Normal file
@@ -0,0 +1,95 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
local json = VyHub.Lib.json
|
||||
|
||||
VyHub.Dashboard = VyHub.Dashboard or {}
|
||||
VyHub.Dashboard.last_update = VyHub.Dashboard.last_update or {}
|
||||
VyHub.Dashboard.data = VyHub.Dashboard.data or {}
|
||||
|
||||
util.AddNetworkString("vyhub_dashboard")
|
||||
util.AddNetworkString("vyhub_dashboard_reload")
|
||||
|
||||
function VyHub.Dashboard:reset()
|
||||
VyHub.Dashboard.data = {}
|
||||
VyHub.Dashboard.last_update = {}
|
||||
net.Start("vyhub_dashboard_reload")
|
||||
net.Broadcast()
|
||||
end
|
||||
|
||||
function VyHub.Dashboard:fetch_data(user_id, callback)
|
||||
VyHub.API:get("/server/%s/user-activity?morph_user_id=%s", {VyHub.server.id, user_id}, nil, function(code, result)
|
||||
callback(result)
|
||||
end)
|
||||
end
|
||||
|
||||
function VyHub.Dashboard:get_data(steamid64, callback)
|
||||
if VyHub.Dashboard.data[steamid64] == nil or VyHub.Dashboard.last_update[steamid64] == nil or os.time() - VyHub.Dashboard.last_update[steamid64] > 30 then
|
||||
VyHub.Player:get(steamid64, function (user)
|
||||
if user then
|
||||
VyHub.Dashboard:fetch_data(user.id, function (data)
|
||||
VyHub.Dashboard.data[steamid64] = data
|
||||
VyHub.Dashboard.last_update[steamid64] = os.time()
|
||||
|
||||
callback(VyHub.Dashboard.data[steamid64])
|
||||
end)
|
||||
end
|
||||
end)
|
||||
else
|
||||
callback(VyHub.Dashboard.data[steamid64])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function VyHub.Dashboard:get_permissions(ply)
|
||||
return {
|
||||
warning_show = VyHub.Player:check_property(ply, 'warning_show'),
|
||||
warning_edit = VyHub.Player:check_property(ply, 'warning_edit'),
|
||||
warning_delete = VyHub.Player:check_property(ply, 'warning_delete'),
|
||||
ban_show = VyHub.Player:check_property(ply, 'ban_show'),
|
||||
ban_edit = VyHub.Player:check_property(ply, 'ban_edit'),
|
||||
}
|
||||
end
|
||||
|
||||
net.Receive("vyhub_dashboard", function(_, ply)
|
||||
if not IsValid(ply) then return end
|
||||
|
||||
VyHub.Dashboard:get_data(ply:SteamID64(), function (users)
|
||||
local users_json = json.encode(users)
|
||||
local users_json_compressed = util.Compress(users_json)
|
||||
local users_json_compressed_len = #users_json_compressed
|
||||
local perms = VyHub.Dashboard:get_permissions(ply)
|
||||
local perms_json = json.encode(perms)
|
||||
|
||||
net.Start("vyhub_dashboard")
|
||||
net.WriteUInt(users_json_compressed_len, 16)
|
||||
net.WriteData(users_json_compressed, users_json_compressed_len)
|
||||
net.WriteString(perms_json)
|
||||
net.Send(ply)
|
||||
end)
|
||||
end)
|
||||
|
||||
local function open_dashboard(ply, args)
|
||||
if ply and IsValid(ply) then
|
||||
ply:ConCommand("vh_dashboard")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_dashboard_vyhub_ready", function ()
|
||||
for _, cmd in ipairs(VyHub.Config.commands_dashboard) do
|
||||
VyHub.Util:register_chat_command(cmd, open_dashboard)
|
||||
end
|
||||
end)
|
||||
|
||||
hook.Add("vyhub_dashboard_data_changed", "vyhub_dahboard_vyhub_dashboard_data_changed", function ()
|
||||
VyHub.Dashboard:reset()
|
||||
end)
|
||||
375
addons/vyhub-gmod/lua/vyhub/server/sv_group.lua
Normal file
375
addons/vyhub-gmod/lua/vyhub/server/sv_group.lua
Normal file
@@ -0,0 +1,375 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
local json = VyHub.Lib.json
|
||||
|
||||
VyHub.Group = VyHub.Group or {}
|
||||
|
||||
VyHub.groups = VyHub.groups or nil
|
||||
VyHub.groups_mapped = VyHub.groups_mapped or nil
|
||||
VyHub.Group.group_changes = VyHub.Group.group_changes or {} -- dict(steamid64, groupname) of the last in-game group change (VyHub -> GMOD). Used to prevent loop.
|
||||
|
||||
util.AddNetworkString("vyhub_group_data")
|
||||
|
||||
local meta_ply = FindMetaTable("Player")
|
||||
|
||||
function VyHub.Group:refresh()
|
||||
VyHub.API:get("/group/", nil, nil, function(code, result)
|
||||
if result != VyHub.groups then
|
||||
VyHub.groups = result
|
||||
|
||||
VyHub:msg(f("Found groups: %s", json.encode(result)), "debug")
|
||||
|
||||
VyHub.groups_mapped = {}
|
||||
|
||||
for _, group in ipairs(VyHub.groups) do
|
||||
for _, mapping in ipairs(group.mappings) do
|
||||
if mapping.serverbundle_id == nil or mapping.serverbundle_id == VyHub.server.serverbundle.id then
|
||||
VyHub.groups_mapped[mapping.name] = group
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
VyHub.Group:send_groups()
|
||||
end
|
||||
end, function (code, reason)
|
||||
VyHub:msg("Could not refresh groups.", "error")
|
||||
end)
|
||||
end
|
||||
|
||||
function VyHub.Group:send_groups(ply)
|
||||
if not VyHub.groups_mapped then return end
|
||||
|
||||
local groups_to_send = VyHub.groups_mapped
|
||||
|
||||
net.Start("vyhub_group_data")
|
||||
net.WriteUInt(table.Count(groups_to_send), 8)
|
||||
|
||||
for name_game, group in pairs(groups_to_send) do
|
||||
net.WriteString(name_game)
|
||||
net.WriteString(group.name)
|
||||
net.WriteString(group.color)
|
||||
end
|
||||
|
||||
if ply != nil and IsValid(ply) then
|
||||
net.Send(ply)
|
||||
else
|
||||
net.Broadcast()
|
||||
end
|
||||
end
|
||||
|
||||
function VyHub.Group:set(steamid, groupname, seconds, processor_id, callback)
|
||||
if seconds != nil and seconds == 0 then
|
||||
seconds = nil
|
||||
end
|
||||
|
||||
if VyHub.groups_mapped == nil then
|
||||
VyHub:msg("Groups not initialized yet. Please try again later.", "error")
|
||||
if callback then
|
||||
callback(!VyHub.Config.strict_group_sync)
|
||||
end
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
local group = VyHub.groups_mapped[groupname]
|
||||
|
||||
if group == nil then
|
||||
VyHub:msg(f("Could not find VyHub group with name %s", groupname), "debug")
|
||||
|
||||
if callback then
|
||||
callback(!VyHub.Config.strict_group_sync)
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
VyHub.Player:get(steamid, function (user)
|
||||
if user == nil then
|
||||
if callback then
|
||||
callback(false)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
local end_date = nil
|
||||
|
||||
if seconds != nil then
|
||||
end_date = VyHub.Util:format_datetime(os.time() + seconds)
|
||||
end
|
||||
|
||||
local url = '/user/%s/membership'
|
||||
|
||||
if processor_id != nil then
|
||||
url = url .. '?morph_user_id=' .. processor_id
|
||||
end
|
||||
|
||||
local ply = player.GetBySteamID64(steamid)
|
||||
|
||||
VyHub.API:post(url, {user.id}, {
|
||||
begin = VyHub.Util.format_datetime(),
|
||||
["end"] = end_date,
|
||||
group_id = group.id,
|
||||
serverbundle_id = VyHub.server.serverbundle.id
|
||||
}, function (code, result)
|
||||
VyHub:msg(f("Added membership in group %s for user %s.", groupname, steamid), "success")
|
||||
|
||||
if IsValid(ply) then
|
||||
timer.Simple(5, function()
|
||||
VyHub.Player:refresh(ply)
|
||||
end)
|
||||
end
|
||||
|
||||
if callback then
|
||||
callback(true)
|
||||
end
|
||||
end, function (code, reason)
|
||||
VyHub:msg(f("Could not add membership in group %s for user %s.", groupname, steamid), "error")
|
||||
if callback then
|
||||
callback(false)
|
||||
end
|
||||
|
||||
if IsValid(ply) then
|
||||
timer.Simple(2, function()
|
||||
VyHub.Player:refresh(ply)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
function VyHub.Group:remove(steamid, processor_id, callback)
|
||||
VyHub.Player:get(steamid, function (user)
|
||||
if user == nil then
|
||||
if callback then
|
||||
callback(false)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
local url = f('/user/%%s/membership?serverbundle_id=%s', VyHub.server.serverbundle.id)
|
||||
|
||||
if processor_id != nil then
|
||||
url = url .. '&morph_user_id=' .. processor_id
|
||||
end
|
||||
|
||||
VyHub.API:delete(url, {user.id}, function (code, result)
|
||||
VyHub:msg(f("Removed %s from all groups.", steamid), "success")
|
||||
|
||||
local ply = player.GetBySteamID64(steamid)
|
||||
|
||||
if IsValid(ply) then
|
||||
VyHub.Player:refresh(ply)
|
||||
end
|
||||
|
||||
if callback then
|
||||
callback(true)
|
||||
end
|
||||
end, function (code, reason)
|
||||
VyHub:msg(f("Could not remove %s from all groups.", steamid), "error")
|
||||
|
||||
if callback then
|
||||
callback(false)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
function VyHub.Group:override_admin_mods()
|
||||
if VyHub.Config.group_disable_sync then return end
|
||||
|
||||
local _setusergroup = meta_ply.SetUserGroup
|
||||
|
||||
if not ULib and not serverguard and not sam and not (xAdmin and xAdmin.Admin.RegisterBan) and not sAdmin then
|
||||
hook.Remove("PlayerInitialSpawn", "PlayerAuthSpawn")
|
||||
|
||||
meta_ply.SetUserGroup = function(ply, name, ignore_vh)
|
||||
if ply:GetUserGroup() == name then
|
||||
VyHub:msg(ply:SteamID64() .. " already in group " .. name .. ". Ignoring change...")
|
||||
return
|
||||
end
|
||||
|
||||
local steamid = ply:SteamID64()
|
||||
|
||||
if not ignore_vh and VyHub.Group.group_changes[steamid] != name then
|
||||
if VyHub.Group:set(steamid, name) or VyHub.Config.disable_group_check then
|
||||
_setusergroup(ply, name)
|
||||
end
|
||||
else
|
||||
_setusergroup(ply, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if xAdmin and xAdmin.Admin.RegisterBan then
|
||||
local xadmin_setgroup = xAdmin.SetGroup
|
||||
|
||||
xAdmin.SetGroup = function(ply, group, ignore_vh)
|
||||
local steamid32 = isstring(ply) and ply or ply:SteamID()
|
||||
local steamid64 = util.SteamIDTo64(steamid32)
|
||||
|
||||
if not ignore_vh then
|
||||
VyHub.Group:set(steamid64, group, nil, nil, function(success)
|
||||
if success then
|
||||
xadmin_setgroup( ply, group )
|
||||
end
|
||||
end)
|
||||
else
|
||||
xadmin_setgroup( ply, group )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if ULib then
|
||||
local ulx_adduser = ULib.ucl.addUser
|
||||
local ulx_removeuser = ULib.ucl.removeUser
|
||||
|
||||
ULib.ucl.addUser = function(steamid32, allow, deny, groupname, ignore_vh)
|
||||
if not ignore_vh then
|
||||
local steamid64 = util.SteamIDTo64(steamid32)
|
||||
VyHub.Group:set(steamid64, groupname, nil, nil, function(success)
|
||||
if success then
|
||||
ulx_adduser( steamid32, allow, deny, groupname )
|
||||
end
|
||||
end)
|
||||
else
|
||||
ulx_adduser( steamid32, allow, deny, groupname )
|
||||
end
|
||||
end
|
||||
|
||||
ULib.ucl.removeUser = function(id)
|
||||
local steamid64 = nil
|
||||
|
||||
if string.find(id, ":") then
|
||||
steamid64 = util.SteamIDTo64(id)
|
||||
else
|
||||
local ply = player.GetByUniqueID(id)
|
||||
|
||||
if IsValid(ply) then
|
||||
steamid64 = ply:SteamID64()
|
||||
end
|
||||
end
|
||||
|
||||
if steamid64 then
|
||||
VyHub.Group:remove(steamid64, nil, function (success)
|
||||
if success then
|
||||
ulx_removeuser( id )
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if serverguard then
|
||||
local servergaurd_setrank = serverguard.player["SetRank"]
|
||||
|
||||
function serverguard.player:SetRank(target, rank, length, ignore_vh)
|
||||
if not ignore_vh then
|
||||
if target then
|
||||
if type(target) == "Player" and IsValid(target) then
|
||||
VyHub.Group:set(target:SteamID64(), rank, nil, nil, function(success)
|
||||
if success then
|
||||
servergaurd_setrank(self, target, rank, length)
|
||||
end
|
||||
end)
|
||||
elseif type(target) == "string" and string.match(target, "STEAM_%d:%d:%d+") then
|
||||
local steamid = util.SteamIDTo64(target)
|
||||
|
||||
VyHub.Group:set(steamid, rank, nil, nil, function(success)
|
||||
if success then
|
||||
servergaurd_setrank(self, target, rank, length)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
else
|
||||
servergaurd_setrank(self, target, rank, length)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if sam then
|
||||
local sam_setrank = sam.player.set_rank
|
||||
|
||||
function sam.player.set_rank(ply, rank, length, ignore_vh)
|
||||
if not ignore_vh then
|
||||
if not sam.isnumber(length) or length < 0 then
|
||||
length = nil
|
||||
end
|
||||
|
||||
local seconds = nil
|
||||
|
||||
if length != nil then
|
||||
seconds = math.Round(length * 60, 0)
|
||||
end
|
||||
|
||||
VyHub.Group:set(ply:SteamID64(), rank, seconds, nil, function(success)
|
||||
if success then
|
||||
sam_setrank(ply, rank, length)
|
||||
end
|
||||
end)
|
||||
else
|
||||
sam_setrank(ply, rank, length)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if sAdmin then
|
||||
local sadmin_setrank = sAdmin.setRank
|
||||
|
||||
sAdmin.setRank = function(ply, rank, expire, noupdate, ignore_vh)
|
||||
rank = rank or "user"
|
||||
|
||||
if not ignore_vh and not noupdate then
|
||||
local seconds = nil
|
||||
|
||||
if isnumber(expire) and expire > 0 then
|
||||
seconds = math.max(expire, 0)
|
||||
end
|
||||
|
||||
VyHub.Group:set(ply:SteamID64(), rank, seconds, nil, function(success)
|
||||
if success then
|
||||
sadmin_setrank(ply, rank, nil, noupdate)
|
||||
end
|
||||
end)
|
||||
else
|
||||
sadmin_setrank(ply, rank, expire, noupdate)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_group_vyhub_ready", function ()
|
||||
VyHub.Group:refresh()
|
||||
|
||||
timer.Create("vyhub_group_refresh", VyHub.Config.group_refresh_time, 0, function ()
|
||||
VyHub.Group:refresh()
|
||||
end)
|
||||
|
||||
hook.Add("vyhub_ply_connected", "vyhub_group_vyhub_ply_connected", function(ply)
|
||||
VyHub.Group:send_groups(ply)
|
||||
end)
|
||||
|
||||
concommand.Add("vh_setgroup", function(ply, _, args)
|
||||
if VyHub.Util:is_server(ply) then
|
||||
local steamid = args[1]
|
||||
local group = args[2]
|
||||
local bundle = args[3]
|
||||
|
||||
if steamid and group then
|
||||
VyHub.Group:set(steamid, group)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
VyHub.Group:override_admin_mods()
|
||||
end)
|
||||
92
addons/vyhub-gmod/lua/vyhub/server/sv_main.lua
Normal file
92
addons/vyhub-gmod/lua/vyhub/server/sv_main.lua
Normal file
@@ -0,0 +1,92 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
|
||||
VyHub.frontend_url = VyHub.frontend_url or nil
|
||||
|
||||
function VyHub:server_data_ready()
|
||||
VyHub:msg(f("I am server %s in bundle %s.", VyHub.server.name, VyHub.server.serverbundle.name))
|
||||
|
||||
VyHub.ready = true
|
||||
|
||||
hook.Run("vyhub_ready")
|
||||
end
|
||||
|
||||
function VyHub:get_frontend_url(callback)
|
||||
if VyHub.frontend_url != nil then
|
||||
if callback then
|
||||
callback(VyHub.frontend_url)
|
||||
end
|
||||
|
||||
return VyHub.frontend_url
|
||||
end
|
||||
|
||||
VyHub.API:get('/general/frontend-url', nil, nil, function (code, result)
|
||||
VyHub.frontend_url = result.frontend_url
|
||||
VyHub.Cache:save('frontend_url', VyHub.frontend_url)
|
||||
|
||||
if callback then
|
||||
callback(VyHub.frontend_url)
|
||||
end
|
||||
end, function ()
|
||||
local frontend_url = VyHub.Cache:get('frontend_url')
|
||||
|
||||
if frontend_url == nil then
|
||||
VyHub:msg("Could not get frontend_url!", "error")
|
||||
end
|
||||
|
||||
if callback then
|
||||
callback(frontend_url)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
hook.Add("vyhub_api_ready", "vyhub_main_vyhub_api_ready", function ()
|
||||
VyHub.API:get("/server/%s", { VyHub.Config.server_id }, nil, function(code, result)
|
||||
VyHub.server = result
|
||||
|
||||
VyHub:server_data_ready()
|
||||
|
||||
VyHub.Cache:save("server", VyHub.server)
|
||||
end, function (code, result)
|
||||
VyHub:msg(f("Could not find server with id %s", VyHub.Config.server_id), "error")
|
||||
|
||||
timer.Simple(60, function ()
|
||||
hook.Run("vyhub_loading_finish")
|
||||
end)
|
||||
end)
|
||||
|
||||
VyHub:get_frontend_url()
|
||||
end)
|
||||
|
||||
hook.Add("vyhub_api_failed", "vyhub_main_vyhub_api_failed", function ()
|
||||
local server = VyHub.Cache:get("server", 604800)
|
||||
|
||||
if server != nil then
|
||||
VyHub.server = server
|
||||
|
||||
VyHub:server_data_ready()
|
||||
else
|
||||
VyHub:msg("Could not find cached server data or cached data is too old. Please make sure that the server is able to reach the VyHub API.", "error")
|
||||
|
||||
timer.Simple(60, function ()
|
||||
hook.Run("vyhub_loading_finish")
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
timer.Create("vyhub_not_ready_msg", 30, 0, function ()
|
||||
if VyHub.ready then
|
||||
timer.Remove("vyhub_not_ready_msg")
|
||||
else
|
||||
VyHub.Util:print_chat_all("<green>VyHub</green> is not ready. Please check the server log/console for errors.")
|
||||
end
|
||||
end)
|
||||
350
addons/vyhub-gmod/lua/vyhub/server/sv_ply.lua
Normal file
350
addons/vyhub-gmod/lua/vyhub/server/sv_ply.lua
Normal file
@@ -0,0 +1,350 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
|
||||
VyHub.Player = VyHub.Player or {}
|
||||
|
||||
VyHub.Player.connect_queue = VyHub.Player.connect_queue or {}
|
||||
VyHub.Player.table = VyHub.Player.table or {}
|
||||
|
||||
util.AddNetworkString("vyhub_user_id")
|
||||
|
||||
local meta_ply = FindMetaTable("Player")
|
||||
|
||||
function VyHub.Player:initialize(ply, retry)
|
||||
if not IsValid(ply) then return end
|
||||
|
||||
local steamid = ply:SteamID64()
|
||||
|
||||
VyHub:msg(f("Initializing user %s, %s", ply:Nick(), steamid))
|
||||
|
||||
VyHub.API:get("/user/%s", {steamid}, {type = "STEAM"}, function(code, result)
|
||||
VyHub:msg(f("Found existing user %s for steam id %s (%s).", result.id, steamid, ply:Nick()), "success")
|
||||
|
||||
VyHub.Player.table[steamid] = result
|
||||
|
||||
VyHub.Player:refresh(ply)
|
||||
|
||||
VyHub.Player:send_user_id(ply)
|
||||
|
||||
hook.Run("vyhub_ply_initialized", ply)
|
||||
|
||||
local ply_timer_name = "vyhub_player_" .. steamid
|
||||
|
||||
timer.Create(ply_timer_name, VyHub.Config.player_refresh_time, 0, function()
|
||||
if IsValid(ply) then
|
||||
VyHub.Player:refresh(ply)
|
||||
else
|
||||
timer.Remove(ply_timer_name)
|
||||
end
|
||||
end)
|
||||
end, function(code, reason)
|
||||
if code != 404 then
|
||||
VyHub:msg(f("Could not check if users %s exists. Retrying in a minute..", steamid), "error")
|
||||
|
||||
timer.Simple(60, function ()
|
||||
VyHub.Player:initialize(ply)
|
||||
end)
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
if retry then
|
||||
VyHub:msg(f("Could not create user %s. Retrying in a minute..", steamid), "error")
|
||||
|
||||
timer.Simple(60, function()
|
||||
VyHub.Player:initialize(ply)
|
||||
end)
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
VyHub.Player:create(steamid, function()
|
||||
VyHub.Player:initialize(ply, true)
|
||||
end, function ()
|
||||
VyHub.Player:initialize(ply, true)
|
||||
end)
|
||||
end, { 404 })
|
||||
end
|
||||
|
||||
function VyHub.Player:send_user_id(ply)
|
||||
if not IsValid(ply) then return end
|
||||
|
||||
ply:VyHubID(function (user_id)
|
||||
net.Start("vyhub_user_id")
|
||||
net.WriteString(user_id)
|
||||
net.Send(ply)
|
||||
end)
|
||||
end
|
||||
|
||||
local creation_began = {}
|
||||
local creation_success = {}
|
||||
local creation_err = {}
|
||||
|
||||
function VyHub.Player:create(steamid, success, err)
|
||||
-- Creation can take longer. If multiple creation requests occur, merge them to one.
|
||||
if not istable(creation_success[steamid]) then creation_success[steamid] = {} end
|
||||
if not istable(creation_err[steamid]) then creation_err[steamid] = {} end
|
||||
|
||||
table.insert(creation_success[steamid], success)
|
||||
table.insert(creation_err[steamid], err)
|
||||
|
||||
if creation_began[steamid] and os.time() - creation_began[steamid] < 10 then
|
||||
VyHub:msg(f("Queued creation request for steamid %s", steamid), "debug")
|
||||
return
|
||||
end
|
||||
|
||||
VyHub:msg(f("No existing user found for steam id %s. Creating..", steamid))
|
||||
|
||||
creation_began[steamid] = os.time()
|
||||
|
||||
local function reset_queue()
|
||||
creation_began[steamid] = 0
|
||||
creation_success[steamid] = {}
|
||||
creation_err[steamid] = {}
|
||||
end
|
||||
|
||||
VyHub.API:post('/user/', nil, { identifier = steamid, type = 'STEAM' }, function()
|
||||
for _, success_callback in pairs(creation_success[steamid]) do
|
||||
if success_callback then
|
||||
success_callback()
|
||||
end
|
||||
end
|
||||
reset_queue()
|
||||
end, function()
|
||||
for _, err_callback in pairs(creation_err[steamid]) do
|
||||
if err_callback then
|
||||
err_callback()
|
||||
end
|
||||
end
|
||||
reset_queue()
|
||||
end)
|
||||
end
|
||||
|
||||
-- Return nil if steamid is nil or API error
|
||||
-- Return false if steamid is false or could not create user
|
||||
function VyHub.Player:get(steamid, callback, retry)
|
||||
if steamid == nil then
|
||||
callback(nil)
|
||||
return
|
||||
end
|
||||
|
||||
if steamid == false then
|
||||
callback(false)
|
||||
return
|
||||
end
|
||||
|
||||
if VyHub.Player.table[steamid] != nil then
|
||||
callback(VyHub.Player.table[steamid])
|
||||
else
|
||||
VyHub.API:get("/user/%s", {steamid}, {type = "STEAM"}, function(code, result)
|
||||
VyHub:msg(f("Received user %s for steam id %s.", result.id, steamid), "debug")
|
||||
|
||||
VyHub.Player.table[steamid] = result
|
||||
|
||||
callback(result)
|
||||
end, function(code)
|
||||
VyHub:msg(f("Could not receive user %s.", steamid), "error")
|
||||
|
||||
if code == 404 and retry == nil then
|
||||
VyHub.Player:create(steamid, function ()
|
||||
VyHub.Player:get(steamid, callback, true)
|
||||
end, function ()
|
||||
callback(false)
|
||||
end)
|
||||
else
|
||||
callback(nil)
|
||||
end
|
||||
end, {404})
|
||||
end
|
||||
end
|
||||
|
||||
function VyHub.Player:change_game_group(ply, group)
|
||||
if not IsValid(ply) then return end
|
||||
|
||||
local steamid64 = ply:SteamID64()
|
||||
local nick = ply:Nick()
|
||||
|
||||
VyHub.Group.group_changes[steamid64] = group
|
||||
|
||||
if serverguard then
|
||||
serverguard.player:SetRank(ply, group, false, true)
|
||||
elseif ulx then
|
||||
ULib.ucl.addUser( ply:SteamID(), {}, {}, group, true )
|
||||
elseif sam then
|
||||
sam.player.set_rank(ply, group, 0, true)
|
||||
elseif xAdmin and xAdmin.Admin.RegisterBan then
|
||||
xAdmin.SetGroup(ply, group, true)
|
||||
elseif sAdmin then
|
||||
sAdmin.setRank(ply, group, 0, false, true)
|
||||
else
|
||||
ply:SetUserGroup(group, true)
|
||||
end
|
||||
|
||||
VyHub:msg("Added " .. nick .. " to group " .. group, "success")
|
||||
VyHub.Util:print_chat(ply, f(VyHub.lang.ply.group_changed, group))
|
||||
end
|
||||
|
||||
function VyHub.Player:check_group(ply, callback)
|
||||
if VyHub.Config.group_disable_sync then return end
|
||||
|
||||
if ply:VyHubID() == nil then
|
||||
VyHub:msg(f("Could not check groups for user %s, because no VyHub id is available.", ply:SteamID64()), "debug")
|
||||
return
|
||||
end
|
||||
|
||||
VyHub.API:get("/user/%s/group", {ply:VyHubID()}, { serverbundle_id = VyHub.server.serverbundle_id }, function(code, result)
|
||||
if not IsValid(ply) then return end
|
||||
|
||||
local steamid64 = ply:SteamID64()
|
||||
local nick = ply:Nick()
|
||||
|
||||
local highest = nil
|
||||
|
||||
for _, group in ipairs(result) do
|
||||
if highest == nil or highest.permission_level < group.permission_level then
|
||||
highest = group
|
||||
end
|
||||
end
|
||||
|
||||
if highest == nil then
|
||||
VyHub:msg(f("Could not find any active group for %s (%s)", nick, steamid64), "debug")
|
||||
return
|
||||
end
|
||||
|
||||
local group = nil
|
||||
|
||||
for _, mapping in ipairs(highest.mappings) do
|
||||
if mapping.serverbundle_id == nil or mapping.serverbundle_id == VyHub.server.serverbundle.id then
|
||||
group = mapping.name
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if group == nil then
|
||||
VyHub:msg(f("Could not find group name mapping for group %s.", highest.name), "debug")
|
||||
return
|
||||
end
|
||||
|
||||
local delay = 0
|
||||
|
||||
if sAdmin then
|
||||
delay = 3
|
||||
end
|
||||
|
||||
timer.Simple(delay, function ()
|
||||
local curr_group = ply:GetUserGroup()
|
||||
|
||||
if curr_group != group then
|
||||
VyHub.Player:change_game_group(ply, group)
|
||||
end
|
||||
end)
|
||||
end, function()
|
||||
|
||||
end)
|
||||
end
|
||||
|
||||
function VyHub.Player:refresh(ply, callback)
|
||||
VyHub.Player:check_group(ply)
|
||||
end
|
||||
|
||||
function VyHub.Player:get_group(ply)
|
||||
if not IsValid(ply) then
|
||||
return nil
|
||||
end
|
||||
|
||||
local group = VyHub.groups_mapped[ply:GetUserGroup()]
|
||||
|
||||
if group == nil then
|
||||
return nil
|
||||
end
|
||||
|
||||
return group
|
||||
end
|
||||
|
||||
function VyHub.Player:check_property(ply, property)
|
||||
if not IsValid(ply) then return false end
|
||||
|
||||
local group = VyHub.Player:get_group(ply)
|
||||
|
||||
if group != nil then
|
||||
local prop = group.properties[property]
|
||||
|
||||
if prop != nil and prop.granted then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local steamid64 = ply:SteamID64()
|
||||
|
||||
if VyHub.Player.table[steamid64] then
|
||||
return VyHub.Player.table[steamid64].admin
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function meta_ply:VyHubID(callback)
|
||||
if IsValid(self) then
|
||||
local user = VyHub.Player.table[self:SteamID64()]
|
||||
local id = nil
|
||||
|
||||
if user != nil then
|
||||
id = user.id
|
||||
end
|
||||
|
||||
if id == nil or id == "" then
|
||||
VyHub.Player:get(self:SteamID64(), function(user)
|
||||
if user then
|
||||
if callback then
|
||||
callback(user.id)
|
||||
end
|
||||
else
|
||||
if callback then
|
||||
callback(nil)
|
||||
end
|
||||
end
|
||||
end)
|
||||
else
|
||||
if callback then
|
||||
callback(id)
|
||||
end
|
||||
end
|
||||
|
||||
return id
|
||||
end
|
||||
end
|
||||
|
||||
hook.Add("vyhub_ply_connected", "vyhub_ply_vyhub_ply_connected", function(ply)
|
||||
VyHub.Player:initialize(ply)
|
||||
end)
|
||||
|
||||
hook.Add("PlayerInitialSpawn","vyhub_ply_PlayerInitialSpawn", function(ply)
|
||||
if IsValid(ply) and not ply:IsBot() then
|
||||
if VyHub.ready then
|
||||
hook.Run("vyhub_ply_connected", ply)
|
||||
else
|
||||
VyHub.Player.connect_queue[#VyHub.Player.connect_queue+1] = ply
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_ply_vyhub_ready", function ()
|
||||
timer.Simple(5, function()
|
||||
for _, ply in ipairs(VyHub.Player.connect_queue) do
|
||||
if IsValid(ply) then
|
||||
hook.Run("vyhub_ply_connected", ply)
|
||||
end
|
||||
end
|
||||
|
||||
VyHub.Player.connect_queue = {}
|
||||
end)
|
||||
end)
|
||||
266
addons/vyhub-gmod/lua/vyhub/server/sv_reward.lua
Normal file
266
addons/vyhub-gmod/lua/vyhub/server/sv_reward.lua
Normal file
@@ -0,0 +1,266 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
local json = VyHub.Lib.json
|
||||
|
||||
VyHub.Reward = VyHub.Reward or {}
|
||||
VyHub.Reward.executed_rewards_queue = VyHub.Reward.executed_rewards_queue or {}
|
||||
VyHub.Reward.executed_rewards = VyHub.Reward.executed_rewards or {}
|
||||
VyHub.rewards = VyHub.rewards or {}
|
||||
|
||||
local RewardEvent = {
|
||||
DIRECT = "DIRECT",
|
||||
CONNECT = "CONNECT",
|
||||
SPAWN = "SPAWN",
|
||||
DEATH = "DEATH",
|
||||
DISCONNECT = "DISCONNECT",
|
||||
DISABLE = "DISABLE",
|
||||
}
|
||||
|
||||
local RewardType = {
|
||||
COMMAND = "COMMAND",
|
||||
SCRIPT = "SCRIPT",
|
||||
CREDITS = "CREDITS",
|
||||
MEMBERSHIP = "MEMBERSHIP",
|
||||
}
|
||||
|
||||
function VyHub.Reward:refresh(callback, limit_players, err)
|
||||
local user_ids = ""
|
||||
local players = limit_players or player.GetHumans()
|
||||
|
||||
for _, ply in ipairs(players) do
|
||||
if IsValid(ply) then
|
||||
local id = ply:VyHubID()
|
||||
|
||||
if id and string.len(id) == 36 then
|
||||
local glue = '&'
|
||||
|
||||
if user_ids == "" then
|
||||
glue = '?'
|
||||
end
|
||||
|
||||
user_ids = user_ids .. glue .. 'user_id=' .. id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if user_ids == "" then
|
||||
VyHub.rewards = {}
|
||||
else
|
||||
local query = f("%s&active=true&serverbundle_id=%s&status=OPEN&for_server_id=%s&foreign_ids=true", user_ids, VyHub.server.serverbundle.id, VyHub.server.id)
|
||||
|
||||
VyHub.API:get('/packet/reward/applied/user' .. query, nil, nil,
|
||||
function(code, result)
|
||||
if limit_players == nil then
|
||||
VyHub.rewards = result
|
||||
VyHub:msg(f("Found %i users with open rewards.", table.Count(result)), "debug")
|
||||
else
|
||||
for steamid, arewards in pairs(result) do
|
||||
VyHub.rewards[steamid] = arewards
|
||||
end
|
||||
end
|
||||
|
||||
if callback then
|
||||
callback()
|
||||
end
|
||||
end, function (code, reason)
|
||||
if err then
|
||||
err()
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function VyHub.Reward:set_executed(areward_id)
|
||||
VyHub.Reward.executed_rewards_queue[areward_id] = true
|
||||
table.insert(VyHub.Reward.executed_rewards, areward_id)
|
||||
|
||||
VyHub.Reward:save_executed()
|
||||
end
|
||||
|
||||
function VyHub.Reward:save_executed()
|
||||
VyHub.Cache:save("executed_rewards_queue", VyHub.Reward.executed_rewards_queue)
|
||||
end
|
||||
|
||||
function VyHub.Reward:send_executed()
|
||||
for areward_id, val in pairs(VyHub.Reward.executed_rewards_queue) do
|
||||
if val != nil then
|
||||
VyHub.API:patch('/packet/reward/applied/%s', { areward_id }, { executed_on = { VyHub.server.id } }, function (code, result)
|
||||
VyHub.Reward.executed_rewards_queue[areward_id] = nil
|
||||
VyHub.Reward:save_executed()
|
||||
end, function (code, reason)
|
||||
if code >= 400 and code < 500 then
|
||||
VyHub:msg(f("Could not mark reward %s as executed. Aborting.", areward_id), "error")
|
||||
VyHub.Reward.executed_rewards_queue[areward_id] = nil
|
||||
VyHub.Reward:save_executed()
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function VyHub.Reward:exec_rewards(event, steamid)
|
||||
steamid = steamid or nil
|
||||
|
||||
local allowed_events = { event }
|
||||
|
||||
local rewards_by_player = VyHub.rewards
|
||||
|
||||
if steamid != nil then
|
||||
rewards_by_player = {}
|
||||
rewards_by_player[steamid] = VyHub.rewards[steamid]
|
||||
else
|
||||
if event != RewardEvent.DIRECT then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if event == RewardEvent.DIRECT then
|
||||
table.insert(allowed_events, RewardEvent.DISABLE)
|
||||
end
|
||||
|
||||
for steamid, arewards in pairs(rewards_by_player) do
|
||||
local ply = player.GetBySteamID64(steamid)
|
||||
|
||||
if not IsValid(ply) then
|
||||
VyHub:msg(f("Player %s not valid, skipping.", steamid), "debug")
|
||||
continue
|
||||
end
|
||||
|
||||
for _, areward in ipairs(arewards) do
|
||||
local se = true
|
||||
local reward = areward.reward
|
||||
|
||||
if not table.HasValue(allowed_events, reward.on_event) then
|
||||
continue
|
||||
end
|
||||
|
||||
if table.HasValue(VyHub.Reward.executed_rewards, areward.id) then
|
||||
VyHub:msg(f("Skipped reward %s, because it already has been executed.", areward.id), "debug")
|
||||
continue
|
||||
end
|
||||
|
||||
local data = reward.data
|
||||
|
||||
if reward.type == RewardType.COMMAND then
|
||||
if data.command != nil then
|
||||
local cmd = VyHub.Reward:do_string_replacements(data.command, ply, areward)
|
||||
|
||||
if VyHub.Config.reward_command_whitelist and #VyHub.Config.reward_command_whitelist > 0 then
|
||||
local matched = false
|
||||
|
||||
for _, cmd_pattern in ipairs(VyHub.Config.reward_command_whitelist) do
|
||||
if string.match(cmd, cmd_pattern) != nil then
|
||||
matched = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not matched then
|
||||
VyHub:msg(f("Failed to execute reward '%s': Command '%s' does not match a command on the whitelist.", reward.name, cmd), "error")
|
||||
continue
|
||||
end
|
||||
end
|
||||
|
||||
game.ConsoleCommand(cmd.. "\n")
|
||||
end
|
||||
elseif reward.type == RewardType.SCRIPT then
|
||||
if VyHub.Config.reward_disable_scripts then
|
||||
VyHub:msg(f("Failed to execute reward '%s': Scripts are not allowed on this server. You can enable scripts in sv_config.lua or by entering 'vh_config reward_disable_scripts false' in the server console.", reward.name), "error")
|
||||
continue
|
||||
end
|
||||
|
||||
local lua_str = data.script
|
||||
|
||||
if lua_str != nil then
|
||||
lua_str = VyHub.Reward:do_string_replacements(lua_str, ply, areward)
|
||||
|
||||
RunString("local PLAYER = player.GetBySteamID64(\"" .. steamid .. "\") " .. lua_str, "vyhub_reward_script")
|
||||
end
|
||||
else
|
||||
VyHub:msg(f("No implementation for reward type %s", reward.type) "warning")
|
||||
end
|
||||
|
||||
VyHub:msg(f("Executed reward %s for user %s (%s): %s", reward.type, ply:Nick(), ply:SteamID64(), json.encode(data)))
|
||||
|
||||
if se and reward.once then
|
||||
VyHub.Reward:set_executed(areward.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
VyHub.Reward:send_executed()
|
||||
end
|
||||
|
||||
function VyHub.Reward:do_string_replacements(inp_str, ply, areward)
|
||||
local purchase_amount = "-"
|
||||
|
||||
if areward.applied_packet.purchase != nil then
|
||||
purchase_amount = areward.applied_packet.purchase.amount_text
|
||||
end
|
||||
|
||||
local replacements = {
|
||||
["user_id"] = ply:VyHubID(),
|
||||
["nick"] = ply:Nick(),
|
||||
["steamid64"] = ply:SteamID64(),
|
||||
["steamid32"] = ply:SteamID(),
|
||||
["uniqueid"] = ply:UniqueID(),
|
||||
["applied_packet_id"] = areward.applied_packet_id,
|
||||
["packet_title"] = areward.applied_packet.packet.title,
|
||||
["purchase_amount"] = purchase_amount,
|
||||
}
|
||||
|
||||
for k, v in pairs(replacements) do
|
||||
inp_str = string.Replace(tostring(inp_str), "%" .. tostring(k) .. "%", tostring(v))
|
||||
end
|
||||
|
||||
return inp_str
|
||||
end
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_reward_vyhub_ready", function ()
|
||||
VyHub.Reward.executed_rewards_queue = VyHub.Cache:get("executed_rewards_queue") or {}
|
||||
|
||||
VyHub.Reward:refresh(function ()
|
||||
VyHub.Reward:exec_rewards(RewardEvent.DIRECT)
|
||||
end)
|
||||
|
||||
timer.Create("vyhub_reward_refresh", 60, 0, function ()
|
||||
VyHub.Reward:refresh(function ()
|
||||
VyHub.Reward:exec_rewards(RewardEvent.DIRECT)
|
||||
end)
|
||||
end)
|
||||
|
||||
hook.Add("vyhub_ply_initialized", "vyhub_reward_vyhub_ply_initialized", function(ply)
|
||||
local function exec_ply_rewards()
|
||||
VyHub.Reward:exec_rewards(RewardEvent.CONNECT, tostring(ply:SteamID64()))
|
||||
hook.Run("vyhub_reward_post_connect", ply)
|
||||
end
|
||||
|
||||
VyHub.Reward:refresh(exec_ply_rewards, { ply }, exec_ply_rewards)
|
||||
end)
|
||||
|
||||
hook.Add("PlayerSpawn", "vyhub_reward_PlayerSpawn", function(ply)
|
||||
if ply:Alive() then
|
||||
VyHub.Reward:exec_rewards(RewardEvent.SPAWN, tostring(ply:SteamID64()))
|
||||
end
|
||||
end)
|
||||
|
||||
hook.Add("PostPlayerDeath", "vyhub_reward_PostPlayerDeath", function(ply)
|
||||
VyHub.Reward:exec_rewards(RewardEvent.DEATH, tostring(ply:SteamID64()))
|
||||
end)
|
||||
|
||||
-- Does not work
|
||||
hook.Add("PlayerDisconnect", "vyhub_reward_PlayerDisconnect", function(ply)
|
||||
if IsValid(ply) then
|
||||
VyHub.Reward:exec_rewards(RewardEvent.Disconnect, tostring(ply:SteamID64()))
|
||||
end
|
||||
end)
|
||||
end)
|
||||
157
addons/vyhub-gmod/lua/vyhub/server/sv_server.lua
Normal file
157
addons/vyhub-gmod/lua/vyhub/server/sv_server.lua
Normal file
@@ -0,0 +1,157 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
local json = VyHub.Lib.json
|
||||
|
||||
VyHub.Server = VyHub.Server or {}
|
||||
|
||||
VyHub.Server.extra_defaults = {
|
||||
res_slots = 0,
|
||||
res_slots_keep_free = false,
|
||||
res_slots_hide = false,
|
||||
}
|
||||
|
||||
VyHub.Server.reserved_slot_plys = VyHub.Server.reserved_slot_plys or {}
|
||||
|
||||
function VyHub.Server:get_extra(key)
|
||||
if VyHub.server.extra != nil and VyHub.server.extra[key] != nil then
|
||||
return VyHub.server.extra[key]
|
||||
end
|
||||
|
||||
return VyHub.Server.extra_defaults[key]
|
||||
end
|
||||
|
||||
function VyHub.Server:update_status()
|
||||
local user_activities = {}
|
||||
|
||||
for _, ply in ipairs(player.GetHumans()) do
|
||||
local id = ply:VyHubID()
|
||||
|
||||
if id and string.len(id) == 36 then
|
||||
local tt = string.FormattedTime( ply:TimeConnected() )
|
||||
|
||||
table.insert(user_activities, { user_id = id, extra = {
|
||||
Score = ply:Frags(),
|
||||
Deaths = ply:Deaths(),
|
||||
Nickname = ply:Nick(),
|
||||
Playtime = f('%02d:%02d:%02d', tt.h, tt.m, tt.s),
|
||||
Ping = f('%i ms', ply:Ping()),
|
||||
}})
|
||||
end
|
||||
end
|
||||
|
||||
local data = {
|
||||
users_max = VyHub.Server.max_slots_visible,
|
||||
users_current = #player.GetAll(),
|
||||
map = game.GetMap(),
|
||||
is_alive = true,
|
||||
user_activities = user_activities,
|
||||
}
|
||||
|
||||
VyHub:msg(f("Updating status: %s", json.encode(data)), "debug")
|
||||
|
||||
VyHub.API:patch(
|
||||
'/server/%s',
|
||||
{VyHub.server.id},
|
||||
data,
|
||||
function ()
|
||||
hook.Run("vyhub_dashboard_data_changed")
|
||||
end,
|
||||
function ()
|
||||
VyHub:msg("Could not update server status.", "error")
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
function VyHub.Server:update_max_slots()
|
||||
RunConsoleCommand("sv_visiblemaxplayers", VyHub.Server.max_slots_visible)
|
||||
end
|
||||
|
||||
function VyHub.Server:init_slots()
|
||||
VyHub.Server.max_slots = game.MaxPlayers() - VyHub.Server:get_extra("res_slots")
|
||||
VyHub.Server.max_slots_visible = VyHub.Server.max_slots
|
||||
|
||||
if VyHub.Server:get_extra("res_slots_hide") then
|
||||
VyHub.Server:update_max_slots()
|
||||
|
||||
hook.Add("PlayerDisconnected", "vyhub_server_PlayerDisconnected", function(ply)
|
||||
timer.Create("vyhub_slots", 0.5, 20, function()
|
||||
if not IsValid(ply) then
|
||||
timer.Remove("vyhub_slots")
|
||||
VyHub.Server:update_max_slots()
|
||||
end
|
||||
end)
|
||||
end)
|
||||
else
|
||||
VyHub.Server.max_slots_visible = game.MaxPlayers()
|
||||
end
|
||||
end
|
||||
|
||||
function VyHub.Server:can_use_rslot(ply)
|
||||
if not IsValid(ply) or ply:IsBot() then
|
||||
return false
|
||||
end
|
||||
|
||||
if table.HasValue(VyHub.Server.reserved_slot_plys, ply:SteamID64()) then
|
||||
return true
|
||||
end
|
||||
|
||||
local group = VyHub.Player:get_group(ply)
|
||||
|
||||
if group != nil then
|
||||
if group.properties.reserved_slot_use != nil then
|
||||
return group.properties.reserved_slot_use.granted
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function VyHub.Server:handle_ply_connect(ply)
|
||||
if IsValid(ply) then
|
||||
if #player.GetHumans() > VyHub.Server.max_slots then
|
||||
if VyHub.Server:can_use_rslot(ply) then
|
||||
if VyHub.Server:get_extra("res_slots_keep_free") then
|
||||
local tokick = nil
|
||||
|
||||
for _, v in ipairs(player.GetHumans()) do
|
||||
if v:SteamID64() != ply:SteamID64() and not VyHub.Server:can_use_rslot(v) then
|
||||
if tokick == nil or (IsValid(tokick) and v:TimeConnected() < tokick:TimeConnected()) then
|
||||
tokick = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if tokick and IsValid(tokick) then
|
||||
tokick:Kick(VyHub.lang.rslots.kick)
|
||||
else
|
||||
ply:Kick(VyHub.lang.rslots.full)
|
||||
end
|
||||
end
|
||||
else
|
||||
ply:Kick(VyHub.lang.rslots.full_no_slot)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_server_vyhub_ready", function ()
|
||||
VyHub.Server:init_slots()
|
||||
VyHub.Server:update_status()
|
||||
|
||||
timer.Create("vyhub_status_update", 60, 0, function ()
|
||||
VyHub.Server:update_status()
|
||||
end)
|
||||
|
||||
hook.Add("vyhub_reward_post_connect", "vyhub_server_vyhub_reward_post_connect", function (ply)
|
||||
VyHub.Server:handle_ply_connect(ply)
|
||||
end)
|
||||
end)
|
||||
143
addons/vyhub-gmod/lua/vyhub/server/sv_statistic.lua
Normal file
143
addons/vyhub-gmod/lua/vyhub/server/sv_statistic.lua
Normal file
@@ -0,0 +1,143 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
local json = VyHub.Lib.json
|
||||
|
||||
VyHub.Statistic = VyHub.Statistic or {}
|
||||
VyHub.Statistic.playtime = VyHub.Statistic.playtime or {}
|
||||
VyHub.Statistic.attr_def = VyHub.Statistic.attr_def or nil
|
||||
|
||||
function VyHub.Statistic:save_playtime()
|
||||
VyHub:msg(f("Saved playtime statistics: %s", json.encode(VyHub.Statistic.playtime)), "debug")
|
||||
|
||||
VyHub.Cache:save("playtime", VyHub.Statistic.playtime)
|
||||
end
|
||||
|
||||
function VyHub.Statistic:add_one_minute()
|
||||
for _, ply in ipairs(player.GetHumans()) do
|
||||
local steamid = ply:SteamID64()
|
||||
ply:VyHubID(function (user_id)
|
||||
if user_id == nil or string.len(user_id) < 10 then
|
||||
VyHub:msg(f("Could not add playtime for user %s", steamid))
|
||||
return
|
||||
end
|
||||
|
||||
VyHub.Statistic.playtime[user_id] = VyHub.Statistic.playtime[user_id] or 0
|
||||
VyHub.Statistic.playtime[user_id] = VyHub.Statistic.playtime[user_id] + 60
|
||||
end)
|
||||
end
|
||||
|
||||
VyHub.Statistic:save_playtime()
|
||||
end
|
||||
|
||||
function VyHub.Statistic:send_playtime()
|
||||
VyHub.Statistic:get_or_create_attr_definition(function (attr_def)
|
||||
if attr_def == nil then
|
||||
VyHub:msg("Could not send playtime statistics to API.", "warning")
|
||||
return
|
||||
end
|
||||
|
||||
local user_ids = table.GetKeys(VyHub.Statistic.playtime)
|
||||
|
||||
timer.Create("vyhub_send_stats", 0.3, table.Count(user_ids), function ()
|
||||
local i = table.Count(user_ids)
|
||||
local user_id = user_ids[i]
|
||||
|
||||
if user_id != nil then
|
||||
local seconds = VyHub.Statistic.playtime[user_id]
|
||||
table.remove(user_ids, i)
|
||||
|
||||
if seconds != nil and seconds > 0 then
|
||||
local hours = math.Round(seconds / 60 / 60, 2)
|
||||
|
||||
if hours > 0 then
|
||||
if string.len(user_id) < 10 then
|
||||
VyHub.Statistic.playtime[user_id] = nil
|
||||
return
|
||||
end
|
||||
|
||||
VyHub.API:post("/user/attribute/", nil, {
|
||||
definition_id = attr_def.id,
|
||||
user_id = user_id,
|
||||
serverbundle_id = VyHub.server.serverbundle.id,
|
||||
value = tostring(hours),
|
||||
}, function (code, result)
|
||||
VyHub.Statistic.playtime[user_id] = nil
|
||||
VyHub.Statistic:save_playtime()
|
||||
end, function (code, reason)
|
||||
if code == 404 then
|
||||
VyHub.Statistic.playtime[user_id] = nil
|
||||
VyHub.Statistic:save_playtime()
|
||||
end
|
||||
|
||||
VyHub:msg(f("Could not send %s seconds playtime of %s to API.", seconds, user_id), "warning")
|
||||
end)
|
||||
end
|
||||
else
|
||||
VyHub.Statistic.playtime[user_id] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
function VyHub.Statistic:get_or_create_attr_definition(callback)
|
||||
local function cb_wrapper(attr_def)
|
||||
VyHub.Statistic.attr_def = attr_def
|
||||
|
||||
callback(attr_def)
|
||||
end
|
||||
|
||||
if VyHub.Statistic.attr_def != nil then
|
||||
callback(VyHub.Statistic.attr_def)
|
||||
return
|
||||
end
|
||||
|
||||
VyHub.API:get("/user/attribute/definition/%s", { "playtime" }, nil, function (code, result)
|
||||
VyHub.Cache:save("playtime_attr_def", result)
|
||||
cb_wrapper(result)
|
||||
end, function (code, reason)
|
||||
if code != 404 then
|
||||
local attr_def = VyHub.Cache:get("playtime_attr_def")
|
||||
|
||||
cb_wrapper(attr_def)
|
||||
else
|
||||
VyHub.API:post("/user/attribute/definition/", nil, {
|
||||
name = "playtime",
|
||||
title = "Play Time",
|
||||
unit = "Hours",
|
||||
type = "ACCUMULATED",
|
||||
accumulation_interval = "day",
|
||||
unspecific = "true",
|
||||
}, function (code, result)
|
||||
VyHub.Cache:save("playtime_attr_def", result)
|
||||
cb_wrapper(result)
|
||||
end, function (code, reason)
|
||||
cb_wrapper(nil)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_statistic_vyhub_ready", function ()
|
||||
VyHub.Statistic.playtime = VyHub.Cache:get("playtime") or {}
|
||||
|
||||
VyHub.Statistic:send_playtime()
|
||||
|
||||
timer.Create("vyhub_statistic_playtime_tick", 60, 0, function ()
|
||||
VyHub.Statistic:add_one_minute()
|
||||
end)
|
||||
|
||||
timer.Create("vyhub_statistic_send_playtime", 3600, 0, function ()
|
||||
VyHub.Statistic:send_playtime()
|
||||
end)
|
||||
end)
|
||||
183
addons/vyhub-gmod/lua/vyhub/server/sv_warning.lua
Normal file
183
addons/vyhub-gmod/lua/vyhub/server/sv_warning.lua
Normal file
@@ -0,0 +1,183 @@
|
||||
--[[
|
||||
| This file was obtained through the combined efforts
|
||||
| of Madbluntz & Plymouth Antiquarian Society.
|
||||
|
|
||||
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
||||
| Maloy, DrPepper10 @ RIP, Atle!
|
||||
|
|
||||
| Visit for more: https://plymouth.thetwilightzone.ru/
|
||||
--]]
|
||||
|
||||
local f = string.format
|
||||
|
||||
VyHub.Warning = VyHub.Warning or {}
|
||||
|
||||
function VyHub.Warning:create(steamid, reason, processor_steamid)
|
||||
processor_steamid = processor_steamid or nil
|
||||
|
||||
VyHub.Player:get(steamid, function (user)
|
||||
if user == nil then
|
||||
VyHub.Util:print_chat_steamid(processor_steamid, f("<red>Cannot find VyHub user with SteamID %s.</red>", steamid))
|
||||
return
|
||||
end
|
||||
|
||||
VyHub.Player:get(processor_steamid, function (processor)
|
||||
if processor_steamid != nil and processor == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local url = '/warning/'
|
||||
|
||||
if processor != nil then
|
||||
url = url .. f('?morph_user_id=%s', processor.id)
|
||||
end
|
||||
|
||||
VyHub.API:post(url, nil, {
|
||||
reason = reason,
|
||||
serverbundle_id = VyHub.server.serverbundle.id,
|
||||
user_id = user.id
|
||||
}, function (code, result)
|
||||
VyHub.Ban:refresh()
|
||||
VyHub:msg(f("Added warning for player %s: %s", user.username, reason))
|
||||
VyHub.Util:print_chat_all(f(VyHub.lang.warning.user_warned, user.username, processor.username, reason))
|
||||
VyHub.Util:print_chat_steamid(steamid, f(VyHub.lang.warning.received, processor.username, reason))
|
||||
VyHub.Util:play_sound_steamid(steamid, "https://cdn.vyhub.net/sound/negativebeep.wav")
|
||||
hook.Run("vyhub_dashboard_data_changed")
|
||||
end, function (code, err_reason, _, err_text)
|
||||
VyHub:msg(f("Error while adding warning for player %s: %s", user.username, err_text), "error")
|
||||
VyHub.Util:print_chat_steamid(processor_steamid, f(VyHub.lang.warning.create_error, user.username, err_text))
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
function VyHub.Warning:delete(warning_id, processor_steamid)
|
||||
processor_steamid = processor_steamid or nil
|
||||
|
||||
VyHub.Player:get(processor_steamid, function (processor)
|
||||
if not processor then return end
|
||||
|
||||
local url = '/warning/%s'
|
||||
|
||||
if processor != nil then
|
||||
url = url .. f('?morph_user_id=%s', processor.id)
|
||||
end
|
||||
|
||||
VyHub.API:delete(url, { warning_id }, function (code, result)
|
||||
VyHub:msg(f("%s deleted warning %s.", processor.username, warning_id))
|
||||
VyHub.Util:print_chat_steamid(processor_steamid, f(VyHub.lang.warning.deleted))
|
||||
VyHub.Util:print_chat_steamid(steamid, VyHub.lang.warning.deleted_self)
|
||||
hook.Run("vyhub_dashboard_data_changed")
|
||||
end, function (code, err_reason, _, err_text)
|
||||
VyHub:msg(f("Error while deleteing warning %s: %s", warning_id, err_text), "error")
|
||||
VyHub.Util:print_chat_steamid(processor_steamid, f(VyHub.lang.other.error_api, err_text))
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
function VyHub.Warning:toggle(warning_id, processor_steamid)
|
||||
processor_steamid = processor_steamid or nil
|
||||
|
||||
VyHub.Player:get(processor_steamid, function (processor)
|
||||
if not processor then return end
|
||||
|
||||
local url = '/warning/%s/toggle'
|
||||
|
||||
if processor != nil then
|
||||
url = url .. f('?morph_user_id=%s', processor.id)
|
||||
end
|
||||
|
||||
VyHub.API:patch(url, { warning_id }, nil, function (code, result)
|
||||
VyHub:msg(f("%s toggled warning %s.", processor.username, warning_id))
|
||||
VyHub.Util:print_chat_steamid(processor_steamid, f(VyHub.lang.warning.toggled))
|
||||
VyHub.Util:print_chat_steamid(steamid, VyHub.lang.warning.toggled_self)
|
||||
hook.Run("vyhub_dashboard_data_changed")
|
||||
end, function (code, err_reason, _, err_text)
|
||||
VyHub:msg(f("Error while toggling status of warning %s: %s", warning_id, err_text), "error")
|
||||
VyHub.Util:print_chat_steamid(processor_steamid, f(VyHub.lang.other.error_api, err_text))
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
local function warn_command(ply, args)
|
||||
if not VyHub.Player:check_property(ply, "warning_edit") then
|
||||
VyHub.Util:print_chat(ply, VyHub.lang.ply.no_permissions)
|
||||
return
|
||||
end
|
||||
|
||||
if args[1] and args[2] then
|
||||
local reason = VyHub.Util:concat_args(args, 2)
|
||||
|
||||
local target = VyHub.Util:get_player_by_nick(args[1])
|
||||
|
||||
if target and IsValid(target) then
|
||||
local nickparts = string.Explode(' ', target:Nick())
|
||||
|
||||
if #nickparts > 1 then
|
||||
nickparts = VyHub.Util:concat_args(nickparts, 2) .. ' '
|
||||
reason = string.Replace(reason, nickparts, '')
|
||||
end
|
||||
|
||||
VyHub.Warning:create(target:SteamID64(), reason, ply:SteamID64())
|
||||
end
|
||||
end
|
||||
|
||||
if IsValid(ply) then
|
||||
VyHub.Util:print_chat(ply, VyHub.lang.warning.cmd_help)
|
||||
end
|
||||
|
||||
return false;
|
||||
end
|
||||
|
||||
hook.Add("vyhub_ready", "vyhub_warning_vyhub_ready", function ()
|
||||
concommand.Add("vh_warn", function(ply, _, args)
|
||||
if not args[1] or not args[2] then return end
|
||||
|
||||
if VyHub.Util:is_server(ply) then
|
||||
VyHub.Warning:create(args[1], args[2])
|
||||
elseif IsValid(ply) then
|
||||
if VyHub.Player:check_property(ply, "warning_edit") then
|
||||
VyHub.Warning:create(args[1], args[2], ply:SteamID64())
|
||||
else
|
||||
VyHub.Util:print_chat(ply, VyHub.lang.ply.no_permissions)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
concommand.Add("vh_warning_toggle", function(ply, _, args)
|
||||
if not args[1] then return end
|
||||
|
||||
local warning_id = args[1]
|
||||
|
||||
if VyHub.Util:is_server(ply) then
|
||||
VyHub.Warning:toggle(warning_id)
|
||||
elseif IsValid(ply) then
|
||||
if VyHub.Player:check_property(ply, "warning_edit") then
|
||||
VyHub.Warning:toggle(warning_id, ply:SteamID64())
|
||||
else
|
||||
VyHub.Util:print_chat(ply, VyHub.lang.ply.no_permissions)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
concommand.Add("vh_warning_delete", function(ply, _, args)
|
||||
if not args[1] then return end
|
||||
|
||||
local warning_id = args[1]
|
||||
|
||||
if VyHub.Util:is_server(ply) then
|
||||
VyHub.Warning:delete(warning_id)
|
||||
elseif IsValid(ply) then
|
||||
if VyHub.Player:check_property(ply, "warning_delete") then
|
||||
VyHub.Warning:delete(warning_id, ply:SteamID64())
|
||||
else
|
||||
VyHub.Util:print_chat(ply, VyHub.lang.ply.no_permissions)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
for _, cmd in ipairs(VyHub.Config.commands_warn) do
|
||||
VyHub.Util:register_chat_command(cmd, warn_command)
|
||||
end
|
||||
end)
|
||||
Reference in New Issue
Block a user