Пример #1
0
// GetLatestModel returns the asset id of the latest model for a given user.
// While this is useful for retrieving the asset id of a newly created model
// that was just uploaded, it is not necessarily reliable for this purpose.
func GetLatestModel(client *rbxweb.Client, userId int32) (assetId int64, err error) {
	if userId == 0 {
		return 0, errors.New("invalid user id")
	}

	query := url.Values{
		"Category":          {"Models"},
		"SortType":          {"RecentlyUpdated"},
		"IncludeNotForSale": {"true"},
		"ResultsPerPage":    {"1"},
		"CreatorID":         {client.I32toa(userId)},
	}
	resp, err := client.Get(client.GetURL(`api`, `/catalog/json`, query))
	if err = client.AssertResp(resp, err); err != nil {
		return 0, err
	}
	defer resp.Body.Close()

	dec := json.NewDecoder(resp.Body)
	var asset []struct {
		AssetId int64
	}
	if err = dec.Decode(&asset); err != nil {
		return 0, errors.New("JSON decode failed: " + err.Error())
	}
	return asset[0].AssetId, nil
}
Пример #2
0
// GetInfo returns information about the current user.
//
// This function requires the client to be logged in.
func GetInfo(client *rbxweb.Client) (info Info, err error) {
	resp, err := client.Get(client.GetURL(`www`, `/MobileAPI/UserInfo`, nil))
	if err = client.AssertResp(resp, err); err != nil {
		return info, err
	}
	defer resp.Body.Close()
	dec := json.NewDecoder(resp.Body)
	if err = dec.Decode(&info); err != nil {
		return info, errors.New("JSON decode failed: " + err.Error())
	}
	return info, nil
}
Пример #3
0
// GetInfo returns information about an asset, given an asset id.
func GetInfo(client *rbxweb.Client, id int64) (info Info, err error) {
	query := url.Values{
		"assetId": {client.I64toa(id)},
	}
	resp, err := client.Get(client.GetURL(`api`, `/marketplace/productinfo`, query))
	if err = client.AssertResp(resp, err); err != nil {
		return Info{}, err
	}
	defer resp.Body.Close()

	dec := json.NewDecoder(resp.Body)
	dec.Decode(&info)
	return
}
Пример #4
0
// GetCurrentId returns the id of the user currently logged in.
//
// This function requires the client to be logged in.
func GetCurrentId(client *rbxweb.Client) (id int32, err error) {
	resp, err := client.Get(client.GetURL(`www`, `/Game/GetCurrentUser.ashx`, nil))
	if err = client.AssertResp(resp, err); err != nil {
		return 0, err
	}
	defer resp.Body.Close()

	var r bytes.Buffer
	r.ReadFrom(resp.Body)
	id, err = client.Atoi32(r.String())
	if err != nil {
		return 0, errors.New("user is not authorized")
	}
	return id, nil
}
Пример #5
0
// GetNameFromId returns a user name from a user id.
func GetNameFromId(client *rbxweb.Client, id int32) (name string, err error) {
	if id == 0 {
		return "", errors.New("id not specified")
	}
	resp, err := client.Get(client.GetURL(`api`, `/users/`+client.I32toa(id), nil))
	if err = client.AssertResp(resp, err); err != nil {
		return "", err
	}
	defer resp.Body.Close()
	dec := json.NewDecoder(resp.Body)
	var user struct {
		Username string
	}
	if err = dec.Decode(&user); err != nil {
		return "", errors.New("JSON decode failed: " + err.Error())
	}
	return user.Username, nil
}