Exemple #1
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))
}
Exemple #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 := 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))

	}()

}
Exemple #3
0
func (w *Worker) Loop() {

	for {

		mo := <-w.C
		m := &mo

		// todo: lock planet
		planet, _ := model_live_planet.Get(*m.PlanetUUID)
		lockPlanet(planet)

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

		switch m.Type {

		case MT_Build:
			w.MT_BuildProc(planet, m)

		case MT_TurnOn:

			logger.String(fmt.Sprintf("TurnOnTime: %v", time.Now().UnixNano()))

			buildingUUID := m.Params["*building_uuid"].(*gocql.UUID)
			building, _ := model_building.Get(*buildingUUID)
			building.Update(model2.Fields{
				"TurnOnTime": time.Now().UnixNano(),
			})

		case MT_Treat:

			ptt := planet.TreatTime / 1000000000
			ctt := time.Now().UnixNano() / 1000000000

			for ptt < ctt {

				conn := planet.GetConnection()

				att := planet.TreatTime + 1000000000

				// check for build queue
				for len(planet.QueueBuildType) > 0 {

					btype := planet.QueueBuildType[0]
					x := planet.QueueBuildX[0]
					y := planet.QueueBuildY[0]

					i1 := EnergyStation["levelParams"].([]interface{})
					i2 := i1[0].(map[string]interface{})
					i3 := i2["level_up"]
					i4 := i3.(map[string]interface{})

					costMinerals := i4["minerals"].(int)
					costCrystals := i4["crystals"].(int)
					duration := i4["time"].(int)
					population := i4["pop"].(int)

					building, _ := model_building.Create()

					building.Update(model2.Fields{
						"Type":              btype,
						"Level":             0,
						"TurnOn":            false,
						"TurnOnTime":        0,
						"X":                 x,
						"Y":                 y,
						"UpgradeInProgress": true,
						"UpgradeDuration":   duration,
						"UpgradeElapsed":    0,
						"UpgradePopulation": population,
					})

					planet.Update(model2.Fields{
						"Minerals":        planet.Minerals - float64(costMinerals),
						"Crystals":        planet.Crystals - float64(costCrystals),
						"PopulationAvail": planet.PopulationAvail - float64(population),
						"Buildings":       append(planet.Buildings, building.UUID),
						"QueueBuildType":  planet.QueueBuildType[1:],
						"QueueBuildX":     planet.QueueBuildX[1:],
						"QueueBuildY":     planet.QueueBuildY[1:],
						"BuildInProgress": append(planet.BuildInProgress, building.UUID),
					})

					conn := planet.GetConnection()
					if conn != nil {

						conn.Send(building.NCBuildingUpdate(m.PlanetUUID))
						conn.Send(planet.NCUpdatePlanetResources())

					}

				}

				again := true
				for len(planet.BuildInProgress) > 0 && again {
					again = false
					for i, UUID := range planet.BuildInProgress {
						building, _ := model_building.Get(UUID)
						if building == nil {
							continue
						}

						building.Update(model2.Fields{
							"UpgradeElapsed": building.UpgradeElapsed + 1,
						})

						if building.UpgradeElapsed >= building.UpgradeDuration {

							population := building.UpgradePopulation
							if building.Level == 0 {
								building.Update(model2.Fields{
									"Level":             1,
									"UpgradeInProgress": false,
									"UpgradeElapsed":    0,
									"UpgradeDuration":   0,
									"UpgradePopulation": 0,
									"TurnOnTime":        time.Now().UnixNano(),
								})
							}

							planet.Update(model2.Fields{
								"BuildInProgress": append(planet.BuildInProgress[:i], planet.BuildInProgress[i+1:]...),
								"PopulationAvail": planet.PopulationAvail + float64(population),
								"TurnOnBuildings": append(planet.TurnOnBuildings, building.UUID),
							})

							if conn != nil {

								conn.Send(building.NCBuildingUpdate(m.PlanetUUID))
								conn.Send(planet.NCUpdatePlanetResources())

							}

							again = true
							break

						} else {

							if conn != nil {
								conn.Send(building.NCBuildingUpdate(m.PlanetUUID))
							}

						}
					}
				}

				// 1. check turn on buildings
				again = true
				for len(planet.TurnOnBuildings) > 0 && again {
					again = false
					for i, UUID := range planet.TurnOnBuildings {
						building, _ := model_building.Get(UUID)

						if building.TurnOnTime < att {

							levelInfo := GetBuildingLevelInfo(building.Type, building.Level)
							popUsage, energyUsage := GetBuildingUsage(levelInfo)

							avail := false
							if popUsage <= planet.PopulationAvail && energyUsage <= planet.EnergyAvail {
								avail = true
							}

							if avail {

								building.Update(model2.Fields{
									"TurnOn":     true,
									"TurnOnTime": 0,
								})

								planet.Update(model2.Fields{
									"TurnOnBuildings": append(planet.TurnOnBuildings[:i], planet.TurnOnBuildings[i+1:]...),
									"PopulationAvail": planet.PopulationAvail - float64(popUsage),
									"EnergyAvail":     planet.EnergyAvail - float64(energyUsage),
								})

								// custom building logic
								Treators[building.Type].TurnOn(building, planet)

								if conn != nil {
									conn.Send(building.NCBuildingUpdate(m.PlanetUUID))
									conn.Send(planet.NCUpdatePlanetResources())
								}

								again = true
								break
							} else {
								// send problem turn on

							}

						}

					}
				}

				// custom work

				th := &TreatHint{UpdateResource: false}
				for _, UUID := range planet.Buildings {
					building, _ := model_building.Get(UUID)
					if building == nil || !building.TurnOn {
						continue
					}

					t, ok := Treators[building.Type]
					if ok {
						t.TreatSecond(building, planet, th)
					}
				}

				if th.UpdateResource && conn != nil {
					conn.Send(planet.NCUpdatePlanetResources())
				}

				planet.Update(model2.Fields{
					"TreatTime": planet.TreatTime + 1000000000,
				})

				ptt = planet.TreatTime / 1000000000
			}

			// logger.String(fmt.Sprintf("treat planet %s", m.PlanetUUID.String()))

		}

		// todo: unlock planet
		unlockPlanet(planet)

	}
}