Files
LaDOSE/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs

241 lines
8.3 KiB
C#
Raw Normal View History

2019-03-09 14:03:25 +01:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Caliburn.Micro;
2019-03-09 14:03:25 +01:00
using LaDOSE.DesktopApp.Utils;
using LaDOSE.DTO;
using LaDOSE.REST;
2019-03-09 14:03:25 +01:00
using Action = System.Action;
namespace LaDOSE.DesktopApp.ViewModels
{
public class WordPressViewModel : Screen
{
2019-03-09 14:03:25 +01:00
public override string DisplayName => "Events";
2019-03-12 22:23:38 +01:00
private WPEventDTO _selectedWpEvent;
private GameDTO _selectedGame;
private ObservableCollection<WPUserDTO> _players;
private ObservableCollection<WPUserDTO> _playersOptions;
private ObservableCollection<WPUserDTO> _optionalPlayers;
2019-03-12 21:41:30 +01:00
private RestService RestService { get; set; }
2019-03-09 14:03:25 +01:00
public WordPressViewModel(RestService restService)
{
this.RestService = restService;
2019-03-12 22:23:38 +01:00
Players = new ObservableCollection<WPUserDTO>();
PlayersOptions = new ObservableCollection<WPUserDTO>();
OptionalPlayers = new ObservableCollection<WPUserDTO>();
}
2019-03-12 21:41:30 +01:00
#region Auto Property
protected override void OnInitialize()
{
2019-03-12 21:41:30 +01:00
base.OnInitialize();
Task.Factory.StartNew(new Action(this.Load), TaskCreationOptions.LongRunning).ContinueWith(t => { },
2019-03-09 14:03:25 +01:00
CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted,
TaskScheduler.FromCurrentSynchronizationContext());
}
public bool CanGenerate
{
2019-03-12 21:41:30 +01:00
get { return SelectedWpEvent != null && SelectedGame != null && Players?.Count() > 0; }
2019-03-09 14:03:25 +01:00
}
2019-03-12 22:23:38 +01:00
public List<WPEventDTO> Events { get; set; }
2019-03-09 14:03:25 +01:00
2019-03-12 22:23:38 +01:00
public WPEventDTO SelectedWpEvent
2019-03-09 14:03:25 +01:00
{
get => _selectedWpEvent;
set
{
_selectedWpEvent = value;
SelectedGame = null;
ParseGame(_selectedWpEvent);
}
}
2019-03-12 22:23:38 +01:00
public GameDTO SelectedGame
2019-03-09 14:03:25 +01:00
{
get => _selectedGame;
set
{
_selectedGame = value;
2019-03-12 21:41:30 +01:00
2019-03-09 14:03:25 +01:00
Players.Clear();
PlayersOptions.Clear();
2019-03-13 00:59:40 +01:00
Task.Factory.StartNew(LoadPlayers, TaskCreationOptions.LongRunning).ContinueWith(
t => { NotifyOfPropertyChange(() => this.CanGenerate); },
CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion,
2019-03-09 14:03:25 +01:00
TaskScheduler.FromCurrentSynchronizationContext());
NotifyOfPropertyChange(() => SelectedGame);
NotifyOfPropertyChange(() => Players);
NotifyOfPropertyChange(() => PlayersOptions);
2019-03-13 00:59:40 +01:00
NotifyOfPropertyChange(() => this.CanGenerate);
2019-03-09 14:03:25 +01:00
}
}
2019-03-12 22:23:38 +01:00
public ObservableCollection<WPUserDTO> Players
2019-03-09 14:03:25 +01:00
{
get => _players;
set
{
_players = value;
2019-03-12 21:41:30 +01:00
NotifyOfPropertyChange(() => Players);
2019-03-09 14:03:25 +01:00
}
}
2019-03-12 22:23:38 +01:00
public ObservableCollection<WPUserDTO> PlayersOptions
2019-03-09 14:03:25 +01:00
{
get => _playersOptions;
set
{
_playersOptions = value;
NotifyOfPropertyChange(() => PlayersOptions);
}
}
2019-03-12 22:23:38 +01:00
public ObservableCollection<WPUserDTO> OptionalPlayers
2019-03-12 21:41:30 +01:00
{
get => _optionalPlayers;
set
{
_optionalPlayers = value;
NotifyOfPropertyChange(() => OptionalPlayers);
}
}
2019-03-09 14:03:25 +01:00
2019-03-12 22:23:38 +01:00
public ObservableCollection<GameDTO> GamesFound { get; set; }
public List<GameDTO> Games { get; set; }
2019-03-09 14:03:25 +01:00
2019-03-12 21:41:30 +01:00
#endregion
#region Commands
public void UpdateDb()
{
WpfUtil.Await(()=>this.RestService.RefreshDb(), "Updated");
2019-03-12 21:41:30 +01:00
}
public void Generate()
{
2019-03-12 22:23:38 +01:00
List<WPUserDTO> test = new List<WPUserDTO>();
2019-03-12 21:41:30 +01:00
test = OptionalPlayers.ToList();
var messageBoxText = this.RestService.CreateChallonge2(SelectedGame.Id, SelectedWpEvent.Id, test);
if (messageBoxText != null && messageBoxText.Length > 0 && !messageBoxText.Contains("error"))
{
System.Diagnostics.Process.Start($"https://challonge.com/{messageBoxText}");
}
else
MessageBox.Show("Didn't work :(");
}
public void LoadEvents()
{
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
var tsk = Task.Factory.StartNew(Load);
tsk.ContinueWith(t =>
{
MessageBox.Show(t.Exception.InnerException.Message);
Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
},
CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted,
TaskScheduler.FromCurrentSynchronizationContext());
}
#endregion
//TODO : Remove the Meta of WPEvent (parse it in Update DB)
2019-03-12 22:23:38 +01:00
private void ParseGame(WPEventDTO selectedWpEvent)
2019-03-09 14:03:25 +01:00
{
var reservation = SelectedWpEvent.WpBookings.FirstOrDefault();
var games = WpEventDeserialize.Parse(reservation.Meta);
GamesFound.Clear();
var foundGames = new List<GameDTO>();
2019-03-09 14:03:25 +01:00
if (games != null)
{
foreach (string wpTag in games.Select(e => e.Name))
{
var foundGame = Games.FirstOrDefault(e =>
e.WordPressTag != null && e.WordPressTag.Split(';').Contains(wpTag));
if (foundGame != null)
{
if (!foundGames.Contains(foundGame))
2019-03-09 14:03:25 +01:00
{
foundGames.Add(foundGame);
2019-03-09 14:03:25 +01:00
}
}
}
}
var orderedEnumerable = foundGames.OrderBy(e => e.Order);
foreach (var gameDto in orderedEnumerable)
{
GamesFound.Add(gameDto);
}
2019-03-09 14:03:25 +01:00
NotifyOfPropertyChange(() => GamesFound);
}
private void LoadPlayers()
{
if (SelectedWpEvent != null)
if (SelectedGame != null)
{
2019-03-12 21:41:30 +01:00
var findUser = FindUser(SelectedWpEvent.Id, SelectedGame);
var findUser2 = FindUser(SelectedWpEvent.Id, SelectedGame,true);
2019-03-14 01:31:56 +01:00
findUser.OrderBy(e=>e.Name).ToList().ForEach((e) => this.Players.AddUI(e,()=>NotifyOfPropertyChange(() => this.CanGenerate)));
findUser2.OrderBy(e => e.Name).ToList().ForEach((e) => this.PlayersOptions.AddUI(e,null));
2019-03-13 00:59:40 +01:00
2019-03-09 14:03:25 +01:00
}
}
2019-03-12 21:41:30 +01:00
private void Load()
2019-03-09 14:03:25 +01:00
{
2019-03-12 21:41:30 +01:00
Application.Current.Dispatcher.Invoke(() =>
System.Windows.Input.Mouse.OverrideCursor = Cursors.Wait);
2019-03-12 22:23:38 +01:00
GamesFound = new ObservableCollection<GameDTO>();
2019-03-12 21:41:30 +01:00
this.Games = this.RestService.GetGames();
var events = this.RestService.GetEvents();
events.ForEach(e => e.WpBookings = e.WpBookings.OrderBy(x => x.WpUser.Name).ToList());
this.Events = events;
2019-03-12 21:41:30 +01:00
NotifyOfPropertyChange("Events");
Application.Current.Dispatcher.Invoke(() =>
System.Windows.Input.Mouse.OverrideCursor = null);
2019-03-09 14:03:25 +01:00
}
2019-03-12 22:23:38 +01:00
public List<WPUserDTO> FindUser(int wpEventId, GameDTO game,bool optional = false)
2019-03-09 14:03:25 +01:00
{
2019-03-12 21:41:30 +01:00
string[] selectedGameWpId;
selectedGameWpId = !optional ? game.WordPressTag.Split(';') : game.WordPressTagOs.Split(';');
var currentWpEvent = this.Events.Where(e => e.Id == wpEventId).ToList();
2019-03-12 22:23:38 +01:00
List<WPBookingDTO> bookings = currentWpEvent.SelectMany(e => e.WpBookings).ToList();
List<WPUserDTO> users = new List<WPUserDTO>();
2019-03-12 21:41:30 +01:00
foreach (var booking in bookings)
{
var reservations = WpEventDeserialize.Parse(booking.Meta);
if (reservations != null)
{
var gamesReservation = reservations.Where(e => e.Valid).Select(e => e.Name);
if (selectedGameWpId.Any(e => gamesReservation.Contains(e)))
{
users.Add(booking.WpUser);
}
}
}
return users;
}
}
}