-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherData.cs
123 lines (110 loc) · 4.53 KB
/
WeatherData.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UnityEngine;
using static WeatherForecast;
public static class WeatherData
{
public static void Load()
{
string rootFolder = Application.persistentDataPath + Path.DirectorySeparatorChar;
string path = rootFolder + "Forecast" + Path.DirectorySeparatorChar + "Forecast.txt";
bool forecastSection = false;
if (File.Exists(path))
{
string[] data = File.ReadAllLines(path);
regionWeatherProbability = new Dictionary<string, Dictionary<Weather.WeatherType, float>>();
regionWeatherForecasts = new Dictionary<string, List<Weather.WeatherType>>();
for (int i = 0; i < data.Length; i++)
{
if (!forecastSection)
{
if (data[i].ToLower().StartsWith("weather"))
continue;
if (data[i].ToLower().StartsWith("region"))
{
forecastSection = true;
continue;
}
string reg = Regex.Split(data[i], "~")[0];
string[] weathers = Regex.Split(Regex.Split(data[i], "~")[1], ">");
Dictionary<Weather.WeatherType, float> regionWeathers = new Dictionary<Weather.WeatherType, float>();
for (int s = 0; s < weathers.Length; s++)
{
if (s == weathers.Length - 1)
{
continue;
}
string type = Regex.Split(weathers[s], "<")[0];
float chance = float.Parse(Regex.Split(weathers[s], "<")[1]);
Weather.WeatherType weatherType = (Weather.WeatherType)Enum.Parse(typeof(Weather.WeatherType), type);
regionWeathers.Add(weatherType, chance);
}
if (!regionWeatherProbability.ContainsKey(reg))
{
regionWeatherProbability.Add(reg, regionWeathers);
}
}
else
{
string reg = Regex.Split(data[i], "~")[0];
string[] weathers = Regex.Split(Regex.Split(data[i], "~")[1], ":");
List<Weather.WeatherType> forecast = new List<Weather.WeatherType>();
for (int s = 0; s < weathers.Length; s++)
{
Weather.WeatherType weatherType = (Weather.WeatherType)Enum.Parse(typeof(Weather.WeatherType), weathers[s]);
forecast.Add(weatherType);
}
if (!regionWeatherForecasts.ContainsKey(reg))
{
regionWeatherForecasts.Add(reg, forecast);
}
}
}
}
}
public static void Save()
{
string rootFolder = Application.persistentDataPath + Path.DirectorySeparatorChar;
string path = rootFolder + "Forecast" + Path.DirectorySeparatorChar + "Forecast.txt";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(rootFolder + "Forecast");
}
string data = "Weather Probabilities:\n";
if (regionWeatherProbability != null && regionWeatherForecasts != null)
{
foreach (string reg in regionWeatherProbability.Keys)
{
//Region Name
data += $"{reg}~";
//Weather types and probabilities
foreach (Weather.WeatherType type in regionWeatherProbability[reg].Keys)
{
data += $"{type}<{regionWeatherProbability[reg][type]}>";
}
data += "\n";
}
data += "Region Forecasts:\n";
foreach (string reg in regionWeatherForecasts.Keys)
{
//Region Name
data += $"{reg}~";
for (int i = 0; i < regionWeatherForecasts[reg].Count; i++)
{
data += $"{regionWeatherForecasts[reg][i]}";
if (i < regionWeatherForecasts[reg].Count - 1)
{
data += ":";
}
}
data += "\n";
}
File.WriteAllText(path, data);
}
}
}