/// <summary> /// Informs the server about a new ball location which resulted in a goal. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MultiplayerGame_GoalsChanged(object sender, BallEventArgs e) { string serializedGoalImage = new JavaScriptSerializer().Serialize(e.Image); CurrentClient.Send("goal<>:" + serializedGoalImage); }
/// <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; } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void CurrentClient_GoalImageReceived(object sender, BallEventArgs e) { MultiplayerGame.UseBallEventImage(e.Image); }