-
Notifications
You must be signed in to change notification settings - Fork 3
calculate speed (test pull request) #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/speedometer
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,11 @@ interface IProps {} | |
| interface IState { | ||
| gw2data_ipc: IGw2MumbleLinkData | null; | ||
| gw2data_ws: IGw2MumbleLinkData | null; | ||
| now_position: any; | ||
| now_timestamp: any; | ||
| last_position: any; | ||
| last_timestamp: any; | ||
| speed: any; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. speed is number :) Also consider if it should be null before calculating speed the first time. |
||
| } | ||
|
|
||
| const WS_CLIENT = "ws://localhost:19939/position"; | ||
|
|
@@ -18,7 +23,15 @@ class App extends React.Component<IProps, IState> { | |
| super(props); | ||
| this.state = { | ||
| gw2data_ipc: null, | ||
| gw2data_ws: null | ||
| gw2data_ws: null, | ||
|
|
||
| now_position: {x:0,y:0,z:0}, | ||
| now_timestamp: 0, | ||
|
|
||
| last_position: {x:0,y:0,z:0}, | ||
| last_timestamp: 0, | ||
|
|
||
| speed: 0, | ||
| }; | ||
| this.gw2DataListener = this.gw2DataListener.bind(this); | ||
| ipcRenderer.on('gw2data', this.gw2DataListener); | ||
|
|
@@ -30,9 +43,38 @@ class App extends React.Component<IProps, IState> { | |
| webSocket.onmessage = function (event) { | ||
| var data = JSON.parse(event.data); | ||
| self.setState({gw2data_ws: data}); | ||
| if (self.state.last_timestamp + 10 < new Date().getTime()) { | ||
| // limit the calculation every 500 ms | ||
| self.calculateSpeed() | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| calculateSpeed() { | ||
|
|
||
| this.setState({now_position: this.state.gw2data_ws?.coordinates.playerPosition}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're not using now_position and now_timestamp, so might want to skip these :) |
||
| this.setState({now_timestamp: new Date().getTime()}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use https://developer.mozilla.org/en-US/docs/Web/API/Performance/now for more accurate timings instead of Date().getTime() https://stackoverflow.com/questions/6233927/microsecond-timing-in-javascript |
||
| if (JSON.stringify(this.state.last_position) == JSON.stringify(this.state.now_position)) { | ||
| return | ||
| } | ||
|
|
||
|
|
||
| var a = this.state.last_position.X - this.state.now_position.X; | ||
| var b = this.state.last_position.Y - this.state.now_position.Y; | ||
| var c = this.state.last_position.Z - this.state.now_position.Z; | ||
|
|
||
| var distance = Math.sqrt(a * a + b * b + c * c); | ||
| var time = this.state.now_timestamp - this.state.last_timestamp | ||
|
|
||
| let speed = (distance * 39.3700787) / 0.01 ; | ||
| speed = Math.round((speed*100/10000)*99/72) | ||
|
|
||
| this.setState({speed: speed}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These three setstate can be done in one call :) |
||
| this.setState({last_position: this.state.now_position}) | ||
| this.setState({last_timestamp: this.state.now_timestamp}) | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| ipcRenderer.removeAllListeners('gw2data'); | ||
| } | ||
|
|
@@ -50,6 +92,9 @@ class App extends React.Component<IProps, IState> { | |
| <div> | ||
| Coordinates (WS): {this.state.gw2data_ws?.coordinates.playerPosition.X} {this.state.gw2data_ws?.coordinates.playerPosition.Y} {this.state.gw2data_ws?.coordinates.playerPosition.Z} | ||
| </div> | ||
| <div> | ||
| speed: {this.state.speed} | ||
| </div> | ||
| </Window> | ||
| ); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice if you had type here, it's an interface IPosition that has this structure.
That would also highlight a bug on line 28 where you use small xyz instead of capital XYZ