This repository has been archived on 2020-12-20. You can view files and clone it, but cannot push or open issues or pull requests.

243 lines
7.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
2019-06-25 21:17:52 -04:00
using Xamarin.Forms;
using Grpc.Core;
using Aurora.Services;
using Aurora.Proto.General;
using Aurora.Proto.Party;
using Aurora.Proto.Playback;
using Aurora.Proto.Events;
2019-07-05 14:17:09 -04:00
namespace Aurora.Design.Views.Party
{
2019-06-25 21:17:52 -04:00
enum PartyState
{
SelectingHost,
InParty,
Connecting,
}
public class PartyViewModel : BaseViewModel
{
2019-06-25 21:17:52 -04:00
private PartyState _state;
private string _hostname;
2019-07-05 17:37:10 -04:00
private ObservableCollection<PartyMember> _members;
//Client fields
private Channel _channel;
private RemotePartyService.RemotePartyServiceClient _remotePartyClient;
private RemotePlaybackService.RemotePlaybackServiceClient _remotePlaybackClient;
private RemoteEventService.RemoteEventServiceClient _remoteEventsClient;
private Task _eventsTask;
CancellationTokenSource _eventCancellationTokenSource;
2019-06-25 21:17:52 -04:00
public PartyViewModel()
{
2019-06-25 21:17:52 -04:00
this.JoinCommand = new Command(OnJoinExecute, CanJoinExecute);
this.HostCommand = new Command(OnHostExecute, CanHostExecute);
_members = new ObservableCollection<PartyMember>
{
new PartyMember{UserName = "asdf"}
};
2019-06-25 21:17:52 -04:00
SetState(PartyState.SelectingHost);
}
~PartyViewModel()
{
Task.Run(ServerService.Instance.Stop);
}
2019-06-25 21:17:52 -04:00
#region Properties
2019-07-05 17:37:10 -04:00
public ObservableCollection<PartyMember> Members
{
2019-07-05 17:37:10 -04:00
get
{
return _members;
}
set
{
SetProperty(ref _members, value);
}
}
2019-06-25 21:17:52 -04:00
public bool IsSelectingHost
{
get { return _state == PartyState.SelectingHost; }
}
public bool IsNotSelectingHost
{
get { return _state != PartyState.SelectingHost; }
}
public Command JoinCommand { get; set; }
public Command HostCommand { get; set; }
public string Hostname
{
get { return _hostname; }
set { SetProperty(ref _hostname, value); }
}
#endregion Properties
#region Commands
private void OnJoinExecute()
{
InitializeClients(Hostname, SettingsService.Instance.DefaultPort.ToString());
JoinParty();
2019-07-06 15:52:28 -04:00
SetState(PartyState.Connecting);
2019-06-25 21:17:52 -04:00
}
private bool CanJoinExecute()
{
return true;
}
private void OnHostExecute()
{
ServerService.Instance.Start();
string localHost = ServerService.GetLocalIPAddress();
InitializeClients(localHost, SettingsService.Instance.DefaultPort.ToString());
JoinParty();
2019-07-05 17:37:10 -04:00
//Change state
SetState(PartyState.Connecting);
2019-06-25 21:17:52 -04:00
}
private bool CanHostExecute()
{
return true;
}
#endregion Commands
#region Private Methods
private void InitializeClients(string hostname, string port)
2019-07-06 15:52:28 -04:00
{
_channel = new Channel(string.Format("{0}:{1}", hostname, port), ChannelCredentials.Insecure);
_remotePartyClient = new RemotePartyService.RemotePartyServiceClient(_channel);
_remotePlaybackClient = new RemotePlaybackService.RemotePlaybackServiceClient(_channel);
_remoteEventsClient = new RemoteEventService.RemoteEventServiceClient(_channel);
//Assign but don't start task
_eventsTask = new Task(GetEvents);
_eventCancellationTokenSource = new CancellationTokenSource();
}
/// <summary>
/// Join the remote party.
/// </summary>
/// <returns></returns>
private async void JoinParty()
{
await _remotePartyClient.JoinPartyAsync(new JoinPartyRequest
2019-07-06 15:52:28 -04:00
{
UserName = SettingsService.Instance.Username,
});
RefreshMembers();
//Subscribe to events
SubscribeRequest req = new SubscribeRequest();
req.EventTypes.Add(EventType.PartyMemberJoined);
req.EventTypes.Add(EventType.PartyMemberLeft);
try
{
_remoteEventsClient.SubscribeToEvents(req);
}
catch (Exception ex)
{
Console.WriteLine("Error subscribing to events: " + ex.Message);
}
_eventsTask.Start();
}
/// <summary>
/// Refresh members list.
/// </summary>
private void RefreshMembers()
{
MembersResponse response = _remotePartyClient.GetPartyMembers(new Empty());
//Add members
foreach (PartyMember member in response.Members)
{
Members.Add(member);
}
//Remove out of date members
// foreach (PartyMember member in Members)
// {
// if (!response.Members.Contains(member))
// {
// Members.Remove(member);
// }
// }
}
private void SetState(PartyState state)
{
_state = state;
OnPropertyChanged("IsSelectingHost");
OnPropertyChanged("IsNotSelectingHost");
}
/// <summary>
/// Asynchronous function for processing events off of the event stream.
/// </summary>
/// <returns></returns>
private async void GetEvents()
{
using (AsyncServerStreamingCall<BaseEvent> eventStream = _remoteEventsClient.GetEvents(new Empty()))
{
while (!_eventCancellationTokenSource.Token.IsCancellationRequested)
{
if (await eventStream.ResponseStream.MoveNext(_eventCancellationTokenSource.Token))
{
//Convert derived event type
BaseEvent e = eventStream.ResponseStream.Current;
switch (e.DerivedEventCase)
{
case BaseEvent.DerivedEventOneofCase.None:
{
throw new InvalidOperationException();
}
case BaseEvent.DerivedEventOneofCase.PartyMemberJoinedEvent:
{
PartyMemberJoinedEvent derivedEvent = e.PartyMemberJoinedEvent;
Members.Add(derivedEvent.Member);
break;
}
case BaseEvent.DerivedEventOneofCase.PartyMemberLeftEvent:
{
PartyMemberJoinedEvent derivedEvent = e.PartyMemberJoinedEvent;
var found = Members.Where(x => x.Id == derivedEvent.Member.Id);
foreach (PartyMember member in found)
{
Members.Remove(member);
}
break;
}
}
}
}
}
2019-07-06 15:52:28 -04:00
}
#endregion Private Methods
}
}