forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_weather.go
207 lines (151 loc) · 3.85 KB
/
content_weather.go
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
package servermanager
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/cj123/ini"
"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
)
const weatherInfoFile = "weather.ini"
type Weather map[string]string
// defaultWeather is loaded if there aren't any weather options on the server
var defaultWeather = Weather{
"1_heavy_fog": "Heavy Fog",
"2_light_fog": "Light Fog",
"3_clear": "Clear",
"4_mid_clear": "Mid Clear",
"5_light_clouds": "Light Clouds",
"6_mid_clouds": "Mid Clouds",
"7_heavy_clouds": "Heavy Clouds",
}
func ListWeather() (Weather, error) {
baseDir := filepath.Join(ServerInstallPath, "content", "weather")
weatherFolders, err := ioutil.ReadDir(baseDir)
if os.IsNotExist(err) {
return defaultWeather, nil
} else if err != nil {
return nil, err
}
weather := defaultWeather
for _, weatherFolder := range weatherFolders {
if !weatherFolder.IsDir() {
continue
}
// read the weather info file
name, err := getWeatherName(baseDir, weatherFolder.Name())
if err != nil {
return nil, err
}
if name == "" {
name = weatherFolder.Name()
}
weather[weatherFolder.Name()] = name
}
if len(weather) == 0 {
return defaultWeather, nil
}
return weather, nil
}
func getWeatherName(folder, weather string) (string, error) {
f, err := os.Open(filepath.Join(folder, weather, weatherInfoFile))
if err != nil {
return "", nil
}
defer f.Close()
i, err := ini.Load(f)
if err != nil {
return "", err
}
s, err := i.GetSection("LAUNCHER")
if err != nil {
return "", err
}
k, err := s.GetKey("NAME")
if err != nil {
return "", nil
}
return k.String(), nil
}
func getWeatherType(weather string) (int, error) {
baseDir := filepath.Join(ServerInstallPath, "content", "weather")
f, err := os.Open(filepath.Join(baseDir, weather, weatherInfoFile))
if err != nil {
return 0, err
}
defer f.Close()
i, err := ini.Load(f)
if err != nil {
return 0, err
}
s, err := i.GetSection("__LAUNCHER_CM")
if err != nil {
return 0, err
}
k, err := s.GetKey("WEATHER_TYPE")
if err != nil {
return 0, nil
}
weatherType, err := k.Int()
if err != nil {
return 0, nil
}
return weatherType, nil
}
type WeatherHandler struct {
*BaseHandler
}
func NewWeatherHandler(baseHandler *BaseHandler) *WeatherHandler {
return &WeatherHandler{
BaseHandler: baseHandler,
}
}
type weatherListTemplateVars struct {
BaseTemplateVars
Weathers Weather
}
func (wh *WeatherHandler) list(w http.ResponseWriter, r *http.Request) {
weather, err := ListWeather()
if err != nil {
logrus.WithError(err).Errorf("could not get weather list")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
wh.viewRenderer.MustLoadTemplate(w, r, "content/weather.html", &weatherListTemplateVars{
Weathers: weather,
})
}
func (wh *WeatherHandler) delete(w http.ResponseWriter, r *http.Request) {
weatherKey := chi.URLParam(r, "key")
weatherPath := filepath.Join(ServerInstallPath, "content", "weather")
existingWeather, err := ListWeather()
if err != nil {
logrus.WithError(err).Errorf("could not get weather list")
AddErrorFlash(w, r, "Couldn't get weather list")
http.Redirect(w, r, r.Referer(), http.StatusFound)
return
}
var found bool
for key := range existingWeather {
if weatherKey == key {
// Delete car
found = true
err := os.RemoveAll(filepath.Join(weatherPath, weatherKey))
if err != nil {
found = false
logrus.WithError(err).Errorf("could not remove weather files")
}
delete(existingWeather, key)
break
}
}
if found {
// confirm deletion
AddFlash(w, r, "Weather preset successfully deleted!")
} else {
// inform weather wasn't found
AddErrorFlash(w, r, "Sorry, weather preset could not be deleted. Are you sure it was installed?")
}
http.Redirect(w, r, r.Referer(), http.StatusFound)
}