aromancer 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,14 +14,20 @@ module Aromancer
14
14
  class Routes
15
15
 
16
16
  def self.resume
17
+ return main_menu if Aromancer::Storage.get_signed_out_flag
18
+
17
19
  unless Thread.main[:route].nil?
18
- if (Thread.main[:route_params] || []).any?
19
- self.send(Thread.main[:route], *Thread.main[:route_params])
20
- else
21
- self.send(Thread.main[:route])
20
+ begin
21
+ Thread.main[:route_params] ||= []
22
+ if Thread.main[:route_params].any?
23
+ self.send(Thread.main[:route], *Thread.main[:route_params])
24
+ Thread.main[:route_params] = []
25
+ else
26
+ self.send(Thread.main[:route])
27
+ end
28
+ rescue ArgumentError => e
29
+ authentication
22
30
  end
23
- Thread.main[:route_params] = nil
24
- Thread.main[:route] = nil
25
31
  return
26
32
  end
27
33
 
@@ -38,18 +44,22 @@ module Aromancer
38
44
  end
39
45
 
40
46
  def self.active_legend
41
- Aromancer::Api.get_legends
42
-
43
47
  (
44
- Aromancer::Storage.get_legends&.select{|l| l["id"] == Aromancer::Storage.get_player["active_legend_id"]} || []
48
+ Aromancer::Storage.get_legends&.select{|l| l["id"] == (Aromancer::Storage.get_player || {})["active_legend_id"]} || []
45
49
  ).first
46
50
  end
47
51
 
48
52
  def self.arena_data
49
53
  arena = Aromancer::Storage.get_arena
54
+ if arena.nil?
55
+ Aromancer::Api.load_arena
56
+ sleep(1)
57
+ end
58
+
50
59
  if arena.nil?
51
60
  Aromancer.print_last_error("routes.legend.arena_failed")
52
61
  Aromancer::Api.set_active_legend_id(nil)
62
+ Aromancer::Prompt.p.ask(I18n.t("shared.key_press"))
53
63
  main_menu
54
64
  else
55
65
  arena
@@ -61,32 +71,29 @@ module Aromancer
61
71
  Aromancer::Storage.set_signed_out_flag(false)
62
72
  return main_menu
63
73
  end
64
- Aromancer::Prompt.say(I18n.t("shared.navigate", route: :authentication))
65
74
 
66
- choices = [
75
+ Aromancer::Prompt.say(I18n.t("shared.navigate", route: :authentication)) if Aromancer::Storage.get_preference(:debug)
76
+
77
+ Thread.main[:menu_options][__method__.to_sym] ||= [
67
78
  {name: I18n.t("routes.authentication.sign_in"), value: :in},
68
79
  {name: I18n.t("routes.authentication.sign_up"), value: :up},
69
80
  {name: I18n.t("routes.main_menu.configure"), value: :configure},
70
81
  {name: I18n.t("shared.exit"), value: :exit}
71
82
  ]
72
-
73
- Thread.main[:input_controls] = {
74
- choices: choices,
75
- runners: [
76
- {value: :in, proc: Proc.new{
77
- Aromancer::Api.sign_in
78
- authentication
79
- }},
80
- {value: :up, proc: Proc.new{
81
- Aromancer::Api.sign_up
82
- authentication
83
- }},
84
- {value: :configure, proc: Proc.new{
85
- configure
86
- }}
87
- ]
88
- }
89
- Thread.main[:route] = __method__.to_s
83
+ Thread.main[:menu_runners][__method__.to_sym] ||= [
84
+ {value: :in, proc: Proc.new{
85
+ Aromancer::Api.sign_in
86
+ authentication
87
+ }},
88
+ {value: :up, proc: Proc.new{
89
+ Aromancer::Api.sign_up
90
+ authentication
91
+ }},
92
+ {value: :configure, proc: Proc.new{
93
+ configure
94
+ }}
95
+ ]
96
+ Thread.main[:route] = __method__.to_sym
90
97
  end
91
98
 
92
99
  def self.configure
@@ -100,13 +107,6 @@ module Aromancer
100
107
  Aromancer::Prompt.say(l)
101
108
  }
102
109
 
103
- choices = [
104
- {name: I18n.t("shared.back"), value: :back},
105
- {name: I18n.t("routes.configure.server_host", stored: Aromancer::Storage.instance.get_key(:server_url_host)), value: :host},
106
- {name: I18n.t("routes.configure.server_port", stored: Aromancer::Storage.instance.get_key(:server_url_port)), value: :port},
107
- {name: I18n.t("routes.configure.force_ssl", stored: Aromancer::Storage.get_preference(:force_ssl, default: true)), value: :force_ssl}
108
- ]
109
-
110
110
  server_url_update = Proc.new{|property|
111
111
 
112
112
  updated = nil
@@ -142,38 +142,54 @@ module Aromancer
142
142
  Aromancer::Prompt.say(I18n.t("shared.server_url", url: Aromancer::Storage.get_server_url))
143
143
  }
144
144
 
145
- Thread.main[:input_controls] = {
146
- choices: choices,
147
- runners: [
148
- {value: :back, proc: Proc.new{ main_menu }},
149
- {value: :host, proc: Proc.new{
150
- server_url_update.call(:host)
151
- configure
152
- }},
153
- {value: :port, proc: Proc.new{
154
- server_url_update.call(:port)
155
- configure
156
- }},
157
- {value: :force_ssl, proc: Proc.new{
158
- Aromancer::Storage.toggle_force_ssl
159
- Aromancer::Prompt.say(I18n.t("shared.server_url", url: Aromancer::Storage.get_server_url))
160
- Aromancer::Prompt.say(
161
- I18n.t("routes.configure.force_ssl_toggle", value: Aromancer::Storage.get_preference(:force_ssl))
162
- )
163
- sleep(2)
164
- configure
165
- }},
166
- ]
167
- }
168
- Thread.main[:route] = __method__.to_s
145
+ Thread.main[:menu_options][__method__.to_sym] = [
146
+ {name: I18n.t("shared.back"), value: :back},
147
+ {name: I18n.t("routes.configure.server_host", stored: Aromancer::Storage.instance.get_key(:server_url_host)), value: :host},
148
+ {name: I18n.t("routes.configure.server_port", stored: Aromancer::Storage.instance.get_key(:server_url_port)), value: :port},
149
+ {name: I18n.t("routes.configure.force_ssl", stored: Aromancer::Storage.get_preference(:force_ssl)), value: :force_ssl},
150
+ {name: I18n.t("routes.configure.debug", stored: Aromancer::Storage.get_preference(:debug)), value: :debug}
151
+ ]
152
+
153
+ Thread.main[:menu_runners][__method__.to_sym] = [
154
+ {value: :back, proc: Proc.new{ main_menu }},
155
+ {value: :host, proc: Proc.new{
156
+ server_url_update.call(:host)
157
+ configure
158
+ }},
159
+ {value: :port, proc: Proc.new{
160
+ server_url_update.call(:port)
161
+ configure
162
+ }},
163
+ {value: :force_ssl, proc: Proc.new{
164
+ Aromancer::Storage.toggle_force_ssl
165
+ Aromancer::Prompt.say(I18n.t("shared.server_url", url: Aromancer::Storage.get_server_url))
166
+ Aromancer::Prompt.say(
167
+ I18n.t("routes.configure.force_ssl_toggle", value: Aromancer::Storage.get_preference(:force_ssl))
168
+ )
169
+ sleep(2)
170
+ configure
171
+ }},
172
+ {value: :debug, proc: Proc.new{
173
+ Aromancer::Storage.toggle_preference(:debug)
174
+
175
+ Aromancer::Prompt.say(
176
+ I18n.t("routes.configure.debug_toggle", value: Aromancer::Storage.get_preference(:debug))
177
+ )
178
+ sleep(2)
179
+ configure
180
+ }},
181
+ ]
182
+ Thread.main[:route] = __method__.to_sym
169
183
  end
170
184
 
171
185
  def self.main_menu
172
186
  cls
173
- Aromancer::Prompt.say(I18n.t("shared.navigate", route: :main_menu))
187
+ Aromancer::Prompt.say(I18n.t("shared.navigate", route: :main_menu)) if Aromancer::Storage.get_preference(:debug)
174
188
  if Aromancer::Storage.get_signed_out_flag
175
189
  Aromancer::Prompt.say(I18n.t("routes.main_menu.not_signed_in"))
176
190
  Aromancer::Api.sign_out
191
+ Thread.main[:route] = :authentication
192
+ Thread.main[:menu_options] = {}
177
193
  return authentication
178
194
  end
179
195
 
@@ -184,49 +200,49 @@ module Aromancer
184
200
 
185
201
  return authentication if player.nil?
186
202
 
187
- choices = [
203
+ Thread.main[:menu_options][__method__.to_sym] ||= [
188
204
  {name: I18n.t("routes.main_menu.new_game"), value: :new},
189
205
  {name: I18n.t("routes.main_menu.load_game"), value: :load},
206
+ {name: I18n.t("routes.main_menu.find_game"), value: :find},
190
207
  {name: I18n.t("routes.main_menu.configure"), value: :configure},
191
208
  {name: I18n.t("routes.main_menu.sign_out"), value: :out},
192
209
  {name: I18n.t("shared.exit"), value: :exit}
193
210
  ]
194
211
 
195
- Thread.main[:input_controls] = {
196
- choices: choices,
197
- runners: [
198
- {value: :new, proc: Proc.new{
199
- # this will run on the main thread
200
- params = Aromancer::Api::DEFAULT_LEGEND_PARAMS.dup
212
+ Thread.main[:menu_runners][__method__.to_sym] = [
213
+ {value: :new, proc: Proc.new{
214
+ # this will run on the main thread
215
+ params = Aromancer::Api::DEFAULT_LEGEND_PARAMS.dup
201
216
 
202
- player_count = nil
203
- while player_count.nil? || ![1, 2].include?(player_count&.to_i)
204
- player_count = Aromancer::Prompt.p.ask(I18n.t("routes.main_menu.player_count"))
205
- end
206
- params[:player_count] = player_count
207
- new_game(params)
208
- }},
209
- {value: :load, proc: Proc.new{ Aromancer::Routes::legends }},
210
- {value: :configure, proc: Proc.new{
211
- configure
212
- }},
213
- {value: :out, proc: Proc.new{
214
- case Aromancer::Prompt.p.select(I18n.t("shared.confirm"), [:no, :yes], cycle: true, per_page: 11)
215
- when :no
216
- main_menu
217
- when :yes
218
- Aromancer::Api.sign_out
219
- authentication
220
- end
221
- }}
222
- ]
223
- }
224
- Thread.main[:route] = __method__.to_s
217
+ player_count = nil
218
+ while player_count.nil? || ![1, 2].include?(player_count&.to_i)
219
+ player_count = Aromancer::Prompt.p.ask(I18n.t("routes.main_menu.player_count"))
220
+ end
221
+ params[:player_count] = player_count
222
+ Thread.main[:route_params] = [params]
223
+ new_game(params)
224
+ }},
225
+ {value: :load, proc: Proc.new{ Aromancer::Routes::legends }},
226
+ {value: :find, proc: Proc.new{ Aromancer::Routes::find_game }},
227
+ {value: :configure, proc: Proc.new{
228
+ configure
229
+ }},
230
+ {value: :out, proc: Proc.new{
231
+ case Aromancer::Prompt.p.select(I18n.t("shared.confirm"), [:no, :yes], cycle: true, per_page: 11)
232
+ when :no
233
+ main_menu
234
+ when :yes
235
+ Aromancer::Api.sign_out
236
+ authentication
237
+ end
238
+ }}
239
+ ]
240
+ Thread.main[:route] = __method__.to_sym
225
241
  end
226
242
 
227
243
  def self.new_game(params)
228
244
  cls
229
- Aromancer::Prompt.say(I18n.t("shared.navigate", route: :new_game))
245
+ Aromancer::Prompt.say(I18n.t("shared.navigate", route: :new_game)) if Aromancer::Storage.get_preference(:debug)
230
246
 
231
247
  data = [
232
248
  [I18n.t("routes.new_game.player_count"), params[:player_count]],
@@ -238,67 +254,84 @@ module Aromancer
238
254
 
239
255
  Aromancer::Prompt.say(I18n.t("routes.new_game.starting"))
240
256
 
241
- choices = [
257
+ Thread.main[:menu_options][__method__.to_sym] = [
242
258
  {name: :continue, value: :continue},
243
259
  {name: :cancel, value: :cancel}
244
260
  ]
245
- Thread.main[:input_controls] = {
246
- choices: choices,
247
- runners: [
248
- {value: :continue, proc: Proc.new{
249
- Aromancer::Api.create_legend(params)
250
- timeout = 1
251
- while active_legend.nil? || timeout >= 11
252
- Aromancer::Prompt.say(I18n.t("routes.new_game.starting"))
253
- sleep(2)
254
- timeout += 1
255
- end
261
+ Thread.main[:menu_runners][__method__.to_sym] = [
262
+ {value: :continue, proc: Proc.new{
263
+ Aromancer::Storage.instance.clear_cache!
264
+ Aromancer::Api.create_legend(params)
265
+ legend_menu
266
+ # Aromancer::Api.get_legends
267
+
268
+ # unless active_legend.nil? || active_legend["id"] != Aromancer::Storage.get_player["active_legend_id"]
269
+ # Aromancer.print_last_error
270
+ # sleep(1)
271
+ # main_menu
272
+ # else
273
+ # legend_menu
274
+ # end
275
+ }},
276
+ {value: :cancel, proc: Proc.new{
277
+ Aromancer::Prompt.say(I18n.t("routes.new_game.cancelled"))
278
+ main_menu
279
+ }}
280
+ ]
281
+ Thread.main[:route] = __method__.to_sym
282
+ end
256
283
 
257
- legend_menu
258
- }},
259
- {value: :cancel, proc: Proc.new{
260
- Aromancer::Prompt.say(I18n.t("routes.new_game.cancelled"))
261
- main_menu
262
- }}
263
- ]
264
- }
284
+ def self.find_game
285
+ legends(JSON.parse(Aromancer::Server.find_legends.body || []))
265
286
  end
266
287
 
267
- def self.legends
268
- Aromancer::Api.get_legends
269
- legend_array = Aromancer::Storage.get_legends
270
- unless legend_array.any?
271
- Aromancer::Prompt.say(I18n.t("shared.empty"))
272
- # Aromancer::Prompt.p.ask(I18n.t("shared.key_press"))
273
- main_menu
288
+ def self.legends(legend_array = nil)
289
+
290
+ # if legend_array.nil?, load player's legends
291
+ if legend_array.nil?
292
+ Aromancer::Storage.instance.clear_cache!
293
+ Aromancer::Api.get_legends
294
+ legend_array = Aromancer::Storage.get_legends
295
+ unless legend_array.any?
296
+ Aromancer::Prompt.say(I18n.t("shared.empty"))
297
+ sleep(1)
298
+ return main_menu
299
+ end
274
300
  end
275
- choices = [{name: I18n.t("shared.back"), value: :back}] + legend_array.map{|l|
301
+
302
+ Thread.main[:menu_options][__method__.to_sym] = [{name: I18n.t("shared.back"), value: :back}] + legend_array.map{|l|
276
303
  {
277
304
  name: "#{I18n.t("routes.legend.title")}_#{l["id"]} (#{l["status"]}) #{DateTime.parse(l["created_at"])&.strftime(Aromancer::Api::DATE_FORMAT)}",
278
305
  value: l["id"]
279
306
  }
280
307
  }
281
308
 
282
- Thread.main[:input_controls] = {
283
- choices: choices,
284
- runners: [
285
- {value: :back, proc: Proc.new{ main_menu }},
286
- ] + legend_array.map{|l|
287
- {
288
- value: l["id"],
289
- proc: Proc.new{
290
- Aromancer::Api.set_active_legend_id(l["id"])
309
+ if Aromancer::Storage.get_preference(:debug)
310
+ Aromancer::Prompt.say(JSON.pretty_generate(legend_array))
311
+ end
312
+
313
+ Thread.main[:menu_runners][__method__.to_sym] = [
314
+ {value: :back, proc: Proc.new{ main_menu }},
315
+ ] + legend_array.map{|l|
316
+ {
317
+ value: l["id"],
318
+ proc: Proc.new{
319
+ Aromancer::Api.set_active_legend_id(l["id"])
320
+
321
+ unless active_legend.nil? || active_legend["id"] != Aromancer::Storage.get_player["active_legend_id"]
322
+ Aromancer.print_last_error
323
+ sleep(1)
324
+ main_menu
325
+ else
291
326
  legend_menu
292
- }
327
+ end
293
328
  }
294
329
  }
295
330
  }
296
- Thread.main[:route] = __method__.to_s
331
+ Thread.main[:route] = __method__.to_sym
297
332
  end
298
333
 
299
334
  def self.stream
300
- # todo: this needs to be set as a view mode
301
- # currently it gets overwritten
302
335
  Aromancer::Api.load_stream
303
336
  stream = Aromancer::Storage.get_stream
304
337
  Aromancer::Prompt.say(I18n.t("routes.stream.title"))
@@ -312,11 +345,11 @@ module Aromancer
312
345
  Aromancer::Prompt.say([e["updated_at"], e["description"]].join("\n"))
313
346
  }
314
347
 
315
- Thread.main[:input_controls] = {
316
- choices: [{name: I18n.t("shared.key_press"), value: nil}],
317
- runners: [{value: nil, proc: Proc.new{ legend_menu }}]
318
- }
319
- Thread.main[:route] = __method__.to_s
348
+ Thread.main[:menu_options][__method__.to_sym] ||= [{name: I18n.t("shared.key_press"), value: nil}]
349
+ Thread.main[:menu_runners][__method__.to_sym] ||= [
350
+ {value: nil, proc: Proc.new{ legend_menu }}
351
+ ]
352
+ Thread.main[:route] = __method__.to_sym
320
353
  end
321
354
 
322
355
  def self.legend_menu
@@ -329,28 +362,27 @@ module Aromancer
329
362
  if al["status"] == "complete"
330
363
  Aromancer::Api.set_active_legend_id(nil)
331
364
  Aromancer::Prompt.say(I18n.t("routes.legend.legend_complete"))
332
- # Aromancer::Prompt.p.ask(I18n.t("shared.key_press"))
333
365
  return legends
334
366
  elsif al["status"] == "created"
335
- choices = [{name: :reload, value: :reload}, {name: I18n.t("shared.back"), value: :back}]
336
- Thread.main[:input_controls] = {
337
- choices: choices,
338
- runners: [
339
- {value: :reload, proc: Proc.new{ legend_menu }},
340
- {value: :back, proc: Proc.new{
341
- Aromancer::Api.set_active_legend_id(nil)
342
- main_menu
343
- }}
344
- ]
345
- }
346
- Thread.main[:route] = __method__.to_s
367
+ Thread.main[:menu_options][__method__.to_sym] = [
368
+ {name: :reload, value: :reload}, {name: I18n.t("shared.back"), value: :back}
369
+ ]
370
+ Thread.main[:menu_runners][__method__.to_sym] ||= [
371
+ {value: :reload, proc: Proc.new{ legend_menu }},
372
+ {value: :back, proc: Proc.new{
373
+ Aromancer::Api.set_active_legend_id(nil)
374
+ main_menu
375
+ }}
376
+ ]
377
+ Thread.main[:route] = __method__.to_sym
347
378
  return
348
379
  end
349
380
  end
350
-
351
- hs = Aromancer::Storage.get_preference(:hide_sentences, default: true)
381
+ hs = Aromancer::Storage.get_preference(:hide_sentences)
352
382
  hs_value = "#{hs ? "" : "hide_"}signed_sentences".to_sym
353
- choices = [
383
+
384
+ Thread.main[:route] = __method__.to_sym
385
+ Thread.main[:menu_options][__method__.to_sym] = [
354
386
  {name: I18n.t("routes.legend.unspoken_words"), value: :words},
355
387
  {name: I18n.t("routes.legend.spoken_words"), value: :spoken_words},
356
388
  {name: I18n.t("routes.legend.unsigned_sentences"), value: :unsigned_sentences},
@@ -359,56 +391,62 @@ module Aromancer
359
391
  {name: I18n.t("routes.main_menu.title"), value: :main_menu}
360
392
  ]
361
393
 
362
- Thread.main[:input_controls] = {
363
- choices: choices,
364
- runners: [
365
- {value: :words, proc: Proc.new{ words }},
366
- {value: :spoken_words, proc: Proc.new{ spoken_words }},
367
- {value: :unsigned_sentences, proc: Proc.new{ unsigned_sentences }},
368
- {value: hs_value, proc: Proc.new{
369
- Aromancer::Storage.set_preference(:hide_sentences, !hs)
370
- legend_menu
371
- }},
372
- {value: :stream, proc: Proc.new{ stream }},
373
- {value: :main_menu, proc: Proc.new{
374
- Aromancer::Api.set_active_legend_id(nil)
375
- main_menu
376
- }},
377
- ]
378
- }
379
- Thread.main[:route] = __method__.to_s
394
+ Thread.main[:menu_runners][__method__.to_sym] = [
395
+ {value: :words, proc: Proc.new{ words }},
396
+ {value: :spoken_words, proc: Proc.new{ spoken_words }},
397
+ {value: :unsigned_sentences, proc: Proc.new{ unsigned_sentences }},
398
+ {value: hs_value, proc: Proc.new{
399
+ Aromancer::Storage.toggle_preference(:hide_sentences)
400
+ legend_menu
401
+ }},
402
+ {value: :stream, proc: Proc.new{ stream }},
403
+ {value: :main_menu, proc: Proc.new{
404
+ Aromancer::Api.set_active_legend_id(nil)
405
+ main_menu
406
+ }},
407
+ ]
380
408
  end
381
409
 
382
410
  def self.legend
383
- Aromancer::Prompt.say(I18n.t("shared.navigate", route: :legend))
411
+ Aromancer::Prompt.say(I18n.t("shared.navigate", route: :legend)) if Aromancer::Storage.get_preference(:debug)
384
412
  Aromancer::Api.load_arena
385
413
  arena = arena_data
386
414
  al = active_legend
387
- if !al.nil? && al["status"] == "created"
415
+ al_status = ((al || {})["status"] || "").to_sym
416
+ if !al.nil? &&
417
+ [:created, :complete].include?(al_status)
388
418
  Aromancer::Prompt.say(arena["title"])
419
+ Aromancer::Prompt.say(I18n.t("routes.legend.title"))
420
+ Aromancer::Prompt.say(I18n.t("routes.legend.subtitle", subtitle: al["status"]))
389
421
  Aromancer::Prompt.say(I18n.t("routes.legend.players"))
390
- Aromancer::Prompt.say(arena["players"].map{|p| p["alias"]})
391
- # Aromancer::Prompt.p.ask(I18n.t("shared.key_press"))
392
- return
393
- elsif !al.nil? && al["status"] == "complete"
394
- Aromancer::Api.set_active_legend_id(nil)
395
- main_menu
422
+ Aromancer::Prompt.say(JSON.pretty_generate(arena["players"]))
423
+ Aromancer::Prompt.p.ask(I18n.t("shared.key_press"))
424
+ if al_status == :complete
425
+ Aromancer::Api.set_active_legend_id(nil)
426
+ sleep(1)
427
+ return main_menu
428
+ else
429
+ Aromancer::Api.get_legends
430
+ legend_menu
431
+ end
396
432
  end
397
433
 
398
434
  return if arena.nil? || arena["turn_player"].nil?
399
435
 
400
- # puts arena.to_json
436
+ # todo: pre-calculate needed widths. this isn't really working
437
+ word_width = 5.0
438
+ wpr = (TTY::Screen.width / word_width).floor
439
+
440
+ # puts "wpr: #{wpr}"
441
+ # puts TTY::Screen.width
442
+
401
443
  turn_player = arena["turn_player"]
402
444
 
403
445
  # title
404
446
  Aromancer::Prompt.say("\n\n")
405
447
  data = [
406
448
  [arena["title"]],
407
- ["#{
408
- turn_player["id"] == Aromancer::Storage.get_player["id"] ?
409
- "your" :
410
- "#{turn_player["alias"]}'s"
411
- } turn"],
449
+ ["#{turn_player["alias"]}'s turn"],
412
450
  ["dealer is #{arena["dealer"]["alias"]}"]
413
451
  ]
414
452
  title_table = TTY::Table.new(data)
@@ -416,51 +454,52 @@ module Aromancer
416
454
 
417
455
  # players
418
456
  data = [
419
- [I18n.t("routes.legend.players")] + arena["players"].map{|p| p["alias"]}
457
+ [I18n.t("routes.legend.players")] + arena["players"].map{|p| "#{p["alias"]} (#{p["score"]})"}
420
458
  ]
421
459
  players_table = TTY::Table.new(data)
422
460
  Aromancer::Prompt.say(players_table.render(:ascii))
423
461
 
424
462
  # spoken words
425
463
  Aromancer::Prompt.say(I18n.t("routes.legend.spoken_words"))
426
- data = [
427
- arena["scroll_words"].map{|sw| sw["value_display"]}
428
- ]
464
+
465
+ data = arena["scroll_words"].map{|sw| sw["value_display"]}.each_slice(wpr).to_a || []
466
+ while data.any? && data.last.length < data.first.length
467
+ data.last << ""
468
+ end
429
469
  spoken_words_table = TTY::Table.new(data)
430
470
  Aromancer::Prompt.say(spoken_words_table.render(:unicode))
431
471
 
432
472
  unless arena["sentences"].empty?
433
473
  # unsigned sentences
434
474
  Aromancer::Prompt.say(I18n.t("routes.legend.unsigned_sentences"))
435
- data = [
436
- arena["sentences"].map{|sw| "#{sw["value"]} (#{sw["words"].map{|w| w["value_display"]}.sort.join(", ")})"}
437
- ]
475
+ data = arena["sentences"].map{|sw| sw["value_display"]}.each_slice(wpr).to_a || []
476
+ while data.any? && data.last.length < data.first.length
477
+ data.last << ""
478
+ end
438
479
  unsigned_sentences_table = TTY::Table.new(data)
439
480
  Aromancer::Prompt.say(unsigned_sentences_table.render(:unicode))
440
481
  end
441
482
 
442
483
  # unspoken words
443
484
  Aromancer::Prompt.say(I18n.t("routes.legend.unspoken_words"))
444
- data = [
445
- arena["player"]["words"].map{|w| w["value_display"]}
446
- ]
485
+ data = arena["player"]["words"].map{|w| w["value_display"]}.each_slice(wpr).to_a || []
486
+ while data.any? && data.last.length < data.first.length
487
+ data.last << ""
488
+ end
447
489
  words_table = TTY::Table.new(data)
448
490
  Aromancer::Prompt.say(words_table.render(:ascii))
449
491
 
450
- unless arena["player"]["sentences"].empty? || Aromancer::Storage.get_preference(:hide_sentences, default: true)
492
+ unless arena["player"]["sentences"].empty?
451
493
  # signed sentences
452
- Aromancer::Prompt.say(I18n.t("routes.legend.signed_sentences"))
453
- data = [
454
- arena["player"]["sentences"].map{|s|
455
- "#{s["value"]}: " + (
456
- [s["signature"]["value_display"]] + s["words"].map{|w|
457
- w["value_display"]
458
- }
459
- ).join(", ")
460
- }
461
- ]
462
- sentences_table = TTY::Table.new(data)
463
- Aromancer::Prompt.say(sentences_table.render(:ascii))
494
+ Aromancer::Prompt.say("#{I18n.t("routes.legend.signed_sentences")} (#{arena["player"]["sentences"].length})")
495
+ unless Aromancer::Storage.get_preference(:hide_sentences)
496
+ data = arena["player"]["sentences"].map{|s| s["value_display"]}.each_slice(wpr).to_a || []
497
+ while data.any? && data.last.length < data.first.length
498
+ data.last << ""
499
+ end
500
+ sentences_table = TTY::Table.new(data)
501
+ Aromancer::Prompt.say(sentences_table.render(:ascii))
502
+ end
464
503
  end
465
504
  end
466
505
 
@@ -469,19 +508,18 @@ module Aromancer
469
508
  legend
470
509
 
471
510
  arena = arena_data
472
- choices = [{name: I18n.t("shared.back"), value: :back}] + arena["player"]["words"].map{|w| {name: w["value_display"], value: w["id"]}}
473
-
474
- Thread.main[:input_controls] = {
475
- choices: choices,
476
- runners: [
477
- {value: :back, proc: Proc.new{ legend_menu }}
478
- ] + arena["player"]["words"].map{|w|
479
- {value: w["id"], proc: Proc.new{
480
- word(w["id"])
481
- }}
482
- }
511
+ Thread.main[:menu_options][__method__.to_sym] = [
512
+ {name: I18n.t("shared.back"), value: :back}
513
+ ] + arena["player"]["words"].map{|w| {name: w["value_display"], value: w["id"]}}
514
+
515
+ Thread.main[:menu_runners][__method__.to_sym] = [
516
+ {value: :back, proc: Proc.new{ legend_menu }}
517
+ ] + arena["player"]["words"].map{|w|
518
+ {value: w["id"], proc: Proc.new{
519
+ word(w["id"])
520
+ }}
483
521
  }
484
- Thread.main[:route] = __method__.to_s
522
+ Thread.main[:route] = __method__.to_sym
485
523
  end
486
524
 
487
525
  def self.spoken_words
@@ -489,19 +527,18 @@ module Aromancer
489
527
  legend
490
528
 
491
529
  arena = arena_data
492
- choices = [{name: I18n.t("shared.back"), value: :back}] + arena["scroll_words"].map{|w| {name: w["value_display"], value: w["id"]}}
493
-
494
- Thread.main[:input_controls] = {
495
- choices: choices,
496
- runners: [
497
- {value: :back, proc: Proc.new{ legend_menu }}
498
- ] + arena["scroll_words"].map{|w|
499
- {value: w["id"], proc: Proc.new{
500
- word(w["id"])
501
- }}
502
- }
530
+ Thread.main[:menu_options][__method__.to_sym] = [
531
+ {name: I18n.t("shared.back"), value: :back}
532
+ ] + arena["scroll_words"].map{|w| {name: w["value_display"], value: w["id"]}}
533
+
534
+ Thread.main[:menu_runners][__method__.to_sym] = [
535
+ {value: :back, proc: Proc.new{ legend_menu }}
536
+ ] + arena["scroll_words"].map{|w|
537
+ {value: w["id"], proc: Proc.new{
538
+ word(w["id"])
539
+ }}
503
540
  }
504
- Thread.main[:route] = __method__.to_s
541
+ Thread.main[:route] = __method__.to_sym
505
542
  end
506
543
 
507
544
  def self.unsigned_sentences
@@ -509,37 +546,30 @@ module Aromancer
509
546
  legend
510
547
 
511
548
  arena = arena_data
512
- choices = [{name: I18n.t("shared.back"), value: :back}] + arena["sentences"].map{|s| {name: "id: #{s["id"]}, value: #{s["value"]}", value: s["id"]}}
513
-
514
- Thread.main[:input_controls] = {
515
- choices: choices,
516
- runners: [
517
- {value: :back, proc: Proc.new{ legend_menu }}
518
- ] + arena["sentences"].map{|s|
519
- ss_words = s["words"]
520
- wid = ss_words.first["id"]
521
- {value: s["id"], proc: Proc.new{
522
-
523
- # todo: probably do not need this check
524
- # if ss_words.nil? || ss_words.empty?
525
- # legend_menu
526
- # else
527
- word(wid)
528
- # end
529
- }}
530
- }
549
+ Thread.main[:menu_options][__method__.to_sym] = [
550
+ {name: I18n.t("shared.back"), value: :back}
551
+ ] + arena["sentences"].map{|s| {name: "id: #{s["id"]}, value: #{s["value"]}", value: s["id"]}}
552
+
553
+ Thread.main[:menu_runners][__method__.to_sym] = [
554
+ {value: :back, proc: Proc.new{ legend_menu }}
555
+ ] + arena["sentences"].map{|s|
556
+ ss_words = s["words"]
557
+ wid = ss_words.first["id"]
558
+
559
+ {value: s["id"], proc: Proc.new{word(wid)}}
531
560
  }
532
- Thread.main[:route] = __method__.to_s
561
+ Thread.main[:route] = __method__.to_sym
533
562
  end
534
563
 
535
564
  def self.word(selected_word_id)
536
565
  return legend_menu if selected_word_id.nil?
537
566
 
538
- Aromancer::Prompt.say(I18n.t("shared.navigate", route: :word))
567
+ Aromancer::Prompt.say(I18n.t("shared.navigate", route: :word)) if Aromancer::Storage.get_preference(:debug)
539
568
  possibilities = Aromancer::Api.load_word_possibilities(selected_word_id)
540
569
 
541
- # todo: hide/show this
542
- # puts JSON.pretty_generate possibilities
570
+ if Aromancer::Storage.get_preference(:debug)
571
+ Aromancer::Prompt.say(JSON.pretty_generate(possibilities))
572
+ end
543
573
 
544
574
  options = possibilities["options"]
545
575
  selected_word = possibilities["word"]
@@ -548,73 +578,51 @@ module Aromancer
548
578
  arena = arena_data
549
579
  player_words = arena["player"]["words"]
550
580
 
551
- option_display = Proc.new{|o, sw|
552
- a = o["action"].humanize.downcase
553
- o_value = ""
554
- o_value_display = ""
555
- if o["sentence"].nil?
556
- o_value = o["word"]["value"]
557
- o_value_display = o["word"]["value_display"]
558
- else
559
- o_value = o["sentence"]["value"]
560
- o_value_display = "#{o_value} (#{I18n.t("routes.legend.sentences")})"
561
- end
562
- # todo: this sucks
563
- # case a
564
- # when :can_sign
565
- # when :can_combine
566
- # when :can_stack
567
- # end
568
- calc = "(#{sw["value"]} + #{o_value} = #{sw["value"] + o_value})"
569
-
570
- "#{sw["value_display"]} #{a} #{o_value_display} #{calc}"
571
- }
572
- place_title = I18n.t("routes.legend.place_on_scroll", word: selected_word["value_display"])
573
- choices = [
574
- {name: I18n.t("shared.back"), value: :back}
575
- ]
581
+ choices = [{name: I18n.t("shared.back"), value: :back}]
576
582
  if for_type == :player_word
583
+
577
584
  # show place on scroll option if selected word is from player's words
578
- choices << {name: place_title, value: :player_word}
585
+ choices << {
586
+ name: I18n.t("routes.legend.place_on_scroll", word: selected_word["value_display"]),
587
+ value: :player_word
588
+ }
579
589
  end
580
- choices += options.map{|o| {name: option_display.call(o, selected_word), value: o}}
581
-
582
- Thread.main[:input_controls] = {
583
- choices: choices,
584
- runners: [
585
- {value: :back, proc: Proc.new{
586
- case for_type
587
- when :player_word
588
- words
589
- when :scroll_word
590
- spoken_words
591
- when :scroll_sentence
592
- unsigned_sentences
593
- end
594
- }},
595
- {value: :player_word, proc: Proc.new{
596
- wytyd = I18n.t("routes.legend.place_on_scroll", word: selected_word["value_display"])
597
- Aromancer::Prompt.say(wytyd)
598
- Aromancer::Api.create_turn({
599
- wytyd: wytyd,
600
- word_id: selected_word["id"]
601
- })
602
- legend_menu
603
- }},
604
- {value: nil, proc: Proc.new{|o|
605
- if o["http_verb"] == "post"
606
- Aromancer::Api.create_turn(JSON.parse(o["payload"]))
607
- else
608
- Aromancer::Api.update_turn(JSON.parse(o["payload"]))
609
- end
610
590
 
611
- legend_menu
612
- }}
613
- ]
614
- }
591
+ choices += options.map{|o| {name: "#{o["humanized_title"]} -> weight: #{o["total_weight"]}", value: o}}
592
+
593
+ Thread.main[:menu_options][__method__.to_sym] = choices
594
+ Thread.main[:menu_runners][__method__.to_sym] = [
595
+ {value: :back, proc: Proc.new{
596
+ case for_type
597
+ when :player_word
598
+ words
599
+ when :scroll_word
600
+ spoken_words
601
+ when :scroll_sentence
602
+ unsigned_sentences
603
+ end
604
+ }},
605
+ {value: :player_word, proc: Proc.new{
606
+ wytyd = I18n.t("routes.legend.place_on_scroll", word: selected_word["value_display"])
607
+ Aromancer::Prompt.say(wytyd)
608
+ Aromancer::Api.create_turn({
609
+ wytyd: wytyd,
610
+ word_id: selected_word["id"]
611
+ })
612
+ legend_menu
613
+ }},
614
+ {value: nil, proc: Proc.new{|o|
615
+ if o["http_verb"] == "post"
616
+ Aromancer::Api.create_turn(JSON.parse(o["payload"]))
617
+ else
618
+ Aromancer::Api.update_turn(JSON.parse(o["payload"]))
619
+ end
615
620
 
616
- Thread.main[:route] = __method__.to_s
621
+ legend_menu
622
+ }}
623
+ ]
617
624
  Thread.main[:route_params] = [selected_word_id]
625
+ Thread.main[:route] = __method__.to_sym
618
626
  end
619
627
  end
620
628
  end