name: Magikey
card_text: You can Ritual Summon this card with "Magikey Maftea". If this card is Ritual Summoned: You can add 1 "Magikey" card from your Deck to your hand. You can only use this effect of "Magikey Mechmusket - Batosbuster" once per turn. Once per turn, when an attack is declared involving this card and an opponent's monster that has the same Attribute as a Normal Monster or "Magikey" monster in your GY: You can place any number of cards from your hand on the bottom of the Deck, and if you do, negate the effects of that opponent's monster until the end of this turn, then draw the same number of cards placed under the Deck.
external_ids: local SETCARD_ID_0 = 0x165 --Magikey
  
lua: local SETCARD_ID_0 = 0x165 --Magikey
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--search
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.srcon)
  	e1:SetTarget(s.srtg)
  	e1:SetOperation(s.srop)
  	c:RegisterEffect(e1)
  	--disable & draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TODECK + CATEGORY_DISABLE + CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.discon)
  	e2:SetTarget(s.distg)
  	e2:SetOperation(s.disop)
  	c:RegisterEffect(e2)
  end
  function s.srcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
  end
  function s.srfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.srtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.srfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.srop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.srfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.cfilter(c, attr)
  	return (c:IsType(TYPE_NORMAL) or c:IsSetCard(SETCARD_ID_0)) and c:IsAttribute(attr)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = c:GetBattleTarget()
  	e:SetLabelObject(tc)
  	return tc and tc:IsControler(1 - tp) and tc:IsType(TYPE_MONSTER) and aux.NegateMonsterFilter(tc) and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_GRAVE, 0, 1, nil, tc:GetAttribute())
  end
  function s.distg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local tc = e:GetLabelObject()
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp) and Duel.IsExistingMatchingCard(Card.IsAbleToDeck, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_HAND)
  	Duel.SetOperationInfo(0, CATEGORY_DISABLE, tc, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = e:GetLabelObject()
  	local p = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER)
  	Duel.Hint(HINT_SELECTMSG, p, HINTMSG_TODECK)
  	local g = Duel.SelectMatchingCard(p, Card.IsAbleToDeck, p, LOCATION_HAND, 0, 1, 63, nil)
  	if g:GetCount() == 0 then
  		return
  	end
  	local ct = Duel.SendtoDeck(g, nil, SEQ_DECKTOP, REASON_EFFECT)
  	if ct == 0 then
  		return
  	end
  	Duel.SortDecktop(p, p, ct)
  	for i = 1, ct do
  		local mg = Duel.GetDecktopGroup(p, 1)
  		Duel.MoveSequence(mg:GetFirst(), SEQ_DECKBOTTOM)
  	end
  	if tc and tc:IsRelateToBattle() and tc:IsControler(1 - tp) and tc:IsCanBeDisabledByEffect(e) then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_DISABLE_EFFECT)
  		tc:RegisterEffect(e2)
  		Duel.BreakEffect()
  		Duel.Draw(p, ct, REASON_EFFECT)
  	end
  end
  
  
source_text: Magikey
  You can Ritual Summon this card with "Magikey Maftea". If this card is Ritual Summoned: You can add 1 "Magikey" card from your Deck to your hand. You can only use this effect of "Magikey Mechmusket - Batosbuster" once per turn. Once per turn, when an attack is declared involving this card and an opponent's monster that has the same Attribute as a Normal Monster or "Magikey" monster in your GY: You can place any number of cards from your hand on the bottom of the Deck, and if you do, negate the effects of that opponent's monster until the end of this turn, then draw the same number of cards placed under the Deck.
target_text: -- Magikey
  local SETCARD_ID_0 = 0x165 --Magikey
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--search
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.srcon)
  	e1:SetTarget(s.srtg)
  	e1:SetOperation(s.srop)
  	c:RegisterEffect(e1)
  	--disable & draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TODECK + CATEGORY_DISABLE + CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.discon)
  	e2:SetTarget(s.distg)
  	e2:SetOperation(s.disop)
  	c:RegisterEffect(e2)
  end
  function s.srcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
  end
  function s.srfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.srtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.srfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.srop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.srfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.cfilter(c, attr)
  	return (c:IsType(TYPE_NORMAL) or c:IsSetCard(SETCARD_ID_0)) and c:IsAttribute(attr)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = c:GetBattleTarget()
  	e:SetLabelObject(tc)
  	return tc and tc:IsControler(1 - tp) and tc:IsType(TYPE_MONSTER) and aux.NegateMonsterFilter(tc) and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_GRAVE, 0, 1, nil, tc:GetAttribute())
  end
  function s.distg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local tc = e:GetLabelObject()
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp) and Duel.IsExistingMatchingCard(Card.IsAbleToDeck, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_HAND)
  	Duel.SetOperationInfo(0, CATEGORY_DISABLE, tc, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = e:GetLabelObject()
  	local p = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER)
  	Duel.Hint(HINT_SELECTMSG, p, HINTMSG_TODECK)
  	local g = Duel.SelectMatchingCard(p, Card.IsAbleToDeck, p, LOCATION_HAND, 0, 1, 63, nil)
  	if g:GetCount() == 0 then
  		return
  	end
  	local ct = Duel.SendtoDeck(g, nil, SEQ_DECKTOP, REASON_EFFECT)
  	if ct == 0 then
  		return
  	end
  	Duel.SortDecktop(p, p, ct)
  	for i = 1, ct do
  		local mg = Duel.GetDecktopGroup(p, 1)
  		Duel.MoveSequence(mg:GetFirst(), SEQ_DECKBOTTOM)
  	end
  	if tc and tc:IsRelateToBattle() and tc:IsControler(1 - tp) and tc:IsCanBeDisabledByEffect(e) then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_DISABLE_EFFECT)
  		tc:RegisterEffect(e2)
  		Duel.BreakEffect()
  		Duel.Draw(p, ct, REASON_EFFECT)
  	end
  end
  
  