Example #1
0
func refreshPlayerCount() {
	if playerCount.LastUpdate.Add(time.Duration(30) * time.Second).Before(time.Now()) {
		playerCount = steam.GetPlayerCount(STEREDENN)

		common.Info("SERVER", fmt.Sprintf("Updating player count: %v right now", playerCount.Count))
	}
}
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
}
// 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))
}
Example #4
0
// Start the server.
func Start() {

	common.Info("SERVER", "Loading...")

	steam.DebugRequests = false

	// Check API key
	// Request API Key: http://steamcommunity.com/dev/apikey
	err := godotenv.Load()
	if err != nil {
		common.Err("SERVER", fmt.Sprintf("Error loading .env file: %v ", err))
	} else {
		steamAPIkey := getAPIKey()

		if steamAPIkey == "" {
			common.Warn("SERVER", "Missing Steam API key, some features will not work. Get one at http://steamcommunity.com/dev/apikey and add it to a .env file for key STEAM_API_KEY.")
		} else {
			common.Info("SERVER", "Steam key found!")
		}
	}

	// Configure
	common.ConfigureDB(config.DbFile)
	steam.Configure(getAPIKey(), STEREDENN, []int{1006063}, steredenn.Steredenn{})

	// Get player count
	refreshPlayerCount()

	common.Info("SERVER", "Loading completed!")

	// Initial loading?
	if config.LoadAllOnStart {

		common.Info("SERVER", "Starting complete data reload...")

		leaderboards, _ := steam.GetLeaderboards()

		for _, l := range leaderboards.List {
			steam.GetLeaderboard(l.SteamID)
		}

		common.Info("SERVER", "Data reload completed!")
	}

	// Routing
	r := mux.NewRouter()
	r.HandleFunc("/", HomeHandler)
	r.HandleFunc("/{page:[0-9]+}", HomeHandler)
	r.HandleFunc("/leaderboard/{id:[0-9]+}", LeaderboardHandler)
	r.HandleFunc("/leaderboard/{id:[0-9]+}/{page:[0-9]+}", LeaderboardHandler)
	r.HandleFunc("/player/{id:[0-9]+}", PlayerHandler)

	// Use a custom file server in dev mode to serve static file.
	// Useless in production (nginx will handle that).
	if config.Dev {
		r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(config.StaticFolder))))
	}

	http.Handle("/", r)
	http.ListenAndServe(":3000", nil)
}