rapgod1 Posted June 26, 2016 Share Posted June 26, 2016 Всем привет. Как можно получить данные из базы данных (sqlite) и сравнить их с чем-либо? Например: local acName = getAccountName(getPlayerAccount(client)) if acName == 'здесь данные из определённой таблицы с бд' then Вот структура БД: Link to comment
Kenix Posted June 26, 2016 Share Posted June 26, 2016 Данные всех аккаунтов? getAccounts Link to comment
rapgod1 Posted June 26, 2016 Author Share Posted June 26, 2016 Данные всех аккаунтов? getAccounts Нет, к примеру игрок пишет команду, и мне нужно проверить его ник и приравнять его к нику (значению) из базы данных (не internal) Link to comment
Kenix Posted June 26, 2016 Share Posted June 26, 2016 Пример запроса, где: field1, field2 - Поля, которые нужны тебе после выборки users - Название таблицы, откуда делаем выборку nick - Поле, по которому ведем поиск SELECT field1, field2 FROM users WHERE nick = ? LIMIT 1 Для работы используем db функции ( dbConnect, dbQuery, dbPoll ): https://wiki.multitheftauto.com/wiki/Se ... _functions Урок примера работы с базой данной: https://forum.multitheftauto.com/viewtopic.php?f=153&t=37482 1 Link to comment
rapgod1 Posted June 27, 2016 Author Share Posted June 27, 2016 Пример запроса, где:field1, field2 - Поля, которые нужны тебе после выборки users - Название таблицы, откуда делаем выборку nick - Поле, по которому ведем поиск SELECT field1, field2 FROM users WHERE nick = ? LIMIT 1 Для работы используем db функции ( dbConnect, dbQuery, dbPoll ): https://wiki.multitheftauto.com/wiki/Se ... _functions Урок примера работы с базой данной: https://forum.multitheftauto.com/viewtopic.php?f=153&t=37482 Вроде сделал, но вот одна проблема: если игрок поставит объект, он его не сможет удалить, только после рестарта ресурса (ниже будет видео). Как это можно исправить? server.lua: local db local count = 0 local DoorsTable = {} local DoorsTable1 = {} function onStart() if not fileExists("db/bases.db") then local h = fileCreate("db/bases.db") if h then fileClose(h) outputDebugString("[DayZ] Bases database not found, creating...") end db = dbConnect( "sqlite", "db/bases.db" ) outputDebugString("[DayZ] Inserting tables into database...") dbExec(db, "CREATE TABLE IF NOT EXISTS base_objects(id INT AUTO_INCREMENT, model INT, owner VARCHAR, x FLOAT, y FLOAT, z FLOAT, rx FLOAT, ry FLOAT, rz FLOAT, health FLOAT, encampment VARCHAR)") outputDebugString("[DayZ] Tables inserted.") else db = dbConnect( "sqlite", "db/bases.db" ) local qh = dbQuery( db, "SELECT * FROM base_objects" ) local result = dbPoll( qh, 10000 ) for i, ob in ipairs(result) do local tOb = createObject(ob['model'], ob['x'], ob['y'], ob['z'], ob['rx'], ob['ry'], ob['rz']) setTimer(function() if ob['health'] > 0 then triggerClientEvent("setTheObjectUnbreakable",root,tOb) end end,1000,1,tOb) setElementData(tOb, "bc.creator", ob['owner']) setElementData(tOb, "bc.ID", ob['id']) if ob['model'] == 1900 then setupDoor(tOb,ob['encampment']) elseif ob['model'] == 1895 then setupDoor1(tOb,ob['encampment']) end setElementData(tOb,"object.health",ob['health']) if getElementData(tOb,"object.health") <= 0 then if getElementData(tOb,"parent") then destroyElement(getElementData(tOb,"parent")) end destroyElement(tOb) end count = count + 1 end outputDebugString("Base object loaded. TOTAL: "..tostring(count)) end end addEventHandler("onResourceStart", resourceRoot, onStart) function newObject(model,x,y,z,rx,ry,rz,health) if model and x and y and z and rx and ry and rz then local ob = createObject(model, x, y, z, rx, ry, rz) local acName = getAccountName(getPlayerAccount(client)) setElementData(ob, "bc.creator", acName) setElementData(ob,"object.health",health) triggerClientEvent("setTheObjectUnbreakable",root,ob) local encampment = getElementData(client,"gang") if model == 1900 then setupDoor(ob,encampment) elseif model == 1895 then setupDoor1(ob,encampment) end if ob then local x,y,z = getElementPosition(ob) local rx,ry,rz = getElementRotation(ob) local model = getElementModel(ob) count = count+1 dbExec(db, "INSERT INTO base_objects VALUES (?,?,?,?,?,?,?,?,?,?,?)", count, model, acName, x, y, z, rx, ry, rz, health, tostring(encampment)) end end end addEvent("addon.basecreator:newObject", true) addEventHandler("addon.basecreator:newObject", root, newObject) function onObjectDamage(object,health,id) if object then dbExec(db,'UPDATE base_objects SET health=? WHERE id=?',health,id) end end addEvent("onObjectDamage",true) addEventHandler("onObjectDamage",root,onObjectDamage) function onObjectDestroy(object,id) if object then dbExec(db,"DELETE FROM base_objects WHERE id=?",id) if getElementData(object,"parent") then destroyElement(getElementData(object,"parent")) end destroyElement(object) count = count-1 end end addEvent("onObjectDestroy",true) addEventHandler("onObjectDestroy",root,onObjectDestroy) function onObjectRemove(object,id,ob,owner) local acName = getAccountName(getPlayerAccount(client)) -- local qhh = dbQuery( db, "SELECT * FROM base_objects WHERE owner=?",id,tostring(owner)) -- local result = dbPoll( qhh, -1 ) local queryHandle = dbQuery(db,"SELECT * FROM base_objects WHERE id=?",id) -- ждем результата (сервер в это время простаивает) local resultTable = dbPoll ( queryHandle, -1 ) -- проверим результат if resultTable then -- outputChatBox('Запрос выполнен:') for rowNum, rowData in ipairs(resultTable) do if rowData['owner'] == acName then if object then dbExec(db,"DELETE FROM base_objects WHERE id=?",id) if getElementData(object,"parent") then destroyElement(getElementData(object,"parent")) end destroyElement(object) count = count-1 end else --outputChatBox("Owner: VU5n6h216h") end -- outputChatBox( ' запись '..rowNum..': '..rowData['id']..', '..rowData['owner']..', '..rowData['health']..' '..rowData['encampment']) end end --[[ if (isObjectInACLGroup("user.h37N8XeDh2", aclGetGroup("Admin"))) then if object then dbExec(db,"DELETE FROM base_objects WHERE id=?",id) if getElementData(object,"parent") then destroyElement(getElementData(object,"parent")) end destroyElement(object) count = count-1 end -- end]] end addEvent("onObjectRemove",true) addEventHandler("onObjectRemove",root,onObjectRemove) function setupDoor(object,encampment) local x,y,z = getElementPosition(object) local rx,ry,rz = getElementRotation(object) local col = createColSphere(x,y,z,2) setElementData(object,"parent",col) setElementData(col,"parent",object) DoorsTable[col] = {pos = {x = x,y = y,z = z}, rot= {x = rx,y = ry,z = rz}, col = col, camp = encampment, door = object} end function closeDoorForReal(doorCol) local data = DoorsTable[doorCol] setElementRotation(data.door,data.rot.x,data.rot.y,data.rot.z) end function openDoor(hitElement) local data = DoorsTable[source] if data == nil then return end local rx,ry,rz=getElementRotation(data["door"]) if getElementData(hitElement,"gang") == data["camp"] or data.camp == "false" or not data["camp"] then moveObject(data["door"],2000,data.pos.x,data.pos.y,data.pos.z,0,0,90) setTimer(closeDoor,5000,1,source) end end addEventHandler("onColShapeHit",resourceRoot,openDoor) function closeDoor(doorCol) local data = DoorsTable[doorCol] moveObject(data.door,2000,data.pos.x,data.pos.y,data.pos.z,0,0,-90) setTimer(closeDoorForReal,2050,1,doorCol) end function setupDoor1(object,encampment) local x,y,z = getElementPosition(object) local rx,ry,rz = getElementRotation(object) local col = createColSphere(x,y,z,6) setElementData(object,"parent",col) setElementData(col,"parent",object) DoorsTable1[col] = {pos = {x = x,y = y,z = z}, rot= {x = rx,y = ry,z = rz}, col = col, camp = encampment, door1 = object} end function closeDoorForReal1(doorCol) local data = DoorsTable1[doorCol] setElementRotation(data.door1,data.rot.x,data.rot.y,data.rot.z) end function closeDoor1(doorCol) local data = DoorsTable1[doorCol] moveObject(data.door1,2000,data.pos.x,data.pos.y,data.pos.z,0,0,0) setTimer(closeDoorForReal1,5000,1,doorCol) end function openDoor1(hitElement) local data = DoorsTable1[source] if data == nil then return end local rx,ry,rz=getElementRotation(data["door1"]) if getElementData(hitElement,"gang") == data["camp"] or data.camp == "false" or not data["camp"] then moveObject(data["door1"],2000,data.pos.x,data.pos.y,data.pos.z-10,0,0,0) setTimer(closeDoor1,5000,1,source) end end addEventHandler("onColShapeHit",resourceRoot,openDoor1) Link to comment
nikitafloy Posted June 28, 2016 Share Posted June 28, 2016 Пример запроса, где:field1, field2 - Поля, которые нужны тебе после выборки users - Название таблицы, откуда делаем выборку nick - Поле, по которому ведем поиск SELECT field1, field2 FROM users WHERE nick = ? LIMIT 1 Для работы используем db функции ( dbConnect, dbQuery, dbPoll ): https://wiki.multitheftauto.com/wiki/Se ... _functions Урок примера работы с базой данной: https://forum.multitheftauto.com/viewtopic.php?f=153&t=37482 Вроде сделал, но вот одна проблема: если игрок поставит объект, он его не сможет удалить, только после рестарта ресурса (ниже будет видео). Как это можно исправить? server.lua: local db local count = 0 local DoorsTable = {} local DoorsTable1 = {} function onStart() if not fileExists("db/bases.db") then local h = fileCreate("db/bases.db") if h then fileClose(h) outputDebugString("[DayZ] Bases database not found, creating...") end db = dbConnect( "sqlite", "db/bases.db" ) outputDebugString("[DayZ] Inserting tables into database...") dbExec(db, "CREATE TABLE IF NOT EXISTS base_objects(id INT AUTO_INCREMENT, model INT, owner VARCHAR, x FLOAT, y FLOAT, z FLOAT, rx FLOAT, ry FLOAT, rz FLOAT, health FLOAT, encampment VARCHAR)") outputDebugString("[DayZ] Tables inserted.") else db = dbConnect( "sqlite", "db/bases.db" ) local qh = dbQuery( db, "SELECT * FROM base_objects" ) local result = dbPoll( qh, 10000 ) for i, ob in ipairs(result) do local tOb = createObject(ob['model'], ob['x'], ob['y'], ob['z'], ob['rx'], ob['ry'], ob['rz']) setTimer(function() if ob['health'] > 0 then triggerClientEvent("setTheObjectUnbreakable",root,tOb) end end,1000,1,tOb) setElementData(tOb, "bc.creator", ob['owner']) setElementData(tOb, "bc.ID", ob['id']) if ob['model'] == 1900 then setupDoor(tOb,ob['encampment']) elseif ob['model'] == 1895 then setupDoor1(tOb,ob['encampment']) end setElementData(tOb,"object.health",ob['health']) if getElementData(tOb,"object.health") <= 0 then if getElementData(tOb,"parent") then destroyElement(getElementData(tOb,"parent")) end destroyElement(tOb) end count = count + 1 end outputDebugString("Base object loaded. TOTAL: "..tostring(count)) end end addEventHandler("onResourceStart", resourceRoot, onStart) function newObject(model,x,y,z,rx,ry,rz,health) if model and x and y and z and rx and ry and rz then local ob = createObject(model, x, y, z, rx, ry, rz) local acName = getAccountName(getPlayerAccount(client)) setElementData(ob, "bc.creator", acName) setElementData(ob,"object.health",health) triggerClientEvent("setTheObjectUnbreakable",root,ob) local encampment = getElementData(client,"gang") if model == 1900 then setupDoor(ob,encampment) elseif model == 1895 then setupDoor1(ob,encampment) end if ob then local x,y,z = getElementPosition(ob) local rx,ry,rz = getElementRotation(ob) local model = getElementModel(ob) count = count+1 dbExec(db, "INSERT INTO base_objects VALUES (?,?,?,?,?,?,?,?,?,?,?)", count, model, acName, x, y, z, rx, ry, rz, health, tostring(encampment)) end end end addEvent("addon.basecreator:newObject", true) addEventHandler("addon.basecreator:newObject", root, newObject) function onObjectDamage(object,health,id) if object then dbExec(db,'UPDATE base_objects SET health=? WHERE id=?',health,id) end end addEvent("onObjectDamage",true) addEventHandler("onObjectDamage",root,onObjectDamage) function onObjectDestroy(object,id) if object then dbExec(db,"DELETE FROM base_objects WHERE id=?",id) if getElementData(object,"parent") then destroyElement(getElementData(object,"parent")) end destroyElement(object) count = count-1 end end addEvent("onObjectDestroy",true) addEventHandler("onObjectDestroy",root,onObjectDestroy) function onObjectRemove(object,id,ob,owner) local acName = getAccountName(getPlayerAccount(client)) -- local qhh = dbQuery( db, "SELECT * FROM base_objects WHERE owner=?",id,tostring(owner)) -- local result = dbPoll( qhh, -1 ) local queryHandle = dbQuery(db,"SELECT * FROM base_objects WHERE id=?",id) -- ждем результата (сервер в это время простаивает) local resultTable = dbPoll ( queryHandle, -1 ) -- проверим результат if resultTable then -- outputChatBox('Запрос выполнен:') for rowNum, rowData in ipairs(resultTable) do if rowData['owner'] == acName then if object then dbExec(db,"DELETE FROM base_objects WHERE id=?",id) if getElementData(object,"parent") then destroyElement(getElementData(object,"parent")) end destroyElement(object) count = count-1 end else --outputChatBox("Owner: VU5n6h216h") end -- outputChatBox( ' запись '..rowNum..': '..rowData['id']..', '..rowData['owner']..', '..rowData['health']..' '..rowData['encampment']) end end --[[ if (isObjectInACLGroup("user.h37N8XeDh2", aclGetGroup("Admin"))) then if object then dbExec(db,"DELETE FROM base_objects WHERE id=?",id) if getElementData(object,"parent") then destroyElement(getElementData(object,"parent")) end destroyElement(object) count = count-1 end -- end]] end addEvent("onObjectRemove",true) addEventHandler("onObjectRemove",root,onObjectRemove) function setupDoor(object,encampment) local x,y,z = getElementPosition(object) local rx,ry,rz = getElementRotation(object) local col = createColSphere(x,y,z,2) setElementData(object,"parent",col) setElementData(col,"parent",object) DoorsTable[col] = {pos = {x = x,y = y,z = z}, rot= {x = rx,y = ry,z = rz}, col = col, camp = encampment, door = object} end function closeDoorForReal(doorCol) local data = DoorsTable[doorCol] setElementRotation(data.door,data.rot.x,data.rot.y,data.rot.z) end function openDoor(hitElement) local data = DoorsTable[source] if data == nil then return end local rx,ry,rz=getElementRotation(data["door"]) if getElementData(hitElement,"gang") == data["camp"] or data.camp == "false" or not data["camp"] then moveObject(data["door"],2000,data.pos.x,data.pos.y,data.pos.z,0,0,90) setTimer(closeDoor,5000,1,source) end end addEventHandler("onColShapeHit",resourceRoot,openDoor) function closeDoor(doorCol) local data = DoorsTable[doorCol] moveObject(data.door,2000,data.pos.x,data.pos.y,data.pos.z,0,0,-90) setTimer(closeDoorForReal,2050,1,doorCol) end function setupDoor1(object,encampment) local x,y,z = getElementPosition(object) local rx,ry,rz = getElementRotation(object) local col = createColSphere(x,y,z,6) setElementData(object,"parent",col) setElementData(col,"parent",object) DoorsTable1[col] = {pos = {x = x,y = y,z = z}, rot= {x = rx,y = ry,z = rz}, col = col, camp = encampment, door1 = object} end function closeDoorForReal1(doorCol) local data = DoorsTable1[doorCol] setElementRotation(data.door1,data.rot.x,data.rot.y,data.rot.z) end function closeDoor1(doorCol) local data = DoorsTable1[doorCol] moveObject(data.door1,2000,data.pos.x,data.pos.y,data.pos.z,0,0,0) setTimer(closeDoorForReal1,5000,1,doorCol) end function openDoor1(hitElement) local data = DoorsTable1[source] if data == nil then return end local rx,ry,rz=getElementRotation(data["door1"]) if getElementData(hitElement,"gang") == data["camp"] or data.camp == "false" or not data["camp"] then moveObject(data["door1"],2000,data.pos.x,data.pos.y,data.pos.z-10,0,0,0) setTimer(closeDoor1,5000,1,source) end end addEventHandler("onColShapeHit",resourceRoot,openDoor1) setElementID getElementByID destroyElement Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now