/// <summary> /// Read a CSV file and import ship purchase lines /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonImportShipPurchaseRestrictions_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "CSV (*.csv)|*.csv|All Files|*.*"; dialog.Title = "Open CSV File"; if (dialog.ShowDialog() == DialogResult.OK) { try { // We expect the first row to contain the ship nicknames and the first // column to contain the id nicknames. The first field on the first row // must be empty. using (StreamReader sr = File.OpenText(dialog.FileName)) { CsvStream cs = new CsvStream(sr); Dictionary<int, List<GameDataSet.HashListRow>> shipItemsByCol = new Dictionary<int, List<GameDataSet.HashListRow>>(); List<string> shipTypeRow = cs.GetNextRow(); if (shipTypeRow == null) throw new Exception("No ship type row found"); // Validate the nicknames in each field. for (int col = 1; col < shipTypeRow.Count; col++) { List<GameDataSet.HashListRow> shipItems = new List<GameDataSet.HashListRow>(); foreach (string nickname in shipTypeRow[col].Split(',')) { if (nickname.Length > 0) { GameDataSet.HashListRow item = gameData.GetItemByNickName(nickname.Trim()); if (item == null) throw new Exception(String.Format("Item '{0}' not found", nickname)); if (item.ItemType != FLGameData.GAMEDATA_SHIPS) throw new Exception(String.Format("Item '{0}' is not ship", nickname)); shipItems.Add(item); } } shipItemsByCol.Add(col, shipItems); } List<string> row = cs.GetNextRow(); while (row != null) { // Extract and verify item nicknames for this row. List<GameDataSet.HashListRow> controlItems = new List<GameDataSet.HashListRow>(); foreach (string nickname in row[0].Split(',')) { if (nickname.Length > 0) { GameDataSet.HashListRow item = gameData.GetItemByNickName(nickname.Trim()); if (item == null) throw new Exception(String.Format("Item '{0}' not found", nickname)); controlItems.Add(item); } } // For all cell with a 1 in them add lines to data set. for (int col = 1; col < row.Count; col++) { if (row[col] == "1") { foreach (GameDataSet.HashListRow shipItem in shipItemsByCol[col]) { foreach (GameDataSet.HashListRow controlItem in controlItems) { uIDataSet.ShipPurchaseRestrictionItems.AddShipPurchaseRestrictionItemsRow(shipItem.IDSName, shipItem.ItemNickName, controlItem.IDSName, controlItem.ItemNickName); } } } } row = cs.GetNextRow(); } } } catch (Exception ex) { MessageBox.Show(this, "Error '" + ex.Message + "'", "Error"); } } }
/// <summary> /// Read a CSV file and import ship purchase lines /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonImportShipPurchaseRestrictions_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog() { Filter = "CSV (*.csv)|*.csv|All Files|*.*", Title = "Open CSV File" }; if (dialog.ShowDialog() == DialogResult.OK) { try { // We expect the first row to contain the ship nicknames and the first // column to contain the id nicknames. The first field on the first row // must be empty. using (StreamReader sr = File.OpenText(dialog.FileName)) { CsvStream cs = new CsvStream(sr); Dictionary <int, List <GameDataSet.HashListRow> > shipItemsByCol = new Dictionary <int, List <GameDataSet.HashListRow> >(); List <string> shipTypeRow = cs.GetNextRow(); if (shipTypeRow == null) { throw new Exception("No ship type row found"); } // Validate the nicknames in each field. for (int col = 1; col < shipTypeRow.Count; col++) { List <GameDataSet.HashListRow> shipItems = new List <GameDataSet.HashListRow>(); foreach (string nickname in shipTypeRow[col].Split(',')) { if (nickname.Length > 0) { GameDataSet.HashListRow item = gameData.GetItemByNickName(nickname.Trim()); if (item == null) { throw new Exception(String.Format("Item '{0}' not found", nickname)); } if (item.ItemType != FLGameData.GAMEDATA_SHIPS) { throw new Exception(String.Format("Item '{0}' is not ship", nickname)); } shipItems.Add(item); } } shipItemsByCol.Add(col, shipItems); } List <string> row = cs.GetNextRow(); while (row != null) { // Extract and verify item nicknames for this row. List <GameDataSet.HashListRow> controlItems = new List <GameDataSet.HashListRow>(); foreach (string nickname in row[0].Split(',')) { if (nickname.Length > 0) { GameDataSet.HashListRow item = gameData.GetItemByNickName(nickname.Trim()); if (item == null) { throw new Exception(String.Format("Item '{0}' not found", nickname)); } controlItems.Add(item); } } // For all cell with a 1 in them add lines to data set. for (int col = 1; col < row.Count; col++) { if (row[col] == "1") { foreach (GameDataSet.HashListRow shipItem in shipItemsByCol[col]) { foreach (GameDataSet.HashListRow controlItem in controlItems) { uIDataSet.ShipPurchaseRestrictionItems.AddShipPurchaseRestrictionItemsRow(shipItem.IDSName, shipItem.ItemNickName, controlItem.IDSName, controlItem.ItemNickName); } } } } row = cs.GetNextRow(); } } } catch (Exception ex) { MessageBox.Show(this, "Error '" + ex.Message + "'", "Error"); } } }