diff --git a/Tetris/Migrations/20220607174015_initialSetup.Designer.cs b/Tetris/Migrations/20220607174015_initialSetup.Designer.cs new file mode 100644 index 0000000..adf209f --- /dev/null +++ b/Tetris/Migrations/20220607174015_initialSetup.Designer.cs @@ -0,0 +1,42 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Tetris.Models; + +#nullable disable + +namespace Tetris.Migrations +{ + [DbContext(typeof(ScoreContext))] + [Migration("20220607174015_initialSetup")] + partial class initialSetup + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.0-preview.4.22229.2"); + + modelBuilder.Entity("Tetris.Models.Score", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Points") + .HasColumnType("INTEGER"); + + b.Property("UserName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Scores"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Tetris/Migrations/20220607174015_initialSetup.cs b/Tetris/Migrations/20220607174015_initialSetup.cs new file mode 100644 index 0000000..d7acdd2 --- /dev/null +++ b/Tetris/Migrations/20220607174015_initialSetup.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Tetris.Migrations +{ + /// + public partial class initialSetup : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Scores", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Points = table.Column(type: "INTEGER", nullable: false), + UserName = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Scores", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Scores"); + } + } +} diff --git a/Tetris/Migrations/ScoreContextModelSnapshot.cs b/Tetris/Migrations/ScoreContextModelSnapshot.cs new file mode 100644 index 0000000..08c8499 --- /dev/null +++ b/Tetris/Migrations/ScoreContextModelSnapshot.cs @@ -0,0 +1,39 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Tetris.Models; + +#nullable disable + +namespace Tetris.Migrations +{ + [DbContext(typeof(ScoreContext))] + partial class ScoreContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.0-preview.4.22229.2"); + + modelBuilder.Entity("Tetris.Models.Score", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Points") + .HasColumnType("INTEGER"); + + b.Property("UserName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Scores"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Tetris/Models/Game.cs b/Tetris/Models/Game.cs index 4411e88..73d6c19 100644 --- a/Tetris/Models/Game.cs +++ b/Tetris/Models/Game.cs @@ -28,6 +28,16 @@ public class Game : INotifyPropertyChanged { } } + private bool _playing = true; + + public bool Playing { + get => _playing; + set { + _playing = value; + OnPropertyChanged("Playing"); + } + } + private Grid _grid; public Grid Grid => _grid; @@ -78,8 +88,14 @@ public class Game : INotifyPropertyChanged { return; _grid.PrintTetrominoe(_currentTetrominoe); - _currentTetrominoe = _nextTetrominoe; - _nextTetrominoe = GetNewTetrominoe(); + + if (Playing) { + _currentTetrominoe = _nextTetrominoe; + _nextTetrominoe = GetNewTetrominoe(); + } else { + _currentTetrominoe = null; + _nextTetrominoe = null; + } } public Tetrominoe GetNewTetrominoe() { @@ -87,6 +103,14 @@ public class Game : INotifyPropertyChanged { return new Tetrominoe(_grid, tetrominoes[_random.Next(tetrominoes.Count)]); } + public void SaveGame() { + Console.WriteLine("Game over"); + using var db = new ScoreContext(); + var score = new Score{Points = Score, UserName = UserName}; + db.Scores.Add(score); + db.SaveChanges(); + } + public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) diff --git a/Tetris/Models/Score.cs b/Tetris/Models/Score.cs new file mode 100644 index 0000000..dda2478 --- /dev/null +++ b/Tetris/Models/Score.cs @@ -0,0 +1,11 @@ +namespace Tetris.Models; + +public class Score { + public int Id { get; set; } + public int Points { get; set; } + public string UserName { get; set; } + + public override string ToString() { + return UserName + ": " + Points; + } +} diff --git a/Tetris/Models/ScoreContext.cs b/Tetris/Models/ScoreContext.cs new file mode 100644 index 0000000..114500b --- /dev/null +++ b/Tetris/Models/ScoreContext.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; + +namespace Tetris.Models; + +public class ScoreContext : DbContext { + public DbSet Scores { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite(@"Data Source=.\Resources\db.sl3"); + } +} diff --git a/Tetris/Tetris.csproj b/Tetris/Tetris.csproj index f0ad596..d0ce089 100644 --- a/Tetris/Tetris.csproj +++ b/Tetris/Tetris.csproj @@ -20,11 +20,21 @@ Always + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/Tetris/Views/GameWindow.xaml b/Tetris/Views/GameWindow.xaml index 80015c7..51346c5 100644 --- a/Tetris/Views/GameWindow.xaml +++ b/Tetris/Views/GameWindow.xaml @@ -6,7 +6,19 @@ mc:Ignorable="d" Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}" KeyDown="UIElement_OnKeyDown"> - + + + + + + + + + + \ No newline at end of file diff --git a/Tetris/Views/GameWindow.xaml.cs b/Tetris/Views/GameWindow.xaml.cs index 561455f..85108ef 100644 --- a/Tetris/Views/GameWindow.xaml.cs +++ b/Tetris/Views/GameWindow.xaml.cs @@ -13,11 +13,19 @@ public partial class GameWindow DataContext = new GameViewModel(); } + public void Start(string userName) { + GameViewModel.Game.UserName = userName; + Show(); + } + [DllImport("kernel32.dll")] private static extern bool AttachConsole(int dwProcessId); private void UIElement_OnKeyDown(object sender, KeyEventArgs e) { + if (!GameViewModel.Game.Playing) + return; + switch (e.Key) { // If key is space diff --git a/Tetris/Views/MainWindow.xaml b/Tetris/Views/MainWindow.xaml index 8993ed9..b91dcf7 100644 --- a/Tetris/Views/MainWindow.xaml +++ b/Tetris/Views/MainWindow.xaml @@ -10,7 +10,12 @@ -