Ejemplo n.º 1
0
        private List <int> GetFromQueryInternal(int battleId)
        {
            string     query    = String.Format("{0} where BattleId = {1} ;", SqlPlayerInBattleSelect, battleId);
            List <int> toReturn = new List <int>();

            try
            {
                using (DbCommand cmd = MyConnection.CreateCommand())
                {
                    cmd.CommandText = query;

                    DbDataReader rdr = cmd.ExecuteReader();

                    while (rdr.Read())
                    {
                        int instance = GetPlayerFromReader(rdr);
                        toReturn.Add(instance);
                    }
                    if (toReturn.Count != 0)
                    {
                        allData[battleId] = toReturn;
                    }
                }
            }
            catch (Exception e)
            {
                throw new DAOException(String.Format("Error while reading database query=({0}) : {1}", query, e.Message));
            }
            return(toReturn);
        }
Ejemplo n.º 2
0
        private void InsertIntoDatabase(Battle toSave)
        {
            try
            {
                using (DbCommand cmd = MyConnection.CreateCommand())
                {
                    cmd.CommandText = SqlBattleInsert;

                    SqlCommand sqlCmd = cmd as SqlCommand;
                    if (sqlCmd != null)
                    {
                        sqlCmd.Parameters.Add("@typ", SqlDbType.SmallInt);
                        sqlCmd.Parameters["@typ"].Value = toSave.GameType;
                        sqlCmd.Parameters.Add("@tst", SqlDbType.NVarChar);
                        sqlCmd.Parameters["@tst"].Value = toSave.GameTypeString;
                        sqlCmd.Parameters["@tst"].Size  = 20;
                        sqlCmd.Parameters.Add("@bid", SqlDbType.Int);
                        sqlCmd.Parameters["@bid"].Value = toSave.BattleId;
                        sqlCmd.Prepare();
                    }
                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                }
                gridDao.SaveGrid(new Tuple <int, Grid>(toSave.BattleId, toSave.CurrentState));
            }
            catch (Exception e)
            {
                throw new DAOException(String.Format("Error while creating new battle : {0}", e.Message));
            }
        }
Ejemplo n.º 3
0
        public async Task <T> SingleOrDefaultAsync()
        {
            try
            {
                if (MyConnection.State == ConnectionState.Closed)
                {
                    MyConnection.Open();
                }

                using (var command = MyConnection.CreateCommand())
                {
                    command.CommandText = SqlStr;

                    if (MyParameters != null && MyParameters.Length > 0)
                    {
                        command.Parameters.AddRange(MyParameters);
                    }

                    return((T)await command.ExecuteScalarAsync().ConfigureAwait(false));
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                if (MyConnection.State == ConnectionState.Open)
                {
                    MyConnection.Close();
                }
            }
        }
Ejemplo n.º 4
0
        private void UpdateGrid(Tuple <int, Grid> toSave)
        {
            try
            {
                using (DbCommand cmd = MyConnection.CreateCommand())
                {
                    cmd.CommandText = String.Format("{0} where BattleID={1};", SqlGridUpdate, toSave.Item1);
                    //PawnLocations = ?, NextPlayerToPlay = ?, CurrentMessage = ?, LastMove = ?

                    Grid         grid  = toSave.Item2;
                    SqlCeCommand ceCmd = cmd as SqlCeCommand;
                    if (ceCmd != null)
                    {
                        ceCmd.Parameters.Add(new SqlCeParameter("@PawnLocations", SqlDbType.NVarChar));
                        ceCmd.Parameters.Add(new SqlCeParameter("@NextPlayerToPlay", SqlDbType.SmallInt));
                        ceCmd.Parameters.Add(new SqlCeParameter("@CurrentMessage", SqlDbType.NVarChar));
                        ceCmd.Parameters.Add(new SqlCeParameter("@LastMove", SqlDbType.NVarChar));

                        ceCmd.Parameters["@PawnLocations"].Size  = 500;
                        ceCmd.Parameters["@CurrentMessage"].Size = 100;
                        ceCmd.Parameters["@LastMove"].Size       = 100;
                        ceCmd.Prepare();

                        ceCmd.Parameters["@PawnLocations"].Value    = grid.PawnLocations.PawnLocationsToDb();
                        ceCmd.Parameters["@NextPlayerToPlay"].Value = grid.NextPlayerToPlay;
                        ceCmd.Parameters["@CurrentMessage"].Value   = grid.CurrentMessage.CurrentMessageToDb();;
                        ceCmd.Parameters["@LastMove"].Value         = grid.LastMove.LastMoveToDb();
                    }
                    else
                    {
                        SqlCommand sqlCmd = cmd as SqlCommand;
                        if (sqlCmd != null)
                        {
                            sqlCmd.Parameters.Add("@PawnLocations", SqlDbType.NVarChar);
                            sqlCmd.Parameters.Add("@NextPlayerToPlay", SqlDbType.SmallInt);
                            sqlCmd.Parameters.Add("@CurrentMessage", SqlDbType.NVarChar);
                            sqlCmd.Parameters.Add("@LastMove", SqlDbType.NVarChar);

                            sqlCmd.Parameters["@PawnLocations"].Size  = 500;
                            sqlCmd.Parameters["@CurrentMessage"].Size = 100;
                            sqlCmd.Parameters["@LastMove"].Size       = 100;
                            sqlCmd.Prepare();

                            sqlCmd.Parameters["@PawnLocations"].Value    = grid.PawnLocations.PawnLocationsToDb();
                            sqlCmd.Parameters["@NextPlayerToPlay"].Value = grid.NextPlayerToPlay;
                            sqlCmd.Parameters["@CurrentMessage"].Value   = grid.CurrentMessage.CurrentMessageToDb();;
                            sqlCmd.Parameters["@LastMove"].Value         = grid.LastMove.LastMoveToDb();
                        }
                    }

                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                }
            }
            catch (Exception e)
            {
                throw new DAOException(String.Format("Error while updating battle : {0}", e.Message));
            }
        }
Ejemplo n.º 5
0
        private void InsertIntoDatabase(string login, string password)
        {
            try
            {
                using (DbCommand cmd = MyConnection.CreateCommand())
                {
                    cmd.CommandText = SqlPlayerInsert;

                    SqlCommand sqlCmd = cmd as SqlCommand;
                    if (sqlCmd != null)
                    {
                        sqlCmd.Parameters.Add("@log", SqlDbType.NVarChar);
                        sqlCmd.Parameters["@log"].Value = login;
                        sqlCmd.Parameters.Add("@pas", SqlDbType.NVarChar);
                        sqlCmd.Parameters["@pas"].Value = login;
                        sqlCmd.Parameters["@log"].Size  = 15;
                        sqlCmd.Parameters["@pas"].Size  = 15;
                        sqlCmd.Prepare();
                    }
                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                }
            }
            catch (Exception e)
            {
                throw new DAOException(String.Format("Error while creating new player ({0}) : {1}", login, e.Message));
            }
        }
Ejemplo n.º 6
0
        public void Create(PlayerInBattle playersInBattle)
        {
            battleDao.SaveBattle(playersInBattle.Battle);
            int battleId     = playersInBattle.Battle.BattleId;
            int playerNumber = 1;

            foreach (int playerId in playersInBattle.Battle.Players)
            {
                //"INSERT INTO PlayerInBattles (PlayerId, BattleId, PlayerNumber) VALUES (...)";
                try
                {
                    using (DbCommand cmd = MyConnection.CreateCommand())
                    {
                        cmd.CommandText = SqlPlayerInBattleInsert;


                        SqlCeCommand ceCmd = cmd as SqlCeCommand;
                        if (ceCmd != null)
                        {
                            ceCmd.Parameters.Add(new SqlCeParameter("@pla", SqlDbType.Int));
                            ceCmd.Parameters.Add(new SqlCeParameter("@bat", SqlDbType.Int));
                            ceCmd.Parameters.Add(new SqlCeParameter("@num", SqlDbType.SmallInt));
                            ceCmd.Prepare();

                            ceCmd.Parameters["@pla"].Value = playerId;
                            ceCmd.Parameters["@bat"].Value = battleId;
                            ceCmd.Parameters["@num"].Value = playerNumber;
                        }
                        else
                        {
                            SqlCommand sqlCmd = cmd as SqlCommand;
                            if (sqlCmd != null)
                            {
                                sqlCmd.Parameters.Add("@pla", SqlDbType.Int);
                                sqlCmd.Parameters["@pla"].Value = playerId;
                                sqlCmd.Parameters.Add("@bat", SqlDbType.Int);
                                sqlCmd.Parameters["@bat"].Value = battleId;
                                sqlCmd.Parameters.Add("@num", SqlDbType.SmallInt);
                                sqlCmd.Parameters["@num"].Value = playerNumber;
                                sqlCmd.Prepare();
                            }
                        }
                        cmd.ExecuteNonQuery();

                        cmd.Parameters.Clear();
                    }
                    playerNumber++;
                }
                catch (Exception e)
                {
                    throw new DAOException(String.Format("Error while creating new PlayerInBattle : {0}", e.Message));
                }
            }
        }
Ejemplo n.º 7
0
        private void InsertIntoDatabase(Tuple <int, Grid> toSave)
        {
            //BattleId, NumberLines, NumberColumns, IsGridShifted, MoveNumber,
            //PawnLocations, NextPlayerToPlay, CurrentMessage, LastMove
            try
            {
                using (DbCommand cmd = MyConnection.CreateCommand())
                {
                    cmd.CommandText = SqlGridInsert;
                    Grid       grid   = toSave.Item2;
                    SqlCommand sqlCmd = cmd as SqlCommand;
                    if (sqlCmd != null)
                    {
                        sqlCmd.Parameters.Add("@BattleId", SqlDbType.Int);
                        sqlCmd.Parameters.Add("@NumberLines", SqlDbType.SmallInt);
                        sqlCmd.Parameters.Add("@NumberColumns", SqlDbType.SmallInt);
                        sqlCmd.Parameters.Add("@IsGridShifted", SqlDbType.NChar);
                        sqlCmd.Parameters.Add("@MoveNumber", SqlDbType.SmallInt);

                        sqlCmd.Parameters.Add("@PawnLocations", SqlDbType.NVarChar);
                        sqlCmd.Parameters.Add("@NextPlayerToPlay", SqlDbType.SmallInt);
                        sqlCmd.Parameters.Add("@CurrentMessage", SqlDbType.NVarChar);
                        sqlCmd.Parameters.Add("@LastMove", SqlDbType.NVarChar);

                        sqlCmd.Parameters["@IsGridShifted"].Size  = 5;
                        sqlCmd.Parameters["@PawnLocations"].Size  = 500;
                        sqlCmd.Parameters["@CurrentMessage"].Size = 100;
                        sqlCmd.Parameters["@LastMove"].Size       = 100;

                        sqlCmd.Parameters["@BattleId"].Value         = toSave.Item1;
                        sqlCmd.Parameters["@NumberLines"].Value      = grid.NumberLines;
                        sqlCmd.Parameters["@NumberColumns"].Value    = grid.NumberColumns;
                        sqlCmd.Parameters["@IsGridShifted"].Value    = grid.IsGridShiftedToDb();
                        sqlCmd.Parameters["@MoveNumber"].Value       = grid.MoveNumber;
                        sqlCmd.Parameters["@PawnLocations"].Value    = grid.PawnLocations.PawnLocationsToDb();
                        sqlCmd.Parameters["@NextPlayerToPlay"].Value = grid.NextPlayerToPlay;
                        sqlCmd.Parameters["@CurrentMessage"].Value   = grid.CurrentMessage.CurrentMessageToDb();
                        sqlCmd.Parameters["@LastMove"].Value         = grid.LastMove.LastMoveToDb();

                        sqlCmd.Prepare();
                    }

                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                }
            }
            catch (Exception e)
            {
                throw new DAOException(String.Format("Error while creating new battle : {0}", e.Message));
            }
        }
Ejemplo n.º 8
0
        public T FirstOrDefault()
        {
            try
            {
                if (MyConnection.State == ConnectionState.Closed)
                {
                    MyConnection.Open();
                }

                using (var command = MyConnection.CreateCommand())
                {
                    command.CommandText = SqlStr;

                    if (MyParameters != null && MyParameters.Length > 0)
                    {
                        command.Parameters.AddRange(MyParameters);
                    }

                    var rtnList = new List <T>();
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            rtnList = AutoFillEntity <T>(reader);
                        }
                        else
                        {
                            rtnList.Add(new T());
                        }
                    }
                    return(rtnList[0]);
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                if (MyConnection.State == ConnectionState.Open)
                {
                    MyConnection.Close();
                }
            }
        }
Ejemplo n.º 9
0
        public async Task <T> FirstOrDefaultAsync()
        {
            try
            {
                if (MyConnection.State == ConnectionState.Closed)
                {
                    MyConnection.Open();
                }

                using (var command = MyConnection.CreateCommand())
                {
                    command.CommandText = SqlStr;

                    if (MyParameters != null && MyParameters.Length > 0)
                    {
                        command.Parameters.AddRange(MyParameters);
                    }

                    var rtnList = new List <T>();
                    using (var reader = await command.ExecuteReaderAsync().ConfigureAwait(false))
                    {
                        rtnList = await AutoFillEntityAsync <T>(reader).ConfigureAwait(false);
                    }

                    if (rtnList.Count == 0)
                    {
                        rtnList.Add(new T());
                    }

                    return(rtnList[0]);
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                if (MyConnection.State == ConnectionState.Open)
                {
                    MyConnection.Close();
                }
            }
        }