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 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)
	})
}
예제 #3
0
func TestAcceleration(t *testing.T) {
	Convey("Acceleration Command", t, func() {
		acceleration := cmd.NewAcceleration(.4)
		So(acceleration.UniqueId, ShouldEqual, "")
		So(acceleration.Subtype, ShouldEqual, cmd.Acceleration)
		So(acceleration.Type, ShouldEqual, cmd.Post)
		So(acceleration.Value, ShouldAlmostEqual, .4, .00001)
		So(acceleration.Type, ShouldEqual, acceleration.Command().Type)
		So(acceleration.Subtype, ShouldEqual, acceleration.Command().Subtype)
		So(acceleration.UniqueId, ShouldEqual, acceleration.Command().UniqueId)
	})
}
func main() {
	var waiter sync.WaitGroup
	waiter.Add(1)
	//get connection to host and sets a timeout for connection when completed
	conn, err := net.DialTimeout("udp", host, 1*time.Second)

	// start goroutine  to listen for messages
	go responsePrinter(conn)

	for i := 0; i < 12; i++ {

		if err != nil {
			log.Print(err)
		} else {
			var message []byte

			// creates unique identifier to be returned for the packet ack
			uuid := uuid.NewUUID().String()
			if i == 0 {
				a := cmd.NewConnect("Dan")
				a.UniqueId = uuid
				message, err = json.Marshal(a)
			} else {
				// randomly select between commands
				randType := rand.Intn(2)
				if randType == 0 {
					a := cmd.NewTurn(.5)
					a.UniqueId = uuid
					message, err = json.Marshal(a)
				}
				if randType == 1 {
					a := cmd.NewAcceleration(.5)
					a.UniqueId = uuid
					message, err = json.Marshal(a)
				}
			}

			//send the message
			conn.Write(message)

			time.Sleep(1 * time.Millisecond)
		}
	}
	waiter.Wait()

	//clean up connection
	conn.Close()

}
func TestDecodeAcceleration(t *testing.T) {
	Convey("Acceleration Decode", t, func() {
		formatter := encoder.JsonEncoderDecoder{Tag: "DecodeTest"}

		data := cmd.NewAcceleration(.2)
		data.UniqueId = "ABC123"
		buffer, error := json.Marshal(data)

		command, err := formatter.Decode(buffer)

		acceleration := command.(*cmd.AccelerationCommand)

		So(error, ShouldEqual, nil)
		So(acceleration.Command().Type, ShouldEqual, cmd.Post)
		So(acceleration.Command().Subtype, ShouldEqual, cmd.Acceleration)
		So(err, ShouldEqual, nil)
		So(acceleration.Value, ShouldAlmostEqual, .2, .000001)
		So(acceleration.Command().UniqueId, ShouldEqual, "ABC123")
	})
}