public override void DoWork(IEnumerable<string> args) { if (Utils.CheckArgs(ArgsNeed, args.Count())) { if (Utils.IsLoggedIn() && Utils.IsNotInGame()) { try { int gameId = Convert.ToInt32(args.ToArray()[0]); var request = new JoinGameRequest { GameID = gameId, NewPlayer = new User {Name = CurrentUser.Name} }; var response = ServerProvider.MakeRequest(request); if (response.Status == Statuses.Ok) { Console.WriteLine("You joined game. ID: " + gameId); CurrentUser.CurrentGame = gameId; } else { if (response.Status == Statuses.GameIsRunning) { Console.WriteLine("Game is already running."); } if (response.Status == Statuses.GameNotFound) { Console.WriteLine("Game does not not exist."); } if (response.Status == Statuses.GameCanceld) { Console.WriteLine("Game was canceled by initiator."); } } } catch (FormatException) { Console.WriteLine("Game id should be integer number."); } } } }
public ActionResult StartFree() { User newPlayer = null; if (Request.IsAuthenticated) { newPlayer = new User { Name = User.Identity.Name }; } var requestCreateGame = new CreateGameRequest { NewPlayer = newPlayer }; var responseCreateGame = ServerProvider.MakeRequest<CreateGameResponse>(requestCreateGame); int? gameId = null; if (responseCreateGame.Status == Statuses.Ok) { gameId = responseCreateGame.ID; FormsAuthentication.SetAuthCookie(responseCreateGame.FirstPlayer.Name, false); var requestJoinGame = new JoinGameRequest { GameID = gameId.Value, NewPlayer = responseCreateGame.FirstPlayer }; var responseJoinGame = ServerProvider.MakeRequest(requestJoinGame); if (responseJoinGame.Status != Statuses.Ok) { gameId = null; } } return Json(gameId, JsonRequestBehavior.AllowGet); }