pull/100/head
parent
0153fd24a7
commit
8e9bca4b9a
@ -1,16 +1,82 @@
|
|||||||
|
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace Trek_12.Views;
|
namespace Trek_12.Views;
|
||||||
using Stub;
|
using Stub;
|
||||||
|
using Models.Game;
|
||||||
|
|
||||||
public partial class PageSelectMap : ContentPage
|
public partial class PageSelectMap : ContentPage
|
||||||
{
|
{
|
||||||
|
public Game SelectMapManager => (App.Current as App).Manager;
|
||||||
|
private Map? _selectedMap;
|
||||||
|
|
||||||
|
protected override async void OnAppearing()
|
||||||
|
{
|
||||||
|
base.OnAppearing();
|
||||||
|
if (SelectMapManager.Games.Any(g => g.IsRunning))
|
||||||
|
{
|
||||||
|
|
||||||
|
await DisplayAlert("Warning", "You've previously quit in the middle of a game.\nIf you start a new game, this one will be permanently lost.", "I understand");
|
||||||
|
|
||||||
public Stub MyStub { get; set; } = new Stub();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public PageSelectMap()
|
public PageSelectMap()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
BindingContext = MyStub;
|
BindingContext = SelectMapManager;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||||
|
{
|
||||||
|
_selectedMap = e.CurrentSelection.FirstOrDefault() as Map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void BackButton_Clicked(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
await Shell.Current.GoToAsync("..");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void PlayButton_Clicked(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_selectedMap == null)
|
||||||
|
{
|
||||||
|
await DisplayAlert("Selection Required", "Please select a map you want to play to continue.", "OK");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (SelectMapManager.Players.Count == 0)
|
||||||
|
{
|
||||||
|
await DisplayAlert("No player found", "Please add a player in the profile page.", "OK");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string[] profiles = GetProfiles().ToArray();
|
||||||
|
string choosenPlayerName = await DisplayActionSheet("Choose a player", "Cancel", null, profiles);
|
||||||
|
if (choosenPlayerName == null || choosenPlayerName == "Cancel") return;
|
||||||
|
|
||||||
|
Player chosenPlayer = GetProfileByName(choosenPlayerName);
|
||||||
|
|
||||||
|
SelectMapManager.InitializeGame(_selectedMap, chosenPlayer);
|
||||||
|
|
||||||
|
if (SelectMapManager.UsedMap == _selectedMap && Equals(SelectMapManager.CurrentPlayer, chosenPlayer))
|
||||||
|
{
|
||||||
|
await Shell.Current.GoToAsync(nameof(PageBoard));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await DisplayAlert("Error", "An error occured while initializing the game. Please try again.", "OK");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<string> GetProfiles()
|
||||||
|
{
|
||||||
|
return SelectMapManager.Players.Select(p => p.Pseudo).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Player GetProfileByName(string pseudo)
|
||||||
|
{
|
||||||
|
return SelectMapManager.Players.FirstOrDefault(p => p.Pseudo == pseudo);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in new issue