-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveAnalyzerInstanceHelper.cs
320 lines (275 loc) · 17.3 KB
/
SaveAnalyzerInstanceHelper.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using fortunestreetanalyzer.DatabaseModels.fortunestreet;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
namespace fortunestreetanalyzer;
public class SaveAnalyzerInstanceHelper
{
public static JsonResult SaveGameSettings(Global.AnalyzerDataModel saveGameSettingsParameter, FortuneStreetAppContext fortuneStreetAppContext)
{
Global.Response response = new Global.Response();
try
{
Rules rulesResult = fortuneStreetAppContext.Rules.SingleOrDefault(id => id.ID == saveGameSettingsParameter.GameSettingsData.RuleData.ID);
if (rulesResult == null)
{
response.AlertData = new Global.Response.Alert
{
Type = "alert-danger",
Title = "Invalid rule selection."
};
response.Error = true;
return new JsonResult(response);
}
Boards boardsResult = fortuneStreetAppContext.Boards.SingleOrDefault(id => id.ID == saveGameSettingsParameter.GameSettingsData.BoardData.ID);
if (boardsResult == null)
{
response.AlertData = new Global.Response.Alert
{
Type = "alert-danger",
Title = "Invalid board selection."
};
response.Error = true;
return new JsonResult(response);
}
Colors colorsResult = fortuneStreetAppContext.Colors.SingleOrDefault(id => id.ID == saveGameSettingsParameter.GameSettingsData.ColorData.ID);
if (colorsResult == null)
{
response.AlertData = new Global.Response.Alert
{
Type = "alert-danger",
Title = "Invalid mii color selection."
};
response.Error = true;
return new JsonResult(response);
}
fortuneStreetAppContext.GameSettings.Add(new GameSettings
{
AnalyzerInstanceID = saveGameSettingsParameter.AnalyzerInstanceID,
RuleID = rulesResult.ID,
BoardID = boardsResult.ID,
MiiColorID = colorsResult.ID
});
fortuneStreetAppContext.SaveChanges();
BoardCharacteristics boardCharacteristicResult = fortuneStreetAppContext.BoardCharacteristics.SingleOrDefault(board_characteristic => board_characteristic.RuleID == rulesResult.ID && board_characteristic.BoardID == boardsResult.ID);
Global.LoadAnalyzerInstance loadAnalyzerInstanceTurnOrderDeterminationSettings = LoadAnalyzerInstanceHelper.LoadTurnOrderDeterminationSettings(saveGameSettingsParameter.AnalyzerInstanceID, fortuneStreetAppContext);
response.HTMLResponse = JsonSerializer.Serialize(new
{
Data = new Global.AnalyzerDataModel
{
GameSettingsData = new Global.AnalyzerDataModel.GameSettingsDataModel
{
RuleData = new Global.AnalyzerDataModel.GameSettingsDataModel.RuleDataModel
{
ID = rulesResult.ID,
Name = rulesResult.Name,
StandingThreshold = boardCharacteristicResult.StandingThreshold,
NetWorthThreshold = boardCharacteristicResult.NetWorthThreshold
},
BoardData = new Global.AnalyzerDataModel.GameSettingsDataModel.BoardDataModel
{
ID = boardsResult.ID,
Name = boardsResult.Name,
ReadyCashStart = boardCharacteristicResult.ReadyCashStart,
SalaryStart = boardCharacteristicResult.SalaryStart,
SalaryIncrease = boardCharacteristicResult.SalaryIncrease,
MaxDieRoll = boardCharacteristicResult.MaxDieRoll
},
ColorData = new Global.AnalyzerDataModel.GameSettingsDataModel.ColorDataModel
{
ID = colorsResult.ID,
SystemColor = colorsResult.SystemColor,
CharacterColor = colorsResult.CharacterColor
}
},
CharacterData = loadAnalyzerInstanceTurnOrderDeterminationSettings.Data.CharacterData
},
Response = loadAnalyzerInstanceTurnOrderDeterminationSettings.Response
});
return new JsonResult(response);
}
catch (Exception e)
{
return Global.ServerErrorResponse(e);
}
}
public static JsonResult SaveTurnOrderDeterminationSettings(Global.AnalyzerDataModel saveTurnOrderDeterminationSettingsParameter, FortuneStreetAppContext fortuneStreetAppContext, FortuneStreetSavePreRollContext fortuneStreetSavePreRollContext)
{
Global.Response response = new Global.Response();
try
{
fortuneStreetAppContext.TurnOrderDetermination.AddRange(saveTurnOrderDeterminationSettingsParameter.CharacterData.PlayerData.Select(player => new TurnOrderDetermination
{
AnalyzerInstanceID = saveTurnOrderDeterminationSettingsParameter.AnalyzerInstanceID,
CharacterID = player.ID,
Value = (byte) player.TurnOrderValue
}));
List<TurnIterators> initialTurnIteratorsData = saveTurnOrderDeterminationSettingsParameter.CharacterData.PlayerData.OrderByDescending(player => player.TurnOrderValue).Select((player_result, player_index) => new TurnIterators
{
AnalyzerInstanceID = saveTurnOrderDeterminationSettingsParameter.AnalyzerInstanceID,
CharacterID = player_result.ID,
TurnResetCounter = 1,
TurnNumber = 1,
TurnOrder = (byte) (player_index + 1)
}).ToList();
fortuneStreetAppContext.TurnIterators.AddRange(initialTurnIteratorsData);
fortuneStreetAppContext.SaveChanges();
BoardCharacteristics boardCharacteristicResult = fortuneStreetAppContext.BoardCharacteristics.SingleOrDefault(board_characteristic => board_characteristic.RuleID == saveTurnOrderDeterminationSettingsParameter.GameSettingsData.RuleData.ID && board_characteristic.BoardID == saveTurnOrderDeterminationSettingsParameter.GameSettingsData.BoardData.ID);
Global.IndexDataModel indexData = Global.GetIndexData(saveTurnOrderDeterminationSettingsParameter.GameSettingsData.RuleData.ID, saveTurnOrderDeterminationSettingsParameter.GameSettingsData.BoardData.ID, fortuneStreetAppContext);
List<long> spaceIDs = indexData.SpaceIndexData.Select(space_index => space_index.SpaceData.ID).ToList();
List<SpaceLayouts> spaceLayoutResults = fortuneStreetAppContext.SpaceLayouts.Where(space_id => spaceIDs.Contains(space_id.SpaceID)).ToList();
List<Global.AnalyzerDataModel.GameSettingsDataModel.TurnDataModel> initialTurnData = initialTurnIteratorsData.Select(result => new Global.AnalyzerDataModel.GameSettingsDataModel.TurnDataModel
{
TurnPlayerData = new List<Global.AnalyzerDataModel.GameSettingsDataModel.TurnDataModel.TurnPlayerDataModel>
{
new Global.AnalyzerDataModel.GameSettingsDataModel.TurnDataModel.TurnPlayerDataModel
{
TurnIteratorID = result.ID,
LayoutIndex = 0,
Level = 1,
Placing = 1,
ReadyCash = boardCharacteristicResult.ReadyCashStart,
TotalShopValue = 0,
TotalStockValue = 0,
NetWorth = boardCharacteristicResult.ReadyCashStart,
OwnedShopIndices = new List<long>(),
TotalSuitCards = 0,
CollectedSuits = new List<string>(),
ArcadeIndex = 0
}
},
TurnCameoCharactersData = new List<List<Global.AnalyzerDataModel.GameSettingsDataModel.TurnDataModel.TurnCharacterPropertiesDataModel>>()
}).ToList();
List<Global.AnalyzerDataModel.SpaceDataModel> spaceData = new List<Global.AnalyzerDataModel.SpaceDataModel>();
foreach (Global.IndexDataModel.SpaceIndexDataModel currentSpaceIndexData in indexData.SpaceIndexData)
{
if (currentSpaceIndexData.SpaceData.SpaceType.Name.Equals("bank"))
{
foreach (Global.AnalyzerDataModel.GameSettingsDataModel.TurnDataModel currentInitialTurnData in initialTurnData)
currentInitialTurnData.TurnPlayerData.FirstOrDefault().SpaceIndexCurrent = currentSpaceIndexData.Index;
}
List<Global.IndexDataModel.SpaceConstraintIndexDataModel> currentSpaceConstraintIndexData = indexData.SpaceConstraintIndexData.Where(space_constraint_index_result => space_constraint_index_result.SpaceConstraintData.SpaceID == currentSpaceIndexData.SpaceData.ID).ToList();
Global.IndexDataModel.ShopIndexDataModel currentShopIndexData = indexData.ShopIndexData.SingleOrDefault(shop_index_result => currentSpaceIndexData.SpaceData.ShopID != null && shop_index_result.ShopData.ID == (long) currentSpaceIndexData.SpaceData.ShopID);
Global.IndexDataModel.DistrictIndexDataModel currentDistrictIndexData = indexData.DistrictIndexData.SingleOrDefault(district_index_result => currentSpaceIndexData.SpaceData.DistrictID != null && district_index_result.DistrictData.ID == (long) currentSpaceIndexData.SpaceData.DistrictID);
spaceData.Add(new Global.AnalyzerDataModel.SpaceDataModel
{
ID = currentSpaceIndexData.SpaceData.ID,
AdditionalPropertiesData = !string.IsNullOrWhiteSpace(currentSpaceIndexData.SpaceData.AdditionalProperties) ? JsonSerializer.Deserialize<Global.AnalyzerDataModel.SpaceDataModel.AdditionalPropertiesDataModel>(currentSpaceIndexData.SpaceData.AdditionalProperties) : null,
SpaceLayoutData = spaceLayoutResults.Where(space_id => space_id.SpaceID == currentSpaceIndexData.SpaceData.ID).Select(space_layout_result => new Global.AnalyzerDataModel.SpaceDataModel.SpaceLayoutDataModel
{
CenterXFactor = space_layout_result.CenterXFactor,
CenterYFactor = space_layout_result.CenterYFactor
}).ToList(),
SpaceConstraintData = currentSpaceConstraintIndexData.GroupBy(space_constraint_index_result => new { space_constraint_index_result.SpaceConstraintData.SpaceIDFrom, space_constraint_index_result.SpaceConstraintData.LayoutIndex }).Select(space_constraint_groupby => new Global.AnalyzerDataModel.SpaceDataModel.SpaceConstraintDataModel
{
SpaceIndexFrom = indexData.SpaceIndexData.FirstOrDefault(space_index_result => space_index_result.SpaceData.ID == space_constraint_groupby.Key.SpaceIDFrom).Index,
SpaceIndicesTo = indexData.SpaceIndexData.Where(space_index_result => currentSpaceConstraintIndexData.Where(space_constraint_index_result => space_constraint_index_result.SpaceConstraintData.SpaceIDFrom == space_constraint_groupby.Key.SpaceIDFrom).Select(space_constraint_index_result => space_constraint_index_result.SpaceConstraintData.SpaceIDTo).Contains(space_index_result.SpaceData.ID)).Select(index => index.Index).ToList(),
LayoutIndex = space_constraint_groupby.Key.LayoutIndex
}).ToList(),
SpaceTypeIndex = indexData.SpaceTypeIndexData.SingleOrDefault(space_type_index_result => space_type_index_result.SpaceTypeData.ID == currentSpaceIndexData.SpaceData.SpaceTypeID).Index,
ShopIndex = currentShopIndexData?.Index,
DistrictIndex = currentDistrictIndexData?.Index
});
}
List<GetCharacterColorsTVF> getCharacterColorsTVFResults = fortuneStreetAppContext.GetCharacterColorsTVF.FromSqlRaw("SELECT * FROM getcharactercolors_tvf({0})", saveTurnOrderDeterminationSettingsParameter.AnalyzerInstanceID).ToList();
List<Colors> colorsResults = fortuneStreetAppContext.Colors.ToList();
Global.AnalyzerDataModel analyzerData = new Global.AnalyzerDataModel
{
GameSettingsData = new Global.AnalyzerDataModel.GameSettingsDataModel
{
TurnData = new List<List<Global.AnalyzerDataModel.GameSettingsDataModel.TurnDataModel>> { initialTurnData }
},
CharacterData = new Global.AnalyzerDataModel.CharacterDataModel
{
PlayerData = getCharacterColorsTVFResults.Select(result => new Global.AnalyzerDataModel.CharacterDataModel.CharacterPlayerDataModel
{
ID = result.CharacterID,
ColorData = new Global.AnalyzerDataModel.CharacterDataModel.CharacterPlayerDataModel.ColorDataModel
{
ID = result.ColorIDAssigned,
CharacterColor = colorsResults.SingleOrDefault(color => color.ID == result.ColorIDAssigned).CharacterColor
}
}).ToList()
},
SpaceData = spaceData,
SpaceTypeData = indexData.SpaceTypeIndexData.Select(space_type_index_result => new Global.AnalyzerDataModel.SpaceTypeDataModel
{
ID = space_type_index_result.SpaceTypeData.ID,
Name = space_type_index_result.SpaceTypeData.Name,
Icon = space_type_index_result.SpaceTypeData.Icon,
Title = space_type_index_result.SpaceTypeData.Title,
Description = space_type_index_result.SpaceTypeData.Description
}).ToList(),
ShopData = indexData.ShopIndexData.Select(shop_index_result => new Global.AnalyzerDataModel.ShopDataModel
{
ID = shop_index_result.ShopData.ID,
Name = shop_index_result.ShopData.Name,
Value = shop_index_result.ShopData.Value
}).ToList(),
DistrictData = indexData.DistrictIndexData.Select(district_index_result => new Global.AnalyzerDataModel.DistrictDataModel
{
ID = district_index_result.DistrictData.ID,
Name = district_index_result.DistrictData.Name,
Color = district_index_result.DistrictData.Color
}).ToList()
};
SavePreRollTurnData
(
initialTurnData.Select(result => result.TurnPlayerData.FirstOrDefault()).Select(player => new PreRolls
{
TurnIteratorID = player.TurnIteratorID,
SpaceIDCurrent = analyzerData.SpaceData[(int) player.SpaceIndexCurrent].ID,
SpaceIDFrom = player.SpaceIndexFrom != null ? analyzerData.SpaceData[(int) player.SpaceIndexFrom].ID : null,
LayoutIndex = player.LayoutIndex,
Level = player.Level,
Placing = player.Placing,
ReadyCash = player.ReadyCash,
TotalShopValue = player.TotalShopValue,
TotalStockValue = player.TotalStockValue,
NetWorth = player.NetWorth,
OwnedShopIndices = JsonSerializer.Serialize(player.OwnedShopIndices),
TotalSuitCards = player.TotalSuitCards,
CollectedSuits = JsonSerializer.Serialize(player.CollectedSuits),
ArcadeIndex = player.ArcadeIndex,
DieRollRestrictions = player.DieRollRestrictions != null ? JsonSerializer.Serialize(player.DieRollRestrictions) : null
}).ToList(),
fortuneStreetSavePreRollContext
);
response.HTMLResponse = JsonSerializer.Serialize(new
{
Data = analyzerData
});
return new JsonResult(response);
}
catch (Exception e)
{
return Global.ServerErrorResponse(e);
}
}
public static JsonResult SavePreRollTurnData(List<PreRolls> preRollsRecords, FortuneStreetSavePreRollContext fortuneStreetSavePreRollContext)
{
try
{
fortuneStreetSavePreRollContext.PreRolls.AddRange(preRollsRecords);
fortuneStreetSavePreRollContext.SaveChanges();
return new JsonResult(new Global.Response());
}
catch (Exception e)
{
return Global.ServerErrorResponse(e);
}
}
public static JsonResult SavePostRollTurnData(PostRolls postRollsRecord, FortuneStreetSavePostRollContext fortuneStreetSavePostRollContext)
{
try
{
fortuneStreetSavePostRollContext.PostRolls.Add(postRollsRecord);
fortuneStreetSavePostRollContext.SaveChanges();
return new JsonResult(new Global.Response());
}
catch (Exception e)
{
return Global.ServerErrorResponse(e);
}
}
}