mekasu0124

Untitled

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