Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
adding mysql support
Browse files Browse the repository at this point in the history
  • Loading branch information
DasDarki committed Jan 10, 2020
1 parent 34f0a61 commit 7d4791c
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 45 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,26 @@ content["msg-argument-missing"] = "[Zeus] Failed to executed because Argument {1
content["msg-argument-invalid"] = "[Zeus] Failed to executed because Argument {1} is invalid!"
content["msg-mod-success"] = "[Zeus] Module {1} was successfully executed!"
content["msg-mod-disabled"] = "[Zeus] Module {1} was successfully disabled!"
content["msg-mod-failed"] = "[Zeus] Module {1} failed because {2}!"
content["msg-banned"] = "You are banned: {1}"
-- VEH SPAWN --
content["msg-veh-model-not-exist"] = "[Zeus] The Vehicle Model {1} does not exist!"

content["custom-chat"] = false
content["store-type"] = "LOCAL" -- coming soon: mysql
content["admins"] = { } -- Enter SteamID64 in here which will have all permissions
content["dev-mode"] = true -- Every User on the Server has Admin Permissions, when true
content["custom-chat"] = false -- coming soon: mysql
content["store-type"] = "LOCAL" -- or MYSQL
content["dev-mode"] = true

function IsLocalStorage()
return content["store-type"] == "LOCAL"
end

function IsAdmin(steamID)
if content["dev-mode"] == true then
return true
end
content["db-host"] = "localhost"
content["db-user"] = "zeus-db"
content["db-password"] = "this-is-a-safe-pw"
content["db-name"] = "zeus-db"
content["db-charset"] = "utf8mb4"

for _, value in ipairs(content["admins"]) do
if tostring(value) == steamID then
return true
end
end
function GetDatabaseConnection()
return content["db-host"], content["db-user"], content["db-password"], content["db-name"]
end

return false
function IsLocalStorage()
return content["store-type"] == "LOCAL"
end

function FormatMsg(key, ...)
Expand All @@ -61,8 +56,13 @@ return content
| ---------- | -------------| ------------- | -----------|
| **msg-** | *see above*| *every text* | This are the i18n messages printed by Zeus. You can change them like you want to. Some messages have placeholders (e.g. *{1}*) this placeholders will be replaced by Zeus with values dynamically. You can use them, too but you don't need to. |
| **custom-chat** | false | false / true | *not implemented yet* |
| **store-type** | LOCAL | LOCAL / MYSQL (*not implemented yet*) | The way Zeus stores its data |
| **store-type** | LOCAL | LOCAL / MYSQL | The way Zeus stores its data |
| **dev-mode** | true | false / true | When enabled, every player on the server has all permissions. Zeus will warn you on server start, if the dev mode is enabled |
| **db-host** | localhost | any ip | The hostname of th MySQL database |
| **db-user** | zeus-db | text | The username of the MySQL database |
| **db-password** | this-is-a-safe-pw | text | The password of the MySQL database |
| **db-name** | zeus-db | text | The database name of the MySQL database |
| **db-charset** | utf8mb4 | MariaDB Charsets | The charset of the MySQL database |

## Modules
Modules are the main components of Zeus. Without them, Zeus is only a frame. In Zeus are some default modules which offers only the basics. But with a little bit of work and the knowledge to code in LUA you can create own ones easily.
Expand Down
15 changes: 13 additions & 2 deletions client/ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@
const MOD_WRAPPER = $('#wrapper');
const PLAYER_SELECT = $('#player-select');
const PLAYER_SELECT_LIST = $('#player-select ul');
const COMPONENT_PATTERN = /((?<type>[P|T|N])(\((?<name>[a-zA-Z0-9_-]*)\))?(?<optional>\?)?)/gi;
const COMPONENT_PATTERN = /((?<type>[P|T|N])(\((?<name>[a-zA-Z0-9_\- ]*)\))?(?<optional>\?)?)/gi;
let currentPlayerInput;
let currentModules;
let currentModule;
Expand Down Expand Up @@ -496,7 +496,7 @@
name = "Argument " + (id + 1);
}
if (key === "P") {
return '<input type="text" data-type="P" readonly data-idx="' + id + '" class="player-select form-control"' + (!optional ? ' required' : '') + ' placeholder="' + name + ' (player)' + (!optional ? '' : '?') + '" id="mod-control-' + id + '">';
return '<input type="text" onkeydown="onPlayerKeyDown(this, event)" data-type="P" readonly data-idx="' + id + '" class="player-select form-control"' + (!optional ? ' required' : '') + ' placeholder="' + name + ' (player)' + (!optional ? '' : '?') + '" id="mod-control-' + id + '">';
}
if (key === "T") {
return '<input type="text" data-type="T" data-idx="' + id + '" class="form-control"' + (!optional ? ' required' : '') + ' placeholder="' + name + ' (text)' + (!optional ? '' : '?') + '" id="mod-control-' + id + '">';
Expand All @@ -507,6 +507,11 @@
return '<label>Unknown component token: ' + key + '</label>';
}

function onPlayerKeyDown(sender, event) {
if (event.which === 8 || event.which === 46)
$(sender).val("");
}

function showPlayerSelect(input) {
currentPlayerInput = input;
getPlayers(function (players) {
Expand Down Expand Up @@ -573,6 +578,12 @@
}

function setup(json) {
ADMIN_MODS.html("");
FUN_MODS.html("");
OTHER_MODS.html("");
PERM_MODS.html("");
SPAWN_MODS.html("");
UTILS_MODS.html("");
let modules = JSON.parse(json);
currentModules = modules;
for (let i = 0; i < modules.length; i++) {
Expand Down
49 changes: 38 additions & 11 deletions server/api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local zeus = { _VERSION = "1.0:3" }
local zeus = { _VERSION = "1.1:0" }
local date = require('packages/' .. GetPackageName() .. '/server/libs/date')
local config = require('packages/' .. GetPackageName() .. '/server/io/config')
local storage
Expand Down Expand Up @@ -35,7 +35,6 @@ function zeus.FormatDate(val)
local num = ""
for i = 1, #str do
local c = str:sub(i, i)
print(c)
if c == "y" then
local v = tonumber(num)
if v == nil then
Expand Down Expand Up @@ -127,7 +126,9 @@ function zeus.MakeAdmin(player)
steam = GetPlayerSteamId(player)
end

return storage:MakeAdmin(player, true)
local result = storage:MakeAdmin(player, true)
_G.zeus.RefreshPlayer(player)
return result
end
AddFunctionExport("MakeAdmin", zeus.MakeAdmin);

Expand All @@ -137,7 +138,9 @@ function zeus.RemoveAdmin(player)
steam = GetPlayerSteamId(player)
end

return storage:MakeAdmin(player, false)
local result = storage:MakeAdmin(player, false)
_G.zeus.RefreshPlayer(player)
return result
end
AddFunctionExport("RemoveAdmin", zeus.RemoveAdmin);

Expand Down Expand Up @@ -179,7 +182,9 @@ end
AddFunctionExport("HasGroup", zeus.HasGroup);

function zeus.AddGroup(player, group)
return storage:AddGroup(player, group)
local result = storage:AddGroup(player, group)
_G.zeus.RefreshPlayer(player)
return result
end
AddFunctionExport("AddGroup", zeus.AddGroup);

Expand All @@ -189,7 +194,9 @@ end
AddFunctionExport("GetGroup", zeus.GetGroup);

function zeus.RemoveGroup(player, group)
return storage:RemoveGroup(player, group)
local result = storage:RemoveGroup(player, group)
_G.zeus.RefreshPlayer(player)
return result
end
AddFunctionExport("RemoveGroup", zeus.RemoveGroup);

Expand All @@ -204,27 +211,47 @@ end
AddFunctionExport("CreateGroup", zeus.CreateGroup);

function zeus.DeleteGroup(group)
return storage:DeleteGroup(group)
local result = storage:DeleteGroup(group)
for _, v in pairs(GetAllPlayers()) do
_G.zeus.RefreshPlayer(v)
end
return result
end
AddFunctionExport("DeleteGroup", zeus.DeleteGroup);

function zeus.AddPermission(group, permission)
return storage:AddPermission(group, permission)
local result = storage:AddPermission(group, permission)
for _, v in pairs(GetAllPlayers()) do
if zeus.HasGroup(v, group) then
_G.zeus.RefreshPlayer(v)
end
end
return result
end
AddFunctionExport("AddPermission", zeus.AddPermission);

function zeus.RemovePermission(group, permission)
return storage:RemovePermission(group, permission)
local result = storage:RemovePermission(group, permission)
for _, v in pairs(GetAllPlayers()) do
if zeus.HasGroup(v, group) then
_G.zeus.RefreshPlayer(v)
end
end
return result
end
AddFunctionExport("RemovePermission", zeus.RemovePermission);

function zeus.AddPlayerPermission(player, permission)
return storage:AddPlayerPermission(player, permission)
local result = storage:AddPlayerPermission(player, permission)
_G.zeus.RefreshPlayer(player)
return result
end
AddFunctionExport("AddPlayerPermission", zeus.AddPlayerPermission);

function zeus.RemovePlayerPermission(player, permission)
return storage:RemovePlayerPermission(player, permission)
local result = storage:RemovePlayerPermission(player, permission)
_G.zeus.RefreshPlayer(player)
return result
end
AddFunctionExport("RemovePlayerPermission", zeus.RemovePlayerPermission);

Expand Down
12 changes: 11 additions & 1 deletion server/io/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ content["msg-banned"] = "You are banned: {1}"
content["msg-veh-model-not-exist"] = "[Zeus] The Vehicle Model {1} does not exist!"

content["custom-chat"] = false -- coming soon: mysql
content["store-type"] = "LOCAL" -- coming soon: mysql
content["store-type"] = "LOCAL" -- or MYSQL
content["dev-mode"] = true

content["db-host"] = "localhost"
content["db-user"] = "zeus-db"
content["db-password"] = "this-is-a-safe-pw"
content["db-name"] = "zeus-db"
content["db-charset"] = "utf8mb4"

function GetDatabaseConnection()
return content["db-host"], content["db-user"], content["db-password"], content["db-name"]
end

function IsLocalStorage()
return content["store-type"] == "LOCAL"
end
Expand Down
141 changes: 141 additions & 0 deletions server/io/database.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
local config = require('packages/' .. GetPackageName() .. '/server/io/config')
local database = { _VERSION = "1.1:0", current = nil }

local function IsOpen()
return database.current ~= false
end

local function CreateTable(name)
if IsOpen() then
mariadb_await_query(database.current, name, false)
end
end

function database.Insert(query)
if IsOpen() then
mariadb_await_query(database.current, query, false)
end
end

function HasRows()
local flag = mariadb_get_row_count()

if not flag == false then
flag = mariadb_get_row_count() > 0
end

return flag
end

function database.Exists(query)
if IsOpen() then
local result = mariadb_await_query(database.current, query)
local flag = HasRows()

mariadb_delete_result(result)
return flag
end
return true
end

function database.Get(query, field, def)
local result = mariadb_await_query(database.current, query)

local value = def
if HasRows() then
value = mariadb_get_value_name(1, field)

if value == nil or value == "NULL" then
value = def
end
end

mariadb_delete_result(result)
return value
end

function database.GetTwo(query, field1, field2, def1, def2)
local result = mariadb_await_query(database.current, query)

local value = def1
local value2 = def2
if HasRows() then
value = mariadb_get_value_name(1, field1)
value2 = mariadb_get_value_name(1, field2)
end

mariadb_delete_result(result)
return value, value2
end

function database.Init()
mariadb_log("error")
database.current = mariadb_connect(GetDatabaseConnection())

if IsOpen() then
mariadb_set_charset(database.current, config["db-charset"])

-- TABLES
CreateTable([[CREATE TABLE IF NOT EXISTS `zeus_groups` (
`GroupName` VARCHAR(45) NOT NULL,
`Format` VARCHAR(45) NULL,
PRIMARY KEY (`GroupName`))
ENGINE = InnoDB;]])
CreateTable([[CREATE TABLE IF NOT EXISTS `zeus_groupperms` (
`GroupName` VARCHAR(45) NULL,
`Permission` VARCHAR(45) NULL,
CONSTRAINT `GroupPermission`
FOREIGN KEY (`GroupName`)
REFERENCES `zeus_groups` (`GroupName`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;]])
CreateTable([[CREATE TABLE IF NOT EXISTS `zeus_players` (
`SteamID` VARCHAR(45) NOT NULL,
`GroupName` VARCHAR(45) NULL DEFAULT 'def_group',
`IsAdmin` TINYINT(1) DEFAULT '0',
PRIMARY KEY (`SteamID`),
CONSTRAINT `PlayerGroup`
FOREIGN KEY (`GroupName`)
REFERENCES `zeus_groups` (`GroupName`)
ON DELETE SET NULL
ON UPDATE NO ACTION)
ENGINE = InnoDB;]])
CreateTable([[CREATE TABLE IF NOT EXISTS `zeus_playerperms` (
`Player` VARCHAR(45) NULL,
`Permission` VARCHAR(45) NULL,
CONSTRAINT `PlayerPermission`
FOREIGN KEY (`Player`)
REFERENCES `zeus_players` (`SteamID`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;]])
CreateTable([[CREATE TABLE IF NOT EXISTS `zeus_playerbans` (
`Player` VARCHAR(45) NULL,
`Duration` VARCHAR(45) NULL,
`Reason` VARCHAR(45) NULL,
CONSTRAINT `PlayerBan`
FOREIGN KEY (`Player`)
REFERENCES `zeus_players` (`SteamID`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;]])

if not database.Exists(mariadb_prepare(database.current, "SELECT * FROM zeus_groups WHERE GroupName = '?'", tostring("def_group"))) then
database.Insert(mariadb_prepare(database.current, "INSERT INTO zeus_groups (GroupName, Format) VALUES ('?', '?')", tostring("def_group"), tostring("soon")))
end
end
end

function database.Handle()
return database.current
end

function database.Close()
if IsOpen() then
mariadb_close(database.current)
database.current = false
end
end

return database
7 changes: 5 additions & 2 deletions server/io/storage/local.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,14 @@ function initPlayer(player)
end

function returnSteam(id)
local steamId = id

if IsValidPlayer(id) then
return GetPlayerSteamId(id)
steamId = tostring(GetPlayerSteamId(id))
end

return id
initPlayer(steamId)
return steamId
end

function store:IsAdmin(playerId)
Expand Down
Loading

0 comments on commit 7d4791c

Please sign in to comment.