Skip to content

Commit 68377e4

Browse files
committed
First Commit
1 parent c6dacc5 commit 68377e4

9 files changed

+462
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Water-Flow
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

lua/autorun/_fairselection_loader.lua

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FairSelection = FairSelection or {}
2+
3+
if SERVER then
4+
include("fairselection/sv_init.lua")
5+
include("fairselection/sv_functions.lua")
6+
include("fairselection/sv_database.lua")
7+
include("fairselection/sv_player.lua")
8+
end
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
--[[
2+
MySQLoo Handler for FairSelection
3+
]]
4+
require('mysqloo')
5+
local CONN
6+
7+
-- Initialize database setup
8+
function HANDLER:Init()
9+
if not FairSelection.CFG.DB.Port then
10+
FairSelection.CFG.DB.Port = 3306
11+
elseif not isnumber(FairSelection.CFG.DB.Port) then
12+
FairSelection.CFG.DB.Port = tonumber(FairSelection.CFG.DB.Port)
13+
end
14+
15+
CONN = mysqloo.connect(FairSelection.CFG.DB.Hostname, FairSelection.CFG.DB.Username, FairSelection.CFG.DB.Password, FairSelection.CFG.DB.Database, FairSelection.CFG.DB.Port)
16+
end
17+
18+
-- Connects to the database
19+
function HANDLER:connect()
20+
local function CONN:onConnected()
21+
FairSelection:Message("Database connection established!")
22+
23+
HANDLER:CheckTimer()
24+
end
25+
26+
local function CONN:onConnectionFailed(err)
27+
FairSelection:Error("SQL Error: "..err)
28+
end
29+
30+
CONN:connect()
31+
end
32+
33+
-- Checks if connection is established, if not trying to connect
34+
function HANDLER:CheckTimer()
35+
timer.Create("FairSelectionDatabaseConnectionCheck", 90, 0, function()
36+
if CONN:status() != mysqloo.DATABASE_CONNECTED then
37+
FairSelection:Error("Database connection interrupted!")
38+
FairSelection:Message("Try to reconnect...")
39+
HANDLER:connect()
40+
end
41+
end)
42+
end
43+
44+
function HANDLER:CreateDatabase()
45+
46+
end
47+
48+
function HANDLER:query(sql, callback)
49+
if not callback then callback = nil end
50+
51+
local sql = FairSelection.DB:esPrefix(sql)
52+
local query = CONN:query(sql)
53+
54+
if query != nil then
55+
function query:onSuccess(data)
56+
row = data[1]
57+
58+
if isfunction(callback) then
59+
callback(data)
60+
end
61+
end
62+
63+
function query:onError(err)
64+
FairSelection.DB:sqlError(sql, err)
65+
end
66+
67+
query:start()
68+
query:wait()
69+
end
70+
71+
return row
72+
end
73+
74+
function HANDLER:prepare(sql, args, callback)
75+
if not callback then callback = nil end
76+
77+
local sql = FairSelection.DB:esPrefix(sql)
78+
local query = CONN:prepare(sql)
79+
80+
if query != nil then
81+
function query:onSuccess(data)
82+
row = data[1]
83+
84+
if isfunction(callback) then
85+
callback(data)
86+
end
87+
end
88+
89+
function query:onError(err)
90+
FairSelection.DB:sqlError(sql, err)
91+
end
92+
93+
if not args then
94+
args = nil
95+
else
96+
local count = 1
97+
98+
for _, arg in pairs(args) do
99+
if type(arg) == 'string' then
100+
query:setString(count, arg)
101+
elseif type(arg) == 'number' then
102+
query:setNumber(count, arg)
103+
elseif type(arg) == 'boolean' then
104+
query:setBoolean(count, arg)
105+
else
106+
query:setNull(count)
107+
end
108+
109+
count = count + 1
110+
end
111+
end
112+
113+
query:start()
114+
query:wait()
115+
end
116+
117+
return row
118+
end

lua/fairselection/sv_config.lua

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CONFIG.KarmaIncreaseChance = -- Enables karma increased weight. Set CONFIG.KarmaIncreaseChanceThreshold for the minimum karma needed.
2+
CONFIG.KarmaIncreaseChanceThreshold = 950 -- Minimum karma for giving very little bonus weight to the player. Has a chance to give 0 extra weight. (default 950, based off of default max karma)
3+
4+
CONFIG.DB.Type = "mysqloo"
5+
CONFIG.DB.Hostname = "localhost"
6+
CONFIG.DB.Username = "root"
7+
CONFIG.DB.Password = ""
8+
CONFIG.DB.Database = ""
9+
CONFIG.DB.Prefix = "tttfs_"
10+
CONFIG.DB.Port = 3306

lua/fairselection/sv_database.lua

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FairSelection.DB = FairSelection.DB or {}
2+
local DBHandler = DBHandler or {}
3+
4+
function FairSelection.DB:Init()
5+
FairSelection:Message("Initialize database "..string.lower(FairSelection.CFG.DB.Type))
6+
7+
include("fairselection/database/"..string.lower(FairSelection.CFG.DB.Type))
8+
DBHandler = HANDLER
9+
HANDLER = nil
10+
11+
DBHandler:Init()
12+
end
13+
14+
function FairSelection.DB:sqlError(sql, err)
15+
FairSelection:Message("===============================================================")
16+
FairSelection:Error("Query errored!")
17+
FairSelection:Error("Query: "..sql)
18+
FairSelection:Error("Error: "..err)
19+
FairSelection:Message("===============================================================")
20+
end
21+
22+
function FairSelection.DB:esPrefix(sql)
23+
return string.Replace(sql, "prefix_", FairSelection.CFG.DB.Prefix)
24+
end
25+
26+
function FairSelection.DB:connect()
27+
DBHandler:connect()
28+
end
29+
30+
function FairSelection.DB:query()
31+
DBHandler:query()
32+
end
33+
34+
function FairSelection.DB:prepare()
35+
DBHandler:prepare()
36+
end

lua/fairselection/sv_functions.lua

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function FairSelection:Message(msg)
2+
MsgC(Color(84, 13, 165), "[TTTFairSelection] "..msg.."\n")
3+
end
4+
5+
function FairSelection:Error(msg)
6+
MsgC(Color(84, 13, 165), "[TTTFairSelection] ", Color(179, 15, 15), msg.."\n")
7+
end
8+
9+
function FairSelection:DefaultChance()
10+
return math.random(1, 5)
11+
end
12+
13+
function FairSelection:GetActivePlayerTotalChance()
14+
local total = 0
15+
16+
for _, ply in ipairs(player.GetAll()) do
17+
if IsValid(ply) and (ply:GetChance() ~= nil) and (not ply:IsBot()) then
18+
total = total + ply:GetChance()
19+
end
20+
end
21+
22+
return total
23+
end
24+
25+
function FairSelection:UpdatePlayerChance()
26+
for _, ply in ipairs(player.GetAll()) do
27+
if not ply:IsBot() then
28+
FairSelection.DB:prepare("UPDATE prefix_chances SET chance=?, lastupdate=? WHERE steamid=?", {ply:GetChance(), os.time(), ply:SteamID64()})
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)