Beispiel #1
0
 public void RemoveAllGames()
 {
     DatabaseHandler.ClearDatabase(DatabaseConnection);
     _cachedGames.Clear();
 }
Beispiel #2
0
        private void InsertCard(CardModel card)
        {
            StringBuilder fields = new StringBuilder();
            StringBuilder values = new StringBuilder();

            fields.Append("[id],[game_id],[set_real_id],[name],[image],[alternate],[dependent]");
            values.Append("@id,@game_id,(SELECT real_id FROM sets WHERE id = @set_id LIMIT 1),@name,@image,@alternate,@dependent");

            foreach (KeyValuePair <string, object> pair in card.Properties)
            {
                string name = string.Format("{0}{1}", Id.ToString().Replace("-", ""), pair.Key);

                fields.Append(string.Format(",[{0}]", name));
                values.Append(string.Format(",@{0}", name.Replace(" ", "")));

                PropertyType type  = PropertyType.String;
                object       value = "";
                if (pair.Value is string)
                {
                    type  = PropertyType.String;
                    value = (string)pair.Value;
                }
                else if (pair.Value is int)
                {
                    type  = PropertyType.Integer;
                    value = (int)pair.Value;
                }
                else
                {
                    type  = PropertyType.String;
                    value = (string)pair.Value;
                }

                if (!DatabaseHandler.ColumnExists("cards", name, GamesRepository.DatabaseConnection))
                {
                    DatabaseHandler.AddColumn("cards", name, type, GamesRepository.DatabaseConnection);
                }
            }

            StringBuilder query = new StringBuilder();

            query.Append("INSERT INTO [cards](");
            query.Append(fields.ToString());
            query.Append(") VALUES(");
            query.Append(values.ToString());
            query.Append(");");

            using (SQLiteCommand com = GamesRepository.DatabaseConnection.CreateCommand())
            {
                com.CommandText = query.ToString();
                com.Parameters.AddWithValue("@id", card.Id.ToString());
                com.Parameters.AddWithValue("@game_id", Id.ToString());
                com.Parameters.AddWithValue("@set_id", card.Set.Id.ToString());
                com.Parameters.AddWithValue("@name", card.Name);
                com.Parameters.AddWithValue("@image", card.ImageUri);
                com.Parameters.AddWithValue("@alternate", card.Alternate.ToString());
                com.Parameters.AddWithValue("@dependent", card.Dependent.ToString());
                foreach (KeyValuePair <string, object> pair in card.Properties)
                {
                    string name = string.Format("{0}{1}", Id.ToString().Replace("-", ""), pair.Key);
                    string key  = string.Format("@{0}", name.Replace(" ", ""));
                    com.Parameters.AddWithValue(key, pair.Value);
                }
#if (DEBUG)
                Debug.WriteLine(com.CommandText);
                foreach (SQLiteParameter p in com.Parameters)
                {
                    Debug.Write("ParameterName: " + p.ParameterName + "\r\n Value: " + p.Value + "\r\n");
                }
#endif
                com.ExecuteNonQuery();
            }
        }
Beispiel #3
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());
            }
        }