mekasu0124

Untitled

Oct 17th, 2023
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using ReactiveUI;
  2. using System;
  3. using System.Collections.Generic;
  4. using MeksMathGame.Models;
  5. using System.Reactive.Linq;
  6. using System.Linq;
  7. using MeksMathGame.Services;
  8.  
  9. namespace MeksMathGame.ViewModels;
  10.  
  11. public class HomeScreenViewModel : ViewModelBase
  12. {
  13.     public string? _information;
  14.     public Difficulties? _selectedDiff;
  15.     public string? _maxQues;
  16.  
  17.     public List<Difficulties?>? _allDifficulties;
  18.     public List<string?>? _numberRange;
  19.  
  20.     public HomeScreenViewModel()
  21.     {
  22.         List<string> info = new()
  23.         {
  24.             "Welcome to the Math Game! There are 5 buttons on the right side.",
  25.             "To play a game, select a difficulty and the number of questions you",
  26.             "want to answer below, and then click a game button to the right.",
  27.             "To go back to select another user, click the Back button. To view",
  28.             "all the games you've played, click the Previous Games button",
  29.             "\n\nEasy => Number Ranges Between 0 and 300",
  30.             "\nMedium => Number Ranges Between 0 and 600",
  31.             "\nHard => Number Ranges Between 0 and 900"
  32.         };
  33.  
  34.         Information = string.Join(" ", info);
  35.         AllDifficulties = new() { Difficulties.Easy, Difficulties.Medium, Difficulties.Hard };
  36.  
  37.         var numbers = Enumerable.Range(1, 25);
  38.         NumberRange = new() { "Select One" };
  39.         NumberRange.AddRange(numbers.Select(x => x.ToString()));
  40.  
  41.         MaxQues = NumberRange[0];
  42.  
  43.         IObservable<bool> numQuesOk = this.WhenAnyValue(
  44.             x => x.MaxQues,
  45.             x => x != "Select One");
  46.  
  47.         IObservable<bool> diffOk = this.WhenAnyValue(
  48.             x => x.SelectedDiff,
  49.             x => AllDifficulties.Contains(x));
  50.  
  51.         IObservable<bool> okEnabled = numQuesOk
  52.             .Concat(diffOk);
  53.  
  54.         Play = ReactiveCommand.Create<GameTypes, Game>(gameType => PlayGame(gameType), okEnabled);
  55.         PreviousGames = ReactiveCommand.Create<GameTypes, Game>(gameType => ShowPreviousGames(gameType));
  56.         BackButton = ReactiveCommand.Create<GameTypes, Game>(gameType => SendBackButton(gameType));
  57.     }
  58.  
  59.     public Game PlayGame(GameTypes gameType)
  60.     {
  61.         int? totalQues = Convert.ToInt32(MaxQues);
  62.  
  63.         return new Game
  64.         {
  65.             TotalQuestions = totalQues,
  66.             Difficulty = SelectedDiff,
  67.             GameType = gameType
  68.         };
  69.     }
  70.  
  71.     public Game ShowPreviousGames(GameTypes gameType)
  72.     {
  73.         return new Game { GameType = GameTypes.PreviousQuestions };
  74.     }
  75.  
  76.     public Game SendBackButton(GameTypes gameType)
  77.     {
  78.         return new Game { GameType = GameTypes.BackButton };
  79.     }
  80.  
  81.     #region Getters/Setters
  82.     public string? Information
  83.     {
  84.         get => _information;
  85.         set => this.RaiseAndSetIfChanged(ref _information, value);
  86.     }
  87.     public Difficulties? SelectedDiff
  88.     {
  89.         get => _selectedDiff;
  90.         set => this.RaiseAndSetIfChanged(ref _selectedDiff, value);
  91.     }
  92.     public string? MaxQues
  93.     {
  94.         get => _maxQues;
  95.         set => this.RaiseAndSetIfChanged(ref _maxQues, value);
  96.     }
  97.     public List<Difficulties?>? AllDifficulties
  98.     {
  99.         get => _allDifficulties;
  100.         set => this.RaiseAndSetIfChanged(ref _allDifficulties, value);
  101.     }
  102.     public List<string?>? NumberRange
  103.     {
  104.         get => _numberRange;
  105.         set => this.RaiseAndSetIfChanged(ref _numberRange, value);
  106.     }
  107.     #endregion
  108.  
  109.     public ReactiveCommand<GameTypes, Game> Play { get; }
  110.     public ReactiveCommand<GameTypes, Game> PreviousGames { get; }
  111.     public ReactiveCommand<GameTypes, Game> BackButton { get; }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment