119 lines
4.0 KiB
C#
119 lines
4.0 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using System.Collections;
|
|
using Aurora.Proto.PartyV2;
|
|
using Aurora.Proto.General;
|
|
using Aurora.Utils;
|
|
using Grpc.Core;
|
|
|
|
namespace Aurora.Services.Server.Controllers
|
|
{
|
|
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
|
|
{
|
|
private SortedList<string, Member> _memberList;
|
|
|
|
public override Task<ListMembersResponse> ListMembers(ListMembersRequest request, Grpc.Core.ServerCallContext context)
|
|
{
|
|
//Ignoring parent field because there is only one instance of the party
|
|
ListMembersResponse resp = new ListMembersResponse();
|
|
|
|
//Determine start idx
|
|
int startIdx = 0;
|
|
if (!string.IsNullOrEmpty(request.PageToken))
|
|
{
|
|
startIdx = _memberList.IndexOfKey(request.PageToken) + 1;
|
|
}
|
|
|
|
//Assign pageSize
|
|
if (request.PageSize > _memberList.Count)
|
|
{
|
|
request.PageSize = _memberList.Count;
|
|
}
|
|
|
|
|
|
//Gather page
|
|
List<Member> members = new List<Member>(_memberList.Values);
|
|
resp.Members.AddRange(members.GetRange(startIdx, request.PageSize));
|
|
|
|
//Set next page token
|
|
resp.NextPageToken = resp.Members[(startIdx + request.PageSize) - 1].Name;
|
|
|
|
return Task.FromResult(resp);
|
|
}
|
|
|
|
public override Task<Member> GetMember(GetMemberRequest request, Grpc.Core.ServerCallContext context)
|
|
{
|
|
_memberList.TryGetValue(request.Name, out Member member);
|
|
|
|
if (member == null)
|
|
{
|
|
throw new KeyNotFoundException();
|
|
}
|
|
|
|
return Task.FromResult(member);
|
|
}
|
|
|
|
public override Task<Member> UpdateMember(UpdateMemberRequest request, Grpc.Core.ServerCallContext context)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override Task<Member> CreateMember(CreateMemberRequest request, Grpc.Core.ServerCallContext context)
|
|
{
|
|
//Generate Guid
|
|
string memberNameGuid = HashUtil.GetHashGuid(new string[] { context.Peer, request.Member.UserName }).ToString();
|
|
string resourceName = string.Format("{0}/members/{1}", request.Parent, memberNameGuid);
|
|
//Check if already added
|
|
if (_memberList.ContainsKey(resourceName))
|
|
{
|
|
throw new RpcException(new Status(StatusCode.AlreadyExists, "Member already exists"));
|
|
}
|
|
|
|
request.Member.Name = resourceName;
|
|
|
|
_memberList.Add(resourceName, request.Member);
|
|
|
|
BaseEvent @event = new BaseEvent
|
|
{
|
|
EventType = EventType.MemberCreated,
|
|
MemberCreatedEvent = new MemberCreatedEvent
|
|
{
|
|
Member = request.Member,
|
|
}
|
|
};
|
|
|
|
//Fire event manager event
|
|
this._eventManager.FireEvent(@event);
|
|
|
|
return Task.FromResult(request.Member);
|
|
}
|
|
|
|
public override Task<Empty> DeleteMember(DeleteMemberRequest request, Grpc.Core.ServerCallContext context)
|
|
{
|
|
string memberResourceName = request.Name;
|
|
//Check if member exists
|
|
if (!_memberList.ContainsKey(request.Name))
|
|
{
|
|
throw new RpcException(new Status(StatusCode.NotFound, "Member not found"));
|
|
}
|
|
|
|
_memberList.Remove(memberResourceName);
|
|
|
|
BaseEvent @event = new BaseEvent
|
|
{
|
|
EventType = EventType.MemberDeleted,
|
|
MemberDeletedEvent = new MemberDeletedEvent
|
|
{
|
|
MemberName = memberResourceName,
|
|
}
|
|
};
|
|
|
|
_eventManager.FireEvent(@event);
|
|
_eventManager.RemoveAllSubscriptions(memberResourceName);
|
|
_eventManager.CancelEventStream(memberResourceName);
|
|
|
|
return Task.FromResult(new Empty());
|
|
}
|
|
}
|
|
} |