Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/components/Speedometer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ interface IProps {}
interface IState {
gw2data_ipc: IGw2MumbleLinkData | null;
gw2data_ws: IGw2MumbleLinkData | null;
now_position: any;
Copy link
Contributor

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

now_timestamp: any;
last_position: any;
last_timestamp: any;
speed: any;
Copy link
Contributor

Choose a reason for hiding this comment

The 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";
Expand All @@ -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);
Expand All @@ -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})
Copy link
Contributor

Choose a reason for hiding this comment

The 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()})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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})
Copy link
Contributor

Choose a reason for hiding this comment

The 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');
}
Expand All @@ -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>
);
}
Expand Down