Skip to content

Commit

Permalink
Merge pull request #19 from venyii/sun-time
Browse files Browse the repository at this point in the history
Make sun angle readonly and calculate it based on time
  • Loading branch information
Marvin Blum committed Jun 28, 2017
2 parents 94dea6e + 9fc7547 commit ee495f7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
5 changes: 4 additions & 1 deletion db/mig_1.0.3_1.0.4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ ALTER TABLE `weather`
ADD COLUMN `wind_base_speed_min` int(10) NOT NULL AFTER `road_variation`,
ADD COLUMN `wind_base_speed_max` int(10) NOT NULL AFTER `wind_base_speed_min`,
ADD COLUMN `wind_base_direction` int(10) NOT NULL AFTER `wind_base_speed_max`,
ADD COLUMN `wind_variation_direction` int(10) NOT NULL AFTER `wind_base_direction`;
ADD COLUMN `wind_variation_direction` int(10) NOT NULL AFTER `wind_base_direction`
DROP COLUMN `realistic_road_temp`;

ALTER TABLE `configurations` DROP COLUMN `time`;
37 changes: 33 additions & 4 deletions public/app/pages/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Vue.component('Configuration', {
watch: {
condition: function (value) {
this.populateDynamicTrackWithPreset(value);
},
time: function (value) {
this.calculateSunAngleByTime(value);
}
},
methods: {
Expand Down Expand Up @@ -278,8 +281,8 @@ Vue.component('Configuration', {
this.race_wait_time = resp.data.race_wait_time;
this.race_extra_lap = resp.data.race_extra_lap;
this.join_type = resp.data.join_type;
this.time = resp.data.time;
this.sun_angle = resp.data.sun_angle;
this.time = this.calculateTimeBySunAngle(this.sun_angle);
this.legal_tyres = resp.data.legal_tyres;
this.udp_plugin_local_port = resp.data.udp_plugin_local_port;
this.udp_plugin_address = resp.data.udp_plugin_address;
Expand Down Expand Up @@ -347,7 +350,6 @@ Vue.component('Configuration', {
performAddEditConfig: function(){
for(var i = 0; i < this.weather.length; i++){
this.weather[i].base_ambient_temp = parseInt(this.weather[i].base_ambient_temp);
this.weather[i].realistic_road_temp = parseInt(this.weather[i].realistic_road_temp);
this.weather[i].base_road_temp = parseInt(this.weather[i].base_road_temp);
this.weather[i].ambient_variation = parseInt(this.weather[i].ambient_variation);
this.weather[i].road_variation = parseInt(this.weather[i].road_variation);
Expand Down Expand Up @@ -417,8 +419,8 @@ Vue.component('Configuration', {
race_wait_time: parseInt(this.race_wait_time),
race_extra_lap: this.race_extra_lap,
join_type: parseInt(this.join_type),
time: this.time,
sun_angle: parseInt(this.sun_angle),
time: this.calculateTimeBySunAngle(parseInt(this.sun_angle)),
weather: this.weather,
track: this.track.name,
track_config: this.track.config,
Expand Down Expand Up @@ -462,7 +464,6 @@ Vue.component('Configuration', {
this.weather.push({
weather: 'Clear',
base_ambient_temp: 20,
realistic_road_temp: 1,
base_road_temp: 18,
ambient_variation: 1,
road_variation: 1,
Expand Down Expand Up @@ -567,6 +568,34 @@ Vue.component('Configuration', {
},
generateCfgDownloadUrl: function (id) {
return '/api/configuration?id=' + id + '&dl=1';
},
calculateSunAngleByTime: function(time) {
var totalHours = parseInt(time.replace(':', ''), 10);

if (isNaN(totalHours) || totalHours < 800 || totalHours > 1800) {
// Invalid date, default to 0 (13:00)
this.sun_angle = 0;
return;
}

var timeParts = time.split(':');
var hours = parseInt(timeParts[0], 10);
var minutes = parseInt(timeParts[1], 10);

// Calculate the time in minutes and subtract 13 hours
// The sun angle can range from -80 to 80 in the time frame 08:00h - 18:00h
// e.g. 08:00 = 480min. - 780min. = -300 / 30 = -10 * 8 = -80
// e.g. 13:00 = 780min. - 780min. = 0 / 30 = 0 * 8 = 0
// e.g. 16:30 = 990min. - 780min. = 210 / 30 = 7 * 8 = 56
var totalMinutes = (hours * 60 + minutes) - 780;

this.sun_angle = Math.floor(totalMinutes / 30) * 8;
},
calculateTimeBySunAngle: function(sun_angle) {
var totalHours = ((sun_angle / 8) * 30 + 780) / 60;
var hrs = totalHours.toString();

return Math.floor(totalHours).toString() + ':' + (hrs[hrs.length - 1] === '5' ? '30' : '00');
}
}
});
5 changes: 5 additions & 0 deletions public/scss/html.scss
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ input:focus, select:focus, textarea:focus{
border-color: $gray;
}

input[readonly] {
color: #ccc;
border-color: #ccc;
}

.full-width, textarea{
box-sizing: border-box;
width: 100%;
Expand Down
11 changes: 3 additions & 8 deletions public/template/pages/configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ <h2>Create/Edit Configuration</h2>
<tr>
<td>Time:</td>
<td>
<input type="time" name="time" v-model="time" />
(08:00 - 18:00)
<input type="time" name="time" v-model="time" step="1800" min="08:00" max="18:00" />
(08:00 - 18:00, 30 minute steps)
</td>
</tr>
<tr>
<td>Sun angle:</td>
<td><input type="number" name="sun_angle" v-model="sun_angle" /></td>
<td><input type="number" name="sun_angle" v-model="sun_angle" readonly /></td>
</tr>
<tr>
<td colspan="2"><h3>Weather</h3></td>
Expand Down Expand Up @@ -378,11 +378,6 @@ <h2>Create/Edit Configuration</h2>
<td><input type="number" name="base_ambient_temp" v-model="w.base_ambient_temp" /></td>
<td></td>
</tr>
<tr>
<td>Realistic road temp:</td>
<td><input type="number" name="realistic_road_temp" v-model="w.realistic_road_temp" /></td>
<td></td>
</tr>
<tr>
<td>Base road temp:</td>
<td><input type="number" name="base_road_temp" v-model="w.base_road_temp" /></td>
Expand Down
11 changes: 1 addition & 10 deletions src/model/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type Configuration struct {
RaceWaitTime int `db:"race_wait_time" json:"race_wait_time"`
RaceExtraLap bool `db:"race_extra_lap" json:"race_extra_lap"`
JoinType int `db:"join_type" json:"join_type"`
Time string `json:"time"`
SunAngle int `db:"sun_angle" json:"sun_angle"`
Track string `json:"track"`
TrackConfig string `db:"track_config" json:"track_config"`
Expand All @@ -81,7 +80,6 @@ type Weather struct {
Configuration int64 `json:"configuration"`
Weather string `json:"weather"`
BaseAmbientTemp int `db:"base_ambient_temp" json:"base_ambient_temp"`
RealisticRoadTemp int `db:"realistic_road_temp" json:"realistic_road_temp"`
BaseRoadTemp int `db:"base_road_temp" json:"base_road_temp"`
AmbientVariation int `db:"ambient_variation" json:"ambient_variation"`
RoadVariation int `db:"road_variation" json:"road_variation"`
Expand Down Expand Up @@ -204,7 +202,6 @@ func (m *Configuration) saveConfiguration(tx *sqlx.Tx) error {
race_wait_time,
race_extra_lap,
join_type,
time,
sun_angle,
track,
track_config,
Expand Down Expand Up @@ -269,7 +266,6 @@ func (m *Configuration) saveConfiguration(tx *sqlx.Tx) error {
:race_wait_time,
:race_extra_lap,
:join_type,
:time,
:sun_angle,
:track,
:track_config,
Expand Down Expand Up @@ -350,7 +346,6 @@ func (m *Configuration) saveConfiguration(tx *sqlx.Tx) error {
race_wait_time = :race_wait_time,
race_extra_lap = :race_extra_lap,
join_type = :join_type,
time = :time,
sun_angle = :sun_angle,
track = :track,
track_config = :track_config,
Expand All @@ -376,19 +371,17 @@ func (m *Configuration) saveWeather(tx *sqlx.Tx) error {
_, err := tx.Exec(`INSERT INTO weather (configuration,
weather,
base_ambient_temp,
realistic_road_temp,
base_road_temp,
ambient_variation,
road_variation,
wind_base_speed_min,
wind_base_speed_max,
wind_base_direction,
wind_variation_direction
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
m.Id,
weather.Weather,
weather.BaseAmbientTemp,
weather.RealisticRoadTemp,
weather.BaseRoadTemp,
weather.AmbientVariation,
weather.RoadVariation,
Expand All @@ -404,7 +397,6 @@ func (m *Configuration) saveWeather(tx *sqlx.Tx) error {
} else {
_, err := tx.Exec(`UPDATE weather SET weather = ?,
base_ambient_temp = ?,
realistic_road_temp = ?,
base_road_temp = ?,
ambient_variation = ?,
road_variation = ?,
Expand All @@ -415,7 +407,6 @@ func (m *Configuration) saveWeather(tx *sqlx.Tx) error {
WHERE id = ?`,
weather.Weather,
weather.BaseAmbientTemp,
weather.RealisticRoadTemp,
weather.BaseRoadTemp,
weather.AmbientVariation,
weather.RoadVariation,
Expand Down

0 comments on commit ee495f7

Please sign in to comment.