Пример #1
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
}
Пример #2
0
func TestService(t *testing.T) {
	s := auth.NewService()

	loginCode := s.Login(0, username, password)
	if loginCode != message.LoginResponseCodes["unknown_pseudo"] {
		t.Error("Bad response code after wrong login attempt:", loginCode)
	}

	registerCode := s.Register(0, username, password)
	if registerCode != message.RegisterResponseCodes["ok"] {
		t.Error("Bad register response code:", registerCode)
	}

	registerCode = s.Register(0, username, "otherpass")
	if registerCode != message.RegisterResponseCodes["used_pseudo"] {
		t.Error("Bad response code after wrong register attempt:", registerCode)
	}

	loginCode = s.Login(0, username, "otherpass")
	if loginCode != message.LoginResponseCodes["wrong_password"] {
		t.Error("Bad response code after wrong login attempt:", loginCode)
	}

	loginCode = s.Login(0, username, password)
	if loginCode != message.LoginResponseCodes["ok"] {
		t.Error("Bad login response code:", loginCode)
	}

	session := s.GetSession(0)
	if session == nil {
		t.Error("Could not get session after login")
	}

	s.Logout(0)

	session = s.GetSession(0)
	if session != nil {
		t.Error("Session not flushed after logout")
	}
}