-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewModel.cs
255 lines (241 loc) · 9.11 KB
/
MainViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace 抽奖程序
{
public class MainViewModel:ViewModelBase
{
#region 定义
//水晶数
private int _diamondNum;
public int DiamondNum
{
get { return _diamondNum; }
set { _diamondNum = value;OnPropertyChanged(); }
}
//提示内容
private string _numTips;
public string NumTips
{
get { return _numTips; }
set { _numTips = value;OnPropertyChanged(); }
}
//提示隐藏与否
private string _tipsVisibility;
public string TipsVisibility
{
get { return _tipsVisibility; }
set { _tipsVisibility = value; OnPropertyChanged(); }
}
//抽奖窗体隐藏与否
private string _resultVisibility;
public string ResultVisibility
{
get { return _resultVisibility; }
set { _resultVisibility = value; OnPropertyChanged(); }
}
//铲子数
private string _shovelNums;
public string ShovelNums
{
get { return _shovelNums; }
set { _shovelNums = value; OnPropertyChanged(); }
}
//增加量 x
private string _increaseStr1;
public string IncreaseStr1
{
get { return _increaseStr1; }
set { _increaseStr1 = value; OnPropertyChanged(); }
}
//增加量 +
private string _increaseStr2;
public string IncreaseStr2
{
get { return _increaseStr2; }
set { _increaseStr2 = value; OnPropertyChanged(); }
}
#endregion
ValueInfo vi =new ValueInfo();
string vi_fp = "LuckDrawValueInfo.json";
#region 获取水晶数
public void GetDiamondNum()
{
if (File.Exists(vi_fp))
{
vi = JsonConvert.DeserializeObject<ValueInfo>(File.ReadAllText(vi_fp));
}
else
{
vi.DiamondNum = 999;
File.WriteAllText(vi_fp, JsonConvert.SerializeObject(vi));
}
DiamondNum= vi.DiamondNum;
TipsVisibility = "Collapsed";
ResultVisibility = "Collapsed";
}
#endregion
#region 增加水晶数
ValueInfo increase = new ValueInfo();
public void IncreaseDiamond(int num)
{
try
{
string fp = "LuckDrawValueInfo.json";
increase = JsonConvert.DeserializeObject<ValueInfo>(File.ReadAllText(fp));
increase.DiamondNum = num + increase.DiamondNum;
File.Delete(fp);
//写入
File.WriteAllText(fp, JsonConvert.SerializeObject(increase));
increase = JsonConvert.DeserializeObject<ValueInfo>(File.ReadAllText(fp));
DiamondNum = increase.DiamondNum;
}
catch
{
MessageBox.Show("未找到文件");
}
}
#endregion
#region 购买功能
public bool BtnClick(int num)
{
vi = JsonConvert.DeserializeObject<ValueInfo>(File.ReadAllText(vi_fp));
DiamondNum = vi.DiamondNum;
bool isSuccess = true;
if (vi.DiamondNum < num)
{
NumTips = $"还差{num - vi.DiamondNum}个水晶,赠送{num - vi.DiamondNum}个黄金铲可获得";
TipsVisibility = "Visible";
IHelper.Delay(700);
TipsVisibility = "Collapsed";
isSuccess= false;
}
else
{
vi.DiamondNum = vi.DiamondNum - num;
ResultVisibility = "Visible";
}
DiamondNum = vi.DiamondNum;
File.Delete(vi_fp);
File.WriteAllText(vi_fp, JsonConvert.SerializeObject(vi));
return isSuccess;
}
#endregion
#region 抽奖
ValueInfo valueInfo = new ValueInfo();
List<GiftModel> get = new List<GiftModel>();
public List<ResultModel> LuckyDraw(string FileName)
{
try
{
#region unimpossible
List<ResultModel> getResult = new List<ResultModel>();
//保底
string vi_fp = "LuckDrawValueInfo.json";
valueInfo = JsonConvert.DeserializeObject<ValueInfo>(File.ReadAllText(vi_fp));
List<MinGuarantee> mg = new List<MinGuarantee>();
string mg_fp = "LuckDrawMinGuarantee.json";
mg = JsonConvert.DeserializeObject<List<MinGuarantee>>(File.ReadAllText(mg_fp));
//抽奖
List<GiftModel> list = new List<GiftModel>();
string fp = $"LuckDraw{FileName}GiftData.json";
list = JsonConvert.DeserializeObject<List<GiftModel>>(File.ReadAllText(fp));
#endregion
Random rand = new Random();
int randnum = rand.Next(0,100);
int i_value = 2;
if (randnum < 40)
i_value = 3;
//出保底后
if (valueInfo.Count == 0)
{
if (valueInfo.Count != -1)
{
i_value--;
var query = mg.Where(t => t.GiftType == FileName).ToList();
MinGuarantee minGuarantee = new MinGuarantee();
if (query.Count == 1)
minGuarantee = mg.Where(t => t.GiftType == FileName).ToList()[0];
else
{
Random r_mg = new Random();
minGuarantee = mg.Where(t => t.GiftType == FileName).ToList()[r_mg.Next(0, query.Count())];
}
var data = list.Single(t => t.Count == minGuarantee.GuaranteeGiftNum && t.Name == minGuarantee.GuaranteeGiftName);
get.Add(new GiftModel() { Name = data.Name, StartPoint = data.StartPoint, EndPoint = data.EndPoint, Count = data.Count });
// MessageBox.Show("有保底哦");
valueInfo.Count = -1;
}
}
for (int i = 0; i < i_value; i++)
{
//生成随机数
Rand:
Random r = new Random();
Random ran_float = new Random();
int ints = r.Next(0, 100);
double floats = ran_float.NextDouble();
decimal value = Convert.ToDecimal((ints + floats).ToString("F2"));
Thread.Sleep(50);
//如果随机生成数为0重新生成一个随机数
if (value == 0)
goto Rand;
//读取抽到的礼物信息
var data = list.Single(t => t.StartPoint < value && t.EndPoint >= value);
//如果抽取的礼物中已有相同的则重新抽一次来代替
if(get.Count(t=>t.Name== data.Name)>0)
goto Rand;
//判断是否抽到相同的礼物,并对其累加
get.Add(new GiftModel() { Name = data.Name, StartPoint = data.StartPoint, EndPoint = data.EndPoint, Count = data.Count });
}
//保底次数减一
if (valueInfo.Count != -1)
valueInfo.Count--;
//保底数据保存本地
SaveGuarantee(valueInfo);
foreach (var item in get)
{
getResult.Add(new ResultModel() { ImgName = GetGitOrPng(item.Name), Name = $"{item.Name}x{item.Count}" });
}
get.Clear();
return getResult;
}
catch
{
return null;
}
}
private string GetGitOrPng(string str)
{
List<string> result = new List<string>() { "真爱钻戒","吃瓜","璀璨烟花","凤冠霞帔","好声音","黄金骑士","烈鹰指环","胜利权杖","王者之冠"};
if(result.Count(t=>t==str)>0)
{
str= $@"Resources\{str}.gif";
}
else
{
str= $@"Resources\{str}.png";
}
return str;
}
private void SaveGuarantee(ValueInfo data)
{
string vi_fp = "LuckDrawValueInfo.json";
if (!File.Exists(vi_fp)) // 判断是否已有相同文件
{
FileStream fs1 = new FileStream(vi_fp, FileMode.Create, FileAccess.ReadWrite);
fs1.Close();
}
else
File.Delete(vi_fp);
File.WriteAllText(vi_fp, JsonConvert.SerializeObject(data));
}
#endregion
}
}