Add game creation on DB query
This commit is contained in:
parent
2ec51a45ea
commit
9c3665ea5c
BIN
game_saves.db
Normal file
BIN
game_saves.db
Normal file
Binary file not shown.
|
@ -31,7 +31,7 @@ body {
|
|||
.board-cell {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
padding: 5.55556%; }
|
||||
padding: 5.5555555556%; }
|
||||
.board-cell .overlay {
|
||||
position: absolute;
|
||||
box-sizing: border-box;
|
||||
|
@ -42,11 +42,11 @@ body {
|
|||
border-radius: 50%;
|
||||
transition: background-color .25s; }
|
||||
.board-cell.small {
|
||||
padding: 5.55556%; }
|
||||
padding: 5.5555555556%; }
|
||||
.board-cell.medium {
|
||||
padding: 3.84615%; }
|
||||
padding: 3.8461538462%; }
|
||||
.board-cell.large {
|
||||
padding: 2.63158%; }
|
||||
padding: 2.6315789474%; }
|
||||
|
||||
.black-cell .overlay {
|
||||
background-color: black; }
|
||||
|
|
45
src/Go.cr
45
src/Go.cr
|
@ -13,30 +13,47 @@ GAME_CACHE = {} of String => Go::Game
|
|||
def save_game(db, gameid, game)
|
||||
puts "Saving"
|
||||
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|
|
||||
# When using the pg driver, use $1, $2, etc. instead of ?
|
||||
#db.exec "create table game_saves (gameid integer, turn integer, size integer, board string )"
|
||||
db.exec "insert into game_saves values (?, ?, ?, ?)", gameid, turn, size, board
|
||||
# TODO: Table creation function
|
||||
# Creates table
|
||||
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
|
||||
|
||||
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|
|
||||
puts "contacts:"
|
||||
db.query "select gameid, turn, size, board from game_saves order by gameid desc" do |rs|
|
||||
puts "Saves:"
|
||||
db.query "select gameid, turn, size, white_pass, black_pass, board from game_saves where gameid = #{gameid}" do |rs|
|
||||
rs.each do
|
||||
puts rs.size
|
||||
#puts "#{rs.read(Int32)} (#{rs.read(Int32)}) (#{rs.read(Int32)}) (#{rs.read(String)})"
|
||||
end
|
||||
puts rs
|
||||
# puts "#{rs.column_name(0)} (#{rs.column_name(1)})"
|
||||
#rs.each do
|
||||
#puts "#{rs.read(String)} (#{rs.read(Int32)})"
|
||||
#end
|
||||
id = rs.read(Int32)
|
||||
|
||||
turn = rs.read(Int32)
|
||||
size = rs.read(Int32)
|
||||
white_pass = rs.read(String)
|
||||
black_pass = rs.read(String)
|
||||
board = rs.read(String)
|
||||
puts turn
|
||||
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
|
||||
end
|
||||
|
||||
|
|
|
@ -20,6 +20,14 @@ module Go
|
|||
property turn : Color
|
||||
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)
|
||||
@size = size
|
||||
@board = Board.new
|
||||
|
@ -133,7 +141,7 @@ module Go
|
|||
end
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user