-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
73 lines (63 loc) · 1.81 KB
/
app.rb
File metadata and controls
73 lines (63 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require_relative './game'
require 'rack'
require 'erb'
require 'byebug'
require 'json'
require 'open3'
require 'io/wait'
DEFAULT_PROMPT = " player game initiated, you are white. It's your move!"
class App
attr_accessor :game, :player_qty, :rendered
def initialize
@rendered = 0
@game = Game.new(1)
@player_qty
end
def generate_html(board)
index = File.open('index.html')
index_arr = index.read.split('<body>')
index.close
return [index_arr[0], '<body>', board ,'</div>', index_arr[1]].join
end
def insert_error(error, resp)
msg_div = "<div id=\"messages\">"
error = "<div class=\"errors\">" + error + "</div>"
resp_arr = resp.split(msg_div)
[resp_arr[0], msg_div, error, resp_arr[1]].join
end
def player_game(num, prompt = DEFAULT_PROMPT)
@player_qty = num
Proc.new do |env|
req = Rack::Request.new(env)
res = Rack::Response.new
num_str = num == 1 ? "One" : "Two"
prompt_str = "#{num_str}" + prompt
@player_qty = num
@game = Game.new(num)
html_resp = generate_html(@game.display.render)
rendered_html = insert_error(prompt_str, html_resp)
res['Content-Type'] = 'text/html'
res.write(rendered_html)
res.finish
end
end
def gameplay
Proc.new do |env|
req = Rack::Request.new(env)
res = Rack::Response.new
if req.request_method == "POST"
res['Content-Type'] = 'application/json'
move = JSON.parse(req.body.read)["move"]
outcome = @game.play_move(move).merge({players: @player_qty})
res.write(JSON.generate(outcome))
res.finish
else
@game = Game.new(@player_qty)
res['Content-Type'] = 'text/html'
my_test = generate_html(@game.display.render)
res.write(my_test)
res.finish
end
end
end
end