private void OnSave_Button_Clicked(object sender, RoutedEventArgs e) { foreach (var dealCfg in _dealCfgs) { int total = 13; if (dealCfg.IsBanker) { total = 14; } if (dealCfg.TilesHand.Count != 0 && dealCfg.TilesHand.Count != total) { DrawForDealCfg(dealCfg); } } foreach (var dealCfg in _dealCfgs) { int total = 13; if (dealCfg.IsBanker) { total = 14; } if (dealCfg.TilesHand.Count != 0 && dealCfg.TilesHand.Count != total) { MessageBox.Show($"The {dealCfg.Index} set config hand tiles must equal to {total}"); return; } } for (int i = 3; i > 0; i--) { if (_dealCfgs[i].TilesHand.Count > 0 && _dealCfgs[i - 1].TilesHand.Count == 0) { MessageBox.Show("config must continuous"); return; } } if (_dealCfgs[0].TilesHand.Count < 1 || _dealCfgs[1].TilesHand.Count < 1) { MessageBox.Show("at least have 2 player config"); return; } //名称 userID1 手牌 花牌 动作提示 userID2 手牌 花牌 动作提示 userID3 手牌 花牌 动作提示 userID4 手牌 花牌 动作提示 抽牌序列 庄家 风牌 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); var cfgName = "xyz"; if (string.IsNullOrWhiteSpace(tbCfgName.Text) == false) { cfgName = tbCfgName.Text; } dlg.FileName = "user-new-" + cfgName; // Default file name dlg.DefaultExt = ".csv"; // Default file extension dlg.Filter = "CSV documents (.csv)|*.csv"; // Filter files by extension // Show save file dialog box bool?dlgResult = dlg.ShowDialog(); // Process save file dialog box results if (dlgResult == true) { try { // Save document string filename = dlg.FileName; using (var textWriter = new StreamWriter(new FileStream(filename, FileMode.Create, FileAccess.ReadWrite), Encoding.Default)) { var expectedHeaders = MyCsvHeaders.GetHeaders(_gameType); // 第一行 var csv = new CsvWriter(textWriter); foreach (var header in expectedHeaders) { csv.WriteField(header); } csv.NextRecord(); // 第二行 csv.WriteField(cfgName); // 名字 csv.WriteField(MyCsvHeaders.GetRoomTypeName(_gameType)); foreach (var dealCfg in _dealCfgs) { dealCfg.WriteCsv(csv); } csv.WriteField(_drawCfg.ToTilesString()); // 抽牌序列 csv.WriteField(""); // 风牌/加价局 csv.WriteField(0); // 强制一致 csv.WriteField(""); // 房间配置ID if (_gameType == RoomType.DafengMJ) { csv.WriteField("0"); // 是否连庄 csv.WriteField("0"); // 家家庄 } else if (_gameType == RoomType.DongTaiMJ) { csv.WriteField("0"); // 是否连庄 } csv.NextRecord(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void OnLoad_Button_Clicked(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.FileName = "origin"; // Default file name dlg.DefaultExt = ".csv"; // Default file extension dlg.Filter = "CSV documents (.csv)|*.csv"; // Filter files by extension // Show open file dialog box bool?result = dlg.ShowDialog(); // Process open file dialog box results if (result == true) { try { var x = HttpHandlers.WriteSafeReadAllLines(dlg.FileName); if (!string.IsNullOrWhiteSpace(x)) { using (var stringReader = new StringReader(x)) { // csv reader var csvReader = new CsvReader(stringReader); var expectedHeaders = MyCsvHeaders.GetHeaders(_gameType); // 读取头部 if (!csvReader.ReadHeader()) { MessageBox.Show("Invalid input csv file, no record found"); return; } if (expectedHeaders.Length != csvReader.FieldHeaders.Length) { MessageBox.Show("Invalid input csv file, header count not match"); return; } for (var i = 0; i < expectedHeaders.Length; i++) { if (string.Compare(expectedHeaders[i], csvReader.FieldHeaders[i], StringComparison.Ordinal) != 0) { MessageBox.Show("Invalid input csv file, header not match"); return; } } while (csvReader.Read()) { for (var i = 0; i < 4; i++) { var dealCfg = _dealCfgs[i]; dealCfg.ReadCsv(csvReader); dealCfg.Tiles2Ui(); } _drawCfg.ReadCsv(csvReader); _drawCfg.Tiles2Ui(); WallTiles2Ui(); // 仅读取第一个记录 break; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private async Task parseAndSaveRecord(byte [] result, string fileName, bool loadConfig) { var recorder = result.ToProto <SRMsgHandRecorder>(); List <PlayerData> players = FromRecorder(recorder); string drawSequnece = ExtractDrawSequence(recorder); var bankerPlayer = players.Find((p) => p.ChairId == recorder.bankerChairID); var bankderUserId = bankerPlayer.UserId; players = sortPlayers(players, bankderUserId); var extra = recorder.extra; //名称 userID1 手牌 花牌 动作提示 userID2 手牌 花牌 动作提示 userID3 手牌 花牌 动作提示 userID4 手牌 花牌 动作提示 抽牌序列 庄家 风牌 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = fileName; // Default file name dlg.DefaultExt = ".csv"; // Default file extension dlg.Filter = "CSV documents (.csv)|*.csv"; // Filter files by extension // Show save file dialog box bool?dlgResult = dlg.ShowDialog(); // Process save file dialog box results if (dlgResult == true) { // Save document string filename = dlg.FileName; using (var textWriter = new StreamWriter(new FileStream(filename, FileMode.Create, FileAccess.ReadWrite), Encoding.Default)) { var headers = MyCsvHeaders.GetHeaders(); // 第一行 var csv = new CsvWriter(textWriter); foreach (var header in headers) { csv.WriteField(header); } csv.NextRecord(); // 第二行 csv.WriteField("bug"); // 名字 csv.WriteField(MyCsvHeaders.GetRoomTypeName()); // 类型 foreach (var playerData in players) { playerData.WriteCsv(csv); } int pad = 4 - players.Count(); for (int i = 0; i < pad; ++i) { for (int j = 0; j < 3; ++j) { csv.WriteField(""); } } csv.WriteField(drawSequnece); csv.WriteField(1); // 强制一致 csv.WriteField(recorder.roomConfigID); csv.WriteField(recorder.isContinuousBanker ? "1" : "0"); csv.NextRecord(); } if (loadConfig && !string.IsNullOrWhiteSpace(recorder.roomConfigID)) { var dir = Path.GetDirectoryName(filename); if (dir == null) { return; } var jsonFileName = Path.Combine(dir, Path.GetFileNameWithoutExtension(filename) + ".json"); await LoadAndSaveRoomConfig(recorder.roomConfigID, jsonFileName); } } }