private async Task ReadPokemonExcelData(SLDocument document) { document.SelectWorksheet("My Pokemon"); int row = 2, col = 2; this.MyPokemon.Clear(); while (!string.IsNullOrWhiteSpace(document.GetCellValueAsString(row, col))) { col = 2; string name = document.GetCellValueAsString(row, 2); string speciesName = document.GetCellValueAsString(row, 3); PokedexEntry species = this.Pokedex.SingleOrDefault(x => x.Species.Equals(speciesName)); if (species == null) { Debug.WriteLine($"Could not find species {speciesName}"); row++; continue; } Pokemon pokemon = new Pokemon(species, name); pokemon.GameCP = document.GetCellValueAsInt32(row, 8); pokemon.GameHP = document.GetCellValueAsInt32(row, 9); pokemon.DustToPower = document.GetCellValueAsInt32(row, 10); pokemon.HasBeenPowered = document.GetCellValueAsString(row, 11).Equals("Yes"); pokemon.StaminaIVExpression = document.GetCellValueAsString(row, 15); pokemon.AttackIVExpression = document.GetCellValueAsString(row, 16); pokemon.DefenseIVExpression = document.GetCellValueAsString(row, 17); pokemon.LevelExpression = document.GetCellValueAsString(row, 18); string fastMoveName = document.GetCellValueAsString(row, 13); PokedexFastMoveWrapper fastMove = await Task.Run(() => species.FastMoves.SingleOrDefault(x => x.FastMove.Name.Equals(fastMoveName))); if (fastMove != null) { pokemon.FastMove = fastMove; } string chargeMoveName = document.GetCellValueAsString(row, 14); PokedexChargeMoveWrapper chargeMove = await Task.Run(() => species.ChargeMoves.SingleOrDefault(x => x.ChargeMove.Name.Equals(chargeMoveName))); if (chargeMove != null) { pokemon.ChargeMove = chargeMove; } this.MyPokemon.Add(pokemon); row++; } }
public async Task LoadBaseDataFromFile(Stream fileStream) { this.FastMoveList.Clear(); this.ChargeMoveList.Clear(); this.Pokedex.Clear(); //Retrieves the data using the serialize attributes BaseDataWrapper data = new BaseDataWrapper(); try { XmlSerializer dataSerializer = new XmlSerializer(typeof(BaseDataWrapper)); data = await Task.Run(() => dataSerializer.Deserialize(fileStream)) as BaseDataWrapper; } catch (IOException) //File does not exist; set everything to defaults { Debug.WriteLine("Error reading base data"); } catch (InvalidOperationException ex) { Debug.WriteLine("Error reading xml:" + ex.Message); } catch (Exception ex) { Debug.WriteLine($"Error reading xml: " + ex.Message); } //Process the data List <FastMove> tempFastMoves = new List <FastMove>(); List <ChargeMove> tempChargeMoves = new List <ChargeMove>(); foreach (Move move in data.Moves) { if (move.MoveType == MoveType.Fast) { tempFastMoves.Add(move as FastMove); } else if (move.MoveType == MoveType.Charge) { tempChargeMoves.Add(move as ChargeMove); } else { throw new InvalidOperationException("Unknown move type: " + move.MoveType); } } this.FastMoveList.InsertRange(tempFastMoves.OrderBy(x => x.Name)); this.ChargeMoveList.InsertRange(tempChargeMoves.OrderBy(x => x.Name)); List <PokedexEntry> tempPokedex = new List <PokedexEntry>(); foreach (PokedexEntry species in data.PokedexEntries) { //Debug.WriteLine($"{species.Species} has {species.FastMoves.Found fast move {move.Name} for {species.Species}"); for (int i = 0; i < species.FastMoves.Count; i++) { //Debug.WriteLine($"Found fast move {move.Name} for {species.Species}"); PokedexFastMoveWrapper move = species.FastMoves[i]; string moveName = move.FastMove.Name; try { move.FastMove = (this.FastMoveList.Single(x => x.Name.Equals(moveName))); species.FastMoves[i] = move; } catch (Exception ex) { throw new ArgumentException("Cannot find matching fast move " + move.FastMove.Name + " in fast move list.", ex); } } for (int i = 0; i < species.ChargeMoves.Count; i++) { //Debug.WriteLine($"Found fast move {move.Name} for {species.Species}"); PokedexChargeMoveWrapper move = species.ChargeMoves[i]; string moveName = move.ChargeMove.Name; try { move.ChargeMove = (this.ChargeMoveList.Single(x => x.Name.Equals(moveName))); species.ChargeMoves[i] = move; } catch (Exception ex) { throw new ArgumentException("Cannot find matching charge move " + move.ChargeMove.Name + " in charge move list.", ex); } } tempPokedex.Add(species); this.Pokedex.Add(species); } }