public void WriteJson(string winner, string loser, int numOfRounds, int p1HitCount, int p2HitCount, List <Ship> p1Ships, List <Ship> p2Ships) { Gameresult gameResult = new Gameresult(); gameResult.winner = winner; gameResult.loser = loser; gameResult.numOfRounds = numOfRounds; gameResult.p1HitCount = p1HitCount; gameResult.p2HitCount = p2HitCount; gameResult.p1ShipsSats = "Ships alive: "; foreach (var ship in p1Ships.Where(ship => ship.isDead == false)) { gameResult.p1ShipsSats += ship.name.ToString() + " "; } gameResult.p1ShipsSats += " ships sunken: "; foreach (var ship in p1Ships.Where(ship => ship.isDead == true)) { gameResult.p1ShipsSats += ship.name.ToString() + " "; } gameResult.p2ShipsSats = "Ships alive: "; foreach (var ship in p2Ships.Where(ship => ship.isDead == false)) { gameResult.p2ShipsSats += ship.name.ToString() + " "; } gameResult.p2ShipsSats += " ships sunken: "; foreach (var ship in p2Ships.Where(ship => ship.isDead == true)) { gameResult.p2ShipsSats += ship.name.ToString() + " "; } string path = @"..\..\..\json\stats.json"; var jsonData = File.ReadAllText(path); var gamesResultsList = JsonConvert.DeserializeObject <List <Gameresult> >(jsonData) ?? new List <Gameresult>(); string JSONresult = JsonConvert.SerializeObject(gameResult); gamesResultsList.Add(gameResult); jsonData = JsonConvert.SerializeObject(gamesResultsList); File.WriteAllText(path, jsonData); }
public Gameresult Result([FromBody] Playerchoice playerchoice) { gameresult = this.g1.GetGameresult(playerchoice); string procedure_AddGame = "[dbo].[ADD_GAME]"; SqlConnection connection = new SqlConnection(this.connnectionString); connection.Open(); using (SqlCommand command = new SqlCommand(procedure_AddGame, connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@UserName", playerchoice.name)); command.Parameters.Add(new SqlParameter("@GameStarted", DateTime.Now.ToString("MM/dd/yyyy HH:mm"))); command.Parameters.Add(new SqlParameter("@GameResult", gameresult.result)); command.Parameters.Add(new SqlParameter("@NumOfTurns", playerchoice.numberofrounds)); command.ExecuteNonQuery(); } return(gameresult); }