Ejemplo n.º 1
0
// A very basic TCP client, for testing purposes
func main() {
	trn := terrain.New()

	ctx := message.NewClientContext()
	ctx.Terrain = trn
	ctx.Clock = clock.NewService()

	hdlr := handler.New(ctx)

	tlsConfig, err := crypto.GetClientTlsConfig()
	if err != nil {
		log.Println("WARN: could not load TLS config")
	}

	var c net.Conn
	if tlsConfig != nil {
		c, err = tls.Dial("tcp", "127.0.0.1:9999", tlsConfig)
	} else {
		c, err = net.Dial("tcp", "127.0.0.1:9999")
	}

	if err != nil {
		panic("Dial: " + err.Error())
	}

	reader := bufio.NewReader(c)
	clientIO := &message.IO{
		Reader: reader,
		Writer: c,
	}

	go hdlr.Listen(clientIO)

	/*err = builder.SendPing(clientIO.Writer)
	if err != nil {
		panic("SendPing: " + err.Error())
	}*/

	err = builder.SendLogin(clientIO.Writer, "root", "root")
	if err != nil {
		panic("SendLogin: "******"Hello World!")
	if err != nil {
		panic("SendChatSend: " + err.Error())
	}*/

	/*err = c.Close()
	if err != nil {
		panic("Close: " + err.Error())
	}*/
}
Ejemplo n.º 2
0
func New(srv *server.Server, timeSrv *timeserver.Server) *Engine {
	// Create a new context
	ctx := message.NewServerContext()

	// Create the engine
	e := &Engine{
		ctx:     ctx,
		srv:     srv,
		timeSrv: timeSrv,
		auth:    auth.NewService(),
		clock:   clock.NewService(),
		entity:  entity.NewService(),
		action:  action.NewService(),
		terrain: terrain.New(),
		config:  game.DefaultConfig(),

		clients:        make(map[int]*server.Client),
		brdStop:        make(chan bool),
		listenStop:     make(chan bool),
		listenTimeStop: make(chan bool),
	}

	// Set config
	if timeSrv != nil {
		e.config.TimeServerPort = uint16(timeSrv.Port())
	}

	// Populate context
	ctx.Auth = e.auth
	ctx.Entity = e.entity.Frontend()
	ctx.Action = e.action.Frontend()
	ctx.Terrain = e.terrain
	ctx.Clock = e.clock
	ctx.Config = e.config

	// Initialize engine submodules
	e.mover = NewMover(e)

	// Set callbacks
	e.auth.LoginCallback = func(session *message.Session) {
		entity := session.Entity

		// Populate entity
		// TODO: default values are hardcoded
		entity.Position.BX = 20
		entity.Position.BY = 20
		entity.Type = game.PlayerEntity
		entity.Sprite = game.PlayerSprite
		entity.Attributes[game.HealthAttr] = game.Health(1000)
		entity.Attributes[game.CooldownOneAttr] = game.Cooldown(0)
	}

	return e
}
Ejemplo n.º 3
0
func TestService(t *testing.T) {
	s := clock.NewService()

	if s.GetAbsoluteTick() != 0 {
		t.Error("Absolute tick not initialized at 0")
	}
	if s.GetRelativeTick() != 0 {
		t.Error("Relative tick not initialized at 0")
	}

	ticks := 42
	for i := 0; i < ticks; i++ {
		s.Tick()
	}

	if int(s.GetAbsoluteTick()) != ticks {
		t.Error("Absolute tick not incremented, expected", ticks, "but got", s.GetAbsoluteTick())
	}
	if int(s.GetRelativeTick()) != ticks {
		t.Error("Relative tick not incremented, expected", ticks, "but got", s.GetRelativeTick())
	}

	// |--------------------|------------>
	//       ^     ^
	//     tick   now
	relTick := message.Tick(ticks - 6)
	absTick := s.ToAbsoluteTick(relTick)
	if int(absTick) != int(relTick) {
		t.Error("Invalid absolute tick conversion, expected", relTick, "but got", absTick)
	}

	// |--------------------|------------>
	//             ^     ^
	//            now  tick
	// Invalid tick: in the future
	relTick = message.Tick(9304)
	absTick = s.ToAbsoluteTick(relTick)
	if absTick != 0 {
		t.Error("Invalid absolute tick conversion, expected 0 as it is an invalid tick, but got", absTick)
	}

	for i := 0; i < message.MaxTick; i++ {
		s.Tick()
	}

	if int(s.GetAbsoluteTick()) != ticks+message.MaxTick {
		t.Error("Invalid absolute tick after message.MaxTick ticks, expected", 1+message.MaxTick, "but got", s.GetAbsoluteTick())
	}
	if int(s.GetRelativeTick()) != ticks {
		t.Error("Invalid relative tick after message.MaxTick ticks, expected", 1, "but got", s.GetRelativeTick())
	}

	// |--------------------|------------>
	//                          ^     ^
	//                        tick   now
	relTick = message.Tick(6)
	absTick = s.ToAbsoluteTick(relTick)
	if int(absTick) != message.MaxTick+6 {
		t.Error("Invalid absolute tick conversion, expected", message.MaxTick+6, "but got", absTick)
	}

	// |--------------------|------------>
	//                 ^               ^
	//               tick             now
	relTick = message.Tick(message.MaxTick - 42)
	absTick = s.ToAbsoluteTick(relTick)
	if int(absTick) != int(relTick) {
		t.Error("Invalid absolute tick conversion, expected", relTick, "but got", absTick)
	}
}