function tasks.check_xp(map_id, needed_xp)
   if needed_xp <= 0 then
      lobby.team_win(map_id)
   end
end

function tasks.add_xp(pos, node, puncher, swap_to) -- This function adds xp and swaps the task node.
   local player_attributes = puncher:get_meta()
   local mode = player_attributes:get_string('mode')
   local meta = minetest.get_meta(pos)
   local earned_xp = meta:get_int('xp') or 1
   local wield = puncher:get_wielded_item()
   local wield_name = wield:get_name()
   if wield_name ~= 'creative:tool_breaking' then
      local name = puncher:get_player_name()
      local timer = minetest.get_node_timer(pos)
      local min = math.max(meta:get_int('time_min'), 5)
      local max = math.max(meta:get_int('time_max'), 20)
      local random_number = math.random(min,max)
      timer:start(random_number)
      local map_id = lobby.game[name]
      local traitor = lobby.traitors[map_id]
      minetest.swap_node(pos, {name = swap_to, param2 = node.param2})
      meta:set_string('formspec', '')
      if map_id ~= 'lobby' then
         if name ~= traitor then
            if mode == 'ghost' then
               earned_xp = math.floor(earned_xp/2)
               map_id = string.sub(map_id, 0, -7)
            end
            local game_data = lobby.savedata.data[map_id]
            if lobby.xp[map_id] and mode ~= 'solo' then
               lobby.xp[map_id] = lobby.xp[map_id] + earned_xp
               local needed_xp = game_data['xp'] - lobby.xp[map_id]
               minetest.chat_send_player(name, 'You just earned '..earned_xp..' XP for your team.\nYou need '..needed_xp..' more XP to defeat the imposter.')
               minetest.log('action', map_id..': Gained '..earned_xp..' xp, still '..needed_xp..' xp required to complete level.')
               tasks.check_xp(map_id, needed_xp)
            else
               lobby.give_xp(puncher, 1)
            end
         else
            minetest.chat_send_player(name, 'You\'re the traitor, you can do tasks, but you won\'t earn XP. Try killing the other players.')
         end
      elseif map_id == 'lobby' and not minetest.check_player_privs(puncher:get_player_name(), {creative = true}) then
         lobby.give_xp(puncher, 1)
      end
   end
end

function tasks.only_add_xp(xp, name) -- This function only adds XP, you are responsible for modifying the task node yourself.
   local player = minetest.get_player_by_name(name)
   local player_attributes = player:get_meta()
   local mode = player_attributes:get_string('mode')
   local map_id = lobby.game[name] or 'lobby'
   local traitor = lobby.traitors[map_id]
   if map_id ~= 'lobby' then
      if name ~= traitor then
         if mode == 'ghost' then
            xp = math.floor(xp/2)
            map_id = string.sub(map_id, 0, -7)
         end
         local game_data = lobby.savedata.data[map_id]
         if lobby.xp[map_id] and mode ~= 'solo' then
            lobby.xp[map_id] = lobby.xp[map_id] + xp
            local needed_xp = game_data['xp'] - lobby.xp[map_id]
            minetest.chat_send_player(name, 'You just earned '..xp..' XP for your team.\nYou need '..needed_xp..' more XP to defeat the imposter.')
            minetest.log('action', map_id..': Gained '..xp..' xp, still '..needed_xp..' xp required to complete level.')
            tasks.check_xp(map_id, needed_xp)
         else
            lobby.give_xp(player, 1)
         end
      else
         minetest.chat_send_player(name, 'You\'re the traitor, you can do tasks, but you won\'t earn XP. Try killing the other players.')
      end
   elseif map_id == 'lobby' and not minetest.check_player_privs(name, {creative = true}) then
      lobby.give_xp(player, 1)
   end
end

function tasks.right_click_on(pos, node, clicker, formspec)
   local name = clicker:get_player_name()
   local timer = minetest.get_node_timer(pos)
   local meta = minetest.get_meta(pos)
   local min = meta:get_int('time_min') or 30
   local max = meta:get_int('time_max') or 60
   local random_number = math.random(min,max)
   timer:start(random_number)
   local map_id = lobby.game[name]
   local sabotage_level = lobby.sabotage_level[map_id] or 5
   local level = meta:get_int('level') or 0
   if level < sabotage_level then
      minetest.show_formspec(name, 'tasks:good', formspec)
   else
      minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
   end
end

function tasks.right_click_off(pos, node, clicker, formspec)
   local name = clicker:get_player_name()
   local map_id = lobby.game[name]
   local sabotage_level = lobby.sabotage_level[map_id] or 5
   local meta = minetest.get_meta(pos)
   local level = meta:get_int('level') or 0
   if level < sabotage_level then
      tasks.player_config[name] = pos
      minetest.show_formspec(name, 'tasks:part_req_form', formspec)
   else
      minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
   end
end

function tasks.on_construct(pos, info_working, info_repair)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
   inv:set_size('part', 1)
   meta:set_string('infotext', info_working)
   meta:set_string('info_working', info_working)
   meta:set_string('info_repair', info_repair)
   meta:set_int('time_min', 30)
   meta:set_int('time_max', 90)
   meta:set_int('xp', 5)
   meta:set_int('level', 0)
end

function tasks.is_integer(input)
   local number = tonumber(input)
   if number then
      if math.floor(number) == number then
         return true
      end
   end
end

function tasks.valid_input(string)
   local variables = string:split(', ')
   local xp = variables[1]
   local timer = variables[2]
   if tasks.is_integer(xp) and tasks.is_integer(timer) then
      return true
   end
end

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'tasks:part_req_form'then
      if fields.gimme then
         local pos = tasks.player_config[name]
         local node = minetest.get_node(pos).name
         local def = minetest.registered_nodes[node]
         local item = def.req_form
         local max = def.req_count
         local player_inv = player:get_inventory()
         if not player_inv:contains_item('main', {name = item, count = max}) then
            player_inv:add_item('main', item)
         else
            minetest.chat_send_player(name, 'You have too many of these already.')
         end
      end
   end
end)

minetest.register_lbm({ --This can be removed in a few months. 08/27/22
   label = 'Tasks infotext update',
   name = 'tasks:info_update',
   run_at_every_load = false,
   nodenames = {'group:tasks'},
   action = function(pos, node)
      local meta = minetest.get_meta(pos)
      local info = meta:get_string('infotext')
      local info_working = meta:get_string('info_working')
      local info_repair = meta:get_string('info_repair')
      if info_repair == '' then
      local i, j = string.find(info, "%(")
         if i then
            info_repair = info
            info_working = string.sub(info, 1, (i-2))
         else
            info_working = info
            info_repair = info..' (Needs Repair)'
         end
      end
      meta:set_string('info_repair', info_repair)
      meta:set_string('info_working', info_working)
   end
})
