name: Margin
card_text: Your opponent can discard 1 card to negate this effect, otherwise both players look at their opponent's Deck and choose 1 card, then add the card from their own Deck chosen by their opponent to their hand. You can only activate 1 "Margin Trading" per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c, p)
  	return c:IsAbleToHand(p)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil, tp) and Duel.IsExistingMatchingCard(s.filter, tp, 0, LOCATION_DECK, 1, nil, 1 - tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 2, PLAYER_ALL, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, 0, LOCATION_HAND) > 0 and Duel.IsChainDisablable(0) and Duel.SelectYesNo(1 - tp, aux.Stringid(id, 1)) then
  		Duel.DiscardHand(1 - tp, aux.TRUE, 1, 1, REASON_EFFECT + REASON_DISCARD)
  		Duel.NegateEffect(0)
  		return
  	end
  	local d1 = Duel.GetFieldGroup(tp, LOCATION_DECK, 0)
  	local d2 = Duel.GetFieldGroup(tp, 0, LOCATION_DECK)
  	if #d1 == 0 or #d2 == 0 then
  		return
  	end
  	Duel.ConfirmCards(tp, d2)
  	Duel.ConfirmCards(1 - tp, d1)
  	Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 2))
  	local g2 = Duel.SelectMatchingCard(tp, s.filter, tp, 0, LOCATION_DECK, 1, 1, nil, tp)
  	Duel.Hint(HINT_SELECTMSG, 1 - tp, aux.Stringid(id, 2))
  	local g1 = Duel.SelectMatchingCard(1 - tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil, tp)
  	Duel.BreakEffect()
  	Duel.SendtoHand(g2, nil, REASON_EFFECT)
  	Duel.ConfirmCards(tp, g2)
  	Duel.SendtoHand(g1, nil, REASON_EFFECT)
  	Duel.ConfirmCards(1 - tp, g1)
  end
  
  
source_text: Margin
  Your opponent can discard 1 card to negate this effect, otherwise both players look at their opponent's Deck and choose 1 card, then add the card from their own Deck chosen by their opponent to their hand. You can only activate 1 "Margin Trading" per turn.
target_text: -- Margin
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c, p)
  	return c:IsAbleToHand(p)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil, tp) and Duel.IsExistingMatchingCard(s.filter, tp, 0, LOCATION_DECK, 1, nil, 1 - tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 2, PLAYER_ALL, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, 0, LOCATION_HAND) > 0 and Duel.IsChainDisablable(0) and Duel.SelectYesNo(1 - tp, aux.Stringid(id, 1)) then
  		Duel.DiscardHand(1 - tp, aux.TRUE, 1, 1, REASON_EFFECT + REASON_DISCARD)
  		Duel.NegateEffect(0)
  		return
  	end
  	local d1 = Duel.GetFieldGroup(tp, LOCATION_DECK, 0)
  	local d2 = Duel.GetFieldGroup(tp, 0, LOCATION_DECK)
  	if #d1 == 0 or #d2 == 0 then
  		return
  	end
  	Duel.ConfirmCards(tp, d2)
  	Duel.ConfirmCards(1 - tp, d1)
  	Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 2))
  	local g2 = Duel.SelectMatchingCard(tp, s.filter, tp, 0, LOCATION_DECK, 1, 1, nil, tp)
  	Duel.Hint(HINT_SELECTMSG, 1 - tp, aux.Stringid(id, 2))
  	local g1 = Duel.SelectMatchingCard(1 - tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil, tp)
  	Duel.BreakEffect()
  	Duel.SendtoHand(g2, nil, REASON_EFFECT)
  	Duel.ConfirmCards(tp, g2)
  	Duel.SendtoHand(g1, nil, REASON_EFFECT)
  	Duel.ConfirmCards(1 - tp, g1)
  end
  
  