func TestProcessorFactory(t *testing.T) {
	Convey("ProcessorFactory", t, func() {
		physics := processor.DefaultPhysics()
		physics.TurnCommandModifier = 1.0
		physics.AccelerationCommandModifier = 1.0
		factory := processor.NewFactory(&physics)

		t := cmd.NewTurn(1)
		t_comm := cmd.GameCommand(&t)
		turn_cp := factory.GetCommandProcessor(&t_comm)
		So(turn_cp, ShouldHaveSameTypeAs, &processor.TurnCommandProcessor{Physics: &physics})

		a := cmd.NewAcceleration(1)
		a_comm := cmd.GameCommand(&a)
		acceleration_cp := factory.GetCommandProcessor(&a_comm)
		So(acceleration_cp, ShouldHaveSameTypeAs, &processor.AccelerationCommandProcessor{Physics: &physics})

		c := cmd.NewConnect("test")
		c_comm := cmd.GameCommand(&c)
		connect_cp := factory.GetCommandProcessor(&c_comm)
		So(connect_cp, ShouldHaveSameTypeAs, &processor.ConnectCommandProcessor{Physics: &physics})

		f := cmd.NewFire()
		f_comm := cmd.GameCommand(&f)
		fire_cp := factory.GetCommandProcessor(&f_comm)
		So(fire_cp, ShouldHaveSameTypeAs, &processor.FireCommandProcessor{Physics: &physics})

		b := cmd.BaseCommand{}
		b_comm := cmd.GameCommand(&b)
		base_cp := factory.GetCommandProcessor(&b_comm)
		So(base_cp, ShouldEqual, nil)
	})
}
func TestConnectionProcessor(t *testing.T) {
	Convey("Connection Processor", t, func() {
		physics := processor.DefaultPhysics()
		physics.TurnCommandModifier = 1.0
		physics.AccelerationCommandModifier = 1.0
		conn_processor := processor.ConnectCommandProcessor{Physics: &physics}

		c := cmd.NewConnect("abc123")
		c2 := cmd.NewConnect("abc")
		c.UserId = "abc123"
		c2.UserId = "abc"

		connect := cmd.GameCommand(&c)
		connect2 := cmd.GameCommand(&c2)

		game_state := physics.NewGameState()

		So(len(game_state.Vehicles), ShouldEqual, 0)

		conn_processor.Run(&game_state, connect)
		So(len(game_state.Vehicles), ShouldEqual, 1)

		conn_processor.Run(&game_state, connect)
		So(len(game_state.Vehicles), ShouldEqual, 1)

		conn_processor.Run(&game_state, connect2)
		So(len(game_state.Vehicles), ShouldEqual, 2)
	})
}
func TestCommandConsumption(t *testing.T) {
	physics := processor.DefaultPhysics()
	current_state := physics.NewGameState()
	response_channel := make(chan state.StateResponse, 100)

	physics.TurnCommandModifier = 1.0
	physics.AccelerationCommandModifier = 1.0
	factory := processor.NewFactory(&physics)

	manager := NewManager(current_state, response_channel, &factory)

	Convey("Pause", t, func() {
		manager.Pause()
		So(manager.IsPaused(), ShouldEqual, true)
	})

	Convey("AddCommand", t, func() {
		So(len(manager.commandsToProcess), ShouldEqual, 0)
		connect := cmd.NewConnect("tester")
		gamecommand := cmd.GameCommand(&connect)
		manager.AddCommand(gamecommand)
		So(len(manager.commandsToProcess), ShouldEqual, 1)
	})

	Convey("Removed On tick", t, func() {
		So(len(manager.commandsToProcess), ShouldEqual, 1)
		manager.tick()
		So(len(manager.commandsToProcess), ShouldEqual, 0)
	})

}
func TestFindVehicle(t *testing.T) {

	physics := processor.DefaultPhysics()
	Convey("Find Vehicle", t, func() {
		a := physics.NewGameState()

		So(a.Vehicles, ShouldBeEmpty)
		a.Vehicles = make([]*state.Vehicle, 4)
		a.Vehicles[0] = &(state.Vehicle{
			Point: state.NewPoint(1, 0),
			Owner: "me"})
		a.Vehicles[1] = &(state.Vehicle{
			Point: state.NewPoint(2, 0),
			Owner: "you"})
		a.Vehicles[2] = &(state.Vehicle{
			Point: state.NewPoint(3, 0),
			Owner: "austin"})
		a.Vehicles[3] = &(state.Vehicle{
			Point: state.NewPoint(4, 0),
			Owner: "abc"})
		So(a.GetVehicle("me").X, ShouldEqual, 1)
		So(a.GetVehicle("you").X, ShouldEqual, 2)
		So(a.GetVehicle("austin").X, ShouldEqual, 3)
		So(a.GetVehicle("abc").X, ShouldEqual, 4)
		So(a.GetVehicle("gz"), ShouldEqual, nil)

	})
}
func TestRoute(t *testing.T) {
	ack_channel := make(chan state.Ack, 100)
	state_channel := make(chan state.StateResponse, 100)

	physics := processor.DefaultPhysics()
	factory := processor.CommandProcessorFactory{Physics: &physics}

	gameManager := NewManager(physics.NewGameState(), state_channel, &factory)

	router := CommandRouter{Acks: ack_channel, GameManager: &gameManager}
	address, err := net.ResolveUDPAddr("udp", "127.0.0.1:8080")

	Convey("Route", t, func() {
		So(err, ShouldEqual, nil)
		turn := cmd.NewConnect("test")
		cmd := cmd.GameCommand(&turn)

		So(len(gameManager.commandsToProcess), ShouldEqual, 0)

		router.RouteCommand(&cmd, address)
		resp := <-ack_channel

		So(resp.Address, ShouldEqual, address)
		So(len(gameManager.commandsToProcess), ShouldEqual, 1)
	})
}
func TestConnection(t *testing.T) {

	ack_channel := make(chan state.Ack, 100)
	state_channel := make(chan state.StateResponse, 5)
	physics := processor.DefaultPhysics()
	factory := processor.NewFactory(&physics)
	manager := game.NewManager(physics.NewGameState(), state_channel, &factory)
	router := game.CommandRouter{GameManager: &manager, Acks: ack_channel}
	go manager.Start()

	reciever := network.UdpReceiver{
		Router:     router,
		Acks:       ack_channel,
		Responses:  state_channel,
		PortNumber: ":10002"}

	reciever.Run()
	conn, err := net.DialTimeout("udp", "127.0.0.1:10002", 1*time.Second)
	Convey("Test connect", t, func() {
		So(err, ShouldEqual, nil)
		So(conn, ShouldNotEqual, nil)
	})

	Convey("Test Connect command", t, func() {
		a := cmd.NewConnect("Dan")
		a.UniqueId = "abc123"
		message, err := json.Marshal(a)

		So(err, ShouldEqual, nil)

		i, error := conn.Write(message)

		So(i, ShouldBeGreaterThan, 0)
		So(error, ShouldEqual, nil)
	})

	Convey("Test State Responder", t, func() {
		reciever.Responses <- state.StateResponse{State: physics.NewGameState()}
		response := make([]byte, 2048)
		// read response
		conn.SetDeadline(time.Now().Add(2 * time.Second))
		n, err := bufio.NewReader(conn).Read(response)
		So(err, ShouldEqual, nil)
		So(string(response[:n]), ShouldEqual, "abc123")

		conn.SetDeadline(time.Now().Add(2 * time.Second))
		n, err = bufio.NewReader(conn).Read(response)
		So(err, ShouldEqual, nil)

		encoder := encoder.JsonEncoderDecoder{}

		_, encode_error := encoder.Decode(response[:n])
		So(encode_error, ShouldEqual, nil)

	})

}
Example #7
0
func main() {

	/*
		f, err := os.OpenFile("testlogfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
		if err != nil {
			//f.Fatalf("error opening file: %v", err)
		}
		defer f.Close()

		log.SetOutput(f)
		log.Println("This is a test log entry")
	*/
	var waiter sync.WaitGroup
	waiter.Add(1)
	log.Println("Server starting up")
	decoder := encoder.JsonEncoderDecoder{}
	log.Println("Encoder created")

	ackChannel := make(chan state.Ack, 100)
	log.Println("Acking channel created")

	stateChannel := make(chan state.StateResponse)
	log.Println("State channel created")

	physics := processor.DefaultPhysics()
	log.Println("Default Physics created")

	factory := processor.NewFactory(&physics)
	log.Println("Command Processor Factory created")

	current_state := physics.NewGameState()

	gameManager := game.NewManager(current_state, stateChannel, &factory)
	log.Println("Gamemanager created")

	router := game.CommandRouter{Acks: ackChannel, GameManager: &gameManager}
	log.Println("Router created")

	receiver := network.UdpReceiver{PortNumber: ":10001",
		MaxPacket:      8192,
		EncoderDecoder: &decoder,
		Router:         router,
		Acks:           ackChannel,
		Responses:      stateChannel}

	log.Println("Udp reciever created")
	go gameManager.Start()
	log.Println("Gamemanager started")

	receiver.Run()
	log.Println("Udp reciever running, press ctr+c to shutdown")

	waiter.Wait()
	log.Println("Shutting down")

}
func TestAccelerationProcessor(t *testing.T) {
	physics := processor.DefaultPhysics()
	physics.TurnCommandModifier = 1.0
	physics.AccelerationCommandModifier = 1.0
	acceleration_processor := processor.AccelerationCommandProcessor{Physics: &physics}

	c := cmd.NewAcceleration(.5)
	c2 := cmd.NewAcceleration(.2)
	c.UserId = "abc123"
	c2.UserId = "abc123"

	accelerate := cmd.GameCommand(&c)
	accelerate2 := cmd.GameCommand(&c2)

	game_state := physics.NewGameState()

	game_state.Vehicles = append(game_state.Vehicles, &(state.Vehicle{Owner: "abc123", Velocity: 0, IsAlive: true}))

	Convey("Acceleration Processor", t, func() {

		So(len(game_state.Vehicles), ShouldEqual, 1)
		So(game_state.Vehicles[0].Velocity, ShouldAlmostEqual, 0, .0001)

		acceleration_processor.Run(&game_state, accelerate)
		So(game_state.Vehicles[0].Velocity, ShouldAlmostEqual, .5, .0001)

		acceleration_processor.Run(&game_state, accelerate2)
		So(game_state.Vehicles[0].Velocity, ShouldAlmostEqual, .7, .0001)

		accelerate2.Command().UserId = "blach"
		acceleration_processor.Run(&game_state, accelerate2)
		So(game_state.Vehicles[0].Velocity, ShouldAlmostEqual, .7, .0001)

	})

	Convey("Test Max Speed", t, func() {
		c.Value = 10000000
		acceleration_processor.Run(&game_state, accelerate)

		So(game_state.Vehicles[0].Velocity, ShouldBeLessThanOrEqualTo, physics.MaxVehicleVelocity)
	})

	Convey("Test Max  negative Speed", t, func() {
		c.Value = -10000000
		acceleration_processor.Run(&game_state, accelerate)

		So(game_state.Vehicles[0].Velocity, ShouldBeGreaterThanOrEqualTo, -1*physics.MaxVehicleVelocity)
	})
}
func TestTurnProcessor(t *testing.T) {
	Convey("Turn Processor", t, func() {
		physics := processor.DefaultPhysics()
		physics.TurnCommandModifier = 1.0
		physics.AccelerationCommandModifier = 1.0
		conn_processor := processor.TurnCommandProcessor{Physics: &physics}

		c := cmd.NewTurn(.5)
		c2 := cmd.NewTurn(.2)
		c.UserId = "abc123"
		c2.UserId = "abc123"

		turn := cmd.GameCommand(&c)
		turn2 := cmd.GameCommand(&c2)

		game_state := physics.NewGameState()

		game_state.Vehicles = append(game_state.Vehicles, &(state.Vehicle{Owner: "abc123", Velocity: 0, IsAlive: true}))

		So(len(game_state.Vehicles), ShouldEqual, 1)
		So(game_state.Vehicles[0].Angle, ShouldAlmostEqual, 0, .0001)

		conn_processor.Run(&game_state, turn)
		So(game_state.Vehicles[0].Angle, ShouldAlmostEqual, 359.5, .0001)

		conn_processor.Run(&game_state, turn2)
		So(game_state.Vehicles[0].Angle, ShouldAlmostEqual, 359.3, .0001)

		turn2.Command().UserId = "blach"
		conn_processor.Run(&game_state, turn2)
		So(game_state.Vehicles[0].Angle, ShouldAlmostEqual, 359.3, .0001)

		//check rollover
		conn_processor.Run(&game_state, turn)
		So(game_state.Vehicles[0].Angle, ShouldAlmostEqual, 358.8, .0001)

		// check rollover
		c.Value = -1.4
		conn_processor.Run(&game_state, turn)
		So(game_state.Vehicles[0].Angle, ShouldAlmostEqual, .2, .0001)

	})
}
func TestStateCopy(t *testing.T) {
	physics := processor.DefaultPhysics()
	Convey("Copy State", t, func() {
		a := physics.NewGameState()

		a.Rockets = append(a.Rockets, &state.Rocket{})

		a.Vehicles = make([]*state.Vehicle, 4)
		a.Vehicles[0] = &(state.Vehicle{
			Point:   state.NewPoint(1, 0),
			Owner:   "me",
			IsAlive: true})

		a.Vehicles[1] = &(state.Vehicle{
			Point:   state.NewPoint(1, 0),
			Owner:   "you",
			IsAlive: true})

		a.Vehicles[2] = &(state.Vehicle{
			Point:   state.NewPoint(1, 0),
			Owner:   "austin",
			IsAlive: true})
		a.Vehicles[3] = &(state.Vehicle{
			Point:   state.NewPoint(1, 0),
			Owner:   "abc",
			IsAlive: true})

		a.Bullets = make([]*state.Bullet, 4)
		a.Bullets[0] = &(state.Bullet{})
		a.Bullets[1] = &(state.Bullet{})
		a.Bullets[2] = &(state.Bullet{})
		a.Bullets[3] = &(state.Bullet{})

		theCopy := a.Copy()
		So(len(theCopy.Vehicles), ShouldEqual, len(a.Vehicles))
		So(len(theCopy.Bases), ShouldEqual, len(a.Bases))
		So(len(theCopy.ShieldGenerators), ShouldEqual, len(a.ShieldGenerators))
		So(len(theCopy.Rockets), ShouldEqual, len(a.Rockets))
		So(theCopy.GameOver, ShouldEqual, a.GameOver)
	})
}
func TestControl(t *testing.T) {
	physics := processor.DefaultPhysics()
	current_state := physics.NewGameState()
	response_channel := make(chan state.StateResponse, 100)

	physics.TurnCommandModifier = 1.0
	physics.AccelerationCommandModifier = 1.0
	factory := processor.NewFactory(&physics)

	manager := NewManager(current_state, response_channel, &factory)
	go manager.Start()

	Convey("Pause", t, func() {
		manager.Pause()
		So(manager.IsPaused(), ShouldEqual, true)
	})

	Convey("Resume", t, func() {
		manager.Resume()
		So(manager.IsPaused(), ShouldEqual, false)
	})

}
func TestFireProcessor(t *testing.T) {
	Convey("Fire Processor", t, func() {
		physics := processor.DefaultPhysics()
		physics.TurnCommandModifier = 1.0
		physics.AccelerationCommandModifier = 1.0
		fireProcessor := processor.FireCommandProcessor{Physics: &physics}
		connectProcessor := processor.ConnectCommandProcessor{Physics: &physics}

		fireCommand := cmd.NewFire()
		fireCommand.UserId = "abc"

		connectCommand := cmd.NewConnect("abc")
		connectCommand.UserId = "abc"

		fire := cmd.GameCommand(&fireCommand)
		connect := cmd.GameCommand(&connectCommand)

		game_state := physics.NewGameState()
		So(len(game_state.Bullets), ShouldEqual, 0)
		So(len(game_state.Vehicles), ShouldEqual, 0)

		connectProcessor.Run(&game_state, connect)
		So(len(game_state.Vehicles), ShouldEqual, 1)

		fireProcessor.Run(&game_state, fire)
		So(len(game_state.Bullets), ShouldEqual, 1)

		badFireCommand := cmd.NewFire()
		badFireCommand.UserId = "nobody"
		badFire := cmd.GameCommand(&badFireCommand)

		fireProcessor.Run(&game_state, badFire)
		So(len(game_state.Bullets), ShouldEqual, 1)

	})
}