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 shouldn't be used on nodes that use formspecs.
   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.')
               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.')
            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(pos, node, clicker)
   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)
end

function tasks.on_construct(pos)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
   inv:set_size('part', 1)
   meta:set_int('time_min', 30)
   meta:set_int('time_max', 90)
   meta:set_int('xp', 5)
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
