Esempio n. 1
0
        public void UninstallGame(Game game)
        {
            if (!_cachedGames.Any(g => g.Id == game.Id))
            {
                return;
            }

            SQLiteTransaction trans = null;

            try
            {
                trans = DatabaseConnection.BeginTransaction();

                #region remove the custom properties from the database
                using (SQLiteCommand com = DatabaseConnection.CreateCommand())
                {
                    //Build Query
                    com.CommandText = @"DELETE FROM [custom_properties] WHERE [game_id]=@id;";
                    com.Parameters.AddWithValue("@id", game.Id.ToString());
                    com.ExecuteNonQuery();
                }
                #endregion

                #region remove the game from the database
                using (SQLiteCommand com = DatabaseConnection.CreateCommand())
                {
                    //Build Query
                    com.CommandText = @"DELETE FROM [games] WHERE [id]=@id;";
                    com.Parameters.AddWithValue("@id", game.Id.ToString());
                    com.ExecuteNonQuery();
                }
                #endregion

                //remove obsolete columns from the cards table
                DatabaseHandler.RebuildCardTable(DatabaseConnection);

                trans.Commit();
            }
            catch (Exception ex)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
                return;
            }

            var existingGame = _cachedGames.FirstOrDefault(g => g.Id == game.Id);
            if (existingGame != null)
            {
                _cachedGames.Remove(existingGame);
            }
        }
Esempio n. 2
0
        public void InstallGame(Game game, IEnumerable <PropertyDef> properties)
        {
            Game existingGame       = _cachedGames.FirstOrDefault(g => g.Id == game.Id);
            SQLiteTransaction trans = null;

            try
            {
                var sb = new StringBuilder();
                trans = DatabaseConnection.BeginTransaction();

                if (existingGame != null && existingGame.Id == game.Id)
                {
                    UpdateGameDefinition(game, properties);
                }
                else
                {
                    using (SQLiteCommand com = DatabaseConnection.CreateCommand())
                    {
                        //Build Query
                        sb.Append("INSERT OR REPLACE INTO [games](");
                        sb.Append(
                            "[id],[name],[filename],[version], [card_width],[card_height],[card_back],[deck_sections],[shared_deck_sections],[file_hash]");
                        sb.Append(") VALUES(");
                        sb.Append(
                            "@id,@name,@filename,@version,@card_width,@card_height,@card_back,@deck_sections,@shared_deck_sections,@file_hash");
                        sb.Append(");\n");
                        com.CommandText = sb.ToString();

                        com.Parameters.AddWithValue("@id", game.Id.ToString());
                        com.Parameters.AddWithValue("@name", game.Name);
                        com.Parameters.AddWithValue("@filename", game.Filename);
                        com.Parameters.AddWithValue("@version", game.Version.ToString());
                        com.Parameters.AddWithValue("@card_width", game.CardWidth);
                        com.Parameters.AddWithValue("@card_height", game.CardHeight);
                        com.Parameters.AddWithValue("@card_back", game.CardBack);
                        com.Parameters.AddWithValue("@deck_sections", SerializeList(game.DeckSections));
                        if (game.SharedDeckSections != null)
                        {
                            com.Parameters.AddWithValue("@shared_deck_sections", SerializeList(game.SharedDeckSections));
                        }
                        else
                        {
                            com.Parameters.AddWithValue("@shared_deck_sections", DBNull.Value);
                        }
                        com.Parameters.AddWithValue("@file_hash", game.FileHash);

                        com.ExecuteNonQuery();
                        if (!Directory.Exists(Path.Combine(BasePath, "Decks")))
                        {
                            Directory.CreateDirectory(Path.Combine(BasePath, "Decks"));
                        }

                        game.CopyDecks();
                    }
                }
                //Add custom properties for the card.
                sb = new StringBuilder();
                sb.Append("INSERT OR REPLACE INTO [custom_properties](");
                sb.Append("[id],[card_real_id],[game_id],[name], [type],[vint],[vstr]");
                sb.Append(") VALUES(");
                sb.Append(
                    "@id,(SELECT real_id FROM cards WHERE id = @card_id LIMIT 1),@game_id,@name,@type,@vint,@vstr");
                sb.Append(");\n");
                string command = sb.ToString();
                foreach (PropertyDef pair in properties)
                {
                    string name = string.Format("{0}{1}", game.Id.ToString().Replace("-", ""), pair.Name);
                    if (!DatabaseHandler.ColumnExists("cards", name, DatabaseConnection))
                    {
                        DatabaseHandler.AddColumn("cards", name, pair.Type, DatabaseConnection);
                    }

                    using (SQLiteCommand com = DatabaseConnection.CreateCommand())
                    {
                        com.CommandText = command;
                        com.Parameters.AddWithValue("@card_id", "");
                        com.Parameters.AddWithValue("@vint", 0);
                        com.Parameters.AddWithValue("@vstr", " ");
                        com.Parameters.AddWithValue("@id", pair.Name + game.Id);
                        com.Parameters.AddWithValue("@game_id", game.Id.ToString());
                        com.Parameters.AddWithValue("@name", pair.Name);
                        switch (pair.Type)
                        {
                        case PropertyType.String:
                            com.Parameters.AddWithValue("@type", 0);
                            break;

                        case PropertyType.Integer:
                            com.Parameters.AddWithValue("@type", 1);
                            break;

                        default:
                            com.Parameters.AddWithValue("@type", 2);
                            break;
                        }
                        com.ExecuteNonQuery();
                    }
                }
                DatabaseHandler.RebuildCardTable(DatabaseConnection);
                trans.Commit();
            }
            catch (Exception)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
                return;
            }
            existingGame = _cachedGames.FirstOrDefault(g => g.Id == game.Id);
            if (existingGame != null)
            {
                _cachedGames.Remove(existingGame);
            }
            _cachedGames.Add(game);
            if (GameInstalled != null)
            {
                GameInstalled.Invoke(game, new EventArgs());
            }
        }