Ejemplo n.º 1
0
        /// <summary>
        /// Interprets the mesage and raises the suitable event.
        /// 
        /// Syntax:
        /// 
        /// chat<chatId>:Player1;Player2;Player3 --> Raises NewChatCreated()-event with the received users as the UserListEventArgs.
        /// That will create a new chatroom with these users.
        /// 
        /// userlist<chatId>:serialized List<User> --> The userlist of chatroom with this chat id.
        /// info<HH:mm:ss,Username,ServermessageType>: --> The information that someone has joined/left the server or changed the team.
        /// message<chatId,Username,HH:mm:ss>:message content --> A chat message from an user in a specific chatroom
        /// name<>:name --> Raises LoginNameAssigned()-event if the name equals the own name - raises UserNameChanged()-event if not.
        /// settings<>:serialized GameSettings--> Raises HandleReceivedSettings()-method with the serialized GameSetting as the parameter.
        /// start<>:serialized GameSettings  --> Raises the GameCreated()-event with the received settings.
        /// change<>:serialized ChangeOverImage --> Raises the ChangeoverReceived()-event.
        /// useSavegame<>:bool --> Raises UseSavegameChanged()-event with the received bool as the BoolEventArgs.
        /// end<>: --> Raises GameEnded()-event.
        /// action<>:serialized CachedRoundAction --> Raises ActionReceived()-event with the received CachedRoundAction as the ReceivedActionArgs.
        /// team<username, oldTeamId, newTeamId>: --> The information that someone switched the team. Raises HandleTeamEvent()-method.
        /// </summary>
        /// <param name="message">The message from the server</param>
        private void Interpret(string message)
        {
            int paramsStart = message.IndexOf('<');
            int paramsEnd = message.IndexOf('>');

            string paramsString = message.Substring(paramsStart + 1, paramsEnd - (paramsStart + 1));
            string[] parameters = paramsString.Split(',');
            message = message.Remove(paramsStart, paramsEnd - (paramsStart - 1)); //type:content

            string[] splitted = message.Split(':');

            string type = splitted[0];
            string content = message.Substring(message.IndexOf(':') + 1, (message.Length - (message.IndexOf(':') + 1)));

            switch (type)
            {
                case "chat":
                    string[] names = content.Split(';');
                    UserListEventArgs args = new UserListEventArgs(names, Convert.ToInt32(parameters[0]), ListType.ChatRoom);
                    NewChatCreated(this, args);
                    break;

                case "userlist":
                    if (Convert.ToInt32(parameters[0]) == 1)
                    {
                        List<User> users = serializer.Deserialize<List<User>>(content);
                        UserListEventArgs userListArgs = new UserListEventArgs(users, Convert.ToInt32(parameters[0]), ListType.ChatRoom);
                        CompleteUserListReceived(this, userListArgs);
                    }
                    else
                    {
                        string[] usernames = content.Split(';');
                        UserListEventArgs userListArgs = new UserListEventArgs(usernames, Convert.ToInt32(parameters[0]), ListType.ChatRoom);
                        ChatUserListReceived(this, userListArgs);
                    }
                    break;

                case "name":
                    UsernameChangeEventArgs changeArgs;
                    if (parameters[0] == "")
                    {
                        string oldName = ClientName;
                        ClientName = content;
                        changeArgs = new UsernameChangeEventArgs(oldName, content);
                        LoginNameAssigned(this, changeArgs);
                    }
                    else
                    {
                        changeArgs = new UsernameChangeEventArgs(parameters[0], content);
                        UserNameChanged(this, changeArgs);
                        if (parameters[0] == ClientName)
                        {
                            ClientName = content;
                        }
                    }
                    break;

                case "info":
                    HandleInfo(parameters, content);
                    break;

                case "message":
                    HandleNewMessage(parameters, content);
                    break;

                case "action":
                    CachedRoundAction cachedAction = serializer.Deserialize<CachedRoundAction>(content);
                    ReceivedActionArgs actionArgs = new ReceivedActionArgs(cachedAction);
                    ActionReceived(this, actionArgs);
                    break;

                case "team":
                    HandleTeamEvent(parameters, content);
                    break;

                case "settings":
                    HandleReceivedSettings(content);
                    break;

                case "start":
                    GameSettings createSettings = serializer.Deserialize<GameSettings>(content);
                    FieldSettingsEventArgs createSettingsArgs = new FieldSettingsEventArgs(createSettings);
                    GameCreated(this, createSettingsArgs);
                    break;

                case "ready":
                    UserListEventArgs readyArgs = new UserListEventArgs(new string[] { content }, 1, ListType.Team);
                    UserIsReady(this, readyArgs);
                    break;

                case "calculate":
                    List<string> listOfActions = serializer.Deserialize<List<string>>(content);
                    List<CachedRoundAction> actions = new List<CachedRoundAction>();
                    foreach (string serializedAction in listOfActions)
                    {
                        actions.Add(serializer.Deserialize<CachedRoundAction>(serializedAction));
                    }

                    ReceivedActionArgs roundActionsArgs = new ReceivedActionArgs(actions);
                    RoundActionsReceived(this, roundActionsArgs);
                    break;

                case "round":
                    RoundImage status = serializer.Deserialize<RoundImage>(content);
                    RoundImageEventArgs statusArgs = new RoundImageEventArgs(status);
                    RoundImageReceived(this, statusArgs);
                    break;

                case "game":
                    GameImage gameImage = serializer.Deserialize<GameImage>(content);
                    GameImageEventArgs gameArgs = new GameImageEventArgs(gameImage);
                    GameImageReceived(this, gameArgs);
                    break;

                case "goal":
                    BallEventImage image = serializer.Deserialize<BallEventImage>(content);
                    BallEventArgs goalArgs = new BallEventArgs(image);
                    GoalImageReceived(this, goalArgs);
                    break;

                case "status":
                    MultiplayerStatus mpGameStatus = (MultiplayerStatus)Enum.Parse(typeof(MultiplayerStatus), content);
                    GameStatusEventArgs mpStatusArgs = new GameStatusEventArgs(mpGameStatus);
                    MultiplayerStatusReceived(this, mpStatusArgs);
                    break;

                case "useSavegame":
                    BoolEventArgs boolArgs = new BoolEventArgs(Convert.ToBoolean(content));
                    UseSavegameChanged(this, boolArgs);
                    break;

                case "changeover":
                    ChangeoverImage changeImage = serializer.Deserialize<ChangeoverImage>(content);
                    ChangeoverEventArgs changeoverArgs = new ChangeoverEventArgs(changeImage);
                    ChangeoverReceived(this, changeoverArgs);
                    break;

                case "end":
                    GameEndReceived(this, EventArgs.Empty);
                    break;

                default:
                    Console.WriteLine("There is no function for the message: " + message);
                    break;
            }
        }
Ejemplo n.º 2
0
 void UsernameChangeRequest(object sender, UsernameChangeEventArgs e)
 {
     CurrentClient.Send("name<>:" + e.NewName);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Updates the own name in the database and adds the general chat page to the chat if it is not added yet.
        /// If it is added, this is a normal change of the own username.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void LoginNameAssigned(object sender, UsernameChangeEventArgs e)
        {
            string savedName = databaseHandler.GetUsername(CurrentClient.ServerAddress);
            if (string.IsNullOrWhiteSpace(savedName) || savedName.Contains(';') || savedName.Contains(':') || savedName.Contains('<') || savedName.Contains('>'))
            {
                databaseHandler.UpdateUsername(CurrentClient.ServerAddress, e.NewName);
            }

            if (CurrentChats.TabPages.Count == 0)
            {
                AddChat(1, null);
            }
            else
            {
                ChangeUsername(e.OldName, e.NewName);
            }
        }
Ejemplo n.º 4
0
        void UserNameChanged(object sender, UsernameChangeEventArgs e)
        {
            User user = Users.Find(x => x.Username == e.OldName);
            if (user != null)
            {
                user.Username = e.NewName;
            }

            ChatTabPage firstchat = (ChatTabPage)CurrentChats.TabPages[0];
            if (teamChanger != null && teamChanger.Visible)
            {
                teamChanger.UpdateUserList(Users);
            }

            if (string.IsNullOrWhiteSpace(e.OldName))
            {
                AddChat(1, null);
            }

            ChangeUsername(e.OldName, e.NewName);
        }