Skip to content

Commit

Permalink
Connections to MySQL are checked for executing every query
Browse files Browse the repository at this point in the history
* Use table to maintain timers in `botstats.lua`
* Rewrite `Report` function
  • Loading branch information
hjpotter92 committed Aug 9, 2015
1 parent 2f27539 commit 8e8c191
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 85 deletions.
17 changes: 9 additions & 8 deletions botstats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ function OnStartup()
dofile( tPaths.sExtPath.."stats/polls.lua" )

tUserStats, tBotStats = {}, {}
tConfig.iTimerID1 = TmrMan.AddTimer( 90 * 10^3, "UpdateStats" ) -- Every 90 seconds
tConfig.iTimerID2 = TmrMan.AddTimer( 5 * 60 * 10^3, "UpdateToks" ) -- Every 5 minutes
tConfig.iTimerID3 = TmrMan.AddTimer( 24 * 60 * 60 * 10^3, "Inflation" ) -- Once every day
tConfig.iTimerID4 = TmrMan.AddTimer( 24 * 60 * 60 * 10^3, "GrantAllowance" ) -- Once every day
tConfig.tTimers = {
TmrMan.AddTimer( 90 * 10^3, "UpdateStats" ), -- Every 90 seconds
TmrMan.AddTimer( 5 * 60 * 10^3, "UpdateToks" ), -- Every 5 minutes
TmrMan.AddTimer( 24 * 60 * 60 * 10^3, "Inflation" ), -- Once every day
TmrMan.AddTimer( 24 * 60 * 60 * 10^3, "GrantAllowance" ), -- Once every day
}
local fHelp = io.open( tPaths.sTxtPath..tConfig.sHelpFile, "r" )
tHelp.sHelp = fHelp:read "*a"
fHelp:close()
Expand Down Expand Up @@ -187,10 +189,9 @@ end

function OnExit()
Core.UnregBot( tConfig.tBot.sName )
TmrMan.RemoveTimer( tConfig.iTimerID1 )
TmrMan.RemoveTimer( tConfig.iTimerID2 )
TmrMan.RemoveTimer( tConfig.iTimerID3 )
TmrMan.RemoveTimer( tConfig.iTimerID4 )
for _, iTimerID in ipairs( tConfig.tTimers ) do
TmrMan.RemoveTimer( iTimerID )
end
sqlCon:close()
sqlEnv:close()
end
Expand Down
66 changes: 38 additions & 28 deletions external/info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,30 @@ end

_G.tFunction = {
Connect = function()
local luasql
if not luasql then
luasql = require "luasql.mysql"
end
if not SQLEnv then
local luasql = luasql or require "luasql.mysql"
if not ( SQLEnv and SQLCon ) then
_G.SQLEnv = assert( luasql.mysql() )
_G.SQLCon = assert( SQLEnv:connect(Connection 'latest') )
end
return tFunction.CheckCategory()
end,

Report = function( sErrorCode, iErrorNumber )
local sReturn, sErrorCode = "ERROR (%s#%04d): You should check "..tConfig.sHubFAQ.." for more information.", sErrorCode:upper()
return sReturn:format( sErrorCode, iErrorNumber, sErrorCode, iErrorNumber )
Execute = function( sQuery )
local luasql = luasql or require "luasql.mysql"
if not ( SQLEnv and SQLCon ) then
_G.SQLEnv = assert( luasql.mysql() )
_G.SQLCon = assert( SQLEnv:connect(Connection 'latest') )
end
return assert( SQLCon:execute(sQuery) )
end,

Report = ( function()
local sReturn = ( "ERROR (%%s#%%04d): You should check %s for more information." ):format( tConfig.sHubFAQ )
return function( sErrorCode, iErrorNumber )
return sReturn:format( sErrorCode:upper(), iErrorNumber, sErrorCode:upper(), iErrorNumber )
end
end )(),

CheckBnS = function( sInput )
local tAvailable = { "buy", "sell", "loan", "hire" }
for _, sCategories in ipairs( tAvailable ) do
Expand All @@ -48,8 +56,8 @@ _G.tFunction = {
end,

GetCategories = function()
local tReturn, sCategoryQuery = {}, [[SELECT `name` FROM `ctgtable`]]
local SQLCur = assert( SQLCon:execute(sCategoryQuery) )
local tReturn, sCategoryQuery = {}, [[SELECT `name` FROM `ctgtable`]]
local SQLCur = tFunction.Execute( sCategoryQuery )
local tRow = SQLCur:fetch( {}, "a" )
while tRow do
table.insert( tReturn, tRow.name )
Expand All @@ -61,21 +69,22 @@ _G.tFunction = {

FetchRow = function( sTable, iID )
local sFields, sQuery, tReturn = "msg, nick", "SELECT %s FROM `%s` WHERE id = %d LIMIT 1", {}

if sTable == "requests" or sTable == "suggestions" then
sFields = "`ctg`, "..sFields
elseif sTable == "buynsell" then
sFields = sFields..", CASE `type` WHEN 'B' THEN UPPER('buy') WHEN 'S' THEN UPPER('sell') WHEN 'H' THEN UPPER('hire') WHEN 'L' THEN UPPER('loan') WHEN 'T' THEN UPPER('bought') WHEN 'D' THEN UPPER('sold') END `type`"
end
sQuery = sQuery:format( sFields, sTable, iID )
local SQLCur = assert( SQLCon:execute(sQuery) )
local SQLCur = tFunction.Execute( sQuery )
tReturn = SQLCur:fetch( {}, "a" )
SQLCur:close()
return tReturn
end,

FetchReplies = function( iID )
local sReturn, tTemporary, sQuery = "", {}, ([[SELECT id, nick, msg, dated FROM replies WHERE bns_id = %d ORDER BY id ASC]]):format( iID )
local SQLCur = assert( SQLCon:execute(sQuery) )
local SQLCur = tFunction.Execute( sQuery )
local tRow = SQLCur:fetch( {}, "a" )
if tRow then
while tRow do
Expand All @@ -86,6 +95,7 @@ _G.tFunction = {
else
sReturn = ""
end
SQLCur:close()
return sReturn
end,

Expand Down Expand Up @@ -154,7 +164,7 @@ _G.tInfobot = {
end
if sTable == "suggestions" then
sFields = "`ctg`, "..sFields
SQLCur = assert( SQLCon:execute(sReadQuery:format( sFields, sTable, iLimit )) )
SQLCur = tFunction.Execute( sReadQuery:format( sFields, sTable, iLimit ))
local tRow = SQLCur:fetch( {}, "a" )
while tRow do
table.insert( tTemporary, sEntry:format(tRow.id, ("[%s] - %s"):format(tRow.ctg, tRow.msg), tRow.nick, tRow.dated) )
Expand All @@ -164,7 +174,7 @@ _G.tInfobot = {
elseif sTable == "requests" then
sFields = sFields:gsub( "`msg`", "CASE `filled` WHEN 'Y' THEN CONCAT(`msg`, ' (Filled by ', `filledby`, ' on ', filldate, ')') WHEN 'C' THEN CONCAT(`msg`, ' (Closed by ', `filledby`, ' on ', filldate, ')') WHEN 'N' THEN `msg` END `msg`" )..", `ctg`, CASE `filled` WHEN 'Y' THEN UPPER('filled') WHEN 'N' THEN UPPER('empty') WHEN 'C' THEN UPPER('closed') END `filled`"
sReadQuery = sReadQuery:format( sFields, sTable, iLimit )
SQLCur = assert( SQLCon:execute(sReadQuery) )
SQLCur = tFunction.Execute( sReadQuery )
local tRow = SQLCur:fetch( {}, "a" )
while tRow do
table.insert( tTemporary, sEntry:format(tRow.id, ("[%s] [%s] - %s"):format(tRow.filled, tRow.ctg, tRow.msg), tRow.nick, tRow.dated) )
Expand All @@ -174,22 +184,21 @@ _G.tInfobot = {
elseif sTable == "buynsell" then
sFields = sFields..", CASE `type` WHEN 'B' THEN UPPER('buy') WHEN 'S' THEN UPPER('sell') WHEN 'H' THEN UPPER('hire') WHEN 'L' THEN UPPER('loan') WHEN 'T' THEN UPPER('bought') WHEN 'D' THEN UPPER('sold') END `type`"
sReadQuery = sReadQuery:format( sFields, sTable, iLimit )
SQLCur = assert( SQLCon:execute(sReadQuery) )
SQLCur = tFunction.Execute( sReadQuery )
local tRow = SQLCur:fetch( {}, "a" )
while tRow do
table.insert( tTemporary, sEntry:format(tRow.id, ("[%s] - %s"):format(tRow.type, tRow.msg), tRow.nick, tRow.dated)..tFunction.FetchReplies(tRow.id) )
tRow = SQLCur:fetch( {}, "a" )
end

else
SQLCur = assert( SQLCon:execute(sReadQuery:format( sFields, sTable, iLimit )) )
SQLCur = tFunction.Execute( sReadQuery:format( sFields, sTable, iLimit ))
local tRow = SQLCur:fetch( {}, "a" )
while tRow do
table.insert( tTemporary, sEntry:format(tRow.id, tRow.msg, tRow.nick, tRow.dated) )
tRow = SQLCur:fetch( {}, "a" )
end
end

return ( sReturnList..table.concat(tTemporary, "\n") )
end,

Expand All @@ -209,8 +218,8 @@ _G.tInfobot = {
local sQuery = [[INSERT IGNORE INTO `%s`(%s)
VALUES (%s) ]]
sQuery = sQuery:format( tInput.sTable, sFields, sValues )
local SQLCur = assert( SQLCon:execute(sQuery) )
if type(SQLCur) ~= "number" then
local SQLCur = tFunction.Execute( sQuery )
if type( SQLCur ) ~= "number" then
SQLCur:close()
else
SQLCur = nil
Expand All @@ -225,21 +234,22 @@ _G.tInfobot = {
end
local sStorageQuery = [[INSERT INTO messages(message, `from`, `to`, dated)
VALUES ( '%s', '%s', '%s', NOW() ) ]]

sStorageQuery = sStorageQuery:format( SQLCon:escape(sMessage), SQLCon:escape(sSender), SQLCon:escape(sRecipient) )
local SQLCur = assert( SQLCon:execute(sStorageQuery) )
local SQLCur = tFunction.Execute( sStorageQuery )
return SQLCon:getlastautoid()
end,

del = function( tUser, tInput )
if tInput.sTable == "buynsell" then
local sDeleteQuery = string.format( "DELETE b.*, r.* FROM `buynsell` b LEFT JOIN `replies` r ON r.`bns_id` = b.`id` WHERE b.`id` = %d", tInput.iID )
local SQLCur = assert( SQLCon:execute(sDeleteQuery) )
if type(SQLCur) ~= "number" then SQLCur:close() end
local SQLCur = tFunction.Execute( sDeleteQuery )
if type( SQLCur ) ~= "number" then SQLCur:close() end
return true
end
local sDeleteQuery = string.format( "DELETE FROM `%s` WHERE `id` = %d", SQLCon:escape(tInput.sTable), tInput.iID )
local SQLCur = assert( SQLCon:execute(sDeleteQuery) )
if type(SQLCur) ~= "number" then SQLCur:close() end
local SQLCur = tFunction.Execute( sDeleteQuery )
if type( SQLCur ) ~= "number" then SQLCur:close() end
return true
end,

Expand All @@ -251,8 +261,8 @@ _G.tInfobot = {
WHERE id = %d
LIMIT 1]]
sUpdateQuery = sUpdateQuery:format( (bClosure and 'C') or 'Y', SQLCon:escape(tUser.sNick), iID )
local SQLCur = assert( SQLCon:execute(sUpdateQuery) )
if type(SQLCur) ~= "number" then
local SQLCur = tFunction.Execute( sUpdateQuery )
if type( SQLCur ) ~= "number" then
SQLCur:close()
else
SQLCur = nil
Expand All @@ -266,8 +276,8 @@ _G.tInfobot = {
WHERE `id` = %d
LIMIT 1]]
sUpdateQuery = sUpdateQuery:format( iID )
local SQLCur = assert( SQLCon:execute(sUpdateQuery) )
if type(SQLCur) ~= "number" then
local SQLCur = tFunction.Execute( sUpdateQuery )
if type( SQLCur ) ~= "number" then
SQLCur:close()
else
SQLCur = nil
Expand Down
Loading

0 comments on commit 8e8c191

Please sign in to comment.