From a0febf968431576ff7413e992c8c7c7cbee84946 Mon Sep 17 00:00:00 2001 From: BrunoMine Date: Sun, 15 Jul 2018 23:37:44 -0300 Subject: [PATCH] Melhorias --- init.lua | 4 ++ lib/tror.lua | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++ sfinv.lua | 6 +++ shop.lua | 101 ++++++++++++++++++++++++++++++++++ 4 files changed, 261 insertions(+) create mode 100644 lib/tror.lua diff --git a/init.lua b/init.lua index 9226b15..8574be1 100644 --- a/init.lua +++ b/init.lua @@ -27,12 +27,16 @@ notificar("Carregando scripts...") -- Criação do banco de dados xpro.bd = dofile(modpath.."/lib/memor.lua") +-- Plucar itens "PLUS" +xpro.tror = dofile(modpath.."/lib/tror.lua") + -- Variaveis personalizaveis xpro.var = {} -- API dofile(modpath.."/api.lua") dofile(modpath.."/ligas.lua") +dofile(modpath.."/shop.lua") dofile(modpath.."/sfinv.lua") dofile(modpath.."/progresso.lua") dofile(modpath.."/ranking.lua") diff --git a/lib/tror.lua b/lib/tror.lua new file mode 100644 index 0000000..2bb0a4b --- /dev/null +++ b/lib/tror.lua @@ -0,0 +1,150 @@ +--[[ + Lib Tror para Minetest + Copyright (C) 2017 BrunoMine (https://github.com/BrunoMine) + + Recebeste uma cópia da GNU Lesser General + Public License junto com esse software, + se não, veja em . + + Operações de troca de itens + ]] + +local tror = {} + +-- Verificar viabilidade de uma troca +tror.verificar = function(player, item_rem, item_add) + + if not player or not item_add or not item_rem then + minetest.log("error", "[Tror] Faltou dados em (em tror.verificar)") + return false + end + + local inv = player:get_inventory() + + -- Verificar jogador + if not inv:contains_item("main", item_rem) then + return 1 -- Jogador nao pode pagar + elseif not inv:room_for_item("main", item_add) then + return 2 -- Jogador com inventario lotado + end + + return true +end + +-- Realizar uma troca entre com um jogador +tror.trocar = function(player, item_rem, item_add, msg) + + if not player or not item_add or not item_rem then + minetest.log("error", "[Tror] Faltou dados em (em tror.trocar)") + return false + end + + + local v = tror.verificar(player, item_rem, item_add) + if v ~= true then + if v == 1 then + if msg and msg.ins then + minetest.chat_send_player(player:get_player_name(), msg.ins) + end + return 1 + elseif v == 2 then + if msg and msg.lot then + minetest.chat_send_player(player:get_player_name(), msg.lot) + end + return 2 + else + minetest.log("error", "[Tror] Resultado inesperado em tror.trocar (v = "..dump(v)..")") + return false + end + end + + local inv = player:get_inventory() + + -- Retirar itens do inventario + local i = string.split(item_rem, " ") + local n = i[2] or 1 + i = i[1] + for r=1, tonumber(n) do -- 1 eh o tanto que quero tirar + inv:remove_item("main", i) -- tira 1 por vez + end + + inv:add_item("main", item_add) + + + if msg and msg.ok then + minetest.chat_send_player(player:get_player_name(), msg.ok) + end + return true + +end + +-- Realizar uma troca com um jogador +--[[ + Retorna false caso o jogador nao tenha os itens requisitados + e dropa no local todos os itens que não couberem no inventario + ]] +tror.trocar_plus = function(player, item_rem, item_add) + + if not player then + minetest.log("error", "[Tror] Faltou player (em tror.trocar_plus)") + return false + end + + if item_rem and not item_rem[1] then + minetest.log("error", "[Tror] argumento item_rem invalido (em tror.trocar_plus)") + return false + end + + if item_add and not item_add[1] then + minetest.log("error", "[Tror] argumento item_add invalido (em tror.trocar_plus)") + return false + end + + local pos = player:getpos() + + local inv = player:get_inventory() + + -- Verificar se o jogador possui os itens requisitados + local possui = true + if item_rem ~= nil then + for _,item in ipairs(item_rem) do + if not inv:contains_item("main", item) then + possui = false + break + end + end + end + + -- Retorna false por jogador não ter os itens requisitados + if possui == false then + return false + end + + -- Retira itens do jogador + if item_rem ~= nil then + for _,item in ipairs(item_rem) do + local i = string.split(item, " ") + local n = i[2] or 1 + i = i[1] + for r=1, tonumber(n) do -- 1 eh o tanto que quero tirar + inv:remove_item("main", i) -- tira 1 por vez + end + end + end + + -- Transfere todos os itens ao jogador (e dropa os que nao couberem no inventario) + local dropou = false + for _,item in ipairs(item_add) do + if inv:room_for_item("main", item) then + inv:add_item("main", item) + else + dropou = true + minetest.env:add_item({x = pos.x + math.random() * 2 - 1, y = pos.y+1, z = pos.z + math.random() * 2 - 1}, item) + end + end + + return true + +end + +return tror diff --git a/sfinv.lua b/sfinv.lua index 64d43f6..d575edb 100644 --- a/sfinv.lua +++ b/sfinv.lua @@ -22,6 +22,8 @@ sfinv.register_page("xpro:info", { local my_xp = xpro.get_player_xp(name) local my_lvl = xpro.get_player_lvl(name) + local xp_gasto = player:get_attribute("xpro_xp_gasto") + local xp_disp = my_xp-xp_gasto -- Calcula progresso da barra local progresso = 1 @@ -33,6 +35,8 @@ sfinv.register_page("xpro:info", { local formspec = "label[0,0;Nivel "..my_lvl.."]" .."label[0,0.5;Pontos: "..my_xp.."]" + .."label[0,1;Cash XP: "..xp_disp.."]" + .."button[0,1.5;3,1;shop;Loja de Premios]" .."button[0,2.5;3,1;ranking;Ranking Global]" -- Liga @@ -49,6 +53,8 @@ sfinv.register_page("xpro:info", { on_player_receive_fields = function(self, player, context, fields) if fields.ranking then minetest.show_formspec(player:get_player_name(), "xpro:ranking", xpro.ranking_formspec) + elseif fields.shop then + xpro.acessar_shop(player:get_player_name()) end end, diff --git a/shop.lua b/shop.lua index b0de9de..49a13a0 100644 --- a/shop.lua +++ b/shop.lua @@ -11,10 +11,111 @@ local S = xpro.S +-- Assegurar dados +minetest.register_on_joinplayer(function(player) + if not player:get_attribute("xpro_xp_gasto") then + player:set_attribute("xpro_xp_gasto", 0) + end +end) +-- Controle de acessos +local acessos = {} +minetest.register_on_joinplayer(function(player) + acessos[player:get_player_name()] = {} +end) +minetest.register_on_leaveplayer(function(player) + acessos[player:get_player_name()] = nil +end) +-- Itens a venda +xpro.premios = { + {name="Terra", item="default:dirt", qtd=20, custo=5000}, + {name="Pedregulho", item="default:cobble", qtd=25, custo=50}, +} +-- Lista de itens do menu do shop em formato de string +local string_menu_shop = "" +local atualizar_string_menu_shop = function() + for _,d in pairs(xpro.premios) do + if string_menu_shop ~= "" then string_menu_shop = string_menu_shop .. "," end + string_menu_shop = string_menu_shop .. d.name + end +end +atualizar_string_menu_shop() + +-- Acessar shop +xpro.acessar_shop = function(name, aviso) + + local player = minetest.get_player_by_name(name) + local acesso = acessos[name] + local xp_gasto = player:get_attribute("xpro_xp_gasto") + local my_xp = xpro.get_player_xp(name) + local xp_disp = my_xp-xp_gasto + + + local formspec = "size[8,5]" + ..default.gui_bg + ..default.gui_bg_img + .."label[0,0;Loja de Itens por XP]" + .."label[0,0.5;Cash XP: "..xp_disp.."]" + .."textlist[3.2,0;4.5,5;menu;"..string_menu_shop.."]" + + if aviso then + formspec = formspec .. "textarea[0.36,1.1;3.2,1;;"..aviso..";]" + end + + -- Nenhum item escolhido + if acesso.escolha == nil then + formspec = formspec .. "label[0,2;Escolha um Item]" + + -- Exibir item escolhido + else + local escolha = xpro.premios[acesso.escolha] + formspec = formspec .. "label[0,2;Custo: "..escolha.custo.."XP]" + .."label[0,2.5;Unidades: "..escolha.qtd.."]" + .."item_image_button[0,3;2.1,2.1;"..escolha.item..";comprar;Comprar]" + end + + minetest.show_formspec(name, "xpro:shop", formspec) +end + +-- Receber Botoes +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname == "xpro:shop" then + + -- Escolher um item + if fields.menu then + local name = player:get_player_name() + local n = string.split(fields.menu, ":") + acessos[name].escolha = tonumber(n[2]) or 1 + + xpro.acessar_shop(name) + + -- Comprar + elseif fields.comprar then + local name = player:get_player_name() + local acesso = acessos[name] + local escolha = xpro.premios[acesso.escolha] + local my_xp = xpro.get_player_xp(name) + local xp_gasto = player:get_attribute("xpro_xp_gasto") + local xp_disp = my_xp-xp_gasto + + -- Tenta trocar + if xp_disp < escolha.custo then + xpro.acessar_shop(name, "Cash XP insuficiente") + else + if xpro.tror.trocar_plus(player, nil, {escolha.item.." "..escolha.qtd}) == false then + xpro.acessar_shop(name, "Inventario lotado") + else + player:set_attribute("xpro_xp_gasto", (xp_gasto+escolha.custo)) + xpro.acessar_shop(name, "Adiquirido") + end + end + end + end +end) +