From 9fc754788e7cdf43eac7640f7eceb601d9a43039 Mon Sep 17 00:00:00 2001 From: venyii Date: Wed, 21 Jun 2017 21:53:12 +0200 Subject: [PATCH] Make sun angle readonly and calculate it based on time --- db/mig_1.0.3_1.0.4.sql | 5 +++- public/app/pages/configuration.js | 37 +++++++++++++++++++++--- public/scss/html.scss | 5 ++++ public/template/pages/configuration.html | 11 ++----- src/model/configuration.go | 11 +------ 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/db/mig_1.0.3_1.0.4.sql b/db/mig_1.0.3_1.0.4.sql index c589c98..d09d115 100644 --- a/db/mig_1.0.3_1.0.4.sql +++ b/db/mig_1.0.3_1.0.4.sql @@ -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`; diff --git a/public/app/pages/configuration.js b/public/app/pages/configuration.js index e657e90..3668345 100644 --- a/public/app/pages/configuration.js +++ b/public/app/pages/configuration.js @@ -96,6 +96,9 @@ Vue.component('Configuration', { watch: { condition: function (value) { this.populateDynamicTrackWithPreset(value); + }, + time: function (value) { + this.calculateSunAngleByTime(value); } }, methods: { @@ -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; @@ -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); @@ -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, @@ -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, @@ -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'); } } }); diff --git a/public/scss/html.scss b/public/scss/html.scss index e0debbf..a95fa39 100644 --- a/public/scss/html.scss +++ b/public/scss/html.scss @@ -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%; diff --git a/public/template/pages/configuration.html b/public/template/pages/configuration.html index 026d7c6..a0310c7 100644 --- a/public/template/pages/configuration.html +++ b/public/template/pages/configuration.html @@ -342,13 +342,13 @@

Create/Edit Configuration

Time: - - (08:00 - 18:00) + + (08:00 - 18:00, 30 minute steps) Sun angle: - +

Weather

@@ -378,11 +378,6 @@

Create/Edit Configuration

- - Realistic road temp: - - - Base road temp: diff --git a/src/model/configuration.go b/src/model/configuration.go index e1da67b..5c41e9f 100644 --- a/src/model/configuration.go +++ b/src/model/configuration.go @@ -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"` @@ -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"` @@ -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, @@ -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, @@ -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, @@ -376,7 +371,6 @@ 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, @@ -384,11 +378,10 @@ func (m *Configuration) saveWeather(tx *sqlx.Tx) error { 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, @@ -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 = ?, @@ -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,