Game cleaner implemented
This commit is contained in:
parent
c0a1548463
commit
dac5a64a45
82
src/Go.cr
82
src/Go.cr
|
@ -13,20 +13,22 @@ GAME_SAVE = "./game_saves.db"
|
||||||
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(String)[Unused] gameid(String) game(Go::Game)
|
||||||
|
# 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|
|
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, 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, 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, Time.now.to_json, board
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_all()
|
def save_all(cache)
|
||||||
# Function: save_all
|
# Function: save_all
|
||||||
# Parameters: None
|
# Parameters: cache({(String),(Go::Game)})
|
||||||
GAME_CACHE.each do |game_hash|
|
# Returns: None
|
||||||
|
cache.each do |game_hash|
|
||||||
gameid, game = game_hash
|
gameid, game = game_hash
|
||||||
save_game("none", gameid, game)
|
save_game("none", gameid, game)
|
||||||
end
|
end
|
||||||
|
@ -35,6 +37,7 @@ 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(String)[Unused] gameid(String)
|
||||||
|
# Returns: (Go::Game) for a given gameid
|
||||||
turn = 0
|
turn = 0
|
||||||
size = Go::Size::Small
|
size = Go::Size::Small
|
||||||
white_pass = ""
|
white_pass = ""
|
||||||
|
@ -43,9 +46,9 @@ def query_game(db, gameid) : Go::Game?
|
||||||
begin
|
begin
|
||||||
DB.open "sqlite3:./#{GAME_SAVE}" do |db|
|
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 * 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
|
||||||
id = rs.read(String) # Reduntant
|
#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)
|
||||||
|
@ -76,16 +79,50 @@ def query_game(db, gameid) : Go::Game?
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
# Catch bad query
|
# Catch bad query
|
||||||
puts "DB query Failed"
|
# 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)
|
||||||
|
# Function: game_cleaner
|
||||||
|
# Parameters: cache({(String),(Go::Game)})
|
||||||
|
# Returns: None
|
||||||
|
# Description: Cleans the database and memory of games older than 24 hours, every 2 hours
|
||||||
|
spawn do
|
||||||
|
loop do
|
||||||
|
gameid = ""
|
||||||
|
ntime = Time.now()
|
||||||
|
DB.open "sqlite3:./#{GAME_SAVE}" do |db|
|
||||||
|
# Time span, for the subtraction of two time objects
|
||||||
|
tspan = Time::Span.new(0,0,0)
|
||||||
|
db.query "SELECT time, gameid FROM game_saves" do |rs|
|
||||||
|
rs.each do
|
||||||
|
stime = Time.from_json(rs.read(String))
|
||||||
|
gameid = rs.read(String)
|
||||||
|
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
|
||||||
|
sleep 2.hour
|
||||||
|
end
|
||||||
|
end
|
||||||
|
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(String)[Unused] cache({(String), (Go::Game)}) id(String)
|
||||||
|
# Returns: None
|
||||||
|
# Description: Loads game data from memory, then attempts load from database
|
||||||
if game = cache[id]?
|
if game = cache[id]?
|
||||||
return game
|
return game
|
||||||
else
|
else
|
||||||
|
@ -99,12 +136,15 @@ 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(String)[Unused] cache({(String), (Go::Game)}) game(Go::Game) id(String)
|
||||||
|
# Returns: None
|
||||||
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
|
# Function: handle_message
|
||||||
# Parameters: id(String) game(Go::Game) socket() message()
|
# Parameters: id(String) game(Go::Game) socket(WebSocket) message(String)
|
||||||
|
# Returns: None
|
||||||
|
# Description: Handle placement messages from the WebSocket
|
||||||
split_command = message.split(" ")
|
split_command = message.split(" ")
|
||||||
command = split_command[0]
|
command = split_command[0]
|
||||||
if command == "place"
|
if command == "place"
|
||||||
|
@ -124,21 +164,6 @@ get "/" do |env|
|
||||||
render "src/Go/views/index.ecr", "src/Go/views/base.ecr"
|
render "src/Go/views/index.ecr", "src/Go/views/base.ecr"
|
||||||
end
|
end
|
||||||
|
|
||||||
get "/save" do |env|
|
|
||||||
GAME_CACHE.each do |game_hash|
|
|
||||||
gameid, game = game_hash
|
|
||||||
save_game("none", gameid, game)
|
|
||||||
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|
|
||||||
game_id = env.params.body["id"]?
|
game_id = env.params.body["id"]?
|
||||||
game_password = env.params.body["password"]?
|
game_password = env.params.body["password"]?
|
||||||
|
@ -219,12 +244,13 @@ end
|
||||||
# spawn do
|
# spawn do
|
||||||
# loop do
|
# loop do
|
||||||
# sleep 10.minute
|
# sleep 10.minute
|
||||||
# save_all()
|
# save_all(GAME_CACHE)
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
game_cleaner(GAME_CACHE)
|
||||||
Kemal.run
|
Kemal.run
|
||||||
# If exit is disabled in kemal.stop
|
# If exit is disabled in kemal.stop
|
||||||
|
# For save on close
|
||||||
# at_exit do
|
# at_exit do
|
||||||
# save_all()
|
# save_all(GAME_CACHE)
|
||||||
# puts "Saved"
|
|
||||||
# end
|
# end
|
Loading…
Reference in New Issue
Block a user