Example #1
0
// update aliases so that the player owns only the aliases passed as
// `aliases` parameter
func (p *PlayerController) UpdatePlayerAliases(player uint, aliases []string) error {
	// repack aliases to map, value true indicates that alias was
	// acted upon
	am := make(map[string]bool)
	for _, a := range aliases {
		am[a] = false
	}

	// grab the list of currently owned aliases
	current := models.GetAliases(p.db, player)

	to_claim := make([]string, 0)
	to_unclaim := make([]string, 0)

	for _, a := range current {
		// if currently owned alias is in map, things don't change
		_, ok := am[a.Alias]
		if ok == false {
			// but if it's not, we unclaim it
			to_unclaim = append(to_unclaim, a.Alias)
		}
		// alias already looked at
		am[a.Alias] = true
	}

	// scan remaining aliases, if alias was not acted upon, means
	// we must claim it
	for k, v := range am {
		if v == false {
			// alias was not acted upon, since we're
			// passing a list of aliases that a player
			// should own, it means that this alias will
			// be claimed by this player
			to_claim = append(to_claim, k)
		}
	}

	// claim new ones
	log.Printf("claim new aliases: %v", to_claim)
	models.ClaimAliasesByPlayer(p.db, player, to_claim)
	// clear old ones
	log.Printf("release aliases: %v", to_unclaim)
	models.ClaimAliasesByPlayer(p.db, models.NoUser, to_unclaim)

	return nil
}
Example #2
0
// create new player and return assigned ID or an error
func (p *PlayerController) CreateNewPlayer(name string, aliases []string) (uint, error) {

	if models.HasPlayer(p.db, name) == true {
		return 0, errors.New("player already present")
	}

	id := models.NewPlayer(p.db, name, "")

	// claim aliases
	models.ClaimAliasesByPlayer(p.db, id, aliases)

	return id, nil
}