-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventSaver.cs
More file actions
108 lines (92 loc) · 3.8 KB
/
EventSaver.cs
File metadata and controls
108 lines (92 loc) · 3.8 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
namespace EventCreator
{
static class EventSaver
{
//If something goes wrong, result is set to the error message.
private static string result = "";
/// <summary>
/// Saves the event as a text file.
/// </summary>
/// <param name="frmMain">The completely filled out form.</param>
/// <returns>Returns "" if all went well. Otherwise an error message.</returns>
public static string SaveEvent(frmMain frmMain)
{
//Get file name, check it's valid.
string fileName = frmMain.GetOutputFileName();
if(fileName == null) {
return "Invalid file name";
}
//Create the dictionary for the event, check it's valid.
Dictionary<string, object> eventDictionary = MakeDictionary(frmMain);
if (eventDictionary == null)
{
//If the dictionary wasn't created properly, but result wasn't set - provide a generic error.
if(result == "") result = "An error occured creating the event dictionary.";
return result;
}
//Export to JSON.
MakeJSON(eventDictionary, fileName);
return result;
}
/// <summary>
/// Creates the dictionary for the event.
/// </summary>
/// <param name="frmMain">The filled out event form.</param>
/// <returns>The dictionary for the event. Null if something goes wrong.</returns>
private static Dictionary<string, object> MakeDictionary(frmMain frmMain)
{
string eventId = frmMain.getEventId();
string type = frmMain.getEventType();
List<string> possibleLocations = frmMain.getPossibleLocations();
List<string> reqParty = frmMain.getRequiredParty();
string frequency = frmMain.getFrequency();
string introText = frmMain.getIntroText();
List<ResponseOption> responseOptions = frmMain.getResponseOptions();
Dictionary<string, string> advice = frmMain.generateAdviceDictionary();
bool selectPlayer = frmMain.getSelectPlayer();
Event theEvent = new Event(eventId, type, possibleLocations, reqParty, frequency, introText, responseOptions, advice, selectPlayer);
return theEvent.myDictionary;
}
/// <summary>
/// Creates the JSON for the event.
/// </summary>
/// <param name="eventDictionary">The Event's Dictionary</param>
/// <param name="fileLoc">The location in which to save the file. Should really be a .txt file.</param>
private static void MakeJSON(Dictionary<string, object> eventDictionary, string fileLoc)
{
string output = JsonConvert.SerializeObject(eventDictionary);
if (String.IsNullOrWhiteSpace(output))
{
result = "An error occured creating the Json.";
return;
}
try
{
if (File.Exists(fileLoc)) File.Delete(fileLoc);
}
catch (Exception e)
{
result = ("An error occured deleting the old file: " + fileLoc + ".\n" + e.Message);
return;
}
try
{
using (StreamWriter writer = new StreamWriter(File.Create(fileLoc)))
{
writer.Write(output);
}
}
catch (Exception e)
{
result = ("An error occured writing to the file: " + fileLoc + ".\n" + e.Message);
}
}
}
}