Ejemplo n.º 1
0
func (c *Command) Execute(message []byte) {

	if !c.connection.GetServerAuthState() {
		return
	}

	var commandDetector CommandDetector
	json.Unmarshal(message, &commandDetector)

	var isLock bool = false

	userUUID, err := gocql.ParseUUID(commandDetector.UserUUID)
	if err != nil {

	} else {

		user, err := model_user.Get(userUUID)
		logger.String(fmt.Sprintf("commandDetector.UserUUID %+v, user %+v", commandDetector.UserUUID, user))
		if err == nil && user != nil && user.IsLock {
			isLock = true
		}
	}

	b, _ := json.Marshal(map[string]interface{}{
		"command":    "answer",
		"command_id": commandDetector.CommandId,
		"is_lock":    isLock,
	})

	c.connection.Send(string(b))
}
Ejemplo n.º 2
0
func (c *Command) Execute(message []byte) {

	session := c.connection.GetSession()

	if session == nil || !session.IsAuth {
		return
	}

	var commandDetector CommandDetector
	json.Unmarshal(message, &commandDetector)

	answer := model.Fields{
		"command_id": commandDetector.CommandId,
	}

	func() {

		user, _ := model_user.Get(session.UserUUID)
		if user == nil {
			return
		}

		// answer["user"] = true

		player, _ := model_player.Get(*user.PlayerUUID)

		if player == nil {
			return
		}

		// answer["player"] = true

		planet, _ := model_live_planet.Get(player.CapitalPlanetUUID)
		if planet == nil {
			return
		}

		planet.Connection = c.connection

		// answer["planet"] = true

		answer["planet_info"] = planet.MakeClientInfo()
	}()

	b, _ := json.Marshal(answer)

	c.connection.Send(string(b))
}
Ejemplo n.º 3
0
func (c *Connection) Reading() {

	c.ws.SetReadLimit(MaxMessageSize)
	c.ws.SetReadDeadline(time.Now().Add(PongWait))	

	for {
		_, message, err := c.ws.ReadMessage()
		if err != nil {
			if err != io.EOF && err.Error() != "websocket: close 1005 " {
				logger.Error(errors.New(err))
			}
			break
		}
		smessage := string(message)

		if smessage == `{"command":"ping"}` {
			c.Send(`{"command":"pong"}`)
			continue
		} else {
			logger.String(fmt.Sprintf("message: %v", smessage))
		}

		commandDetector := CommandDetector{}
		err = json.Unmarshal(message, &commandDetector)
		if err != nil {
			logger.Error(errors.New(err))
			continue
		}

		generator, ok := c.commands[commandDetector.Command]
		if ok {
			command := generator(c, c.commandContext)
			go timeWrapper(commandDetector.Command, command, message)
		}
	}

	if c.SessionExists {
		if c.Session.IsAuth {
			user, _ := model2_user.Get(c.Session.UserUUID)
			if user != nil {
				user.Unlock()
			}			
		}
		c.Session.Unlock()

	}

}
Ejemplo n.º 4
0
func (c *Connection) UnAuth() {

	if c.SessionExists && c.Session.IsAuth {

		user, _ := model2_user.Get(c.Session.UserUUID)
		if user != nil {
			user.Unlock()
		}

		
		c.Session.Update(model2.Fields{
			"IsAuth": false,
			"UserUUID": nil,
			"AuthMethod": nil,
		})

		c.Send(`{"command":"reload"}`)
	}

}
Ejemplo n.º 5
0
func (c *Command) Execute(message []byte) {

	session := c.connection.GetSession()

	if session == nil || !session.IsAuth {
		return
	}

	var commandDetector CommandDetector
	json.Unmarshal(message, &commandDetector)

	/*
		answer := model2.Fields{
			"command_id": commandDetector.CommandId,
		}
	*/

	func() {

		user, _ := model_user.Get(session.UserUUID)
		if user == nil {
			return
		}

		player, _ := model_player.Get(*user.PlayerUUID)
		if player == nil {
			return
		}

		planet, _ := model_live_planet.Get(player.CapitalPlanetUUID)
		if planet == nil {
			return
		}

		c.ctx.BDispatcher.Build(&planet.UUID, commandDetector.Building, int(commandDetector.X), int(commandDetector.Y))

	}()

}
Ejemplo n.º 6
0
func (c *Command) Execute(message []byte) {

	session := c.connection.GetSession()

	if session == nil || !session.IsAuth {
		return
	}

	var commandDetector CommandDetector
	json.Unmarshal(message, &commandDetector)

	user, _ := model_user.Get(session.UserUUID)
	if user == nil {
		user, _ = model_user.Create()
		user.UUID = session.UserUUID
		user.Load()
	}

	user.Update(model2.Fields{
		"SectionName": commandDetector.SectionName,
	})

}
Ejemplo n.º 7
0
func (c *Command) Execute(message []byte) {

	session := c.connection.GetSession()

	if session == nil || !session.IsAuth {
		return
	}

	var commandDetector CommandDetector
	json.Unmarshal(message, &commandDetector)

	user, _ := model_user.Get(session.UserUUID)
	if user == nil {
		return
	}

	/*
		player := model_player.New()
		player.Create()
		player.Lock()
	*/

	player, _ := model_player.Create()

	player.Update(model2.Fields{
		"UserUUID": user.UUID,
	})

	user.Update(model2.Fields{
		"PlayerUUID": player.UUID,
	})

	// create live planet

	/*
		livePlanet := model_live_planet.New()
		livePlanet.Create()
	*/
	livePlanet, _ := model_live_planet.Create()

	player.Update(model2.Fields{
		"CapitalPlanetUUID": livePlanet.UUID,
		"Planets":           []gocql.UUID{livePlanet.UUID},
	})

	/*
		building := model_building.New()
		building.Create()
	*/
	building, _ := model_building.Create()

	building.Update(model2.Fields{
		"Type":       "capital",
		"Level":      1,
		"TurnOn":     true,
		"TurnOnTime": 0,
		"X":          0,
		"Y":          0,
	})

	livePlanet.Update(model2.Fields{
		"PlayerUUID":      player.UUID,
		"TreatTime":       time.Now().UnixNano(),
		"Buildings":       []gocql.UUID{building.UUID},
		"Population":      600,
		"PopulationSInc":  0,
		"PopulationUsage": 0,
		"PopulationAvail": 600,
		"Energy":          0,
		"EnergyAvail":     0,
		"Crystals":        3000,
		"CrystalsSInc":    0,
		"Minerals":        5000,
		"MineralsSInc":    0,
		"QueueBuildType":  []string{},
		"QueueBuildX":     []int{},
		"QueueBuildY":     []int{},
	})

	// logger.String(fmt.Sprintf("%+v", user))

	c.connection.Send(fmt.Sprintf(`{"command_id":%d}`, commandDetector.CommandId))
}