Implement "password"-based session joining.

This commit is contained in:
Danila Fedorin 2018-05-25 15:29:22 -07:00
parent f01c891cec
commit 75a386539e
5 changed files with 46 additions and 31 deletions

2
external/GoUI vendored

@ -1 +1 @@
Subproject commit 0d31433b96e1f7e656243a44eaed97b01f8f7d80
Subproject commit 4acfc0911a8864c517cb23712c76cddcbc561e69

View File

@ -9550,10 +9550,7 @@ var _user$project$Go_Ws$wsUrl = function (m) {
return A2(
_elm_lang$core$Basics_ops['++'],
m.sessionUrl,
A2(
_elm_lang$core$Basics_ops['++'],
'/game/',
_elm_lang$core$Basics$toString(m.sessionId)));
A2(_elm_lang$core$Basics_ops['++'], '/game/', m.sessionId));
};
var _user$project$Go_Ws$sendMove = F2(
function (m, c) {
@ -9655,7 +9652,7 @@ var _user$project$Main$initDummy = {
_user$project$Go_Types$Model,
_user$project$Go_Types$Black,
'ws://localhost:3000',
1,
'debug',
9,
_elm_lang$core$Maybe$Nothing,
_elm_lang$core$Maybe$Nothing,
@ -9698,7 +9695,7 @@ var _user$project$Main$main = _elm_lang$html$Html$programWithFlags(
},
A2(_elm_lang$core$Json_Decode$field, 'size', _elm_lang$core$Json_Decode$int));
},
A2(_elm_lang$core$Json_Decode$field, 'id', _elm_lang$core$Json_Decode$int));
A2(_elm_lang$core$Json_Decode$field, 'id', _elm_lang$core$Json_Decode$string));
},
A2(_elm_lang$core$Json_Decode$field, 'black', _elm_lang$core$Json_Decode$bool)));

View File

@ -3,12 +3,22 @@ require "kemal"
require "json"
URL = "localhost"
GAME_CACHE = {} of Int64 => Go::Game
GAME_CACHE = {} of String => Go::Game
def lookup_game(cache, id) : Go::Game?
def query_game(db, id) : Go::Game?
return nil
end
def lookup_game(db, cache, id) : Go::Game?
if game = cache[id]?
return game
else
loaded_game = query_game(db, id)
cache[id] = loaded_game if loaded_game
return loaded_game
end
end
def handle_message(id, game, socket, message)
split_command = message.split(" ")
command = split_command[0]
@ -27,41 +37,43 @@ get "/" do |env|
end
get "/game/:id" do |env|
if game_id = env.params.url["id"].to_i64?
if game = (GAME_CACHE[game_id]? || lookup_game(GAME_CACHE, game_id))
game_id = env.params.url["id"]
game_password = env.params.query["password"]?
if game = lookup_game(nil, GAME_CACHE, game_id)
id = game_id
size = game.size.value
black = nil
if game_password == game.blackPass
black = true
id = game_id
size = game.size.value
render "src/Go/views/game.ecr"
else
render_404
elsif game_password == game.whitePass
black = false
end
black.try { |black| render "src/Go/views/game.ecr"} || render_404
else
render_404
end
end
ws "/game/:id" do |socket, env|
if game_id = env.params.url["id"].to_i64?
if game = (GAME_CACHE[game_id]? || lookup_game(GAME_CACHE, game_id))
socket.send game.to_string
game.sockets << socket
game_id = env.params.url["id"]
if game = lookup_game(nil, GAME_CACHE, game_id)
socket.send game.to_string
game.sockets << socket
socket.on_message do |message|
game.try { |game| handle_message(game_id, game, socket, message) }
end
socket.on_message do |message|
game.try { |game| handle_message(game_id, game, socket, message) }
end
socket.on_close do
game.try { |game| game.sockets.delete socket }
end
else
render_404
socket.on_close do
game.try { |game| game.sockets.delete socket }
end
else
render_404
end
end
GAME_CACHE[1_i64] = Go::Game.new(Go::Size::Small)
GAME_CACHE["debug"] = Go::Game.new(Go::Size::Small, "black", "white")
Kemal.run

View File

@ -14,11 +14,13 @@ module Go
class Game
property size : Size
property blackPass : String
property whitePass : String
property board : Board
property turn : Color
property sockets : Array(HTTP::WebSocket)
def initialize(size : Size)
def initialize(size : Size, @blackPass, @whitePass)
@size = size
@board = Board.new
@turn = Color::Black
@ -56,6 +58,7 @@ module Go
def update(x, y, color)
@board[{x, y}] = color
@turn = @turn == Color::Black ? Color::White : Color::Black
end
private def color_char(color)

View File

@ -9,6 +9,9 @@
.black-cell {
background-color: black;
}
.white-cell {
background-color: white;
}
.board {
max-width: 360px;
}
@ -22,7 +25,7 @@
var app = Elm.Main.embed(node, {
'black' : <%= black %>,
'url' : "<%= "ws://" + URL + ":" + Kemal.config.port.to_s %>",
'id' : <%= id %>,
'id' : "<%= id %>",
'size' : <%= size %>
})
</script>