Skip to content

Commit

Permalink
Added leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
ErrorNoInternet committed May 17, 2021
1 parent 730dc7f commit d3906e7
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/user"
"runtime"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -38,6 +39,13 @@ func reverseArray(arr []string) []string {
return arr
}

func reverseIntArray(arr []int) []int {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
return arr
}

func main() {
databasePath := "."
userObject, errorObject := user.Current()
Expand Down Expand Up @@ -78,18 +86,18 @@ func main() {
resetValues = true
} else if argument == "help" {
showHelpPage = true
} else if argument == "list" {
} else if argument == "list" || argument == "leaderboard" || argument == "lb" {
showPlayerList = true
} else if argument == "submit" {
submitPlayer = true
}
}

if showHelpPage {
helpText := "<fg=white;op=bold;>ok</> - ok\n<fg=white;op=bold;>ok stats</> - shows your statistics\n<fg=white;op=bold;>ok reset</> - resets your statistics\n<fg=white;op=bold;>ok list</> - shows a list of new players\n<fg=white;op=bold;>ok submit</> - submit your profile to the player list\n"
helpText := "<fg=white;op=bold;>ok</> - ok\n<fg=white;op=bold;>ok stats</> - shows your statistics\n<fg=white;op=bold;>ok reset</> - resets your statistics\n<fg=white;op=bold;>ok leaderboard</> - shows the OK leaderboard\n<fg=white;op=bold;>ok submit</> - submit your profile to the player list\n"
color.Printf(helpText)
} else if showPlayerList {
fmt.Println("Fetching player list...")
fmt.Println("Fetching leaderboard...")
httpResponse, errorObject := http.Get("http://ok-server.herokuapp.com/list")
if errorObject != nil {
fmt.Println("\rFailed to fetch player list")
Expand All @@ -102,13 +110,26 @@ func main() {
return
}
_ = json.Unmarshal(responseBytes, &response)
numberArray := []int{}
playerList := make(map[int]string)
if response.Count > 0 {
fmt.Println("")

for _, player := range response.Players {
color.Printf("%v - <fg=white;op=bold;>%v OKs</>\n", player.Name, player.Score)
numberArray = append(numberArray, player.Score)
playerList[player.Score] = player.Name
}
sort.Ints(numberArray)
numberArray = reverseIntArray(numberArray)
for index, number := range numberArray {
playerName := playerList[number]
color.Printf("<fg=white;op=bold;>%v.</> %v - <fg=white;op=bold;>%v OKs</>\n", index+1, playerName, number)
if index == 9 {
return
}
}
} else {
fmt.Println("There are no players on the OK list...")
fmt.Println("There are no players on the OK leaderboard...")
}
return
} else if submitPlayer {
Expand Down

0 comments on commit d3906e7

Please sign in to comment.