// 리스트버튼 클릭시 실행 private void ResetButton_Click(object sender, EventArgs e) { if (orderResultList.Count == 0) { ResetButtonMessageBox(); return; } // 리스트 클리어 foreach (ResultLootItem t in countResultList) { t.Count = 0; } orderResultList.Clear(); for (int i = orderResult_dataGridView.Rows.Count - 1; i >= 0; i--) { orderResult_dataGridView.Rows.RemoveAt(i); } for (int i = 0; i < countResult_dataGridView.Rows.Count; i++) { countResult_dataGridView.Rows[i].Cells[2].Value = countResultList[i].Count; if (loseItem == null) { continue; } if (countResult_dataGridView.Rows[i].Cells[1].Value.ToString() == loseItem.Name) { countResult_dataGridView.Rows.RemoveAt(i); } } loseItem = null; }
// addBtnForm.cs에서 넘겨받은 LootItem을 내 리스트에 추가시킨다. public void AddItem(LootItem myitem, ResultLootItem resultItem) { itemList.Add(myitem); countResultList.Add(resultItem); itemList_dataGridView.Rows.Add(myitem.ItemImage, myitem.Name, myitem.Probability.ToString("N3") + "%"); countResult_dataGridView.Rows.Add(resultItem.ItemImage, resultItem.Name, resultItem.Count); }
// 불러오기 버튼 private void LoadButton_Click(object sender, EventArgs e) { //이미 만들어진 리스트가 있을 떄 덮어쓸것인지 if (itemList.Count != 0 && !LoadButtonCheck(0)) { return; } if (!File.Exists("data.dat")) { LoadButtonCheck(1); return; } string[] saveFile = File.ReadAllLines("data.dat"); if (saveFile != null) { for (int i = itemList.Count - 1; i >= 0; i--) { Debug.WriteLine(i); itemList_dataGridView.Rows.Remove(itemList_dataGridView.Rows[i]); } itemList.Clear(); ClearcountResultGridView(); for (int i = 0; i < saveFile.Length; i += 3) { //이미지 파일이 없는 경우 LootItem temp; ResultLootItem resultTemp; if (saveFile[i + 1].Equals("")) { temp = new LootItem(saveFile[i + 0], decimal.Parse(saveFile[i + 2])); resultTemp = new ResultLootItem(saveFile[i + 0], decimal.Parse(saveFile[i + 2])); } else { Byte[] bytes = File.ReadAllBytes(saveFile[i + 1]); MemoryStream ms = new MemoryStream(bytes); Image image = Image.FromStream(ms); Bitmap sizeChangeImage = new Bitmap(image, new Size(35, 35)); temp = (new LootItem(saveFile[i + 0], decimal.Parse(saveFile[i + 2]), sizeChangeImage, image, saveFile[i + 1])); resultTemp = new ResultLootItem(saveFile[i + 0], decimal.Parse(saveFile[i + 2]), sizeChangeImage, image, saveFile[i + 1]); ms.Flush(); } AddItem(temp, resultTemp); } } }
// 세이브 버튼을 클릭하면 예외(noInputCheck)가 없을 경우 아이템을 추가하는 메서드 private void SaveButton_Click(object sender, EventArgs e) { string img_folder = "images\\"; LootItem lootitem; ResultLootItem resultItem; // 저장 경로가 없을경우 생성 if (!System.IO.Directory.Exists(img_folder)) { System.IO.Directory.CreateDirectory(img_folder); } // 안에 내용이 안들어가거나 내용에 이상이 있을 경우 if (!noInputCheck()) { return; } if (imgFileName == null) { lootitem = new LootItem(nameTextbox.Text, decimal.Parse(probabilityTextbox.Text)); resultItem = new ResultLootItem(nameTextbox.Text, decimal.Parse(probabilityTextbox.Text)); mainForm.AddItem(lootitem, resultItem); } else { using (MemoryStream memory = new MemoryStream()) { string filePath = img_folder + imgFileName; imagePictureBox.Image.Save(filePath); Byte[] bytes = File.ReadAllBytes(img_folder + imgFileName); MemoryStream ms = new MemoryStream(bytes); Image image = Image.FromStream(ms); Bitmap newSize = new Bitmap(image, new Size(35, 35)); lootitem = new LootItem(nameTextbox.Text, decimal.Parse(probabilityTextbox.Text), newSize, image, filePath); resultItem = new ResultLootItem(nameTextbox.Text, decimal.Parse(probabilityTextbox.Text), newSize, image, filePath); mainForm.AddItem(lootitem, resultItem); } } this.Close(); }
// Try버튼을 클릭하였을 때 랜덤 뽑기를 진행 private void TryButton_Click(object sender, EventArgs e) { decimal sum = 0; // 리스트에 아이템이 없을 경우 처리 if (itemList.Count == 0) { TryButtonMessageBox(0); return; } foreach (LootItem item in itemList) { sum += item.Probability; } if (sum > 100) { TryButtonMessageBox(1); return; } Random r = new Random(DateTime.Now.Millisecond); int randomResult = r.Next(0, 100000); decimal min = 0; decimal max = itemList[0].Probability; // 100 미만일 시 나머지를 꽝으로 처리 if (sum < 100) { string[] lang = new string[3] { "lose", "꽝", "空っぽ" }; if (loseItem == null) { loseItem = new ResultLootItem(lang[language_comboList.SelectedIndex], 100.0m - sum); countResultList.Add(loseItem); countResult_dataGridView.Rows.Add(loseItem.ItemImage, loseItem.Name, loseItem.Count); } loseItem.Probability = 100.0m - sum; itemList.Add(loseItem); } for (int i = 0; i < itemList.Count; i++) { Debug.WriteLine("min : {0}, prob : {1}", min * 1000, itemList[i].Probability * 1000); // 성공 if (randomResult >= (min * 1000) && randomResult < (max * 1000)) { Debug.WriteLine("당첨!"); if (itemList[i].ImgFIlePath.Equals("NULL")) { orderResultList.Add(new ResultLootItem(itemList[i].Name, itemList[i].Probability)); } else { orderResultList.Add(new ResultLootItem(itemList[i].Name, itemList[i].Probability, itemList[i].ItemImage, itemList[i].OriginalImage, itemList[i].ImgFIlePath)); } orderResult_dataGridView.Rows.Add(itemList[i].ItemImage, itemList[i].Name); countResultList[i].Count++; Debug.WriteLine(countResult_dataGridView.Rows[i].Cells[2].Value); Debug.WriteLine(countResultList[i].Count); countResult_dataGridView.Rows[i].Cells[2].Value = countResultList[i].Count; ResultPage resultPage = new ResultPage(itemList[i], language_comboList.SelectedIndex); resultPage.ShowDialog(); for (int k = 0; k < itemList.Count; k++) { if (loseItem == null) { break; } if (itemList[k].GetHashCode() == loseItem.GetHashCode()) { // 성공 itemList.RemoveAt(k); break; } } return; } // 실패 else { Debug.WriteLine("실패"); min = itemList[i].Probability; if (itemList.Count == i + 1) { break; } max += itemList[i + 1].Probability; } } MessageBox.Show("꽝!"); }