Вот мой код для действия /сброса
Основная задача, которую я испытываю, находится в коде, где вы видите переменные, которые упоминают King, который больше относится к нижнему < /p>
get("/discard") do
#i will need to do something with discard parameter i think
#has the chosen cards to be discarded
@checked = []
#card to be discarded
@discard = ""
params.each do |key, val|
@checked.push(key)
@discard = @checked.join(",")
end
deck = cookies[:deck_id]
pile_name = "hand"
hand_list = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/list/"
@hand = api_response(hand_list, "piles").fetch(pile_name).fetch("cards")
discarded_arr = []
@discarded_card = ""
#check what card(s) were discarded from hand and change hand accordingly
@hand.each do |h|
if @checked.include?(h)
discarded_arr.push(h)
end
end
@curr_hand = []
@hand.each do |h|
@curr_hand.push(h.fetch("code"))
end
@discarded_card = discarded_arr.join(",")
## ################# hand pile
pile_name = "hand"
#take from hand the cards that were discarded
#add hand before discarding from pile
#draw from the pile which would be discarding in this case
pile = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/draw/?cards=" + @discard
resp = HTTP.get(pile)
raw_response = resp.to_s
parsed_response = JSON.parse(raw_response)
pile_list = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/list/"
resp = HTTP.get(pile_list)
raw_response = resp.to_s
@parsed_response = JSON.parse(raw_response)
@cards = @parsed_response.fetch("piles").fetch("hand").fetch("cards")
@hand_arr = []
@hand_code = []
@cards.each do |c|
@hand_arr.push(c.fetch("image"))
@hand_code.push(c.fetch("code"))
end
############# end of hand pile
#/pile/discard is name of pile - its discard
pile_name = "discard"
pile = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/add/?cards=" + @discard
resp = HTTP.get(pile)
raw_response = resp.to_s
parsed_response = JSON.parse(raw_response)
pile_list = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/list/"
resp = HTTP.get(pile_list)
raw_response = resp.to_s
parsed_response = JSON.parse(raw_response)
cards = parsed_response.fetch("piles").fetch("discard").fetch("cards")
@pile_arr = []
@code_arr = []
cards.each do |c|
@pile_arr.push(c.fetch("image"))
@code_arr.push(c.fetch("code"))
end
################################## ################# end of discard pile
################################################### start of disabled logic
#pile name is currently discard
#pile_name = "discard"
discard_list = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/list/"
@discard_res = api_response(discard_list, "piles").fetch(pile_name).fetch("cards")
#get last added card which would be on top of discard pile
@discard_arr = []
@discard_res.each_with_index do |d, i|
@in = i
@dlen = @discard_res.length
if i == @discard_res.length - 1
@top_discard = d
end
@discard_arr.push(d.fetch("image"))
end
#make array that's filled with cards that do not match the suit or value of card in discard pile or are not king or ace cards
@disabled_arr = []
@disable = []
#has code for cards in hand
@cards.each_with_index do |c, i|
if !(c.fetch("value") == @top_discard["value"] || c.fetch("suit") == @top_discard["suit"] || c.fetch("value") == "KING" || c.fetch("value") == "ACE")
@disabled_arr.push(c)
end
if @disabled_arr.include?(c)
@disable.push(true)
else
@disable.push(false)
end
end
c = @cards.join(",")
d = @disabled_arr.join(",")
######################################### end of disabled logic
################################################### start of check if any card in hand matches discard pile if none then draw card and next player takes their turn kind of is part of discard and do action maybe dont need bc i disable cards
@disable_button = false
if c == d
@disable_button = true
end
################################################### end of check if any card in hand matches discard pile if none then draw card and next player takes their turn kind of is part of discard and do action
################################################### start of change suit with king
#get the suits to pop up after placing king choosing one changes the king on the discard pile
#new partial deck
new_deck = "https://deckofcardsapi.com/api/deck/new ... S,KC,KH,KD"
resp = HTTP.get(new_deck)
raw_response = resp.to_s
parsed_response = JSON.parse(raw_response)
@deck = parsed_response.fetch("deck_id")
king_draw = "https://deckofcardsapi.com/api/deck/" + @deck + "/draw/?count=4"
@cards_to_add = api_response(king_draw, "cards")
king_add = []
@cards_to_add.each do |c|
king_add.push(c.fetch("code"))
end
pile_name = "kings"
cards = king_add.join(",")
pile = "https://deckofcardsapi.com/api/deck/" + @deck + "/pile/" + pile_name + "/add/?cards=" + cards
resp = HTTP.get(pile)
pile_list = "https://deckofcardsapi.com/api/deck/" + @deck + "/pile/" + pile_name + "/list/"
@kings = api_response(pile_list, "piles").fetch("kings").fetch("cards")
@king_arr = []
@king_codes = []
@kings.each do |c|
@king_arr.push(c.fetch("image"))
@king_codes.push(c.fetch("code"))
end
@is_king = false
if @top_discard.fetch("value") == "KING"
@is_king = true
end
#king"code"=on will be parameter so use it to change king in discard pile to that one
@in_king = false
if params.key("king") != nil
king = params.fetch("king")
@in_king = true
pile_name = "discard"
deck = cookies[:deck_id]
@pile = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/draw/?cards=" + @top_discard
#may or may not have to add that discarded card back to deck somehow
@new_king = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/add/?cards=" + king
cards = api_response(new_king, "piles").fetch(pile_name).fetch("cards")
#add the pile list here
@new_king_arr = []
@new_king_codes = []
# then use it to display new pile
cards.each do |c|
@new_king_arr.push(c.fetch("image"))
@new_king_codes.push(c.fetch("code"))
end
################################################### end of change suit with king
end
erb(:discard)
end
< /code>
код для API_Response (url, key) < /p>
def api_response(url, key)
resp = HTTP.get(url)
raw_response = resp.to_s
parsed_response = JSON.parse(raw_response)
return fetched_key = parsed_response.fetch(key)
end
< /code>
Шаблон просмотра.
Your hand
-
[img]
Discard pile
new_king is
new_king is
pile is:
new_king is
- [img]
[img]
Подробнее здесь: https://stackoverflow.com/questions/797 ... by-sinatra