JUNIORCEDE1

Untitled

Nov 5th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.79 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 = dbQuery( DB.Con, "SELECT * FROM statsmj ")
  57.     local result = dbPoll( qh, -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.                 outputDebugString(tostring(l.user_id))
  70.                 outputDebugString(tostring(l.minijuego))
  71.                 local minijuego = l.minijuego
  72.                 if not DataGM[minijuego] then
  73.                     DataGM[minijuego] = {}
  74.                 end
  75.                 for k,pj in ipairs(jugadores) do
  76.                 local id = pj:getData('id_user')
  77.                 if id and tostring(id) == tostring(l.user_id) then
  78.                     DataGM[minijuego][pj]={}
  79.                     local victorias = l.victorias
  80.                     local derrotas = l.derrotas
  81.                     if pj:getData('mj_victoria')and pj:getData('mj_victoria') > (tonumber(victorias) or 0) then
  82.                         victorias = pj:getData('mj_victoria')
  83.                     end
  84.                     if pj:getData('mj_derrotas')and pj:getData('mj_derrotas') > (tonumber(derrotas) or 0) then
  85.                         derrotas = pj:getData('mj_derrotas')
  86.                     end
  87.                     DataGM[minijuego][pj]['victorias'] = victorias
  88.                     DataGM[minijuego][pj]['derrotas'] = derrotas
  89.                     pj:setData('mj_victoria',l.victorias)
  90.                     pj:setData('mj_derrotas',l.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.     outputDebugString(cuenta)
  110.     local result = nil
  111.     local qh2 = dbQuery( DB.Con, "SELECT id FROM dataPlayer where accountname = ?",cuenta)
  112.     result = dbPoll( qh2, -1 )
  113.     if #result == 0 then
  114.         DB.RegisterUser(source.name,cuenta)
  115.         local qh = dbQuery( DB.Con, "SELECT id FROM dataPlayer where accountname = ?",cuenta)
  116.         result = dbPoll( qh, -1 )
  117.     end
  118.     for k,l in ipairs(result) do
  119.         source:setData('id_user',l.id)
  120.         break
  121.     end
  122. end
  123. addEventHandler('onPlayerLogin',root,login)
  124.  
  125. function quitLogout()
  126.     local id = source:getData('id_user');
  127.     if not id then return end
  128.     outputServerLog('[MJ-System]Guardando Datos de jugador')
  129.     outputDebugString('[MJ-System]Guardando Datos de jugador')
  130.     local victthis = source:getData('mj_victoria') or 0
  131.     local derrthis = source:getData('mj_derrotas') or 0
  132.     local name = source.name
  133.     for k,data in pairs(DataGM) do
  134.         if data[source] then
  135.             local vict = data[source]['victorias'] or 0
  136.             local derrotas = data[source]['derrotas'] or 0
  137.             if tostring(k) == modo then
  138.                 vict = victthis
  139.                 derrotas = derrthis
  140.             end
  141.             outputDebugString(derrotas)
  142.             DB.InsertorUpdateStatsMJ(id,name,tostring(k),vict,derrotas)
  143.         end
  144.     end
  145. end
  146. addEventHandler('onPlayerQuit',root,quitLogout)
  147. addEventHandler('onPlayerLogout',root,quitLogout)
  148.  
  149. function AgregarData(jugador,tipo)
  150.     if tipo == 'victoria' then
  151.         jugador:setData('victorias', (jugador:getData('') or 0 ) + 1)
  152.     elseif tipo == 'derrota' then
  153.         jugador:setData('derrotas', (jugador:getData('') or 0 ) + 1)
  154.     end
  155. end
  156.  
  157. --srun getPlayerFromName('Dr.Chapatin'):setData('mj_victoria',10)
  158. --srun getPlayerFromName('Dr.Chapatin'):getData('mj_victoria')
Advertisement
Add Comment
Please, Sign In to add comment