From 12c137ef4f24aa7375bd2985411fb333538fcfa5 Mon Sep 17 00:00:00 2001 From: dogcatfee Date: Wed, 6 Jun 2018 18:33:01 -0700 Subject: [PATCH] Game loading completed --- .../main.scssc | Bin 0 -> 32917 bytes Go | Bin game_saves.db | Bin 8192 -> 12288 bytes src/Go.cr | 97 ++++++++++-------- 4 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 .sass-cache/2da5b7a9ed68f2839637c88aab8550dd9bb593bb/main.scssc mode change 100755 => 100644 Go diff --git a/.sass-cache/2da5b7a9ed68f2839637c88aab8550dd9bb593bb/main.scssc b/.sass-cache/2da5b7a9ed68f2839637c88aab8550dd9bb593bb/main.scssc new file mode 100644 index 0000000000000000000000000000000000000000..0184d123f0d0cb7f0a75228c12c55990a8f06b39 GIT binary patch literal 32917 zcmcg#Ymi*WRo2`jyR)+^t#{?s%aT1>%fhm~FRfOV@3rIhLrP?9#b{+?XOnDZXYTA? zG&^@aGpk1&+Yn-$2jwK?q)1ZOajHyG{7MK_@PjHS0{np=KoR~>JpPbSP&}(BLIFiM zr*C(kK0SSBb|k=*!uCw}*XKKZ&gpm0;_TAw>C#eVu~x4w)J|3E)zh`p3-z_dwRnE* z#cOVS({mJ#n*?zp|h9Z+xXg6D-FvT zz;d;fY;|;PPbQtXRcVZbqvA@Xu^p%39X#8!_-PnEmNwGXNHjJd?iT4*CrO*_7k5FS zN~hCmzc>;WTX8KZ{A*Z(dgCVkHL?Z;Rh|pZqIF2lIAS_ z1;U+!wn#R&(iSMoDDu4e09x)G6mMT|cjC<=fVH3n&rhG8+ekO#xmvnjt#s;fJoni0 z$p_|V&Yi#T$dhyRq*-aqK&$*zTX-$l~yxp zu7i$>%W-E2#M`K}H(uI>l#n%M5)+`9_3cWl7T4xMUF(f>tOEiYLr+vbIU<*79tr0ys z#YD5VkVZ3O(QF!xf=?NX(ypm|?!xxwTHGo{V@ZOdg;S!ry@^<9YjwMsbUL1voJp|^MYbV9L0=)D)t)1U~0du7?S&B8lB*c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o>nloDU`j8)2GzJA}Es;lkc(gD}_yvl|mb$*FaP`Ps6x0fjB-1 zV#8#nh%YS+0@G!7>_)mmGM0z89J4y%$1mZgbUC{iEpJ5MZ3P2)3t2}}WXkUiL&%d? zgthpXLs!-8NXNElHEz*$M~Ne z2wd;S?_W}0*2O^|II^m8niGrTf8CDf3Zmpwj%3l|ryH!Q{J3h{om7a~c1YfpQm!D| z+qZ<8tCMVV(h}1J#g0dw3?MMm58zf-Shjbqx-uz?fL$Hc{~$lL7h=_lpDI{Y0=5bW z48OLU-4#+Xw-Mw=grFik0%?PUb!7TJFQ{Mv{LchKxel3?%akhHo~_7Pyi75Qp@^0r uAqMwSK1sIf?v9*{NcVUGzxIt#@pByb$dyL_&#}|4_z=tz%(N*ZTMT4J#i0!-d0@5Be Go::Game - +GAME_SAVE = "./game_saves.db" def save_game(db, gameid, game) - puts "Saving" - puts game.encode + # Function: save_game + # Parameters: db(String)[Unused] gameid(String) game(Go::Game) turn, size, white_pass, black_pass, board = game.encode - DB.open "sqlite3:./game_saves.db" do |db| - # 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 + DB.open "sqlite3:./#{GAME_SAVE}" do |db| + # 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) )" + # 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 end end def query_game(db, gameid) : Go::Game? + # Function: query_game + # Parameters: db(String)[Unused] gameid(String) + turn = 0 size = Go::Size::Small white_pass = "" black_pass = "" board = "" - turn = "" - - DB.open "sqlite3:./game_saves.db" do |db| - puts "Saves:" - db.query "select gameid, turn, size, white_pass, black_pass, board from game_saves where gameid = #{gameid}" do |rs| + begin + DB.open "sqlite3:./#{GAME_SAVE}" do |db| + # Query whole row where the gameid is found + db.query "SELECT * FROM game_saves WHERE gameid = ?", gameid do |rs| rs.each do - id = rs.read(Int32) - + id = rs.read(String) # Reduntant 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 + # New Go::Game object + game = Go::Game.new() + game.size = Go::Size.from_value(size) + game.white_pass = white_pass + game.black_pass = black_pass + game.turn = Go::Color.from_value(turn) + # Parses game board string + 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 + end + # Finished Go::Game object to return + return game end 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]? return game else 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 end end 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 end def handle_message(id, game, socket, message) + # Function: handle_message + # Parameters: id(String) game(Go::Game) socket() message() + split_command = message.split(" ") command = split_command[0] if command == "place" @@ -89,10 +113,13 @@ get "/" do |env| end get "/save" do |env| - #game = Go::Game.new(Go::Size::Small, "asdf", "sadfasdf") - #save_game(db, 0, game) + GAME_CACHE.each do |game_hash| + gameid, game = game_hash + save_game("none", gameid, game) + end end + post "/game" do |env| game_id = env.params.body["id"]? game_password = env.params.body["password"]? @@ -169,14 +196,4 @@ ws "/game/:id" do |socket, env| 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 \ No newline at end of file