/// <summary> /// Attempts to fetch a game from the model's data. /// If the game does not exist, a check is performed to see if the sending client is in a different game. /// if so, the function closes. /// Otherwise we create the game, and add the client to the game. /// Otherwise, if the game exists, the model attemps to add the client to the game. /// if succeeded, the game now contains 2 different client, so a maze is generated, /// given a name, and sent to the first client. /// then a different starting position is given to the same maze and it is sent to the second client. /// if the client could not be added to the game the function exists. /// </summary> /// <param name="from">the client that sent the command.</param> /// <param name="commandParsed">The parsed command.</param> public override void Execute(object from, string[] commandParsed) { string name = commandParsed[1]; int commandType = 3; MultiplayerGame g = model.GetMultiplayerGame(name); // game does not exist if (g == null) { // client is already in a different multiplayer game if (model.IsClientInGame(from) != null) { return; } // otherwise create a game IMaze maze = GenerateOption.CreateMaze(1); MultiplayerGame game = new MultiplayerGame(model, name, maze); game.AddClient(from); model.AddMultiplayerGame(name, game); } // game exists else { // add second(different) client to game, and if the game had not been started yet if (g.AddClient(from) && !g.IsInProgress()) { g.GameStarted(); string reply; IMaze maze = g.GetMaze(); IMaze secondMaze; object client; string mazeName; // generate answer for first client mazeName = name + "_1"; this.model.AddMaze(mazeName, maze); GenerateAnswer firstClient = BuildMaze(maze, mazeName); // change starting position for second client mazeName = name + "_2"; secondMaze = maze.CreateMazeChangeStartPosition(); this.model.AddMaze(mazeName, secondMaze); GenerateAnswer secondClient = BuildMaze(secondMaze, mazeName); // first client g.RetrieveOtherClient(from, out client); reply = BuildReply(name, commandType, firstClient, secondClient); model.CompletedTask(client, new View.MessageEventArgs(reply)); // second client ('from' is the second player) client = from; reply = BuildReply(name, commandType, secondClient, firstClient); model.CompletedTask(client, new View.MessageEventArgs(reply)); } } }