Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group System #33

Merged
merged 2 commits into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<Compile Include="Base\Interface\IWindow.cs" />
<Compile Include="LunaLog.cs" />
<Compile Include="Base\MessageSystem.cs" />
<Compile Include="Systems\Groups\GroupMessageHandler.cs" />
<Compile Include="Systems\Groups\GroupMessageSender.cs" />
<Compile Include="Systems\Groups\GroupSystem.cs" />
<Compile Include="Systems\VesselDockSys\VesselDockStructure.cs" />
<Compile Include="Systems\VesselDockSys\VesselDockMessageHandler.cs" />
<Compile Include="Systems\VesselDockSys\VesselDockMessageSender.cs" />
Expand Down
4 changes: 4 additions & 0 deletions Client/Network/NetworkReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using LunaClient.Systems.Chat;
using LunaClient.Systems.CraftLibrary;
using LunaClient.Systems.Flag;
using LunaClient.Systems.Groups;
using LunaClient.Systems.Handshake;
using LunaClient.Systems.KerbalSys;
using LunaClient.Systems.Lock;
Expand Down Expand Up @@ -188,6 +189,9 @@ private static void EnqueueMessageToSystem(IServerMessageBase msg)
case ServerMessageType.Mod:
SystemsContainer.Get<ModApiSystem>().EnqueueMessage(msg.Data);
break;
case ServerMessageType.Groups:
SystemsContainer.Get<GroupSystem>().EnqueueMessage(msg.Data);
break;
default:
LunaLog.LogError($"[LMP]: Unhandled Message type {msg.MessageType}");
break;
Expand Down
6 changes: 6 additions & 0 deletions Client/Network/NetworkSimpleMessageSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LunaCommon.Message.Data.Chat;
using LunaCommon.Message.Data.Color;
using LunaCommon.Message.Data.CraftLibrary;
using LunaCommon.Message.Data.Groups;
using LunaCommon.Message.Data.Kerbal;
using LunaCommon.Message.Data.Lock;
using LunaCommon.Message.Data.Motd;
Expand Down Expand Up @@ -77,5 +78,10 @@ public static void SendAdminsRequest()
{
SystemBase.TaskFactory.StartNew(() => NetworkSender.QueueOutgoingMessage(NetworkMain.CliMsgFactory.CreateNew<AdminCliMsg>(new AdminListRequestMsgData())));
}

public static void SendGroupListRequest()
{
SystemBase.TaskFactory.StartNew(() => NetworkSender.QueueOutgoingMessage(NetworkMain.CliMsgFactory.CreateNew<GroupCliMsg>(new GroupListRequestMsgData())));
}
}
}
119 changes: 119 additions & 0 deletions Client/Systems/Groups/GroupMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using LunaClient.Base;
using LunaClient.Base.Interface;
using LunaCommon.Message.Interface;
using System.Collections.Concurrent;
using LunaCommon.Message.Types;
using LunaCommon.Message.Data.Groups;
using LunaClient.Systems.SettingsSys;
using LunaClient.Network;
using LunaCommon.Message.Client;
using LunaCommon.Enums;

namespace LunaClient.Systems.Groups
{
class GroupMessageHandler : SubSystem<GroupSystem>, IMessageHandler
{
public ConcurrentQueue<IMessageData> IncomingMessages { get; set; } = new ConcurrentQueue<IMessageData>();

public void HandleMessage(IMessageData messageData)
{
var msgData = messageData as GroupBaseMsgData;
if (msgData == null) return;

switch (msgData.GroupMessageType)
{
case GroupMessageType.Add:
{
var data = (GroupAddMsgData)messageData;
System.RegisterGroup(data.GroupName, data.Owner);
}
break;
case GroupMessageType.Remove:
{
var data = (GroupRemoveMsgData)messageData;
System.DeregisterGroup(data.GroupName);
}
break;
case GroupMessageType.Invite:
{
var data = (GroupInviteMsgData)messageData;
if (data.AddressedTo == SettingsSystem.CurrentSettings.PlayerName)
{
System.Invite(data.GroupName);
}
}
break;
case GroupMessageType.Accept:
{
var data = (GroupAcceptMsgData)messageData;
System.AddPlayerToGroup(data.GroupName, data.AddressedTo);
}
break;
case GroupMessageType.Kick:
{
var data = (GroupKickMsgData)messageData;
System.KickPlayerFromGroup(data.GroupName, data.Player);
}
break;
case GroupMessageType.ListResponse:
{
var data = (GroupListResponseMsgData)messageData;

if (data.Groups.Length != data.Owners.Length)
{
LunaLog.LogWarning("Malformed message of type GroupSystem.ListResponse");
}
else
{
for(int i = 0; i < data.Groups.Length; i++)
{
if (!System.GroupExists(data.Groups[i]))
{
System.RegisterGroup(data.Groups[i], data.Owners[i]);
}
}
}

if (!System.IsSynced)
{
if (data.Groups.Length == 0) {
System.IsSynced = true;
MainSystem.NetworkState = ClientState.GroupsSynced;
}
System.NumGroups = data.Groups.Length;
System.NumGroupsSynced = 0;
foreach(string groupName in data.Groups)
{
NetworkSender.QueueOutgoingMessage(NetworkMain.CliMsgFactory.CreateNew<GroupCliMsg>(new GroupUpdateRequestMsgData { GroupName = groupName }));
}
}
}
break;
case GroupMessageType.UpdateResponse:
{
var data = (GroupUpdateResponseMsgData)messageData;
if (System.NumGroupsSynced < System.NumGroups)
{
System.NumGroupsSynced += 1;
}

if (System.NumGroupsSynced == System.NumGroups)
{
MainSystem.NetworkState = ClientState.GroupsSynced;
System.IsSynced = true;
}

if (!System.GroupExists(data.Name))
{
System.RegisterGroup(data.Name, data.Owner);
}
foreach(string member in data.Members)
{
System.AddPlayerToGroup(data.Name, member);
}
}
break;
}
}
}
}
18 changes: 18 additions & 0 deletions Client/Systems/Groups/GroupMessageSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LunaClient.Base.Interface;
using LunaCommon.Message.Interface;
using LunaClient.Base;

namespace LunaClient.Systems.Groups
{
class GroupMessageSender : SubSystem<GroupSystem>, IMessageSender
{
public void SendMessage(IMessageData msg)
{
throw new NotImplementedException();
}
}
}
72 changes: 72 additions & 0 deletions Client/Systems/Groups/GroupSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LunaClient.Base;
using LunaClient.Systems.SettingsSys;
using LunaCommon.Groups;

namespace LunaClient.Systems.Groups
{
class GroupSystem : MessageSystem<GroupSystem, GroupMessageSender, GroupMessageHandler>
{
Dictionary<string, Group> groups { get; set; } = new Dictionary<string, Group>();
List<string> pendingInvitations { get; } = new List<string>();

public int NumGroups = 0; // when we are syncing, we'll receive a list of group names. we want to keep retrieving them until we reach a certain total number of groups.
public int NumGroupsSynced = 0;
public bool IsSynced = false;

protected override void OnDisabled()
{
base.OnDisabled();
groups.Clear();
}

public bool IsCurrentPlayerInGroup(string groupName)
{
return groups[groupName].HasMember(SettingsSystem.CurrentSettings.PlayerName);
}

public string[] GetPlayersInGroup(string groupName)
{
return groups[groupName].GetMembers();
}

public void RegisterGroup(string groupName, string ownerName)
{
groups[groupName] = new Group(groupName, ownerName);
}

public void DeregisterGroup(string groupName)
{
groups[groupName] = null;
}

public void Invite(string groupName)
{
pendingInvitations.Add(groupName);
}

public void AddPlayerToGroup(string groupName, string player)
{
if (groups[groupName] != null)
{
groups[groupName].AddMember(player);
}
}

public void KickPlayerFromGroup(string groupName, string player)
{
if (groups[groupName] != null)
{
groups[groupName].RemoveMember(player);
}
}

public bool GroupExists(string groupName)
{
return groups[groupName] != null;
}
}
}
7 changes: 7 additions & 0 deletions Client/Systems/Network/NetworkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ private static void NetworkUpdate()
SystemsContainer.Get<VesselDockSystem>().Enabled = true;
SystemsContainer.Get<VesselSwitcherSystem>().Enabled = true;
SystemsContainer.Get<VesselRangeSystem>().Enabled = true;
NetworkSimpleMessageSender.SendGroupListRequest();
MainSystem.NetworkState = ClientState.SyncingGroups;
break;
case ClientState.SyncingGroups:
SystemsContainer.Get<MainSystem>().Status = "Syncing groups";
break;
case ClientState.GroupsSynced:
NetworkSimpleMessageSender.SendVesselListRequest();
MainSystem.NetworkState = ClientState.SyncingVessels;
break;
Expand Down
1 change: 1 addition & 0 deletions Client/Systems/SettingsSys/SettingsStructures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class SettingStructure
public bool PositionFudgeEnable { get; set; } = false;
public bool UseAlternativePositionSystem { get; set; } = true;
public bool PackOtherControlledVessels { get; set; } = false;
public int MaxGroupsPerPlayer { get; set; } = 1;

#if DEBUG
public bool Debug1 { get; set; } = false;
Expand Down
4 changes: 3 additions & 1 deletion Client/Systems/SystemsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using LunaClient.Systems.Chat;
using LunaClient.Systems.CraftLibrary;
using LunaClient.Systems.Flag;
using LunaClient.Systems.Groups;
using LunaClient.Systems.Handshake;
using LunaClient.Systems.KerbalReassigner;
using LunaClient.Systems.KerbalSys;
Expand Down Expand Up @@ -72,7 +73,8 @@ public static class SystemsHandler
SystemsContainer.Get<FlagSystem>(),
SystemsContainer.Get<KerbalReassignerSystem>(),
SystemsContainer.Get<ScenarioSystem>(),
SystemsContainer.Get<ToolbarSystem>()
SystemsContainer.Get<ToolbarSystem>(),
SystemsContainer.Get<GroupSystem>()
};

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<ItemGroup>
<Compile Include="Common.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Groups\Group.cs" />
<Compile Include="Locks\LockDefinition.cs" />
<Compile Include="Locks\LockQuery.cs" />
<Compile Include="Locks\LockQueryAsteroid.cs" />
Expand All @@ -61,6 +62,17 @@
<Compile Include="Locks\LockStore.cs" />
<Compile Include="Locks\LockType.cs" />
<Compile Include="MasterServerRetriever.cs" />
<Compile Include="Message\Client\GroupCliMsg.cs" />
<Compile Include="Message\Data\Groups\GroupAcceptMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupAddMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupBaseMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupInviteMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupKickMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupListRequestMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupListResponseMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupRemoveMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupUpdateRequestMsgData.cs" />
<Compile Include="Message\Data\Groups\GroupUpdateResponseMsgData.cs" />
<Compile Include="Message\Data\Kerbal\KerbalBaseMsgData.cs" />
<Compile Include="Message\Data\MasterServer\MsBaseMsgData.cs" />
<Compile Include="Message\Data\MasterServer\MsIntroductionMsgData.cs" />
Expand All @@ -79,12 +91,14 @@
<Compile Include="Message\MasterServerMessageFactory.cs" />
<Compile Include="Message\MasterServer\Base\MstSrvMsgBase.cs" />
<Compile Include="Message\MasterServer\MainMstSrvMsg.cs" />
<Compile Include="Message\Server\GroupSrvMsg.cs" />
<Compile Include="Message\Types\AdminMessageType.cs" />
<Compile Include="Message\Types\ChatMessageType.cs" />
<Compile Include="Enums\ClientState.cs" />
<Compile Include="Enums\ClientMessageType.cs" />
<Compile Include="Enums\ConnectionStatus.cs" />
<Compile Include="Enums\MasterServerMessageType.cs" />
<Compile Include="Message\Types\GroupMessageType.cs" />
<Compile Include="Message\Types\MasterServerMessageSubType.cs" />
<Compile Include="Message\Types\MotdMessageType.cs" />
<Compile Include="Message\Types\PlayerConnectionMessageType.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Common/Enums/ClientMessageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum ClientMessageType
Warp = 13,
Lock = 14,
Mod = 15,
Admin = 16
Admin = 16,
Groups = 18
}
}
14 changes: 8 additions & 6 deletions Common/Enums/ClientState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public enum ClientState
LocksSynced = 24,
SyncingAdmins = 25,
AdminsSynced = 26,
SyncingVessels = 27,
VesselsSynced = 28,
TimeLocking = 29,
TimeLocked = 30,
SyncingGroups = 27,
GroupsSynced = 28,
SyncingVessels = 29,
VesselsSynced = 30,
TimeLocking = 31,
TimeLocked = 32,

Starting = 31,
Running = 32
Starting = 33,
Running = 34
}
}
3 changes: 2 additions & 1 deletion Common/Enums/ServerMessageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum ServerMessageType
Lock = 14,
Mod = 15,
Admin = 16,
PlayerConnection = 17
PlayerConnection = 17,
Groups = 18
}
}
Loading