From 75a386539efe8c612f15d0a6f571e2b2a68fdabb Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 25 May 2018 15:29:22 -0700 Subject: [PATCH] Implement "password"-based session joining. --- external/GoUI | 2 +- public/js/Go.js | 9 +++---- src/Go.cr | 56 ++++++++++++++++++++++++++----------------- src/Go/Game.cr | 5 +++- src/Go/views/game.ecr | 5 +++- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/external/GoUI b/external/GoUI index 0d31433..4acfc09 160000 --- a/external/GoUI +++ b/external/GoUI @@ -1 +1 @@ -Subproject commit 0d31433b96e1f7e656243a44eaed97b01f8f7d80 +Subproject commit 4acfc0911a8864c517cb23712c76cddcbc561e69 diff --git a/public/js/Go.js b/public/js/Go.js index 3d6ae51..a33d625 100644 --- a/public/js/Go.js +++ b/public/js/Go.js @@ -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))); diff --git a/src/Go.cr b/src/Go.cr index 2d0483e..25aa9ce 100644 --- a/src/Go.cr +++ b/src/Go.cr @@ -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 diff --git a/src/Go/Game.cr b/src/Go/Game.cr index b6025f2..f388ac6 100644 --- a/src/Go/Game.cr +++ b/src/Go/Game.cr @@ -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) diff --git a/src/Go/views/game.ecr b/src/Go/views/game.ecr index 32f512d..f754d86 100644 --- a/src/Go/views/game.ecr +++ b/src/Go/views/game.ecr @@ -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 %> })