Add game creation on DB query

This commit is contained in:
sessionm21 2018-06-05 10:27:07 -07:00
parent 2ec51a45ea
commit 9c3665ea5c
5 changed files with 43 additions and 18 deletions

BIN
Go Executable file

Binary file not shown.

BIN
game_saves.db Normal file

Binary file not shown.

View File

@ -31,7 +31,7 @@ body {
.board-cell { .board-cell {
display: inline-block; display: inline-block;
position: relative; position: relative;
padding: 5.55556%; } padding: 5.5555555556%; }
.board-cell .overlay { .board-cell .overlay {
position: absolute; position: absolute;
box-sizing: border-box; box-sizing: border-box;
@ -42,11 +42,11 @@ body {
border-radius: 50%; border-radius: 50%;
transition: background-color .25s; } transition: background-color .25s; }
.board-cell.small { .board-cell.small {
padding: 5.55556%; } padding: 5.5555555556%; }
.board-cell.medium { .board-cell.medium {
padding: 3.84615%; } padding: 3.8461538462%; }
.board-cell.large { .board-cell.large {
padding: 2.63158%; } padding: 2.6315789474%; }
.black-cell .overlay { .black-cell .overlay {
background-color: black; } background-color: black; }

View File

@ -13,30 +13,47 @@ GAME_CACHE = {} of String => Go::Game
def save_game(db, gameid, game) def save_game(db, gameid, game)
puts "Saving" puts "Saving"
puts game.encode puts game.encode
turn, size, board = game.encode turn, size, white_pass, black_pass, board = game.encode
DB.open "sqlite3:./game_saves.db" do |db| DB.open "sqlite3:./game_saves.db" do |db|
# When using the pg driver, use $1, $2, etc. instead of ? # TODO: Table creation function
#db.exec "create table game_saves (gameid integer, turn integer, size integer, board string )" # Creates table
db.exec "insert into game_saves values (?, ?, ?, ?)", gameid, turn, size, board db.exec "create table game_saves (gameid integer, turn integer, size integer, white_pass string, black_pass string, board string )"
db.exec "insert into game_saves values (?, ?, ?, ?, ?, ?)", gameid, turn.value, size, white_pass, black_pass, board
end end
end end
def query_game(db, gameid) : Go::Game? def query_game(db, gameid) : Go::Game?
size = Go::Size::Small
white_pass = ""
black_pass = ""
board = ""
turn = ""
DB.open "sqlite3:./game_saves.db" do |db| DB.open "sqlite3:./game_saves.db" do |db|
puts "contacts:" puts "Saves:"
db.query "select gameid, turn, size, board from game_saves order by gameid desc" do |rs| db.query "select gameid, turn, size, white_pass, black_pass, board from game_saves where gameid = #{gameid}" do |rs|
rs.each do rs.each do
puts rs.size id = rs.read(Int32)
#puts "#{rs.read(Int32)} (#{rs.read(Int32)}) (#{rs.read(Int32)}) (#{rs.read(String)})"
end turn = rs.read(Int32)
puts rs size = rs.read(Int32)
# puts "#{rs.column_name(0)} (#{rs.column_name(1)})" white_pass = rs.read(String)
#rs.each do black_pass = rs.read(String)
#puts "#{rs.read(String)} (#{rs.read(Int32)})" board = rs.read(String)
#end puts turn
end end
end end
end
game = Go::Game.new()
game.size = Go::Size.from_value(size)
game.white_pass = white_pass
game.black_pass = black_pass
# Todo: Parse game board string
# game.board =
game.turn = Go::Color.from_value(turn)
# Todo: Fix return type mismatch
# return game
return nil return nil
end end

View File

@ -20,6 +20,14 @@ module Go
property turn : Color property turn : Color
property sockets : Array(HTTP::WebSocket) property sockets : Array(HTTP::WebSocket)
def initialize()
@size = Size::Small
@white_pass = ""
@black_pass = ""
@board = Board.new
@turn = Color::Black
@sockets = [] of HTTP::WebSocket
end
def initialize(size : Size, @black_pass, @white_pass) def initialize(size : Size, @black_pass, @white_pass)
@size = size @size = size
@board = Board.new @board = Board.new
@ -133,7 +141,7 @@ module Go
end end
def encode def encode
{ @turn.to_s, @size.value, board_string(@board) } { @turn, @size.value, @white_pass.to_s, @black_pass.to_s, board_string(@board) }
end end
end end
end end