Example #1
0
        public async Task SaveAsync(String path, TetrisTable table)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(path))
                {
                    writer.Write(table.Size);
                    await writer.WriteLineAsync(" " + table.Time);

                    await writer.WriteLineAsync(table.Shape + " " + table.ShapeCordX + " " + table.ShapeCordY + " " + table.ShapeRotation);

                    for (Int32 i = 0; i < table.Size + 1; i++)
                    {
                        for (Int32 j = 0; j < 16; j++)
                        {
                            await writer.WriteAsync(table.Table[i, j] + " ");
                        }
                        await writer.WriteLineAsync();
                    }
                    for (Int32 i = 0; i < table.Size + 1; i++)
                    {
                        for (Int32 j = 0; j < 16; j++)
                        {
                            await writer.WriteAsync(table.TypeTable[i, j] + " ");
                        }
                        await writer.WriteLineAsync();
                    }
                }
            }
            catch
            {
                throw new TetrisDataException();
            }
        }
        public async Task <TetrisTable> LoadAsync(String name)
        {
            String path = Path.Combine(_saveDirectory, name + ".tt");

            try
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    String line = await reader.ReadLineAsync();

                    String[] numbers   = line.Split(' ');
                    Int32    tableSize = Int32.Parse(numbers[0]);
                    Int32    time      = Int32.Parse(numbers[1]);

                    line = await reader.ReadLineAsync();

                    numbers = line.Split(' ');
                    Int32 shape  = Int32.Parse(numbers[0]);
                    Int32 shapeX = Int32.Parse(numbers[1]);
                    Int32 shapeY = Int32.Parse(numbers[2]);
                    Int32 state  = Int32.Parse(numbers[3]);

                    var temporaryTable      = new byte[tableSize + 1, 16];
                    var temporaryColorTable = new Int32[tableSize + 1, 16];

                    for (Int32 i = 0; i < tableSize + 1; i++)
                    {
                        line = await reader.ReadLineAsync();

                        numbers = line.Split(' ');

                        for (Int32 j = 0; j < 16; j++)
                        {
                            temporaryTable[i, j] = Byte.Parse(numbers[j]);
                        }
                    }
                    for (Int32 i = 0; i < tableSize + 1; i++)
                    {
                        line = await reader.ReadLineAsync();

                        numbers = line.Split(' ');

                        for (Int32 j = 0; j < 16; j++)
                        {
                            temporaryColorTable[i, j] = Byte.Parse(numbers[j]);
                        }
                    }
                    TetrisTable table = new TetrisTable(tableSize, time, shape, shapeX, shapeY, state, temporaryTable, temporaryColorTable);

                    return(table);
                }
            }
            catch
            {
                throw new TetrisDataException();
            }
        }
        /// <summary>
        /// Játékállapot mentése.
        /// </summary>
        /// <param name="name">Név vagy elérési útvonal.</param>
        /// <param name="table">A kiírandó játéktábla.</param>
        public async Task SaveAsync(String name, TetrisTable table)
        {
            try
            {
                // játékmentés keresése azonos névvel
                Game overwriteGame = await _context.Games
                                     .Include(g => g.Fields)
                                     .SingleOrDefaultAsync(g => g.Name == name);

                if (overwriteGame != null)
                {
                    _context.Games.Remove(overwriteGame);             // törlés
                }
                Game dbGame = new Game
                {
                    TableSize  = table.Size,
                    ShapeIndex = table.CurrentShapeIndex,
                    ShapeX     = table.ShapeCordX,
                    ShapeY     = table.ShapeCordY,
                    ShapeR     = table.ShapeRotation,
                    GTime      = table.Time,
                    //RegionSize = table.RegionSize,
                    Name = name
                };                 // új mentés létrehozása

                for (Int32 i = 0; i < table.Size + 1; ++i)
                {
                    for (Int32 j = 0; j < 16; ++j)
                    {
                        Field field = new Field
                        {
                            X     = i,
                            Y     = j,
                            Value = table.Table[i, j],
                            Type  = table.TypeTable[i, j]
                        };
                        dbGame.Fields.Add(field);
                    }
                }                                  // mezők mentése

                _context.Games.Add(dbGame);        // mentés hozzáadása a perzisztálandó objektumokhoz
                await _context.SaveChangesAsync(); // mentés az adatbázisba
            }
            catch (Exception ex)
            {
                throw new TetrisDataException();
            }
        }
        public async Task SaveAsync(String name, TetrisTable table)
        {
            String path = Path.Combine(_saveDirectory, name + ".tt");

            try
            {
                using (StreamWriter writer = new StreamWriter(path))
                {
                    writer.Write(table.Size);
                    await writer.WriteLineAsync(" " + table.Time);

                    await writer.WriteLineAsync(table.CurrentShapeIndex + " " + table.ShapeCordX + " " + table.ShapeCordY + " " + table.ShapeRotation);

                    for (Int32 i = 0; i < table.Size + 1; i++)
                    {
                        for (Int32 j = 0; j < 16; j++)
                        {
                            await writer.WriteAsync(table.Table[i, j] + " ");
                        }
                        await writer.WriteLineAsync();
                    }
                    for (Int32 i = 0; i < table.Size + 1; i++)
                    {
                        for (Int32 j = 0; j < 16; j++)
                        {
                            await writer.WriteAsync(table.TypeTable[i, j] + " ");
                        }
                        await writer.WriteLineAsync();
                    }
                }
            }
            catch
            {
                throw new TetrisDataException();
            }
        }
        /// <summary>
        /// Játékállapot betöltése.
        /// </summary>
        /// <param name="name">Név vagy elérési útvonal.</param>
        /// <returns>A beolvasott játéktábla.</returns>
        public async Task <TetrisTable> LoadAsync(String name)
        {
            try
            {
                Game game = await _context.Games
                            .Include(g => g.Fields)
                            .SingleAsync(g => g.Name == name);         // játék állapot lekérdezése

                byte[,] first   = new byte[game.TableSize + 1, 16];
                Int32[,] second = new Int32[game.TableSize + 1, 16];
                TetrisTable table = new TetrisTable(game.TableSize, game.GTime, game.ShapeIndex, game.ShapeX, game.ShapeY, game.ShapeR, first, second); // játéktábla modell létrehozása

                foreach (Field field in game.Fields)                                                                                                    // mentett mezők feldolgozása
                {
                    table.SetValue(field.X, field.Y, field.Value, field.Type);
                }

                return(table);
            }
            catch
            {
                throw new TetrisDataException();
            }
        }