Advertisement
JUNIORCEDE1

Untitled

Nov 5th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.60 KB | None | 0 0
  1. modo = nil
  2. DataGM = {}
  3.  
  4. outputServerLog('saveMJ iniciado')
  5.  
  6. addEventHandler('onResourceStart',root,function(recurso)
  7.     if recurso == getThisResource() then
  8.         for _,v in ipairs(getResources()) do
  9.             if v.state == 'running' and v:getInfo('type') == 'gamemode' then
  10.                 modo = v.name
  11.                 break
  12.             end
  13.         end
  14.         init()
  15.     end
  16.     if recurso:getInfo('type') ~= 'gamemode' then return end
  17.     if recurso.state == 'running' then
  18.         if not DataGM[modo] then
  19.           DataGM[modo] = {}
  20.         end
  21.         SetDataGM(modo)
  22.         modo = recurso.name
  23.         if not DataGM[modo] then
  24.           DataGM[modo] = {}
  25.         end
  26.         SetDataPlayer(modo)
  27.     end
  28. end)
  29.  
  30. function SetDataPlayer(gm)
  31.     for k,player in ipairs (getElementsByType('player')) do
  32.         if DataGM[gm][player] then
  33.             player:setData('mj_victoria',DataGM[gm][player].victorias)
  34.             player:setData('mj_derrotas',DataGM[gm][player].derrotas)
  35.         else
  36.             DataGM[gm][player] = {}
  37.             DataGM[gm][player]['victorias'] = 0
  38.             DataGM[gm][player]['derrotas'] = 0
  39.             player:setData('mj_victoria',0)
  40.             player:setData('mj_derrotas',0)
  41.         end
  42.     end
  43. end
  44.  
  45. function SetDataGM(gm)
  46.     for k,player in ipairs (getElementsByType('player')) do
  47.         if not DataGM[gm][player] then
  48.             DataGM[gm][player] = {}
  49.         end
  50.             DataGM[gm][player]['victorias'] = player:getData('mj_victoria') or 0
  51.             DataGM[gm][player]['derrotas'] = player:getData('mj_derrotas') or 0
  52.     end
  53. end
  54.  
  55. function init()
  56.     local qh = DB.Con:query( "SELECT * FROM statsmj ")
  57.     local result = qh:poll(-1)
  58.     if #result == 0 then
  59.         for k,pj in ipairs(getElementsByType('player')) do
  60.             local id = pj:getData('id_user')
  61.             if id and modo then
  62.                 DB.InsertorUpdateStatsMJ(id,pj.name,modo,pj:getData('mj_victoria') or 0,pj:getData('mj_derrotas') or 0)
  63.             end
  64.         end
  65.     else
  66.         local jugadores = getElementsByType('player');
  67.         if #jugadores == 0 then return end
  68.             for k,l in ipairs(result) do
  69.                 local minijuego = l.minijuego
  70.                 if not DataGM[minijuego] then
  71.                     DataGM[minijuego] = {}
  72.                 end
  73.                 for k,pj in ipairs(jugadores) do
  74.                 local id = pj:getData('id_user')
  75.                 if id and tostring(id) == tostring(l.user_id) then
  76.                     DataGM[minijuego][pj]={}
  77.                     local victorias = l.victorias
  78.                     local derrotas = l.derrotas
  79.                     if modo == minijuego then
  80.                         if pj:getData('mj_victoria') and pj:getData('mj_victoria') > (tonumber(victorias) or 0) then
  81.                             victorias = pj:getData('mj_victoria')
  82.                         end
  83.                         if pj:getData('mj_derrotas') and pj:getData('mj_derrotas') > (tonumber(derrotas) or 0) then
  84.                             derrotas = pj:getData('mj_derrotas')
  85.                         end
  86.                         pj:setData('mj_victoria',victorias)
  87.                         pj:setData('mj_derrotas',derrotas)
  88.                     end
  89.                     DataGM[minijuego][pj]['victorias'] = victorias
  90.                     DataGM[minijuego][pj]['derrotas'] = derrotas
  91.                 end
  92.             end
  93.         end
  94.     end
  95. end
  96.  
  97. function Join()
  98.     if not DataGM[modo] then DataGM[modo] = {} end
  99.     DataGM[modo][source]={}
  100.     DataGM[modo][source]['victorias'] = 0
  101.     DataGM[modo][source]['derrotas'] = 0
  102.     source:setData('mj_victoria',0)
  103.     source:setData('mj_derrotas',0)
  104. end
  105. addEventHandler("onPlayerJoin", root, Join)
  106.  
  107. function login(_,account)
  108.     local cuenta = getAccountName(account)
  109.     local result = nil
  110.     local qh2 = DB.Con:query("SELECT id FROM dataPlayer where accountname = ?",cuenta)
  111.     result = qh2:poll(-1)
  112.     if #result == 0 then
  113.         DB.RegisterUser(source.name,cuenta)
  114.         local qh = DB.Con:query("SELECT id FROM dataPlayer where accountname = ?",cuenta)
  115.         result = qh:poll(-1)
  116.     end
  117.     for k,l in ipairs(result) do
  118.         source:setData('id_user',l.id)
  119.         break
  120.     end
  121.     local id = source:getData('id_user');
  122.     if not id then return end
  123.     local qh = DB.Con:query("SELECT * FROM statsmj where user_id = "..id)
  124.     local result = qh:poll(-1 )
  125.     if result and #result ~= 0 then
  126.         for k,l in ipairs(result) do
  127.             local minijuego = l.minijuego
  128.             if not DataGM[minijuego] then
  129.                 DataGM[minijuego] = {}
  130.             end
  131.             if not DataGM[minijuego][source] then
  132.                 DataGM[minijuego][source]={}
  133.             end
  134.             local victorias = l.victorias
  135.             local derrotas = l.derrotas
  136.             DataGM[minijuego][source]['victorias'] = victorias
  137.             DataGM[minijuego][source]['derrotas'] = derrotas
  138.             if modo == minijuego then
  139.                 source:setData('mj_victoria',victorias)
  140.                 source:setData('mj_derrotas',derrotas)
  141.             end
  142.         end
  143.     end
  144. end
  145. addEventHandler('onPlayerLogin',root,login)
  146.  
  147. function quitLogout()
  148.     local id = source:getData('id_user');
  149.     if not id then return end
  150.     local victthis = source:getData('mj_victoria') or 0
  151.     local derrthis = source:getData('mj_derrotas') or 0
  152.     local name = source.name
  153.     for k,data in pairs(DataGM) do
  154.         if data[source] then
  155.             local vict = data[source]['victorias'] or 0
  156.             local derrotas = data[source]['derrotas'] or 0
  157.             if tostring(k) == modo then
  158.                 vict = victthis
  159.                 derrotas = derrthis
  160.             end
  161.             DB.InsertorUpdateStatsMJ(id,name,tostring(k),vict or 0,derrotas or 0)
  162.         end
  163.     end
  164.     outputDebugString('[MJ-System]Guardando datos de: '..name)
  165.     outputServerLog('[MJ-System]Guardando datos de: '..name)
  166.     source:removeData('mj_victoria')
  167.     source:removeData('mj_derrotas')
  168.     source:removeData('id_user')
  169. end
  170. addEventHandler('onPlayerQuit',root,quitLogout)
  171. addEventHandler('onPlayerLogout',root,quitLogout)
  172.  
  173. function AgregarData(jugador,tipo)
  174.     if tipo == 'victoria' then
  175.         jugador:setData('mj_victoria', (jugador:getData('mj_victoria') or 0 ) + 1)
  176.     elseif tipo == 'derrota' then
  177.         jugador:setData('mj_derrotas', (jugador:getData('mj_derrotas') or 0 ) + 1)
  178.     end
  179. end
  180. addEvent('WinOrLoser',true)
  181. addEventHandler('WinOrLoser',root,AgregarData)
  182.  
  183. --srun getPlayerFromName('Dr.Chapatin'):setData('mj_victoria',10)
  184. --srun getPlayerFromName('Dr.Chapatin'):getData('mj_derrotas')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement