Skip to content

Commit 3c7adc1

Browse files
authored
Merge pull request #1 from Water-Flow/master
2 parents 68377e4 + d983134 commit 3c7adc1

File tree

6 files changed

+60
-50
lines changed

6 files changed

+60
-50
lines changed

lua/autorun/_fairselection_loader.lua

+8
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,12 @@ if SERVER then
55
include("fairselection/sv_functions.lua")
66
include("fairselection/sv_database.lua")
77
include("fairselection/sv_player.lua")
8+
9+
FairSelection:Message("Initialization...")
10+
FairSelection:VersionCheck()
11+
FairSelection:LoadConfig()
12+
FairSelection:Message("Config loaded.")
13+
FairSelection.DB:Init()
14+
FairSelection.DB:connect()
15+
FairSelection:Message("Initialization completed!")
816
end

lua/fairselection/database/mysqloo.lua

+10-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ end
1717

1818
-- Connects to the database
1919
function HANDLER:connect()
20-
local function CONN:onConnected()
20+
function CONN:onConnected()
2121
FairSelection:Message("Database connection established!")
22-
2322
HANDLER:CheckTimer()
2423
end
2524

26-
local function CONN:onConnectionFailed(err)
25+
function CONN:onConnectionFailed(err)
2726
FairSelection:Error("SQL Error: "..err)
2827
end
2928

@@ -42,13 +41,17 @@ function HANDLER:CheckTimer()
4241
end
4342

4443
function HANDLER:CreateDatabase()
45-
44+
self:prepare("SELECT count(*) FROM information_schema.tables WHERE table_name=?", {FairSelection.CFG.DB.Prefix.."chances"}, function(data)
45+
if table.Count(data) <= 0 then
46+
self:query("CREATE TABLE `prefix_chances` ( `steamid` bigint(19) NOT NULL, `chance` int(11) DEFAULT NULL, `lastupdate` bigint(19) DEFAULT NULL, PRIMARY KEY (`steamid`), UNIQUE KEY `steamid` (`steamid`))")
47+
end
48+
end)
4649
end
4750

4851
function HANDLER:query(sql, callback)
4952
if not callback then callback = nil end
5053

51-
local sql = FairSelection.DB:esPrefix(sql)
54+
sql = FairSelection.DB:esPrefix(sql)
5255
local query = CONN:query(sql)
5356

5457
if query != nil then
@@ -73,8 +76,8 @@ end
7376

7477
function HANDLER:prepare(sql, args, callback)
7578
if not callback then callback = nil end
76-
77-
local sql = FairSelection.DB:esPrefix(sql)
79+
80+
sql = FairSelection.DB:esPrefix(sql)
7881
local query = CONN:prepare(sql)
7982

8083
if query != nil then

lua/fairselection/sv_config.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CONFIG.KarmaIncreaseChance = -- Enables karma increased weight. Set CONFIG.KarmaIncreaseChanceThreshold for the minimum karma needed.
1+
CONFIG.KarmaIncreaseChance = false -- Enables karma increased weight. Set CONFIG.KarmaIncreaseChanceThreshold for the minimum karma needed.
22
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)
33

44
CONFIG.DB.Type = "mysqloo"

lua/fairselection/sv_database.lua

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ FairSelection.DB = FairSelection.DB or {}
22
local DBHandler = DBHandler or {}
33

44
function FairSelection.DB:Init()
5-
FairSelection:Message("Initialize database "..string.lower(FairSelection.CFG.DB.Type))
5+
FairSelection:Message("Initialize database "..FairSelection.CFG.DB.Type)
66

7-
include("fairselection/database/"..string.lower(FairSelection.CFG.DB.Type))
7+
HANDLER = {}
8+
include("fairselection/database/"..string.lower(FairSelection.CFG.DB.Type)..".lua")
89
DBHandler = HANDLER
9-
HANDLER = nil
1010

1111
DBHandler:Init()
1212
end
@@ -25,12 +25,13 @@ end
2525

2626
function FairSelection.DB:connect()
2727
DBHandler:connect()
28+
DBHandler:CreateDatabase()
2829
end
2930

30-
function FairSelection.DB:query()
31-
DBHandler:query()
31+
function FairSelection.DB:query(sql, callback)
32+
DBHandler:query(sql, callback)
3233
end
3334

34-
function FairSelection.DB:prepare()
35-
DBHandler:prepare()
35+
function FairSelection.DB:prepare(sql, args, callback)
36+
DBHandler:prepare(sql, args, callback)
3637
end

lua/fairselection/sv_functions.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ end
2424

2525
function FairSelection:UpdatePlayerChance()
2626
for _, ply in ipairs(player.GetAll()) do
27-
if not ply:IsBot() then
27+
if ply:IsTerror() not ply:IsBot() then
2828
FairSelection.DB:prepare("UPDATE prefix_chances SET chance=?, lastupdate=? WHERE steamid=?", {ply:GetChance(), os.time(), ply:SteamID64()})
2929
end
3030
end

lua/fairselection/sv_init.lua

+32-34
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
FairSelection.Version = "1.0.0"
22

3-
FairSelection:Message("Initialization...")
4-
FairSelection:VersionCheck()
5-
FairSelection:LoadConfig()
6-
FairSelection:Message("Config loaded.")
7-
FairSelection.DB:Init()
8-
FairSelection.DB:connect()
9-
FairSelection:Message("Initialization completed!")
10-
113
function FairSelection:LoadConfig()
12-
local CONFIG = {}
4+
CONFIG = {}
5+
CONFIG.DB = {}
136

147
include("fairselection/sv_config.lua")
158

169
FairSelection.CFG = CONFIG
1710
end
1811

1912
function FairSelection:VersionCheck()
20-
local version
21-
local msg = nil
22-
23-
http.Fetch("", function(body) version = body end, function() FairSelection:Error("Could not check for new version!") end)
24-
25-
local major, minor, patch = version:match("(%d+)%.(%d+)%.(%d+)")
26-
local curmajor, curminor, curpatch = FairSelection.Version:match("(%d+)%.(%d+)%.(%d+)")
27-
28-
major = tonumber(major) or 0
29-
minor = tonumber(minor) or 0
30-
patch = tonumber(patch) or 0
31-
32-
curmajor = tonumber(curmajor) or 0
33-
curminor = tonumber(curminor) or 0
34-
curpatch = tonumber(curpatch) or 0
35-
36-
if curmajor < major or curminor < minor or curpatch < patch then
37-
msg = "A new Version is available (%%%%%%%%)"
38-
end
13+
http.Fetch("https://raw.githubusercontent.com/Water-Flow/TTTFairTraitorSelection/release/VERSION", function(body)
14+
local msg = nil
15+
local version = "0.0.0"
16+
version = body
17+
local major, minor, patch = version:match("(%d+)%.(%d+)%.(%d+)")
18+
local curmajor, curminor, curpatch = FairSelection.Version:match("(%d+)%.(%d+)%.(%d+)")
19+
20+
major = tonumber(major) or 0
21+
minor = tonumber(minor) or 0
22+
patch = tonumber(patch) or 0
23+
24+
curmajor = tonumber(curmajor) or 0
25+
curminor = tonumber(curminor) or 0
26+
curpatch = tonumber(curpatch) or 0
27+
28+
if (curmajor < major) or (curminor < minor) or (curpatch < patch) then
29+
msg = "A new Version is available (%%%%%%%%)!"
30+
else
31+
msg = "Version: (%%%%%%%%)"
32+
end
3933

40-
if msg then
41-
msg = string.gsub(msg, "%%+", version)
42-
FairSelection:Message(msg)
43-
end
34+
if msg then
35+
msg = string.gsub(msg, "%%+", version)
36+
FairSelection:Message(msg)
37+
end
38+
end,
39+
function()
40+
FairSelection:Error("Could not check for new version!")
41+
end)
4442
end
4543

4644
math.randomseed(os.time())
@@ -146,7 +144,7 @@ function FairSelection:SelectRoles(ts, ds, traitor_count, det_count, choices, pr
146144

147145
for _, v in pairs(choices) do
148146
if IsValid(v) and (not v:IsSpec()) and v:GetRole() == ROLE_INNOCENT then
149-
if FairSelection.CFG.KarmaIncreaseChance and v:GetBaseKarma > FairSelection.CFG.KarmaIncreaseChanceThreshold then
147+
if FairSelection.CFG.KarmaIncreaseChance and v:GetBaseKarma() > FairSelection.CFG.KarmaIncreaseChanceThreshold then
150148
local extra = math.random(0, 2)
151149
v:AddChance(extra)
152150
end
@@ -165,7 +163,7 @@ function FairSelection:SelectRoles(ts, ds, traitor_count, det_count, choices, pr
165163
end
166164

167165
hook.Add("PlayerInitialSpawn", "TTFS_PlayerInitialSpawn", function(ply)
168-
if not ply:IsBot() then
166+
if ply:IsTerror() and not ply:IsBot() then
169167
FairSelection.DB:prepare("SELECT chance FROM prefix_chances WHERE steamid=?", {ply:SteamID64()}, function(data)
170168
if table.Count(data) > 0 then
171169
ply:SetChance(data[1].chance)

0 commit comments

Comments
 (0)