2020-01-22 14:25:58 +00:00
|
|
|
-- Copyright (c) 2020 David Vogel
|
|
|
|
--
|
|
|
|
-- This software is released under the MIT License.
|
|
|
|
-- https://opensource.org/licenses/MIT
|
|
|
|
|
2016-10-09 16:11:56 +00:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Storage stuff
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
D3stats.Storage = {}
|
2016-10-09 16:11:56 +00:00
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
function D3stats.Storage.Initialize()
|
2016-10-09 16:11:56 +00:00
|
|
|
-- Storage for map data
|
2018-08-03 09:22:18 +00:00
|
|
|
if not sql.TableExists("D3stats_maps") then
|
|
|
|
local result = sql.Query("CREATE TABLE D3stats_maps (name varchar(255) PRIMARY KEY, count INT, wins INT, avg_players REAL)")
|
2016-10-09 16:11:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
function D3stats.Storage.Map_AddOutcome(name, won, players)
|
2016-10-09 16:11:56 +00:00
|
|
|
local count = 1
|
|
|
|
local wins = 0
|
|
|
|
local avg_players = players
|
|
|
|
if won == true then wins = 1 end
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
local result = sql.QueryRow("SELECT count, wins, avg_players FROM D3stats_maps WHERE name = '" .. name .. "'")
|
2016-10-09 16:11:56 +00:00
|
|
|
if result then
|
|
|
|
if result ~= false then
|
2018-08-03 09:22:18 +00:00
|
|
|
count = count + tonumber(result.count)
|
|
|
|
wins = tonumber(result.wins) + 1 * (won and 1 or 0)
|
|
|
|
avg_players = (players + tonumber(result.avg_players)) / 2
|
2016-10-09 16:11:56 +00:00
|
|
|
else
|
|
|
|
Print(sql.LastError())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
local result = sql.Query("INSERT OR REPLACE INTO D3stats_maps (name, count, wins, avg_players) VALUES ('" .. name .. "', " .. tostring(count) .. ", " .. tostring(wins) .. ", " .. tostring(avg_players) .. ");")
|
2016-10-09 16:11:56 +00:00
|
|
|
end
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
function D3stats.Storage.Map_Get(name)
|
2016-10-09 16:11:56 +00:00
|
|
|
local count = 0
|
|
|
|
local wins = 0
|
|
|
|
local avg_players = 0
|
|
|
|
|
2018-08-03 09:22:18 +00:00
|
|
|
local result = sql.QueryRow("SELECT count, wins, avg_players FROM D3stats_maps WHERE name = '" .. name .. "'")
|
2016-10-09 16:11:56 +00:00
|
|
|
if result then
|
|
|
|
if result ~= false then
|
2018-08-03 09:22:18 +00:00
|
|
|
count = tonumber(result.count)
|
|
|
|
wins = tonumber(result.wins)
|
|
|
|
avg_players = tonumber(result.avg_players)
|
2016-10-09 16:11:56 +00:00
|
|
|
else
|
|
|
|
Print(sql.LastError())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return count, wins, avg_players
|
|
|
|
end
|