Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PokedexS2/Pokedex/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public Command(Pokedex pokedex, string[] commandArguments)
}

public abstract void Execute();

}
22 changes: 21 additions & 1 deletion PokedexS2/Pokedex/Commands/CommandInterpreter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.RegularExpressions;
using System.Reflection;

namespace Pokedex;

Expand Down Expand Up @@ -29,11 +29,19 @@ public Command Interpret(string[] arguments)
//commandName = localizationService.GetText(commandName);
string[] commandArguments = arguments.Skip(1).ToArray();

//return MakeCommand(commandName, pokedex, commandArguments);

switch (commandName)
{
case "test":
return MakeCommand("add", pokedex, commandArguments);

case "add":
return new AddCommand(pokedex, commandArguments);

// case "add_plant":
// return new AddPlantCommand(pokedex, commandArguments);

case "search":
return new SearchCommand(pokedex, commandArguments);

Expand Down Expand Up @@ -64,4 +72,16 @@ public Command Interpret(string[] arguments)
*/
}
}

private Command MakeCommand(string name, Pokedex pokedex, string[] commandArguments)
{
string commandName = "Pokedex." + name[0].ToString().ToUpper() + name.Substring(1) + "Command";

Assembly asm = Assembly.GetExecutingAssembly();
System.Type type = asm.GetType(commandName);

object test = Activator.CreateInstance(type, new object[] { pokedex, commandArguments } );

return test as Command;
}
}
4 changes: 2 additions & 2 deletions PokedexS2/Pokedex/Commands/DiscoverCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public override void Execute()

void Discover(string name)
{
Pokemon pokemon = Pokedex.Get(name);
ICollectible pokemon = Pokedex.Get(name);
Console.WriteLine($"Pokemon {pokemon.Name} set to discovered");
pokemon.Discover();
}

void Discover(int id)
{
Pokemon pokemon = Pokedex.Get(id);
ICollectible pokemon = Pokedex.Get(id);
Console.WriteLine($"Pokemon {pokemon.Name} set to discovered");
pokemon.Discover();
}
Expand Down
2 changes: 1 addition & 1 deletion PokedexS2/Pokedex/Commands/ListCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public ListCommand(Pokedex pokedex, string[] commandArguments)

public override void Execute()
{
// TODO
Console.WriteLine("coucou");
}
}
16 changes: 13 additions & 3 deletions PokedexS2/Pokedex/Commands/LoadCommand.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading.Tasks;

namespace Pokedex;

public class LoadCommand : Command
Expand All @@ -12,6 +14,12 @@ public LoadCommand(Pokedex pokedex, string[] commandArguments)
}

public override void Execute()
{
throw new NotImplementedException("Execute() is not implemented. Use ExecuteAsync() instead.");
}


public async Task ExecuteAsync()
{
if (!isValid)
{
Expand All @@ -29,12 +37,12 @@ public override void Execute()
return;
}

StreamReader reader = new StreamReader(path);
using StreamReader reader = new StreamReader(path);

int count = 0;
while(!reader.EndOfStream)
{
string line = reader.ReadLine();
string line = await reader.ReadLineAsync(); // Ajout Async
if (string.IsNullOrWhiteSpace(line))
{
continue;
Expand Down Expand Up @@ -65,6 +73,8 @@ public override void Execute()

Console.WriteLine($"{count} pokemons loaded from file.");

reader.Close();
//reader.Close();
//reader.Dispose();
return;
}
}
10 changes: 9 additions & 1 deletion PokedexS2/Pokedex/Commands/ReadJsonCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public ReadJsonCommand(
}

public override void Execute()
{
throw new NotImplementedException();
}
public async Task ExecuteAsync()
{
JsonSerializerOptions options = new JsonSerializerOptions
{
Expand All @@ -34,7 +38,7 @@ public override void Execute()
try
{
string path = $"{saveDirecty}/{arguments[0]}.json";
string content = File.ReadAllText(path);
string content = await File.ReadAllTextAsync(path);

PokedexDto pokedexDto = JsonSerializer.Deserialize<PokedexDto>(content, options);
Pokedex.LoadDto(pokedexDto);
Expand All @@ -47,5 +51,9 @@ public override void Execute()
{
Console.WriteLine("Fichier non trouvé");
}
finally
{
Console.WriteLine("Finally");
}
}
}
37 changes: 11 additions & 26 deletions PokedexS2/Pokedex/Commands/SaveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Pokedex;
// save <fichier>
public class SaveCommand : Command
{
string saveDirecty = "Data";
// string saveDirecty = "Data";

public SaveCommand(Pokedex pokedex, string[] commandArguments)
: base(pokedex, commandArguments)
Expand All @@ -16,32 +16,17 @@ public SaveCommand(Pokedex pokedex, string[] commandArguments)

public override void Execute()
{
// TODO: if valid
string path = AddExtension(arguments[0]);
path = $"{saveDirecty}/{path}";
Directory.CreateDirectory(saveDirecty);

StreamWriter streamWriter = new StreamWriter(path);

SavePokedex(streamWriter);
streamWriter.Flush();
streamWriter.Close();

Console.WriteLine($"Pokedex saved to file {path}");
}

void SavePokedex(StreamWriter file)
{
Pokedex.Save(file);
}

string AddExtension(string path)
{
if (!path.Contains("."))
/*
if (isValid)
{
return $"{path}.csv";
IWriter writer = new TextWriter();
string path = $"{saveDirecty}/{arguments[0]}.txt";
writer.SaveFile(Pokedex, path);
}

return path;
else
{
Console.WriteLine("Argument manquant.");
}
*/
}
}
4 changes: 2 additions & 2 deletions PokedexS2/Pokedex/Commands/SearchCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public override void Execute()
return;
}

Pokemon pokemon = Pokedex.Get(arguments[0]);
ICollectible pokemon = Pokedex.Get(arguments[0]);
if (pokemon != null)
{
Console.WriteLine($"Name: {pokemon.Name} ({pokemon.Id})");
Console.WriteLine($"Type: {pokemon.Type}");
//Console.WriteLine($"Type: {pokemon.Type}");
}
else
{
Expand Down
42 changes: 23 additions & 19 deletions PokedexS2/Pokedex/Commands/WriteJsonCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading.Tasks;

namespace Pokedex;

Expand All @@ -12,25 +10,31 @@ public class WriteJsonCommand : Command
public WriteJsonCommand(Pokedex pokedex, string[] commandArguments)
: base(pokedex, commandArguments)
{
if (commandArguments.Length != 1)
{
isValid = false;
}
}

public override void Execute()
{
// Cette classe permet de configurer comment est écrit le fichier JSON
JsonSerializerOptions options = new JsonSerializerOptions
throw new NotImplementedException();
}

public async Task ExecuteAsync()
{
if (isValid)
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
// Le comporte par défault du sérialiseur est débile et échappe tous les caractères non ASCII
// par exemple Salamèche -> Salam\u00E8che. Pour éviter ça on utilise l'option qui suit
Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement),
};

PokedexDto pokedexDto = Pokedex.ToDto();
string jsonString = JsonSerializer.Serialize(pokedexDto, options);
string path = $"{saveDirecty}/{arguments[0]}.json";
File.WriteAllText(path, jsonString);

Console.WriteLine("Fichier bien sauvegardé.");
IWriter writer = new JsonWriter();
string path = $"{saveDirecty}/{arguments[0]}.json";
await writer.SaveFileAsync(Pokedex, path);
}
else
{
Console.WriteLine("Argument manquant.");
}
}

// TODO : faire une commande unique qui refactore WriteJsonCmd et SaveCmd
// qui contient private IWriter GetWriter(string format)
// (bonus: ajout xml)
}
9 changes: 9 additions & 0 deletions PokedexS2/Pokedex/ICollectible.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Pokedex;

public interface ICollectible
{
int Id { get; }
string Name { get; }
bool Discovered { get; }
void Discover();
}
21 changes: 21 additions & 0 deletions PokedexS2/Pokedex/Plant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Pokedex;

public class Plant : ICollectible
{
public int Id { get; private set; }

public string Name { get; private set; }

public bool Discovered { get; private set; }

public Plant(int id, string name)
{
Id = id;
Name = name;
}

public void Discover()
{
Discovered = true;
}
}
Loading