예제 #1
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
}
예제 #2
0
// loadLeaderboardFromWeb fetch data from Steam
func loadLeaderboardFromWeb(l common.Leaderboard) {

	common.Info("LEADERBOARDS", fmt.Sprintf("Loading leaderboard %v from web...", l.SteamID))

	resp, err := http.Get(l.URL)
	if err != nil {
		common.Err("LEADERBOARDS", fmt.Sprintf("%s", err))
		return
	}

	defer resp.Body.Close()
	leaderboardBuffer, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		common.Err("LEADERBOARDS", fmt.Sprintf("%s", err))
		return
	}

	// Parse XML to get objects

	// LeaderboardEntriesRequest is the list of score entries.
	// It is only used for the marshaller
	requestType := struct {
		EntriesContainer struct {
			Entries []common.LeaderboardEntry `xml:"entry"`
		} `xml:"entries"`
	}{}

	xml.Unmarshal(leaderboardBuffer, &requestType)

	entries := requestType.EntriesContainer.Entries

	// Save to database
	// -- CLean all previous
	common.DeleteEntriesDB(l)

	// -- Save & get players
	var players map[string]common.Player
	players = make(map[string]common.Player)

	if len(entries) > 0 {
		var steamIDsToLoad []string
		for _, e := range entries {

			p, _ := common.GetPlayerWithSteamIDFromDB(e.SteamID)

			updatePlayer := true

			if p.UID != 0 {
				// Check last update
				if p.LastUpdate.Add(time.Duration(1) * time.Hour).After(time.Now()) {
					players[e.SteamID] = p
					updatePlayer = false
				}
			}

			if updatePlayer {
				steamIDsToLoad = append(steamIDsToLoad, e.SteamID)
			}
		}

		// Update = web to DB
		for steamID, p := range GetPlayersFromWeb(apiKey, false, steamIDsToLoad) {

			// Save player
			common.InsertOrUpdatePlayerDB(&p)

			players[steamID] = p
		}
	}

	// -- Insert all new ones
	for _, e := range requestType.EntriesContainer.Entries {

		// Set player
		e.Player = players[e.SteamID]
		e.PlayerUID = e.Player.UID

		// Save entry
		common.InsertEntryDB(l, e)
	}

	l.LastUpdate = time.Now()

	common.UpdateLeaderboardDB(l)

	common.Info("LEADERBOARDS", fmt.Sprintf("Loading leaderboard %v completed!", l.SteamID))
}