Skip to content

Commit

Permalink
refactor starting of game into startGame function
Browse files Browse the repository at this point in the history
  • Loading branch information
seeratsekhon committed Jun 10, 2018
1 parent e308b3f commit f5502d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
25 changes: 16 additions & 9 deletions server/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (
"github.com/ubclaunchpad/bumper/server/models"
)

// An instance of Upgrader that upgrades a connection to a WebSocket
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
log.Printf("Accepting client from remote address %v\n", r.RemoteAddr)
return true
},
}

// Game represents a session
type Game struct {
Arena *arena.Arena
Expand All @@ -26,12 +34,11 @@ func CreateGame() *Game {
return &g
}

// An instance of Upgrader that upgrades a connection to a WebSocket
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
log.Printf("Accepting client from remote address %v\n", r.RemoteAddr)
return true
},
// StartGame runs goroutines required to start a session
func (g *Game) StartGame() {
go g.messageEmitter()
go g.run()
go g.tick()
}

// ServeHTTP handles a connection from a client
Expand Down Expand Up @@ -111,7 +118,7 @@ func (g *Game) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (g *Game) Run() {
func (g *Game) run() {
for {
time.Sleep(g.RefreshRate)

Expand All @@ -120,7 +127,7 @@ func (g *Game) Run() {
}
}

func (g *Game) Tick() {
func (g *Game) tick() {
for {
time.Sleep(g.RefreshRate)

Expand All @@ -142,7 +149,7 @@ func (g *Game) Tick() {
}
}

func (g *Game) MessageEmitter() {
func (g *Game) messageEmitter() {
for {
msg := <-arena.MessageChannel

Expand Down
5 changes: 1 addition & 4 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ func main() {

http.Handle("/", http.FileServer(http.Dir("./build")))
http.Handle("/connect", game)
go game.MessageEmitter()
go game.Run()
go game.Tick()

game.StartGame()
log.Println("Starting server on localhost:" + os.Getenv("PORT"))
log.Println(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}

0 comments on commit f5502d0

Please sign in to comment.