func (ship *Ship) LoadLocation(db *loge.LogeDB) bool { var body = db.ReadOne("shiplocation", loge.LogeKey(ship.ID)).(*simulation.PoweredBody) if body == nil { return false } ship.Location = body return true }
func (manager *SectorManager) Create(x int64, y int64, name string) (*Sector, bool) { var coords = SectorCoords{X: x, Y: y} var key = coords.String() sector, ok := manager.Sectors[key] if ok { fmt.Printf("Sector exists %s\n", key) return sector, false } var db = manager.context.DB() var success = false db.Transact(func(t *loge.Transaction) { if !t.Exists("sector", loge.LogeKey(key)) { sector = &Sector{ Coords: coords, Name: name, } t.Set("sector", loge.LogeKey(key), sector) success = true } }, 0) fmt.Printf("Create success: %v\n", success) if success { return sector, true } return nil, false }
func (ship *Ship) SaveLocation(db *loge.LogeDB) { if ship.Location == nil { return } db.SetOne("shiplocation", loge.LogeKey(ship.ID), ship.Location) }
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 }
func GetShip(body *simulation.PoweredBody, db *loge.LogeDB) *Ship { var ship = db.ReadOne("ship", loge.LogeKey(body.ShipID)).(*Ship) if ship == nil { return nil } ship.Location = body return ship }
func CreateAccount(name string, password string, context DriftServerContext) (*Account, bool) { var db = context.DB() var account *Account = NewAccount(name, password) var success bool db.Transact(func(t *loge.Transaction) { if !t.Exists("account", loge.LogeKey(name)) { t.Set("account", loge.LogeKey(name), account) success = true } }, 0) if success { return account, true } return nil, false }
func (sector *Sector) LoadShips() { var db = sector.manager.context.DB() var sectorKey = loge.LogeKey(sector.StorageKey()) var keys = db.Find("shiplocation", "sector", sectorKey) fmt.Printf("Loading %d ships...\n", len(keys)) var start = time.Now() var shipsLoaded = 0 for _, key := range keys { var ship = db.ReadOne("ship", loge.LogeKey(key)).(*ships.Ship) if ship == nil { fmt.Printf("Orphan body: %v\n", key) continue } ship.LoadLocation(db) sector.ShipsByID[ship.ID] = ship shipsLoaded++ } fmt.Printf("Loaded %d ships in %v\n", shipsLoaded, time.Since(start)) }
func (manager *SectorManager) Ensure(x int64, y int64) (*Sector, bool) { manager.Mutex.Lock() defer manager.Mutex.Unlock() var coords = SectorCoords{X: x, Y: y} var key = coords.String() sector, ok := manager.Sectors[key] if !ok { fmt.Printf("Loading sector %s\n", key) sector = manager.context.DB().ReadOne("sector", loge.LogeKey(key)).(*Sector) if sector == nil { fmt.Printf("No such sector: %d, %d\n", x, y) return nil, false } sector.Populate(manager) manager.Sectors[key] = sector sector.Start() } return sector, true }
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() // } }