Add session creation and joining.

This commit is contained in:
Danila Fedorin 2018-05-25 23:36:52 -07:00
parent 3ee9274f49
commit ea96fbd680
2 changed files with 44 additions and 6 deletions

View File

@ -19,6 +19,10 @@ def lookup_game(db, cache, id) : Go::Game?
end end
end end
def create_game(db, cache, game, id)
cache[id] = game
end
def handle_message(id, game, socket, message) def handle_message(id, game, socket, message)
split_command = message.split(" ") split_command = message.split(" ")
command = split_command[0] command = split_command[0]
@ -36,10 +40,12 @@ get "/" do |env|
render "src/Go/views/index.ecr", "src/Go/views/base.ecr" render "src/Go/views/index.ecr", "src/Go/views/base.ecr"
end end
get "/game/:id" do |env| post "/game" do |env|
game_id = env.params.url["id"] game_id = env.params.body["id"]?
game_password = env.params.query["password"]? game_password = env.params.body["password"]?
if game = lookup_game(nil, GAME_CACHE, game_id) if game_id == nil || game_password == nil
render_404
elsif game = lookup_game(nil, GAME_CACHE, game_id)
id = game_id id = game_id
size = game.size.value size = game.size.value
black = nil black = nil
@ -56,6 +62,38 @@ get "/game/:id" do |env|
end end
end end
post "/create" do |env|
game_id = env.params.body["id"]?
user_password = env.params.body["your-password"]?
other_password = env.params.body["their-password"]?
color = env.params.body["color"]?
color_e = nil
if color == "black"
color_e = Go::Color::Black
elsif color == "white"
color_e = Go::Color::White
end
if game_id == nil || user_password == nil || other_password == nil || color == nil || color_e == nil
render_404
elsif game = lookup_game(nil, GAME_CACHE, game_id)
render_404
else
color_e = color_e.as(Go::Color)
user_password = user_password.as(String)
other_password = other_password.as(String)
if color_e == Go::Color::Black
white_pass, black_pass = other_password, user_password
else
white_pass, black_pass = user_password, other_password
end
game = Go::Game.new(Go::Size::Small, black_pass, white_pass)
create_game(nil, GAME_CACHE, game, game_id.as(String))
"Created!"
end
end
ws "/game/:id" do |socket, env| ws "/game/:id" do |socket, env|
game_id = env.params.url["id"] game_id = env.params.url["id"]
if game = lookup_game(nil, GAME_CACHE, game_id) if game = lookup_game(nil, GAME_CACHE, game_id)

View File

@ -1,7 +1,7 @@
<div class="split-wrapper"> <div class="split-wrapper">
<div class="split-item"> <div class="split-item">
<h2>Create Game</h2> <h2>Create Game</h2>
<form autocomplete="off" id="create-form" action="/create"> <form autocomplete="off" id="create-form" action="/create" method="post">
<input required placeholder="Session Name" type="text" name="id"></input> <input required placeholder="Session Name" type="text" name="id"></input>
<input required placeholder="Your Password" type="text" name="your-password"></input> <input required placeholder="Your Password" type="text" name="your-password"></input>
<input required placeholder="Their Password" type="text" name="their-password"></input> <input required placeholder="Their Password" type="text" name="their-password"></input>
@ -20,7 +20,7 @@
</div> </div>
<div class="split-item"> <div class="split-item">
<h2>Join Game</h2> <h2>Join Game</h2>
<form autocomplete="off" id="join-form" action="/game"> <form autocomplete="off" id="join-form" action="/game" method="post">
<input required placeholder="Session Name" type="text" name="id"></input> <input required placeholder="Session Name" type="text" name="id"></input>
<input required placeholder="Your Password" type="text" name="password"></input> <input required placeholder="Your Password" type="text" name="password"></input>
<input type="submit" value="Join Game"></input> <input type="submit" value="Join Game"></input>