Commit 1a0a2d86 authored by Howl's avatar Howl
Browse files

.FIX. Fix the fucking 0PP bug. at least, so I hope

parent 22fa110f
Loading
Loading
Loading
Loading

calculate_pp.go

0 → 100644
+53 −0
Changes for calculate_pp.go: 53 added lines, 0 removed lines.
Original line number Diff line number Diff line
package main

import (
	"fmt"
	"math"

	"github.com/fatih/color"
)

type ppUserMode struct {
	countScores int
	ppTotal     int
}

func opCalculatePP() {
	const ppQuery = "SELECT scores.username, pp, scores.play_mode FROM scores LEFT JOIN users ON users.username=scores.username WHERE completed = '3' AND users.allowed = '1' ORDER BY pp DESC"
	rows, err := db.Query(ppQuery)
	if err != nil {
		queryError(err, ppQuery)
		return
	}

	users := make(map[string]*ppUserMode)
	var count int

	for rows.Next() {
		if count%1000 == 0 {
			fmt.Println("> CalculatePP:", count)
		}
		var (
			username string
			ppAmt    int
			playMode int
		)
		err := rows.Scan(&username, &ppAmt, &playMode)
		if err != nil {
			queryError(err, ppQuery)
			continue
		}
		if users[username] == nil {
			users[username] = &ppUserMode{}
		}
		currentScorePP := math.Ceil(math.Ceil(float64(ppAmt)) * math.Pow(0.95, float64(users[username].countScores)))
		users[username].countScores++
		users[username].ppTotal += int(currentScorePP)
		count++
	}
	rows.Close()

	color.Green("> CalculatePP: done!")

	wg.Done()
}
+7 −0
Changes for cron.go: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ type config struct {
	CleanReplays            bool
	DeleteReplayCache       bool
	BuildLeaderboards       bool
	CalculatePP             bool

	LogQueries bool `description:"You don't wanna do this in prod."`
	Workers    int  `description:"The number of goroutines which should execute queries. Increasing it may make cron faster, depending on your system."`
@@ -120,6 +121,12 @@ func main() {
		go opBuildLeaderboard()
		color.Green(" ok!")
	}
	if c.CalculatePP {
		fmt.Print("Starting calculating pp...")
		wg.Add(1)
		go opCalculatePP()
		color.Green(" ok!")
	}

	wg.Wait()
	color.Green("Data elaboration has been terminated.")
+1 −0
Changes for replays.go: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ func opCleanReplays() {
		// We don't check if the file exists, because that would be an useless I/O operation
		// TODO: WorkGroup?
		os.Remove(filename)
		count++
	}
	rows.Close()
	color.Green("> CleanReplays: done!")