aurora/Aurora/RemoteImpl/RemotePartyImpl.cs
watsonb8 823e1341ca First pass at events almost buttoned up.
The goal is to get the members list to update when new users enter and leave the party.
2019-07-07 17:12:13 -04:00

96 lines
3.0 KiB
C#

using System;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Linq;
using Google.Protobuf.WellKnownTypes;
using Aurora.Proto.Party;
using Aurora.Proto.General;
using Aurora.Proto.Events;
using Aurora.Models;
using Aurora.Services;
namespace Aurora.RemoteImpl
{
public class RemotePartyServiceImpl : RemotePartyService.RemotePartyServiceBase
{
/// <summary>
/// Dictionary of party members. Key -> ClientId
/// </summary>
private ObservableCollection<PartyMember> _partyMembers;
public RemotePartyServiceImpl()
{
_partyMembers = new ObservableCollection<PartyMember>();
//Add self to members list
_partyMembers.Add(new PartyMember
{
UserName = SettingsService.Instance.Username,
Id = "asdf",
IpAddress = ServerService.Instance.Hostname,
Port = ServerService.Instance.Port
});
}
public ObservableCollection<PartyMember> PartyMembers
{
get
{
return _partyMembers;
}
}
public override Task<JoinPartyResponse> JoinParty(JoinPartyRequest request, Grpc.Core.ServerCallContext context)
{
PartyMember partyMember = new PartyMember()
{
UserName = request.UserName,
IpAddress = context.Host,
};
_partyMembers.Add(partyMember);
BaseEvent e = new BaseEvent
{
EventType = EventType.PartyMemberJoined,
PartyMemberJoinedEvent = new PartyMemberJoinedEvent
{
Member = partyMember,
}
};
EventManager.Instance.PushEvent(e);
JoinPartyResponse response = new JoinPartyResponse() { Status = PartyJoinedStatusEnum.Connected };
return Task.FromResult(response);
}
public override Task<LeavePartyResponse> LeaveParty(LeavePartyRequest request, Grpc.Core.ServerCallContext context)
{
PartyMember partyMember = _partyMembers.Where(e => e.Id == request.ClientId).Single();
_partyMembers.Remove(partyMember);
BaseEvent bv = new BaseEvent
{
EventType = EventType.PartyMemberJoined,
PartyMemberLeftEvent = new PartyMemberLeftEvent
{
Member = partyMember,
}
};
EventManager.Instance.PushEvent(bv);
LeavePartyResponse response = new LeavePartyResponse() { Status = PartyJoinedStatusEnum.Disconnected };
return Task.FromResult(response);
}
public override Task<MembersResponse> GetPartyMembers(Proto.General.Empty empty, Grpc.Core.ServerCallContext context)
{
MembersResponse response = new MembersResponse();
response.Members.AddRange(_partyMembers);
return Task.FromResult(response);
}
}
}