Open database once update
This commit is contained in:
parent
2c8bc7aff9
commit
b41035fac7
3
.gitattributes
vendored
3
.gitattributes
vendored
|
@ -1,4 +1,3 @@
|
||||||
*.js linguist-vendored
|
*.js linguist-vendored
|
||||||
*.css linguist-vendored
|
*.css linguist-vendored
|
||||||
*.scss linguist-vendored=false
|
*.scss linguist-vendored=false
|
||||||
|
|
97
src/Go.cr
97
src/Go.cr
|
@ -9,19 +9,20 @@ URL = "localhost"
|
||||||
PORT = "3000"
|
PORT = "3000"
|
||||||
GAME_CACHE = {} of String => Go::Game
|
GAME_CACHE = {} of String => Go::Game
|
||||||
GAME_SAVE = "./game_saves.db"
|
GAME_SAVE = "./game_saves.db"
|
||||||
|
GAME_DB = DB.open "sqlite3:./#{GAME_SAVE}"
|
||||||
|
|
||||||
def save_game(db, gameid, game)
|
def save_game(db, gameid, game)
|
||||||
# Function: save_game
|
# Function: save_game
|
||||||
# Parameters: db(String)[Unused] gameid(String) game(Go::Game)
|
# Parameters: db(Sqlite) gameid(String) game(Go::Game)
|
||||||
# Returns: None
|
# Returns: None
|
||||||
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|
|
# 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 (
|
||||||
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) )"
|
gameid string, turn integer, size integer, white_pass string,
|
||||||
# If duplicate => replace values, else => make new row for gameid
|
black_pass string, time string, board string, UNIQUE(gameid) )"
|
||||||
db.exec "insert or replace into game_saves values (?, ?, ?, ?, ?, ?, ?)",
|
# If duplicate => replace values, else => make new row for gameid
|
||||||
gameid, turn.value, size, white_pass, black_pass, Time.now.to_json, board
|
db.exec "insert or replace into game_saves values (?, ?, ?, ?, ?, ?, ?)",
|
||||||
end
|
gameid, turn.value, size, white_pass, black_pass, Time.now.to_json, board
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_all(cache)
|
def save_all(cache)
|
||||||
|
@ -30,33 +31,33 @@ def save_all(cache)
|
||||||
# Returns: None
|
# Returns: None
|
||||||
cache.each do |game_hash|
|
cache.each do |game_hash|
|
||||||
gameid, game = game_hash
|
gameid, game = game_hash
|
||||||
save_game("none", gameid, game)
|
save_game(GAME_DB, gameid, game)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_game(db, gameid) : Go::Game?
|
def query_game(db, gameid) : Go::Game?
|
||||||
# Function: query_game
|
# Function: query_game
|
||||||
# Parameters: db(String)[Unused] gameid(String)
|
# Parameters: db(Sqlite) gameid(String)
|
||||||
# Returns: (Go::Game) for a given gameid
|
# Returns: (Go::Game) for a given gameid
|
||||||
turn = 0
|
turn = 0
|
||||||
size = Go::Size::Small
|
size = 9
|
||||||
white_pass = ""
|
white_pass = ""
|
||||||
black_pass = ""
|
black_pass = ""
|
||||||
board = ""
|
board = ""
|
||||||
begin
|
begin
|
||||||
DB.open "sqlite3:./#{GAME_SAVE}" do |db|
|
# Query whole row where the gameid is found
|
||||||
# Query whole row where the gameid is found
|
db.query "SELECT turn,size,white_pass,black_pass,board FROM game_saves WHERE gameid = ?", gameid do |rs|
|
||||||
db.query "SELECT turn,size,white_pass,black_pass,board FROM game_saves WHERE gameid = ?", gameid do |rs|
|
rs.each do
|
||||||
rs.each do
|
turn = rs.read(Int32)
|
||||||
#id = rs.read(String) # Reduntant
|
size = rs.read(Int32)
|
||||||
turn = rs.read(Int32)
|
white_pass = rs.read(String)
|
||||||
size = rs.read(Int32)
|
black_pass = rs.read(String)
|
||||||
white_pass = rs.read(String)
|
board = rs.read(String)
|
||||||
black_pass = rs.read(String)
|
|
||||||
board = rs.read(String)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if ( board == "" )
|
||||||
|
return nil
|
||||||
|
end
|
||||||
# New Go::Game object
|
# 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)
|
||||||
|
@ -79,40 +80,37 @@ def query_game(db, gameid) : Go::Game?
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
# Catch bad query
|
# Catch bad query
|
||||||
# puts "DB query Failed"
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
# Finished Go::Game object to return
|
# Finished Go::Game object to return
|
||||||
return game
|
return game
|
||||||
end
|
end
|
||||||
|
|
||||||
def game_cleaner(cache)
|
def game_cleaner(db, cache)
|
||||||
# Function: game_cleaner
|
# Function: game_cleaner
|
||||||
# Parameters: cache({(String),(Go::Game)})
|
# Parameters: db(Sqlite) cache({(String),(Go::Game)})
|
||||||
# Returns: None
|
# Returns: None
|
||||||
# Description: Cleans the database and memory of games older than 24 hours, every 2 hours
|
# Description: Cleans the database and memory of games older than 24 hours, every 2 hours
|
||||||
spawn do
|
spawn do
|
||||||
loop do
|
loop do
|
||||||
gameid = ""
|
gameid = ""
|
||||||
ntime = Time.now()
|
ntime = Time.now()
|
||||||
DB.open "sqlite3:./#{GAME_SAVE}" do |db|
|
# Time span, for the subtraction of two time objects
|
||||||
# Time span, for the subtraction of two time objects
|
tspan = Time::Span.new(0,0,0)
|
||||||
tspan = Time::Span.new(0,0,0)
|
db.query "SELECT time, gameid FROM game_saves" do |rs|
|
||||||
db.query "SELECT time, gameid FROM game_saves" do |rs|
|
rs.each do
|
||||||
rs.each do
|
stime = Time.from_json(rs.read(String))
|
||||||
stime = Time.from_json(rs.read(String))
|
gameid = rs.read(String)
|
||||||
gameid = rs.read(String)
|
tspan = ntime - stime
|
||||||
tspan = ntime - stime
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if( tspan.hours > 24 || tspan.days > 0 )
|
|
||||||
# Delete game from database
|
|
||||||
db.exec("DELETE FROM game_saves WHERE gameid = ?", gameid)
|
|
||||||
# Delete game from memory
|
|
||||||
cache.delete(gameid)
|
|
||||||
puts "Game: #{gameid} deleted due to inactivity"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if( tspan.hours > 24 || tspan.days > 0 )
|
||||||
|
# Delete game from database
|
||||||
|
db.exec("DELETE FROM game_saves WHERE gameid = ?", gameid)
|
||||||
|
# Delete game from memory
|
||||||
|
cache.delete(gameid)
|
||||||
|
puts "Game: #{gameid} deleted due to inactivity"
|
||||||
|
end
|
||||||
sleep 2.hour
|
sleep 2.hour
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -120,7 +118,7 @@ end
|
||||||
|
|
||||||
def lookup_game(db, cache, id) : Go::Game?
|
def lookup_game(db, cache, id) : Go::Game?
|
||||||
# Function: lookup_game
|
# Function: lookup_game
|
||||||
# Parameters: db(String)[Unused] cache({(String), (Go::Game)}) id(String)
|
# Parameters: db(Sqlite) cache({(String), (Go::Game)}) id(String)
|
||||||
# Returns: None
|
# Returns: None
|
||||||
# Description: Loads game data from memory, then attempts load from database
|
# Description: Loads game data from memory, then attempts load from database
|
||||||
if game = cache[id]?
|
if game = cache[id]?
|
||||||
|
@ -135,7 +133,7 @@ end
|
||||||
|
|
||||||
def create_game(db, cache, game, id)
|
def create_game(db, cache, game, id)
|
||||||
# Function: create_game
|
# Function: create_game
|
||||||
# Parameters: db(String)[Unused] cache({(String), (Go::Game)}) game(Go::Game) id(String)
|
# Parameters: db(Sqlite) cache({(String), (Go::Game)}) game(Go::Game) id(String)
|
||||||
# Returns: None
|
# Returns: None
|
||||||
cache[id] = game
|
cache[id] = game
|
||||||
end
|
end
|
||||||
|
@ -154,9 +152,8 @@ 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
|
# If saving game on move
|
||||||
save_game("none", id, game)
|
save_game(GAME_DB, id, game)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -169,7 +166,7 @@ post "/game" do |env|
|
||||||
game_password = env.params.body["password"]?
|
game_password = env.params.body["password"]?
|
||||||
if game_id == nil || game_password == nil
|
if game_id == nil || game_password == nil
|
||||||
render_404
|
render_404
|
||||||
elsif game = lookup_game(nil, GAME_CACHE, game_id)
|
elsif game = lookup_game(GAME_DB, GAME_CACHE, game_id)
|
||||||
id = game_id
|
id = game_id
|
||||||
size = game.size.value
|
size = game.size.value
|
||||||
black = nil
|
black = nil
|
||||||
|
@ -201,7 +198,7 @@ post "/create" do |env|
|
||||||
|
|
||||||
if game_id == nil || user_password == nil || other_password == nil || color == nil || color_e == nil
|
if game_id == nil || user_password == nil || other_password == nil || color == nil || color_e == nil
|
||||||
render_404
|
render_404
|
||||||
elsif game = lookup_game(nil, GAME_CACHE, game_id)
|
elsif game = lookup_game(GAME_DB, GAME_CACHE, game_id)
|
||||||
render_404
|
render_404
|
||||||
else
|
else
|
||||||
color_e = color_e.as(Go::Color)
|
color_e = color_e.as(Go::Color)
|
||||||
|
@ -224,7 +221,7 @@ end
|
||||||
|
|
||||||
ws "/game/:id" do |socket, env|
|
ws "/game/:id" do |socket, env|
|
||||||
game_id = env.params.url["id"]
|
game_id = env.params.url["id"]
|
||||||
if game = lookup_game(nil, GAME_CACHE, game_id)
|
if game = lookup_game(GAME_DB, GAME_CACHE, game_id)
|
||||||
socket.send game.to_json
|
socket.send game.to_json
|
||||||
game.sockets << socket
|
game.sockets << socket
|
||||||
|
|
||||||
|
@ -247,7 +244,7 @@ end
|
||||||
# save_all(GAME_CACHE)
|
# save_all(GAME_CACHE)
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
game_cleaner(GAME_CACHE)
|
game_cleaner(GAME_DB, GAME_CACHE)
|
||||||
Kemal.run
|
Kemal.run
|
||||||
# If exit is disabled in kemal.stop
|
# If exit is disabled in kemal.stop
|
||||||
# For save on close
|
# For save on close
|
||||||
|
|
Loading…
Reference in New Issue
Block a user