Exemplo n.º 1
0
// GetLeaderboard returns a leaderboard object with the given Steam id
func GetLeaderboard(steamID int) (common.Leaderboard, error) {

	l, err := common.GetLeaderboardFromDB(steamID)

	// Now load entries
	// -- Update first
	ttl := time.Duration(24) * time.Hour
	if l.IsDaily() {
		ttl = time.Duration(30) * time.Minute
	}

	if l.LastUpdate.Add(ttl).Before(time.Now()) {
		loadLeaderboardFromWeb(l)
	}

	// -- Read from DB
	entries, err := common.GetEntriesFromDB(l.UID)

	if err != nil {
		return l, err
	}

	// ---------

	// Additionnal datas
	for i := 0; i < len(entries); i++ {
		entry := &entries[i]

		// Metadata
		t, err := hex.DecodeString(entry.MetadataRaw)

		if err == nil {
			if len(t) > 0 {

				// Metadata are 32 bits integer = 4 * 8 bits bytes
				metadatas := []int{}
				for m := 0; m < len(t); m += 4 {

					var val int32
					buf := bytes.NewBuffer(t[m : m+4])
					binary.Read(buf, binary.LittleEndian, &val)

					metadatas = append(metadatas, int(val))
				}

				specificFunctions.UseMetadata(entry, metadatas)
			}
		} else {
			common.Err("DATABASE", fmt.Sprintf("Error parsing metadata: %v", err))
		}
	}

	l.Entries = entries

	return l, nil
}
Exemplo n.º 2
0
func loadLeaderboardsFromWeb() error {

	common.Info("LEADERBOARDS", "Reloading all leaderboards from web...")

	var ldbs common.LeaderboardsList

	// Parse new from URL
	url := fmt.Sprintf("http://steamcommunity.com/stats/%v/leaderboards/?xml=1", appID)

	// Get all leaderboards for the game
	if err := MakeAPICall(url, true, &ldbs); err != nil {
		common.Err("LEADERBOARDS", fmt.Sprintf("%s", err))
		return err
	}

	// Exclude some leaderboards we don't want to show
	for i := 0; i < len(excludedLeaderboardIDs); i++ {
		for j := 0; j < len(ldbs.List); j++ {
			if ldbs.List[j].SteamID == excludedLeaderboardIDs[i] {
				// Remove the element
				ldbs.List = append(ldbs.List[:j], ldbs.List[j+1:]...)
			}
		}
	}

	// Parse dates
	for i := 0; i < len(ldbs.List); i++ {
		l := &ldbs.List[i]

		l.LastUpdate = time.Date(1988, time.April, 29, 3, 0, 0, 0, time.UTC)
		l.Date = specificFunctions.GetDate(l)
	}

	// Save to database
	for _, l := range ldbs.List {

		// Exists?
		existingLeaderboard, _ := common.GetLeaderboardFromDB(l.SteamID)

		if existingLeaderboard.Name == "" {
			common.InsertLeaderboardDB(l)
		} else {
			common.UpdateLeaderboardDB(l)
		}
	}

	lastLeaderboardRefresh = time.Now()

	return nil
}