private void HandleGamePieceSync_One(string[] segments)
        {
            //0#|syncgmepce#|matchId#|playerAccountId#|playerSlot#|pieceNetworkId#|pieceName
            // #|positionX#|positionY#|hitpoints#|defense#|attack#|turnDelay#|rotation

            try
            {
                GamePieceNetworkState pieceNetworkState = new GamePieceNetworkState
                                                          (
                    segments[3],
                    Int32.Parse(segments[4]),
                    segments[5],
                    segments[6],
                    Int32.Parse(segments[7]),
                    Int32.Parse(segments[8]),
                    Int32.Parse(segments[9]),
                    Int32.Parse(segments[10]),
                    Int32.Parse(segments[11]),
                    Int32.Parse(segments[12]),
                    float.Parse(segments[13])
                                                          );

                MatchHandler.SyncGamePieceToMatch(segments[2], pieceNetworkState);
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        private void ProcessSpectateMatchRequest_One(string clientId, string[] segments)
        {
            Account account = ClientManager.GetClientById(clientId).AccountRelative;

            MatchHandler.AddSpectatorToMatch
                (segments[2], clientId, account.AccountId, account.Username);
        }
        private void EndMatch_One(string[] segments)
        {
            string matchId = segments[2];

            MatchHandler.EndMatch(matchId);

            MatchEventDispatcher.InvokeMatchEndEvent(new MatchEndEventArgs(matchId));
        }
        private void HandleTurnStateUpdate_One(string[] segments)
        {
            try
            {
                MatchHandler.HandleTurnStateUpdate(segments[2], Int32.Parse(segments[3]));
            }

            catch (Exception e) { Console.WriteLine(e.ToString()); }
        }
        private void HandleSyncAttackTarget_One(string[] segments)
        {   //protocol#|cmd#|matchId#|attackerId#|dmg#|targetId
            try
            {
                MatchHandler.SynchAttackTarget
                    (segments[2], segments[3], segments[4]);
            }

            catch (Exception e) { Console.WriteLine(e.ToString()); }
        }
        private void ProcessAttackSyncComplete_One(string[] segments)
        {
            try
            {
                MatchHandler.ProcessAttackSyncComplete
                    (segments[2], segments[3], Int32.Parse(segments[4]), Int32.Parse(segments[5]));
            }

            catch (Exception e) { Console.WriteLine(e.ToString()); }
        }
        private void InterEventDispatcher_MatchStartEvent(MatchStartEventArgs args)
        {
            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.StartMatch),
                                       args.MatchId,
                                       args.PlayerIdOfFirstToMove);

            MatchHandler.SendMessageToUsersInMatch(args.MatchId, cmd);
        }
        private void MatchEventDispatcher_SpectatorSyncEvent(SpectatorSyncEventArgs args)
        {
            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}{1}{5}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.SyncMatchSpectator),
                                       args.Match.MatchIdentity,
                                       args.PlayerState.AccountIdentity,
                                       args.PlayerState.Username);

            MatchHandler.SendMessageToUsersInMatch(args.Match, cmd);
        }
        private void InterEventDispatcher_TurnStateSyncEvent(TurnStateSyncEventArgs args)
        {
            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}{1}{5}{1}{6}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.TurnStateSync),
                                       args.MatchId,
                                       args.CurrentPlayerId,
                                       args.TurnStep.ToString(),
                                       args.LowestTurnStep.ToString());

            MatchHandler.SendMessageToUsersInMatch(args.MatchId, cmd);
        }
        private void HandleMatchMessage_One(string[] segments)
        {
            //protocol#|lbymsg#|matchId#|username#|time#|message<#>
            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}{1}{5}{1}{6}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.MatchMessage),
                                       segments[2],
                                       segments[3],
                                       segments[4],
                                       segments[5]);

            MatchHandler.SendMessageToUsersInMatch(segments[2], cmd);
        }
        private void InterEventDispatcher_AttackSyncCompleteEvent(AttackSyncCompleteEventArgs args)
        {
            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}{1}{5}{1}{6}{1}{7}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.ProcessAttack),
                                       args.MatchId,
                                       args.AttackingPieceId,
                                       args.DefendingPlayerId,
                                       args.BoardVectorX.ToString(),
                                       args.BoardVectorY.ToString());

            MatchHandler.SendMessageToUsersInMatch(args.MatchId, cmd);
        }
        private void AttemptMatchCreation_One(string clientId, string[] segments)
        {
            try
            {   //protocol#|crtmtch#|userId#|username
                string accountId = segments[2];
                string username  = segments[3];

                MatchHandler.CreateNewMatch(accountId, username);
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        private void HandleNetworkPieceMove_One(string[] segments)
        {
            //protocol#|command#|matchId#|pieceNetId#|x#|y

            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}{1}{5}{1}{6}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.NetworkPieceMoveEvent),
                                       segments[2],
                                       segments[3],
                                       segments[4],
                                       segments[5]);

            MatchHandler.SendMessageToUsersInMatch(segments[2], cmd);
        }
        private void HandleSpectatorJoinEvent(string matchId, string clientId, bool succeeded)
        {
            MatchState match = MatchHandler.GetMatchStateById(matchId);

            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.SpectateMatchResult),
                                       matchId,
                                       succeeded.ToString().ToLowerInvariant());

            ClientManager.GetClientById(clientId).SendData(cmd);

            SyncMatchSpectators(match);
        }
        private void SendGamePieceSyncToSpectator
            (string matchId, string spectatorId, string controllerId, GamePieceNetworkState[] pieces)
        {
            for (int i = 0; i < pieces.Length; i++)
            {
                MatchHandler.SendMessageToUsersInMatch
                    (matchId, GetSyncCommandFromPieceNetworkState(matchId, pieces[i]));
            }

            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.EndGamePieceSync),
                                       matchId,
                                       controllerId);

            ClientManager.SendMessageToClientByAccount(spectatorId, cmd);
        }
        private void HandleMatchCreationEvent(MatchState match)
        {
            //protocol#|mtchcrtd#|matchId#|playerOneId#|playerOneUsername
            string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}{1}{5}",
                                       (int)ReadProtocol.GetVersion(),
                                       m_SegmentTerminator,
                                       NetworkCommand.GetCommand(NetworkCommandType.MatchCreated),
                                       match.MatchIdentity,
                                       match.PlayerOne.AccountIdentity,
                                       match.PlayerOne.Username);

            MatchHandler.SendMessageToUsersInMatch(match, cmd);

            ClientState client = ClientManager.GetClientByAccount
                                     (AccountHandler.GetAccountById(match.PlayerOne.AccountIdentity));

            ClientManager.SendMessageToAllClients(cmd, client);
        }
        internal static void RemoveOnlineAccount(Account account)
        {
            if (m_AccountsOnline.ContainsKey(account.Username))
            {
                m_AccountsOnline.Remove(account.Username);
            }

            if (m_AccountIdentities.ContainsKey(account.AccountId))
            {
                m_AccountIdentities.Remove(account.AccountId);
            }

            if (account.CurrentMatch != null)
            {
                MatchHandler.HandleDisconnectFromMatch
                    (account.CurrentMatch, account.AccountId);
            }
        }
        private void SendGamePieceSyncToMatch(string matchId, string controllerId, GamePieceNetworkState[] pieces, bool endSync)
        {
            for (int i = 0; i < pieces.Length; i++)
            {
                MatchHandler.SendMessageToUsersInMatch
                    (matchId, GetSyncCommandFromPieceNetworkState(matchId, pieces[i]));
            }

            if (endSync)
            {
                string cmd = string.Format("{0}{1}{2}{1}{3}{1}{4}",
                                           (int)ReadProtocol.GetVersion(),
                                           m_SegmentTerminator,
                                           NetworkCommand.GetCommand(NetworkCommandType.EndGamePieceSync),
                                           matchId,
                                           controllerId);

                MatchHandler.FlagFirstSyncForPlayer(matchId, controllerId);
                MatchHandler.SendMessageToUsersInMatch(matchId, cmd);
            }
        }
 private void HandleEndPieceSync_One(string[] segments)
 {
     //protocol#|cmd#|matchId#|accountId
     MatchHandler.HandlePieceSyncComplete(segments[2], segments[3]);
 }
 private void InterEventDispatcher_MatchDepartureTimerExpiredEvent(MatchDepartureTimerEventArgs args)
 {
     MatchHandler.RemoveUserFromMatch(args.Match, args.PlayerState.AccountIdentity);
 }
 private void ProcessJoinMatchAttempt_One(string clientId, string[] segments)
 {
     MatchHandler.AttemptToJoinMatch
         (ClientManager.GetClientById(clientId), segments[2]);
 }