예제 #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)

	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))
}
예제 #2
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)

	}
}