Fryen 0 #1 Posted July 13, 2011 (edited) Gera um gráfico com as mensagens enviadas/recebidas/respondidas a cada mês. Primeira vez que mecho com GD, e o código tá uma merda mesmo. Pra funcionar você tem que baixar o arquivo TXT com todas suas mensagens, é só clicar aqui. Aí edita no começo do script o nome do arquivo e o seu nome de usuário. local file = 'privatemessages-Raphael-06-01-2011.txt' local user = 'Raphael' require('gd') local msgs = {} local marg = string.rep("#", 80) local marg2 = string.rep("=", 80) local marg3 = string.rep("-", 80) local f = "" local i = 2 local s = 0 local ns = 0 local n = 1 -- 0 = start -- 1 = get folder name -- 2 = skip lines -- 3 = msg header -- 4 = msg body for l in io.lines(file) do if s == 4 then if l == marg then s = 1 elseif l == marg2 then msgs[f][n][5] = msgs[f][n][5]:match("(.+)..") n = n + 1 msgs[f][n] = {} s = 3 else msgs[f][n][5] = msgs[f][n][5] .. l .. "\n" end elseif s == 3 then if l == marg3 then s = 4 msgs[f][n][5] = "" else table.insert(msgs[f][n], l:match("^%S+%s:%s+(.+)$")) end elseif s == 2 then i = i - 1 if i == 0 then s = ns end elseif s == 1 then f = l:match("^%S+%s:%s+(.+)$") msgs[f] = {} n = 1 msgs[f][n] = {} s = 2 i = 3 ns = 3 elseif s == 0 then if l == marg then s = 1 end end end local stats = {} local months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} local m = os.date('*t') m = (m.year - 2010) * 12 + m.month - 4 for i = 1, m do stats[i] = {s = 0, r = 0, a = 0} end local s = 0 -- Sent local r = 0 -- Received local a = 0 -- Answered for _, v in pairs(msgs) do for _, k in ipairs(v) do local y, m = k[3]:match("(%d+)%-(%d+)%-.+") local i = (y - 2010) * 12 + m - 4 if k[1] == user then s = s + 1 stats[i].s = stats[i].s + 1 elseif k[2] == user then r = r + 1 stats[i].r = stats[i].r + 1 if (k[4] or ""):match("^Re:.+") then a = a + 1 stats[i].a = stats[i].a + 1 end end end end local testImg = gd.create(200, 50) local img = gd.createTrueColor(m * 70 + 100, 400) local black = img:colorAllocate(0, 0, 0) local white = img:colorAllocate(255, 255, 255) local red = img:colorAllocate(255, 0, 0) local green = img:colorAllocate(0, 255, 0) local blue = img:colorAllocate(0, 0, 255) local mm = 0 img:filledRectangle(0, 0, img:sizeX(), 400, white) img:line(50, 35, 50, 365, black) img:line(35, 350, img:sizeX() - 35, 350, black) for k, v in ipairs(stats) do mm = math.max(v.s, v.r, v.a, mm) end for i = 1, 6 do img:line(45, 350 - i * 50, 55, 350 - i * 50, black) local p = {testImg:stringFT(black, "Tahoma", 10, 0, 0, 0, math.floor(mm / 6 * i))} img:stringFT(black, "Tahoma", 10, 0, 40 - p[3], 355 - i * 50, math.floor(mm / 6 * i)) end for i = 1, m do local p = {testImg:stringFT(black, "Tahoma", 10, 0, 0, 0, months[(i+3)%12+1])} img:stringFT(black, "Tahoma", 10, 0, (i * 70 - 20) + ((70 - p[3]) / 2), 365, months[(i+3)%12+1]) local p = {testImg:stringFT(black, "Tahoma", 10, 0, 0, 0, 2010 + math.floor((i + 4) / 12))} img:stringFT(black, "Tahoma", 10, 0, (i * 70 - 20) + ((70 - p[3]) / 2), 380, 2010 + math.floor((i + 4) / 12)) end for k, v in ipairs(stats) do if k == #stats then break end img:line(85 + (k - 1) * 70, 349 - (v.s / mm * 300), 85 + k * 70, 349 - (stats[k + 1].s / mm * 300), red) end for k, v in ipairs(stats) do if k == #stats then break end img:line(85 + (k - 1) * 70, 349 - (v.r / mm * 300), 85 + k * 70, 349 - (stats[k + 1].r / mm * 300), blue) end for k, v in ipairs(stats) do if k == #stats then break end img:line(85 + (k - 1) * 70, 349 - (v.a / mm * 300), 85 + k * 70, 349 - (stats[k + 1].a / mm * 300), green) end img:png("oi.png") os.execute("oi.png") Exemplo: Enviadas Recebidas Respondidas Edited July 13, 2011 by Fryen Share this post Link to post Share on other sites
Mock 32 #2 Posted July 14, 2011 cara o codigo não esta uma merda, esta MEIO desorganizado mais ainda sim é um code legal. Share this post Link to post Share on other sites
Elwyn 106 #3 Posted July 14, 2011 é como o mock disse, a não está muito legal de ler o code não, mas tá bem legal. Share this post Link to post Share on other sites
Fryen 0 #4 Posted July 18, 2011 cara o codigo não esta uma merda, esta MEIO desorganizado mais ainda sim é um code legal. Foi o que eu quis dizer, bagunçado. Share this post Link to post Share on other sites
iuniX 4 #5 Posted July 18, 2011 Eu gostei bastante, só esta um pouco bagunçado como o Mock disse. (Se o Skyen ver isso ele vai reclamar OPASKDPOSA) Mas parabéns, fico bem legal. Share this post Link to post Share on other sites