Move game into separate source file and add encode method.
This commit is contained in:
parent
6287968404
commit
f01c891cec
69
src/Go.cr
69
src/Go.cr
|
@ -2,71 +2,10 @@ require "./Go/*"
|
|||
require "kemal"
|
||||
require "json"
|
||||
|
||||
enum Color
|
||||
Black
|
||||
White
|
||||
end
|
||||
|
||||
enum Size
|
||||
Small = 9,
|
||||
Medium = 13,
|
||||
Large = 19
|
||||
end
|
||||
|
||||
alias Board = Hash(Tuple(Int8, Int8), Color)
|
||||
|
||||
|
||||
def cell_json(index, color, json)
|
||||
json.object do
|
||||
json.field "index" do
|
||||
json.object do
|
||||
json.field "x", index[0]
|
||||
json.field "y", index[1]
|
||||
end
|
||||
end
|
||||
json.field "color", color.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def board_json(board, json)
|
||||
json.array do
|
||||
board.each do |key, value|
|
||||
cell_json(key, value, json)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Game
|
||||
property size : Size
|
||||
property board : Board
|
||||
property turn : Color
|
||||
property sockets : Array(HTTP::WebSocket)
|
||||
|
||||
def initialize(size : Size)
|
||||
@size = size
|
||||
@board = Board.new
|
||||
@turn = Color::Black
|
||||
@sockets = [] of HTTP::WebSocket
|
||||
end
|
||||
|
||||
def to_string
|
||||
JSON.build do |json|
|
||||
json.object do
|
||||
json.field "turn", @turn.to_s
|
||||
json.field "board" { board_json(@board, json) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update(x, y, color)
|
||||
@board[{x, y}] = color
|
||||
end
|
||||
end
|
||||
|
||||
URL = "localhost"
|
||||
GAME_CACHE = {} of Int64 => Game
|
||||
GAME_CACHE = {} of Int64 => Go::Game
|
||||
|
||||
def lookup_game(cache, id) : Game?
|
||||
def lookup_game(cache, id) : Go::Game?
|
||||
return nil
|
||||
end
|
||||
|
||||
|
@ -76,7 +15,7 @@ def handle_message(id, game, socket, message)
|
|||
if command == "place"
|
||||
x = split_command[1].to_i8
|
||||
y = split_command[2].to_i8
|
||||
color = split_command[3] == "Black" ? Color::Black : Color::White
|
||||
color = split_command[3] == "Black" ? Go::Color::Black : Go::Color::White
|
||||
|
||||
game.update(x, y, color)
|
||||
game.sockets.each { |socket| socket.send game.to_string }
|
||||
|
@ -123,6 +62,6 @@ ws "/game/:id" do |socket, env|
|
|||
end
|
||||
end
|
||||
|
||||
GAME_CACHE[1_i64] = Game.new(Size::Small)
|
||||
GAME_CACHE[1_i64] = Go::Game.new(Go::Size::Small)
|
||||
|
||||
Kemal.run
|
||||
|
|
81
src/Go/Game.cr
Normal file
81
src/Go/Game.cr
Normal file
|
@ -0,0 +1,81 @@
|
|||
module Go
|
||||
enum Color
|
||||
Black
|
||||
White
|
||||
end
|
||||
|
||||
enum Size
|
||||
Small = 9,
|
||||
Medium = 13,
|
||||
Large = 19
|
||||
end
|
||||
|
||||
alias Board = Hash(Tuple(Int8, Int8), Color)
|
||||
|
||||
class Game
|
||||
property size : Size
|
||||
property board : Board
|
||||
property turn : Color
|
||||
property sockets : Array(HTTP::WebSocket)
|
||||
|
||||
def initialize(size : Size)
|
||||
@size = size
|
||||
@board = Board.new
|
||||
@turn = Color::Black
|
||||
@sockets = [] of HTTP::WebSocket
|
||||
end
|
||||
|
||||
private def cell_json(index, color, json)
|
||||
json.object do
|
||||
json.field "index" do
|
||||
json.object do
|
||||
json.field "x", index[0]
|
||||
json.field "y", index[1]
|
||||
end
|
||||
end
|
||||
json.field "color", color.to_s
|
||||
end
|
||||
end
|
||||
|
||||
private def board_json(board, json)
|
||||
json.array do
|
||||
board.each do |key, value|
|
||||
cell_json(key, value, json)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def to_string
|
||||
JSON.build do |json|
|
||||
json.object do
|
||||
json.field "turn", @turn.to_s
|
||||
json.field "board" { board_json(@board, json) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update(x, y, color)
|
||||
@board[{x, y}] = color
|
||||
end
|
||||
|
||||
private def color_char(color)
|
||||
color == Color::Black ? 'B' : 'W'
|
||||
end
|
||||
|
||||
private def board_string(board)
|
||||
String.build do |str|
|
||||
(0...@size.value).each do |x|
|
||||
(0...@size.value).each do |y|
|
||||
color = @board[{x.to_i8, y.to_i8}]?
|
||||
char = color ? color_char(color) : 'E'
|
||||
str << char
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def encode
|
||||
{ @turn.to_s, @size.value, board_string(@board) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user