Game loading completed
This commit is contained in:
parent
9c3665ea5c
commit
12c137ef4f
BIN
.sass-cache/2da5b7a9ed68f2839637c88aab8550dd9bb593bb/main.scssc
Normal file
BIN
.sass-cache/2da5b7a9ed68f2839637c88aab8550dd9bb593bb/main.scssc
Normal file
Binary file not shown.
BIN
game_saves.db
BIN
game_saves.db
Binary file not shown.
85
src/Go.cr
85
src/Go.cr
|
@ -8,70 +8,94 @@ require "sqlite3"
|
||||||
URL = "localhost"
|
URL = "localhost"
|
||||||
PORT = "3000"
|
PORT = "3000"
|
||||||
GAME_CACHE = {} of String => Go::Game
|
GAME_CACHE = {} of String => Go::Game
|
||||||
|
GAME_SAVE = "./game_saves.db"
|
||||||
|
|
||||||
def save_game(db, gameid, game)
|
def save_game(db, gameid, game)
|
||||||
puts "Saving"
|
# Function: save_game
|
||||||
puts game.encode
|
# Parameters: db(String)[Unused] gameid(String) game(Go::Game)
|
||||||
turn, size, white_pass, black_pass, board = game.encode
|
turn, size, white_pass, black_pass, board = game.encode
|
||||||
DB.open "sqlite3:./game_saves.db" do |db|
|
DB.open "sqlite3:./#{GAME_SAVE}" do |db|
|
||||||
# TODO: Table creation function
|
# Create table if one does not exist, gameid is UNIQUE => No duplicates
|
||||||
# Creates table
|
db.exec "create table if not exists game_saves (gameid string, turn integer, size integer, white_pass string, black_pass string, board string, UNIQUE(gameid) )"
|
||||||
db.exec "create table game_saves (gameid integer, turn integer, size integer, white_pass string, black_pass string, board string )"
|
# If duplicate => replace values, else => make new row for gameid
|
||||||
db.exec "insert into game_saves values (?, ?, ?, ?, ?, ?)", gameid, turn.value, size, white_pass, black_pass, board
|
db.exec "insert or replace 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?
|
||||||
|
# Function: query_game
|
||||||
|
# Parameters: db(String)[Unused] gameid(String)
|
||||||
|
turn = 0
|
||||||
size = Go::Size::Small
|
size = Go::Size::Small
|
||||||
white_pass = ""
|
white_pass = ""
|
||||||
black_pass = ""
|
black_pass = ""
|
||||||
board = ""
|
board = ""
|
||||||
turn = ""
|
begin
|
||||||
|
DB.open "sqlite3:./#{GAME_SAVE}" do |db|
|
||||||
DB.open "sqlite3:./game_saves.db" do |db|
|
# Query whole row where the gameid is found
|
||||||
puts "Saves:"
|
db.query "SELECT * FROM game_saves WHERE gameid = ?", gameid 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
|
||||||
id = rs.read(Int32)
|
id = rs.read(String) # Reduntant
|
||||||
|
|
||||||
turn = rs.read(Int32)
|
turn = rs.read(Int32)
|
||||||
size = rs.read(Int32)
|
size = rs.read(Int32)
|
||||||
white_pass = rs.read(String)
|
white_pass = rs.read(String)
|
||||||
black_pass = rs.read(String)
|
black_pass = rs.read(String)
|
||||||
board = rs.read(String)
|
board = rs.read(String)
|
||||||
puts turn
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
# New Go::Game object
|
||||||
game = Go::Game.new()
|
game = Go::Game.new()
|
||||||
game.size = Go::Size.from_value(size)
|
game.size = Go::Size.from_value(size)
|
||||||
game.white_pass = white_pass
|
game.white_pass = white_pass
|
||||||
game.black_pass = black_pass
|
game.black_pass = black_pass
|
||||||
# Todo: Parse game board string
|
|
||||||
# game.board =
|
|
||||||
game.turn = Go::Color.from_value(turn)
|
game.turn = Go::Color.from_value(turn)
|
||||||
# Todo: Fix return type mismatch
|
# Parses game board string
|
||||||
# return game
|
counter = 0
|
||||||
|
# For each character in the board String
|
||||||
|
board.each_char do |char|
|
||||||
|
x = counter % 9
|
||||||
|
y = counter / 9
|
||||||
|
coord = {x.to_i8, y.to_i8}
|
||||||
|
if(char == 'B')
|
||||||
|
game.board[coord] = Go::Color::Black
|
||||||
|
elsif(char == 'W')
|
||||||
|
game.board[coord] = Go::Color::White
|
||||||
|
end
|
||||||
|
counter += 1
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
# Catch bad query
|
||||||
|
puts "DB query Failed"
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
# Finished Go::Game object to return
|
||||||
|
return game
|
||||||
|
end
|
||||||
|
|
||||||
def lookup_game(db, cache, id) : Go::Game?
|
def lookup_game(db, cache, id) : Go::Game?
|
||||||
|
# Function: lookup_game
|
||||||
|
# Parameters: db(String)[Unused] cache({(String), (Go::Game)}) id(String)
|
||||||
if game = cache[id]?
|
if game = cache[id]?
|
||||||
return game
|
return game
|
||||||
else
|
else
|
||||||
loaded_game = query_game(db, id)
|
loaded_game = query_game(db, id)
|
||||||
cache[id] = loaded_game if loaded_game
|
# Need to convert id to string for some reason
|
||||||
|
cache[id.to_s] = loaded_game if loaded_game
|
||||||
return loaded_game
|
return loaded_game
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_game(db, cache, game, id)
|
def create_game(db, cache, game, id)
|
||||||
|
# Function: create_game
|
||||||
|
# Parameters: db(String)[Unused] cache({(String), (Go::Game)}) game(Go::Game) id(String)
|
||||||
cache[id] = game
|
cache[id] = game
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_message(id, game, socket, message)
|
def handle_message(id, game, socket, message)
|
||||||
|
# Function: handle_message
|
||||||
|
# Parameters: id(String) game(Go::Game) socket() message()
|
||||||
|
|
||||||
split_command = message.split(" ")
|
split_command = message.split(" ")
|
||||||
command = split_command[0]
|
command = split_command[0]
|
||||||
if command == "place"
|
if command == "place"
|
||||||
|
@ -89,9 +113,12 @@ get "/" do |env|
|
||||||
end
|
end
|
||||||
|
|
||||||
get "/save" do |env|
|
get "/save" do |env|
|
||||||
#game = Go::Game.new(Go::Size::Small, "asdf", "sadfasdf")
|
GAME_CACHE.each do |game_hash|
|
||||||
#save_game(db, 0, game)
|
gameid, game = game_hash
|
||||||
|
save_game("none", gameid, game)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
post "/game" do |env|
|
post "/game" do |env|
|
||||||
game_id = env.params.body["id"]?
|
game_id = env.params.body["id"]?
|
||||||
|
@ -169,14 +196,4 @@ ws "/game/:id" do |socket, env|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_save()
|
|
||||||
puts "test"
|
|
||||||
game = Go::Game.new(Go::Size::Small, "asdf", "sadfasdf")
|
|
||||||
|
|
||||||
save_game("none", 1, game)
|
|
||||||
query_game("none", 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
test_save()
|
|
||||||
Kemal.run
|
Kemal.run
|
Loading…
Reference in New Issue
Block a user