예제 #1
0
// Return true if ok
func (up *user) Load_WLwBlWLc(email string) bool {
	// Connect to database
	db := ephenationdb.New()
	if db == nil {
		log.Println("No DB cpnnection", email)
		return false
	}
	err := db.C("avatars").Find(bson.M{"email": email}).One(&up.UserLoad)
	if err != nil {
		log.Println("Avatar for", email, err)
		return false
	}

	// Some post processing

	if up.Maxchunks == 0 {
		// This parameter was not initialized.
		up.Maxchunks = CnfgMaxOwnChunk
	}

	up.logonTimer = time.Now()

	if up.ReviveSP.X == 0 && up.ReviveSP.Y == 0 && up.ReviveSP.Z == 0 {
		// Check if there is any spawn point defined.
		up.ReviveSP = up.Coord
		up.HomeSP = up.Coord
	}

	score.Initialize(up.Id)
	return true
}
예제 #2
0
// Return true if ok, and the uid of the player.
// TODO: Improve error handlng
func (pl *player) Load_WLwBlWLc(name string) (uint32, bool) {
	// Connect to database
	db := ephenationdb.New()
	if db == nil {
		return 0, false
	}
	defer ephenationdb.Release(db)

	// Build a query for the avatar name sent as an argument
	// TODO: Assert that the avatar name is unique and on this server for the current user?
	query := "SELECT jsonstring,id,PositionX,PositionY,PositionZ,isFlying,isClimbing,isDead,DirHor,DirVert,AdminLevel,Level,Experience,HitPoints,Mana,Kills,HomeX,HomeY,HomeZ,ReviveX,ReviveY,ReviveZ,maxchunks,BlocksAdded,BlocksRemoved,TimeOnline,HeadType,BodyType,inventory,TScoreTotal,TScoreBalance,TScoreTime,TargetX,TargetY,TargetZ FROM avatars WHERE name='" + name + "'"
	stmt, err := db.Prepare(query)
	if err != nil {
		log.Println(err)
		return 0, false
	}

	// Execute statement
	err = stmt.Execute()
	if err != nil {
		log.Println(err)
		return 0, false
	}

	// Some helper variables
	var packedline string
	var uid uint32
	var packedInv []byte
	var terrScore, terrScoreBalance float64
	var terrScoreTimestamp uint32
	// Booleans doesn't work
	var flying, climbing, dead int
	stmt.BindResult(&packedline, &uid, &pl.coord.X, &pl.coord.Y, &pl.coord.Z, &flying, &climbing, &dead, &pl.dirHor, &pl.dirVert, &pl.adminLevel, &pl.level,
		&pl.exp, &pl.hitPoints, &pl.mana, &pl.numKill, &pl.homeSP.X, &pl.homeSP.Y, &pl.homeSP.Z, &pl.reviveSP.X, &pl.reviveSP.Y, &pl.reviveSP.Z, &pl.maxchunks,
		&pl.blockAdd, &pl.blockRem, &pl.timeOnline, &pl.head, &pl.body, &packedInv, &terrScore, &terrScoreBalance, &terrScoreTimestamp,
		&pl.targetCoor.X, &pl.targetCoor.Y, &pl.targetCoor.Z)

	for {
		eof, err := stmt.Fetch()
		if err != nil {
			log.Println(err)
			return 0, false
		}
		if eof {
			break
		}
	}

	// log.Println(pl.targetCoor)

	// Some post processing
	pl.name = name
	if flying == 1 {
		pl.flying = true
	}
	if climbing == 1 {
		pl.climbing = true
	}
	if dead == 1 {
		pl.dead = true
	}

	if pl.maxchunks == -1 {
		// This parameter was not initialized.
		pl.maxchunks = CnfgMaxOwnChunk
	}

	pl.logonTimer = time.Now()

	if err = json.Unmarshal([]uint8(packedline), pl); err != nil {
		log.Printf("Unmarshal player %s: %v (%v)\n", name, err, packedline)
		// TODO: This covers errors when updating the jsonstring, should be handled in a more approperiate way
		//return 0, false
	}

	// If there was data in the inventory "blob", unpack it.
	if len(packedInv) > 0 {
		err = pl.inventory.Unpack([]byte(packedInv))
		if err != nil {
			log.Println("Failed to unpack", err, packedInv)
		}
		// Save what can be saved, and remove unknown objects.
		pl.inventory.CleanUp()
	}
	if *verboseFlag > 1 {
		log.Println("Inventory unpacked", pl.inventory)
	}

	//fmt.Printf("Coord: (%v,%v,%v)\n", pl.coord.X, pl.coord.Y, pl.coord.Z )

	if pl.reviveSP.X == 0 && pl.reviveSP.Y == 0 && pl.reviveSP.Z == 0 {
		// Check if there is any spawn point defined.
		pl.reviveSP = pl.coord
		pl.homeSP = pl.coord
	}

	// Load the allocated territories. This is loaded every time a player logs in, but not at logout or player save.
	// It will however be updated immediately when the player changes his allocation.
	terr, ok := chunkdb.ReadAvatar_Bl(uint32(uid))
	if !ok {
		return 0, false
	}
	pl.territory = terr
	score.Initialize(uid, terrScore, terrScoreBalance, terrScoreTimestamp, name, len(terr))
	// fmt.Printf("User: %#v\n", pl)
	return uint32(uid), true
}