2018-05-25 12:15:57 -07:00
|
|
|
require "./Go/*"
|
2018-05-25 12:43:30 -07:00
|
|
|
require "kemal"
|
2018-05-25 13:48:01 -07:00
|
|
|
require "json"
|
2018-05-25 12:15:57 -07:00
|
|
|
|
2018-05-25 12:43:30 -07:00
|
|
|
URL = "localhost"
|
2018-05-25 15:29:22 -07:00
|
|
|
GAME_CACHE = {} of String => Go::Game
|
2018-05-25 13:48:01 -07:00
|
|
|
|
2018-05-25 15:29:22 -07:00
|
|
|
def query_game(db, id) : Go::Game?
|
2018-05-25 13:48:01 -07:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2018-05-25 15:29:22 -07:00
|
|
|
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
|
|
|
|
|
2018-05-25 13:48:01 -07:00
|
|
|
def handle_message(id, game, socket, message)
|
|
|
|
split_command = message.split(" ")
|
|
|
|
command = split_command[0]
|
|
|
|
if command == "place"
|
|
|
|
x = split_command[1].to_i8
|
|
|
|
y = split_command[2].to_i8
|
2018-05-25 15:03:04 -07:00
|
|
|
color = split_command[3] == "Black" ? Go::Color::Black : Go::Color::White
|
2018-05-25 13:48:01 -07:00
|
|
|
|
|
|
|
game.update(x, y, color)
|
|
|
|
game.sockets.each { |socket| socket.send game.to_string }
|
|
|
|
end
|
|
|
|
end
|
2018-05-25 12:43:30 -07:00
|
|
|
|
|
|
|
get "/" do |env|
|
2018-05-25 13:48:01 -07:00
|
|
|
"Hello!"
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/game/:id" do |env|
|
2018-05-25 15:29:22 -07:00
|
|
|
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
|
2018-05-25 13:48:01 -07:00
|
|
|
black = true
|
2018-05-25 15:29:22 -07:00
|
|
|
elsif game_password == game.whitePass
|
|
|
|
black = false
|
2018-05-25 13:48:01 -07:00
|
|
|
end
|
2018-05-25 15:29:22 -07:00
|
|
|
|
2018-05-25 15:35:55 -07:00
|
|
|
black.try { |black| render "src/Go/views/game.ecr", "src/Go/views/base.ecr"} || render_404
|
2018-05-25 13:48:01 -07:00
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ws "/game/:id" do |socket, env|
|
2018-05-25 15:29:22 -07:00
|
|
|
game_id = env.params.url["id"]
|
|
|
|
if game = lookup_game(nil, GAME_CACHE, game_id)
|
|
|
|
socket.send game.to_string
|
|
|
|
game.sockets << socket
|
2018-05-25 13:48:01 -07:00
|
|
|
|
2018-05-25 15:29:22 -07:00
|
|
|
socket.on_message do |message|
|
|
|
|
game.try { |game| handle_message(game_id, game, socket, message) }
|
|
|
|
end
|
2018-05-25 13:48:01 -07:00
|
|
|
|
2018-05-25 15:29:22 -07:00
|
|
|
socket.on_close do
|
|
|
|
game.try { |game| game.sockets.delete socket }
|
2018-05-25 13:48:01 -07:00
|
|
|
end
|
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
2018-05-25 12:43:30 -07:00
|
|
|
end
|
|
|
|
|
2018-05-25 15:29:22 -07:00
|
|
|
GAME_CACHE["debug"] = Go::Game.new(Go::Size::Small, "black", "white")
|
2018-05-25 13:48:01 -07:00
|
|
|
|
2018-05-25 12:43:30 -07:00
|
|
|
Kemal.run
|