Skip to content

Commit

Permalink
Merge pull request #257 from ubclaunchpad/brian/session-finding
Browse files Browse the repository at this point in the history
Brian/session finding
  • Loading branch information
brian-nguyen committed Oct 21, 2018
2 parents a432651 + c8856a9 commit a5bdb4a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
3 changes: 2 additions & 1 deletion client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"globals": {
"window": false,
"WebSocket": false,
"document": false
"document": false,
"fetch": false
},
"rules": {
"react/jsx-filename-extension": 0,
Expand Down
14 changes: 9 additions & 5 deletions client/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {
registerTesterUpdateEvent,
} from './database/database';

const address = process.env.NODE_ENV === 'production'
? 'bumper.ubclaunchpad.com'
: 'localhost:9090';
const address = 'localhost:9090';

export default class App extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -49,7 +47,7 @@ export default class App extends React.Component {

async componentDidMount() {
this.canvas = document.getElementById('ctx');
this.connectPlayer();
await this.connectPlayer();
registerNewTesterEvent();
registerTesterUpdateEvent();
}
Expand All @@ -68,7 +66,13 @@ export default class App extends React.Component {
}

// connect player on load
connectPlayer() {
async connectPlayer() {
const response = await fetch(`http://${address}/start`);
const res = await response.json();

// Address of lobby to connect to
console.log(res.location);

if (window.WebSocket) {
this.socket = new WebSocket(`ws://${address}/connect`);
this.socket.onopen = () => {
Expand Down
23 changes: 23 additions & 0 deletions client/components/Minimap.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export default class Minimap extends React.Component {

// Deep copy and then translate all game objects
const holes = this.holes.map((h) => {
if (!h.position) {
return {
position: { x: 0, y: 0 },
radius: 0,
isAlive: false,
};
}

const newPosition = {
x: (h.position.x / MAP_SCALE) + this.mapX,
y: (h.position.y / MAP_SCALE) + this.mapY,
Expand All @@ -64,6 +72,14 @@ export default class Minimap extends React.Component {
});

const players = this.players.map((p) => {
if (!p.position) {
return {
position: { x: 0, y: 0 },
color: 'black',
angle: 0,
};
}

const newPosition = {
x: (p.position.x / MAP_SCALE) + this.mapX,
y: (p.position.y / MAP_SCALE) + this.mapY,
Expand All @@ -77,6 +93,13 @@ export default class Minimap extends React.Component {
});

const junk = this.junk.map((j) => {
if (!j.position) {
return {
position: { x: 0, y: 0 },
color: 'black',
};
}

const newPosition = {
x: (j.position.x / MAP_SCALE) + this.mapX,
y: (j.position.y / MAP_SCALE) + this.mapY,
Expand Down
2 changes: 1 addition & 1 deletion client/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const config = {
devServer: {
port: 8080,
inline: true,
contentBase: './public'
contentBase: './public',
},
module: {
loaders: [
Expand Down
15 changes: 15 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"log"
"math/rand"
"net/http"
Expand All @@ -13,6 +14,19 @@ import (
"github.com/ubclaunchpad/bumper/server/models"
)

func getLobby(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")

response := struct {
Location string `json:"location"`
}{
"localhost:9090",
}

json.NewEncoder(w).Encode(response)
}

func main() {
rand.Seed(time.Now().UTC().UnixNano())

Expand All @@ -24,6 +38,7 @@ func main() {
log.Println("DBClient not initialized correctly")
}

http.HandleFunc("/start", getLobby)
http.Handle("/connect", game)
game.StartGame()

Expand Down

0 comments on commit a5bdb4a

Please sign in to comment.