Class: Aro::Deck
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Aro::Deck
- Defined in:
- lib/models/deck.rb
Constant Summary collapse
- DECK_FILE =
".deck"- CARD_DELIM =
","- DEV_TAROT_FILE =
"/dev/tarot"- DRAW_COUNT =
update this for # of cards you will draw
7- DISPLAY_WIDTH =
do not modify
Aro::Deck::DRAW_COUNT*Aro::Deck::DRAW_COUNT
- HISTORY_SEPARATOR =
"#{"-" * Aro::Deck::DISPLAY_WIDTH}"- DATE_FORMAT =
change to update how timestamps appear
"%Y:%m:%d:%H:%M:%S"
Class Method Summary collapse
- .card_strip(card) ⇒ Object
- .current_deck ⇒ Object
- .display_selection_menu ⇒ Object
- .fresh_cards ⇒ Object
- .make(new_name) ⇒ Object
-
.read_dev_tarot ⇒ Object
read dev_tarot.
Instance Method Summary collapse
- #draw(is_dt_dimension: true, z_max: 7, z: 1) ⇒ Object
- #explore ⇒ Object
- #generate_log ⇒ Object
-
#get_display_for_cards(input = []) ⇒ Object
todo:, print_nums: false).
- #populate_cards ⇒ Object
- #replace ⇒ Object
- #reset ⇒ Object
- #show(count_n: Aro::Log::DEFAULT_COUNT, order_o: Aro::Log::ORDERING[:DESC]) ⇒ Object
- #shuffle ⇒ Object
-
#summon_ruby_facot(cards_arr) ⇒ Object
summon ruby_facot.
Class Method Details
.card_strip(card) ⇒ Object
79 80 81 |
# File 'lib/models/deck.rb', line 79 def self.card_strip(card) card.gsub(/[+-]/, "").strip end |
.current_deck ⇒ Object
72 73 74 75 76 77 |
# File 'lib/models/deck.rb', line 72 def self.current_deck if File.exist?(DECK_FILE) current_deck_id = File.read(DECK_FILE) return Aro::Deck.find_by(id: current_deck_id) end end |
.display_selection_menu ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/models/deck.rb', line 53 def self. unless Aro::Deck.any? Aro::P.say(I18n.t("cli.messages.no_decks")) exit(CLI::EXIT_CODES[:SUCCESS]) end selection = Aro::P.p.select("choose a deck:") do || Aro::Deck.all.each{|d| if d.id == Aro::Deck.current_deck&.id .default d.id end .choice(d.name, d.id) } end File.open(Aro::Deck::DECK_FILE, "w") do |file| file.write(selection) end end |
.fresh_cards ⇒ Object
35 36 37 |
# File 'lib/models/deck.rb', line 35 def self.fresh_cards I18n.t("cards.index").map{|c| "+#{c}"}.join(Aro::Deck::CARD_DELIM) end |
.make(new_name) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/models/deck.rb', line 24 def self.make(new_name) Aro::Create.new(new_name) new_deck = Aro::Deck.create(name: new_name) if Aro::Deck.current_deck.nil? File.open(Aro::Deck::DECK_FILE, "w") do |file| file.write(new_deck.id) end end new_deck end |
.read_dev_tarot ⇒ Object
read dev_tarot
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/models/deck.rb', line 195 def self.read_dev_tarot dt = nil return dt unless File.exist?(Aro::Deck::DEV_TAROT_FILE) File.open(Aro::Deck::DEV_TAROT_FILE, "r"){|dtf| dt = dtf.read(Aro::Mancy::N)} # VERY IMPORTANT! Aro::P.say(I18n.t("cli.very_important", dev_tarot: dt)) return dt end |
Instance Method Details
#draw(is_dt_dimension: true, z_max: 7, z: 1) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/models/deck.rb', line 233 def draw(is_dt_dimension: true, z_max: 7, z: 1) # the true card abs_dev_tarot = nil # oriented card dev_tarot = nil # get cards cards_arr = cards.split(Aro::Deck::CARD_DELIM) || [] # get abs_cards abs_cards_arr = cards_arr.map{|c| Aro::Deck.card_strip(c)} # get drawn drawn_arr = drawn&.split(Aro::Deck::CARD_DELIM) || [] sleeps = 0 sleeps_max = z_max # find a card that is not already drawn while sleeps <= sleeps_max && dev_tarot.nil? do # use fallback randomness if /dev/tarot unavailable if !is_dt_dimension || !File.exist?(Aro::Deck::DEV_TAROT_FILE) dev_tarot = summon_ruby_facot(cards_arr).split("") else # preferred randomness read_value = Aro::Deck.read_dev_tarot&.strip&.split("") if read_value.count >= Aro::Mancy::N - 1 dev_tarot = read_value end end unless dev_tarot.nil? abs_dev_tarot = dev_tarot[Aro::Mancy::S] + Aro::NUMERALS.key( dev_tarot.join("")[Aro::Mancy::OS..].to_i ).to_s if abs_cards_arr.include?(abs_dev_tarot) # dev_tarot is valid dev_tarot = dev_tarot[Aro::Mancy::O] + abs_dev_tarot else dev_tarot = nil end end if dev_tarot.nil? # dev_tarot is invalid sleeps += 1 sleep(z.to_i) end end # remove from cards cards_arr.delete(cards_arr.select{|c| c.include?(abs_dev_tarot)}.first) # insert dev_tarot to drawn drawn_arr << dev_tarot # update database update( cards: cards_arr.join(Aro::Deck::CARD_DELIM), drawn: drawn_arr.join(Aro::Deck::CARD_DELIM) ) end |
#explore ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/models/deck.rb', line 156 def explore # allows user to browse each card in the current deck. answer = Aro::P.p.select( I18n.t("cli.messages.choose_card"), # formatted for tty-prompt gem cards.split(Aro::Deck::CARD_DELIM).map{|c| [I18n.t("cards.#{Aro::Deck.card_strip(c)}.name"), c]}.to_h, per_page: 7, cycle: true, default: 1 ) Aro::P.say(I18n.t("cards.#{Aro::Deck.card_strip(answer)}")) end |
#generate_log ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/models/deck.rb', line 43 def generate_log prev_cards = Base64::decode64(logs.last.card_data) if logs.any? if prev_cards.present? && prev_cards != cards || prev_cards.nil? logs.create( card_data: Base64::encode64(cards), drawn_data: Base64::encode64(drawn || "") ) end end |
#get_display_for_cards(input = []) ⇒ Object
todo:, print_nums: false)
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/models/deck.rb', line 143 def get_display_for_cards(input = []) # todo:, print_nums: false) result = "" input.each_with_index{|c, i| if i == I18n.t("cards.index").count - 1 result += c.ljust(Aro::Deck::DISPLAY_WIDTH) else result += c.ljust(Aro::Deck::DRAW_COUNT) + ((i + 1) % Aro::Deck::DRAW_COUNT == 0 ? "\n" : "") end } result += "\n" result end |
#populate_cards ⇒ Object
39 40 41 |
# File 'lib/models/deck.rb', line 39 def populate_cards self.cards = Aro::Deck.fresh_cards end |
#replace ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/models/deck.rb', line 181 def replace # replaces all drawn cards FIFO and puts them on the bottom of # the deck. this will preserve all card orientations. cards_arr = cards.split(Aro::Deck::CARD_DELIM) || [] drawn_arr = drawn&.split(Aro::Deck::CARD_DELIM) || [] # append each drawn card to cards drawn_arr.each{|dc| cards_arr << dc } # clear drawn update(drawn: "", cards: cards_arr.join(Aro::Deck::CARD_DELIM)) end |
#reset ⇒ Object
175 176 177 178 179 |
# File 'lib/models/deck.rb', line 175 def reset # completely reset the deck. replace all drawn and reset order. # all orientations will be set to upright. update(cards: Aro::Deck.fresh_cards, drawn: "") end |
#show(count_n: Aro::Log::DEFAULT_COUNT, order_o: Aro::Log::ORDERING[:DESC]) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/models/deck.rb', line 83 def show(count_n: Aro::Log::DEFAULT_COUNT, order_o: Aro::Log::ORDERING[:DESC]) unless count_n.kind_of?(Numeric) && count_n > 0 if count_n&.to_s&.downcase&.to_sym == Aro::Log::ALL count_n = logs.count else count_n = Aro::Log::DEFAULT_COUNT end end count_n = [count_n.to_i, logs.count].min unless Aro::Log::ORDERING.include?(order_o&.to_s&.upcase&.to_sym) Aro::P.say(I18n.t("cli.warnings.invalid_order")) order_o = Aro::Log::ORDERING[:DESC] end # perform query h_logs = logs.order(created_at: order_o.to_s.downcase).first(count_n) # for now tests just expect text output return h_logs if Aro::IS_TEST.call Aro::P.say(I18n.t("cli.messages.showing", name: name, count: count_n, order: order_o)) h_text = "\n" h_text += Aro::Deck::HISTORY_SEPARATOR + "\n\n" h_text += "#{name.upcase.center(Aro::Deck::DISPLAY_WIDTH)}\n\n" h_logs.each_with_index{|l, i| h_text += Aro::Deck::HISTORY_SEPARATOR + "\n" h_text += l.created_at.strftime(Aro::Deck::DATE_FORMAT).center(Aro::Deck::DISPLAY_WIDTH) + "\n" h_text += "#{order_o.to_sym == Aro::Log::ORDERING[:DESC] ? logs.count - i : 1 + i} of #{logs.count}".rjust(Aro::Deck::DISPLAY_WIDTH) + "\n" h_text += Aro::Deck::HISTORY_SEPARATOR + "\n\n" h_text += get_display_for_cards( Base64::decode64(l.card_data).split(Aro::Deck::CARD_DELIM) ) h_text += Aro::Deck::HISTORY_SEPARATOR + "\n" drawn_cards = Base64::decode64(l.drawn_data).split(Aro::Deck::CARD_DELIM) if !drawn_cards.nil? && drawn_cards.any? h_text += I18n.t("cli.messages.history_drawn").center(Aro::Deck::DISPLAY_WIDTH) + "\n" h_text += Aro::Deck::HISTORY_SEPARATOR + "\n\n" h_text += get_display_for_cards( drawn_cards ) h_text += "\n" h_text += Aro::Deck::HISTORY_SEPARATOR + "\n" end 3.times do h_text += Aro::Deck::HISTORY_SEPARATOR + "\n" end } if count_n == Aro::Log::DEFAULT_COUNT Aro::P.say(h_text) else Aro::P.less(h_text) end end |
#shuffle ⇒ Object
170 171 172 173 |
# File 'lib/models/deck.rb', line 170 def shuffle # shuffles the current deck and generates a log record. update(cards: cards.split(Aro::Deck::CARD_DELIM).shuffle.join(Aro::Deck::CARD_DELIM)) end |
#summon_ruby_facot(cards_arr) ⇒ Object
summon ruby_facot
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/models/deck.rb', line 207 def summon_ruby_facot(cards_arr) Aro::P.say(I18n.t("cli.messages.ruby_facot_random")) ruby_facot = cards_arr.sample.split("") # get orientation ruby_facot_str = ["+","-"].sample # get suite ruby_facot_str += ruby_facot[1] # calculate the sym symm = ruby_facot.select{|c| # loops through the characters in ruby_facot # return all characters not matching: # => character[0]: orientation # => character[1]: suite # the first two characters in the dev_tarot format designate the !ruby_facot.first(Aro::Mancy::OS).include?(c) }.join("").to_sym ruby_facot_str += Aro::NUMERALS[symm].to_s # return ruby_facot_str ruby_facot_str end |