Example #1
0
func method_create(args APIData, session Session, context ServerContext) (bool, APIData) {
	var server = context.(*server.DriftServer)
	var response = make(APIData)

	var user = session.User()

	if user == nil {
		response["message"] = "Not logged in"
		return false, response
	}

	var db = server.DB()
	var id = uuid.New()
	ship := ships.NewShip(id, user.ID(), args["name"].(string))

	sector, ok := server.SectorManager.Ensure(0, 0)

	if !ok {
		response["message"] = "Home sector unavailable"
		return false, response
	}

	db.SetOne("ship", loge.LogeKey(id), ship)

	sector.Warp(ship, true)

	ship.SaveLocation(db)

	response["id"] = ship.ID
	return true, response
}
Example #2
0
func createShips() {
	var server = buildServer()

	var manager = server.SectorManager

	var sector, ok = manager.Ensure(0, 0)

	if !ok {
		sector, ok = manager.Create(0, 0, "Home")
		if !ok {
			fmt.Printf("Sector creation error\n")
			return
		}
	}

	fmt.Printf("Sector: %v\n", sector.Name)

	var db = server.DB()
	var account = db.ReadOne("account", "sandbox").(*accounts.Account)

	if account == nil {
		account, ok = accounts.CreateAccount("sandbox", "password", server)
		if !ok {
			fmt.Printf("User load error\n")
			return
		}
	}

	fmt.Printf("User: %v\n", account.Name)

	var sectorLink = []loge.LogeKey{loge.LogeKey(sector.StorageKey())}
	for i := 0; i < 1000; i++ {
		name := fmt.Sprintf("sandbox%03d", i)
		var id = uuid.New()

		var body = &simulation.PoweredBody{
			ShipID: id,
			Coords: sector.Coords,
		}

		body.Position.X = float64(rand.Intn(1000))
		body.Position.Y = float64(rand.Intn(1000))
		body.Velocity.X = rand.Float64()
		body.Velocity.Y = rand.Float64()
		body.Thrust.X = rand.Float64()
		body.Thrust.Y = rand.Float64()

		var rot = math.Pi / 20
		body.Spin.X = math.Cos(rot)
		body.Spin.Y = math.Sin(rot)

		ship := ships.NewShip(id, account.ID(), name)
		ship.Location = body

		db.Transact(func(t *loge.Transaction) {
			var id = loge.LogeKey(id)
			t.Set("ship", id, ship)
			t.Set("shiplocation", id, ship.Location)
			t.SetLinks("shiplocation", "sector", id, sectorLink)
		}, 0)
	}

	fmt.Printf("Sector: %s (%d, %d)\n", sector.Name, sector.Coords.X, sector.Coords.Y)

	sector.DumpShips()

	// for i := 0; i < 100; i++ {
	// 	sector.Tick()
	// 	sector.DumpShips()
	// }

}