Esempio n. 1
0
func create(PlayerOne *player.Player, PlayerTwo *player.Player) {
	if PlayerOne.Disconnected || PlayerTwo.Disconnected {
		if !PlayerTwo.Disconnected {
			Pair(PlayerTwo)
		}
		if !PlayerOne.Disconnected {
			Pair(PlayerOne)
		}
		return
	}
	PlayerOne.Position = [][2]int{
		[2]int{3, 7},
		[2]int{3, 6},
		[2]int{3, 5},
		[2]int{3, 4},
		[2]int{3, 3},
	}
	PlayerOne.Heading = "down"
	PlayerTwo.Position = [][2]int{
		[2]int{46, 42},
		[2]int{46, 43},
		[2]int{46, 44},
		[2]int{46, 45},
		[2]int{46, 46},
	}
	PlayerTwo.Heading = "up"
	game := &Game{
		Width:     50,
		Height:    50,
		PlayerOne: PlayerOne,
		PlayerTwo: PlayerTwo,
		Food:      [][2]int{},
	}
	game.run()
}
Esempio n. 2
0
func pinger(p *player.Player, quit chan int) {
	p.PingSent = time.Now()
	p.ToClient <- map[string]string{"ping": "ping"}
	ticker := time.Tick(30e9)
	for {
		select {
		case t := <-ticker:
			p.PingSent = t
			p.ToClient <- map[string]string{"ping": "ping"}
		case <-quit:
			return
		}
	}
}
Esempio n. 3
0
func receiver(ws *websocket.Conn, p *player.Player) chan int {
	quit := make(chan int, 0)
	go func() {
		for {
			m := &player.Message{}
			err := websocket.JSON.Receive(ws, m)
			if err != nil {
				break
			}
			if m.Ping != "" {
				fmt.Printf(
					"%s %s %s\n",
					time.Now().UTC(),
					time.Since(p.PingSent),
					ws.Request().RemoteAddr,
				)
			} else {
				p.FromClient <- m
			}
		}
		p.Disconnected = true
		quit <- 1
	}()
	return quit
}