Save on move update

This commit is contained in:
dogcatfee 2018-06-06 21:41:07 -07:00
parent b4a31e709c
commit c0a1548463

View File

@ -16,7 +16,8 @@ def save_game(db, gameid, game)
turn, size, white_pass, black_pass, board = game.encode turn, size, white_pass, black_pass, board = game.encode
DB.open "sqlite3:./#{GAME_SAVE}" do |db| DB.open "sqlite3:./#{GAME_SAVE}" do |db|
# Create table if one does not exist, gameid is UNIQUE => No duplicates # Create table if one does not exist, gameid is UNIQUE => No duplicates
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 if not exists game_saves (gameid string, turn integer, size integer, white_pass string, black_pass string, time string, board string, UNIQUE(gameid) )"
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) )"
# If duplicate => replace values, else => make new row for gameid # If duplicate => replace values, else => make new row for gameid
db.exec "insert or replace 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
@ -63,8 +64,8 @@ def query_game(db, gameid) : Go::Game?
counter = 0 counter = 0
# For each character in the board String # For each character in the board String
board.each_char do |char| board.each_char do |char|
x = counter % 9 x = counter / 9
y = counter / 9 y = counter % 9
coord = {x.to_i8, y.to_i8} coord = {x.to_i8, y.to_i8}
if(char == 'B') if(char == 'B')
game.board[coord] = Go::Color::Black game.board[coord] = Go::Color::Black
@ -104,7 +105,6 @@ end
def handle_message(id, game, socket, message) def handle_message(id, game, socket, message)
# Function: handle_message # Function: handle_message
# Parameters: id(String) game(Go::Game) socket() 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"
@ -114,6 +114,9 @@ def handle_message(id, game, socket, message)
game.update(x, y, color) game.update(x, y, color)
game.sockets.each { |socket| socket.send game.to_json } game.sockets.each { |socket| socket.send game.to_json }
# If saving game on move
save_game("none", id, game)
end end
end end
@ -127,6 +130,13 @@ get "/save" do |env|
save_game("none", gameid, game) save_game("none", gameid, game)
end end
end end
get "/state" do |env|
GAME_CACHE.each do |game_hash|
gameid, game = game_hash
puts gameid
puts game.board
end
end
post "/game" do |env| post "/game" do |env|
@ -205,12 +215,16 @@ ws "/game/:id" do |socket, env|
end end
end end
spawn do # For timed-autosave
loop do # spawn do
sleep 10.minute # loop do
puts "Saving" # sleep 10.minute
save_all() # save_all()
end # end
end # end
Fiber.yield Kemal.run
Kemal.run # If exit is disabled in kemal.stop
#at_exit do
# save_all()
# puts "Saved"
#end