aromancer 0.0.5 → 0.0.7

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