/// <summary> /// Join the client to the game. If the game not exist the client gets a message. /// </summary> /// <param name="args">The arguments.</param> /// <param name="client">The client.</param> /// <returns> /// string: "singlePlayer" or "multiPlayer" /// </returns> public string Execute(string[] args, TcpClient client) { if (!this.CheckValid(args, client)) { return("multiPlayer"); } model.GetmodelData().mutexGamePlaying.WaitOne(); model.GetmodelData().mutexGameWating.WaitOne(); // the name of the game to join. string name = args[0]; GameMultiPlayer game = model.FindGameWating(name); if (model.ClientOnGameByName(client, name)) { Controller.NestedErrors nested = new Controller.NestedErrors("You already on the game", client); } // check if the game is in the list of games to play. else if (game != null) { game.Join(client); // Add to Game play list and remove from wating list. model.AddGamePlaying(name, game); model.RemoveGameWating(name); } else { Controller.NestedErrors nested = new Controller.NestedErrors("Error exist game", client); } model.GetmodelData().mutexGamePlaying.ReleaseMutex(); model.GetmodelData().mutexGameWating.ReleaseMutex(); return("multiPlayer"); }
/// <summary> /// Checks the valid. /// </summary> /// <param name="args">The arguments.</param> /// <param name="client">The client.</param> /// <returns></returns> public bool CheckValid(string[] args, TcpClient client) { if (args.Length > 1) { Controller.NestedErrors nested = new Controller.NestedErrors("Bad arguement", client); return(false); } try { string name = args[0]; return(true); } catch (Exception) { Controller.NestedErrors nested = new Controller.NestedErrors("Bad arguement", client); return(false); } }