public async Task <IActionResult> Monitor(Guid id, string gameName, string playerName, bool requestAutoJoin, bool delete) { Game game = Games.GetGame(id); if (game == default) { var err = new CatanResult(CatanError.NoGameWithThatName) { Description = $"Game '{id}' does not exist", Request = this.Request.Path }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(NotFound(errMessage)); } bool success = game.NameToPlayerDictionary.TryGetValue(playerName, out Player player); if (!success) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Player '{playerName}' is not a member of Game with id = '{id}'.", }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(NotFound(errMessage)); } var messages = await player.WaitForLogEntries(); if (messages == null || messages.Count == 0) { var err = new CatanResult(CatanError.Unexpected) { Request = this.Request.Path, Description = $"Why did {playerName} release with no log entries?" }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(errMessage)); } return(Ok(messages)); }
public IActionResult DeleteGame(Guid id, string by) { try { bool success = Games.DeleteGame(id, out Game game); if (!success) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $" Game '{id}' does not exist", }; return(NotFound(err)); } //CatanServiceMessage msg = new CatanServiceMessage() { GameInfo = game.GameInfo, PlayerName = by }; //CatanMessage message = new CatanMessage() //{ // ActionType = ActionType.Normal, // Data = (object)msg, // DataTypeName = typeof(GameInfo).FullName, // From = by, // Sequence = game.GetNextSequenceNumber(), // MessageType = MessageType.DeleteGame //}; //game.PostLog(message); //game.ReleaseLogs(); return(GetGames()); } catch (Exception e) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"{this.Request.Path} threw an exception. {e}", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } }
public IActionResult GetHelp() { var res = new CatanResult(CatanError.NoError) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"ServiceVersion=2.5", }; return(Ok(res)); }
public async Task <List <string> > CreateGame(bool startGame = true) { CatanResult result = await Proxy.DeleteGame(GameName); if (result is null) { Assert.Equal("", Proxy.LastErrorString); } if (result.Error != CatanError.NoError) { Assert.Equal("", Proxy.LastErrorString); } List <string> games = await Proxy.CreateGame(GameName, GameInfo); var response = await Proxy.JoinGame(GameName, "Doug"); if (response is null) { Assert.Equal("", Proxy.LastErrorString); } response = await Proxy.JoinGame(GameName, "Max"); if (response is null) { Assert.Equal("", Proxy.LastErrorString); } response = await Proxy.JoinGame(GameName, "Wallace"); if (response is null) { Assert.Equal("", Proxy.LastErrorString); } response = await Proxy.JoinGame(GameName, "Joe"); if (response is null) { Assert.Equal("", Proxy.LastErrorString); } if (startGame) { await Proxy.StartGame(GameName); } Players = await Proxy.GetUsers(GameName); Assert.Equal(4, Players.Count); return(Players); }
public IActionResult CreateGame([FromBody] GameInfo gameInfo) { try { Game game = Games.GetGame(gameInfo.Id); if (game != default) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $" Game '{gameInfo.Id}' with description '{gameInfo.Name}' created by '{gameInfo.Creator}' already exists. You can join it or delete it", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } Games.AddGame(gameInfo.Id, new Game() { GameInfo = gameInfo }); return(GetGames()); } catch (Exception e) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"{this.Request.Path} threw an exception. {e}", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } }
public IActionResult BroadcastMessage([FromBody] CatanMessage message, Guid gameId) { try { Game game = Games.GetGame(gameId); if (game == null) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Game '{gameId}' does not exists", }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(NotFound(errMessage)); } message.MessageType = MessageType.BroadcastMessage; game.PostLog(message); game.ReleaseLogs(); return(Ok(message)); } catch (Exception e) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"{this.Request.Path} threw an exception. {e}", }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(errMessage)); } }
public IActionResult GetPlayersInGame(Guid gameId) { Game game = Games.GetGame(gameId); if (game == default) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Game '{gameId}' Not Found", }; return(NotFound(err)); } return(Ok(game.NameToPlayerDictionary.Keys)); }
public IActionResult GetGameLogRecords(Guid id, string playerName) { Game game = Games.GetGame(id); if (game == default) { var err = new CatanResult(CatanError.NoGameWithThatName) { Description = $"Game '{id}' does not exist", Request = this.Request.Path }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(NotFound(errMessage)); } bool success = game.NameToPlayerDictionary.TryGetValue(playerName, out Player player); if (!success) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Player '{playerName}' is not a member of Game with id = '{id}'.", }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(NotFound(errMessage)); } return(Ok(game.GameLog)); }
public IActionResult SendPrivateMessage([FromBody] CatanMessage message, Guid gameId, string to) { try { Game game = Games.GetGame(gameId); if (game == null) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Game '{gameId}' does not exists", }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(NotFound(errMessage)); } // // should already be in here since you shoudl have called Monitor() bool success = game.NameToPlayerDictionary.TryGetValue(to, out Player player); if (!success) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Player '{to}' not found for game '{gameId}'. Did you call Monitor() before Join()?", }; return(BadRequest(err)); } message.MessageType = MessageType.PrivateMessage; player.PlayerLog.Enqueue(message); player.ReleaseLog(); return(Ok(message)); } catch (Exception e) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"{this.Request.Path} threw an exception. {e}", }; CatanMessage errMessage = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(errMessage)); } }
public IActionResult Leave([FromBody] GameInfo gameInfo, string playerName) { try { Game game = Games.GetGame(gameInfo.Id); if (game == null) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $" Game '{gameInfo.Name}' with id={gameInfo.Id} does not exist. Call Monitor() before calling Join()", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } if (game.Started) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Player '{playerName}' can't be removed from '{gameInfo.Name}' because it has already been started.", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } if (game.GameInfo.Creator == playerName) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"The Creator can't leave their own game.", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } // // should already be in here since you shoudl have called Monitor() bool success = game.NameToPlayerDictionary.TryRemove(playerName, out Player player); if (!success) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Player '{playerName}' can't be removed from '{gameInfo.Name}'.", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } //CatanMessage leaving = new CatanMessage() //{ // MessageType = MessageType.LeaveGame, // From = playerName, // Sequence = game.GetNextSequenceNumber(), // To = "*" //}; //game.PostLog(leaving); //game.ReleaseLogs(); return(Ok(game.NameToPlayerDictionary.Keys)); // the client is going to want to know the creator } catch (Exception e) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"{this.Request.Path} threw an exception. {e}", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } }
public IActionResult JoinGame([FromBody] GameInfo gameInfo, string playerName) { try { Game game = Games.GetGame(gameInfo.Id); if (game == null) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $" Game '{gameInfo.Name}' with id={gameInfo.Id} does not exist. Call Monitor() before calling Join()", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } // // should already be in here since you shoudl have called Monitor() bool success = game.NameToPlayerDictionary.TryAdd(playerName, new Player(game.GameLog)); if (!success) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"Player '{playerName}' can't be added to '{gameInfo.Name}'.", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } return(Ok(game.GameInfo)); // the client is going to want to know the creator } catch (Exception e) { var err = new CatanResult(CatanError.BadParameter) { CantanRequest = new CatanRequest() { Url = this.Request.Path }, Description = $"{this.Request.Path} threw an exception. {e}", }; CatanMessage message = new CatanMessage() { Data = err, Sequence = 0, DataTypeName = typeof(CatanResult).FullName }; return(BadRequest(message)); } }
private async void OnTest1(object sdr, RoutedEventArgs rea) { var picker = new PlayerPickerDlg(SavedAppState.Players); var result = await picker.ShowAsync(); if (result != ContentDialogResult.Primary || picker.Player == null) { await StaticHelpers.ShowErrorText("You have to pick a player! Stop messing around Dodgy!"); return; } var playingPlayer = picker.Player; var proxy = new CatanProxy() { // HostName = "http://localhost:5000" HostName = "http://jdlgameservice.azurewebsites.net" }; var existingGames = await proxy.GetGames(); foreach (var game in existingGames) { CatanResult r = await proxy.DeleteGame(game); if (r == null) { this.TraceMessage(proxy.LastErrorString); } } var gameInfo = new GameInfo(); string[] gameNames = new string[] { "Game - 1", "Game - 2" }; List <string> games = null; foreach (var game in gameNames) { await proxy.DeleteGame(game); games = await proxy.CreateGame(game, gameInfo); for (int i = 0; i < 4; i++) { var player = SavedAppState.Players[i]; if (player.PlayerName == "Dodgy") { continue; } var resources = await proxy.JoinGame(game, player.PlayerName); Debug.Assert(resources != null); } } var gamesFromService = await proxy.GetGames(); ServiceGameDlg dlg = new ServiceGameDlg(playingPlayer, SavedAppState.Players, gamesFromService) { HostName = proxy.HostName }; await dlg.ShowAsync(); this.TraceMessage($"Game: {dlg.SelectedGame}"); }