Implimented a golang variant of the tooling, now fully in one app. Nov 16, 2023

This commit is contained in:
Rebekah 2024-06-18 15:53:58 -04:00
parent 94d5871cf3
commit 165ab812f0
Signed by: oneechanhax
GPG Key ID: 0074BF373B812798
14 changed files with 497 additions and 319 deletions

View File

@ -1,57 +0,0 @@
const input_time = process.argv[2];
//console.log("input_time")
//console.log(input_time)
/*function convertToSeconds(time) {
var parts = time.split(":");
var seconds = 0;
for (var i = 0; i < parts.length; i++) {
var value = parseFloat(parts[i]);
console.log(`Parsing ${i}: ${value}`)
if (i === 0) {
console.log("as day");
seconds += value * 86400;
} else if (i === 1) {
console.log("as hour");
seconds += value * 3600;
} else if (i === 2) {
console.log("as minutes");
seconds += value * 60;
} else if (i === 3) {
console.log("as seconds");
seconds += value;
}
}
console.log(seconds);
return seconds;
}*/
function convertToSeconds(time) {
var parts = time.split(":");
var seconds = 0;
if(parts.length==1) {
var secs = parseFloat(parts[0]);
seconds += secs;
}
else if(parts.length==2) {
var minutes = parseFloat(parts[0]);
var secs = parseFloat(parts[1]);
seconds += minutes * 60 + secs;
}
else if(parts.length==3) {
var hours = parseFloat(parts[0]);
var minutes = parseFloat(parts[1]);
var secs = parseFloat(parts[2]);
seconds += hours * 3600 + minutes * 60 + secs;
}
else if(parts.length==4) {
var days = parseFloat(parts[0]);
var hours = parseFloat(parts[1]);
var minutes = parseFloat(parts[2]);
var secs = parseFloat(parts[3]);
seconds += days * 86400 + hours * 3600 + minutes * 60 + secs;
}
return seconds;
}
console.log(convertToSeconds(input_time));

View File

@ -1,6 +0,0 @@
var currentTime = Date.now() / 1000;
console.log(currentTime);
// to reverse
/*var seconds = 1673721347.942;
var date = new Date(seconds * 1000);
console.log(date.toLocaleDateString()); console.log(date.toLocaleTimeString());*/

View File

@ -1,66 +0,0 @@
#!/bin/bash -e
usage() {
echo "usage $0 <MAP_NAME> <SOLDIER/DEMOMAN(s/d)>"
}
# Check that a map name was provided
if [ -z "$1" ]; then
echo "Error: No map name provided"
exit 1
fi
WANTED_MAP="$1"
WANTED_CLASS="$2"
case "$WANTED_CLASS" in
s*)
WANTED_CLASS="soldier"
;;
S*)
WANTED_CLASS="soldier"
;;
d*)
WANTED_CLASS="demoman"
;;
D*)
WANTED_CLASS="demoman"
;;
*)
echo "Error: Invalid class. Must be either SOLDIER or DEMOMAN"
usage
exit
;;
esac
FILE_LOC="personal/completed.$WANTED_CLASS/"
files="$(ls "$FILE_LOC/$WANTED_MAP"*.json | sed 's/.json//' | sed 's/.*\///')"
count="$(echo "$files" | wc -l)"
if [ "$count" -gt 1 ]; then
if [ -e "$FILE_LOC/$WANTED_MAP.json" ]; then
files="$WANTED_MAP"
else
if [ "$(echo "$files" | head -l)" ]; then
echo "There are too many maps that match your request: \"$WANTED_MAP\""
if [ "$count" -lt 5 ]; then
echo "The following maps match your request."
echo "$files"
fi
exit
fi
fi
fi
# Get the duration for the map
duration=$(jq '.duration' $FILE_LOC/$files.json)
# Convert the duration to a human-readable format
human_readable=$(awk -v t=$duration 'BEGIN{
h=int(t/3600);
t-=h*3600;
m=int(t/60);
t-=m*60;
s=t
printf "%dh %dm %ds\n",h,m,s
}')
echo "Run time for $files: $human_readable"

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gojump
go 1.21.1

2
gojump.sh Normal file
View File

@ -0,0 +1,2 @@
#!/bin/sh
go run main.go "$@"

View File

@ -1,42 +0,0 @@
#!/bin/bash
# Check that a class was provided
if [ -z "$1" ]; then
echo "Error: No class provided"
exit 1
fi
# Normalize the class argument to uppercase
class=$(echo $1 | tr '[:lower:]' '[:upper:]')
# Check that the class is either SOLDIER or DEMOMAN
case "$class" in
s*)
class="SOLDIER"
;;
S*)
class="SOLDIER"
;;
d*)
class="DEMOMAN"
;;
D*)
class="DEMOMAN"
;;
*)
echo "Error: Invalid class. Must be either SOLDIER or DEMOMAN"
exit 1
;;
esac
# Get a list of maps
maps=$(ls personal/completed.soldier/)
# Iterate over the list of maps
for map in $maps; do
# Strip the .json extension from the map name
map_name=$(echo $map | sed 's/.json//')
# Call the list-tier.sh script with the map name and class as arguments
./list-tier.sh $map_name $class
done

View File

@ -1,2 +0,0 @@
#!/bin/sh
sh list-tiers.sh s | grep -E '(T3|T4)' | shuf | head -n 6

View File

@ -1,2 +0,0 @@
#!/bin/sh
sh list-tiers.sh s | grep -vE '(T5|T6|T0|Tnull)' | shuf | head -n6

View File

@ -1,58 +0,0 @@
#!/bin/sh -e
usage() {
echo "usage list-tier <MAP_NAME> <SOLDIER/DEMOMAN(s/d)>"
}
if [ $# -lt 2 ]; then
echo "Error: At least two arguments is required"
usage
exit 1
fi
WANTED_MAP="$1"
WANTED_CLASS="$2"
case "$WANTED_CLASS" in
s*)
WANTED_CLASS="soldier"
;;
S*)
WANTED_CLASS="soldier"
;;
d*)
WANTED_CLASS="demoman"
;;
D*)
WANTED_CLASS="demoman"
;;
*)
echo "Error: Invalid class. Must be either SOLDIER or DEMOMAN"
usage
exit
;;
esac
FILE_LOC="database/map_info/"
files="$(ls $FILE_LOC/$WANTED_MAP*.json | sed 's/.json//' | sed 's/.*\///')"
count="$(echo "$files" | wc -l)"
if [ "$count" -gt 1 ]; then
if [ -e "$FILE_LOC/$WANTED_MAP.json" ]; then
files="$WANTED_MAP"
else
if [ "$(echo "$files" | head -l)" ]; then
echo "There are too many maps that match your request: \"$WANTED_MAP\""
if [ "$count" -lt 5 ]; then
echo "The following maps match your request."
echo "$files"
fi
exit
fi
fi
fi
JSON_FILEPATH="$FILE_LOC/$files.json"
FOUND_MAP_TIER="$(jq .tiers."$WANTED_CLASS" "$JSON_FILEPATH")"
FOUND_MAP_NAME="$(jq .name "$JSON_FILEPATH" | tr -d '"')"
echo "$FOUND_MAP_NAME: T$FOUND_MAP_TIER"

View File

@ -1,41 +0,0 @@
#!/bin/sh -e
usage() {
echo "usage list-tiers <SOLDIER/DEMOMAN(s/d)>"
}
if [ $# -lt 1 ]; then
echo "Error: At least one argument is required"
usage
exit 1
fi
WANTED_CLASS="$1"
case "$WANTED_CLASS" in
s*)
WANTED_CLASS="soldier"
;;
S*)
WANTED_CLASS="soldier"
;;
d*)
WANTED_CLASS="demoman"
;;
D*)
WANTED_CLASS="demoman"
;;
*)
echo "Unknown class: $1"
usage
exit
;;
esac
for i in database/map_info/*.json; do
# Strip the .json extension from the map name
map_name=$(echo $i | sed 's/.json//' | sed 's/.*\///')
# Call the list-tier.sh script with the map name and class as arguments
./list-tier.sh $map_name $WANTED_CLASS
done

View File

@ -1,2 +0,0 @@
#!/bin/sh
sh list-uncompleted-tiers.sh s | grep -E '(T3|T4)' | shuf | head

View File

@ -1,2 +0,0 @@
#!/bin/sh
sh list-uncompleted-tiers.sh s | grep -vE '(T4|T5|T6|T0|Tnull)' | shuf | head

View File

@ -1,41 +0,0 @@
#!/bin/bash
# Check that a class was provided
if [ -z "$1" ]; then
echo "Error: No class provided"
exit 1
fi
# Normalize the class argument to uppercase
class=$(echo $1 | tr '[:lower:]' '[:upper:]')
# Check that the class is either SOLDIER or DEMOMAN
case "$class" in
s*)
class="soldier"
;;
S*)
class="soldier"
;;
d*)
class="demoman"
;;
D*)
class="demoman"
;;
*)
echo "Error: Invalid class. Must be either SOLDIER or DEMOMAN"
exit 1
;;
esac
# Get a list of maps
completed_maps=$(ls personal/completed.$class/ | sed 's/.json//')
for i in database/map_info/*.json; do
# Strip the .json extension from the map name
map_name=$(echo $i | sed 's/.json//' | sed 's/.*\///')
# Call the list-tier.sh script with the map name and class as arguments
[ "$(echo "$map_name" | grep -v "$completed_maps")" == "$mapname" ] || ./list-tier.sh $map_name $class
done

492
main.go Normal file
View File

@ -0,0 +1,492 @@
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
)
type JumpDatabaseMapEntry struct {
Authors []struct {
Name string `json:"name"`
Player struct {
SteamID string `json:"steamId"`
Country string `json:"country"`
} `json:"player"`
} `json:"authors"`
ID string `json:"id"`
Name string `json:"name"`
Videos struct {
Soldier string `json:"soldier"`
Demoman string `json:"demoman"`
} `json:"videos"`
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
Zones struct {
Bonus []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName any `json:"customName"`
} `json:"bonus"`
BonusEnd []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName any `json:"customName"`
} `json:"bonusEnd"`
Checkpoint []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName any `json:"customName"`
} `json:"checkpoint"`
Course []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName string `json:"customName"`
} `json:"course"`
CourseEnd []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName any `json:"customName"`
} `json:"courseEnd"`
Linear []any `json:"linear"`
Map []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName any `json:"customName"`
} `json:"map"`
MapEnd []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName any `json:"customName"`
} `json:"mapEnd"`
Misc []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName string `json:"customName"`
} `json:"misc"`
Trick []struct {
ID int `json:"id"`
Map struct {
Tiers struct {
Soldier int `json:"soldier"`
Demoman int `json:"demoman"`
} `json:"tiers"`
} `json:"map"`
Type string `json:"type"`
Zoneindex int `json:"zoneindex"`
CustomName string `json:"customName"`
} `json:"trick"`
} `json:"zones"`
}
type JumpmapRunTimeEntry struct {
Nontempusrun bool `json:"nontempusrun"`
ManuallyCreated bool `json:"manually_created"`
ID int `json:"id"`
Duration float64 `json:"duration"`
Rank int `json:"rank"`
Date float64 `json:"date"`
Class string `json:"class"`
Name string `json:"name"`
}
func check(e error) {
if e != nil {
panic(e)
}
}
func ConvertPrettyTimeToSeconds(time string) float64 {
parts := strings.Split(time, ":")
seconds := 0.0
if len(parts) == 1 {
secs, _ := strconv.ParseFloat(parts[0], 64)
seconds += secs
} else if len(parts) == 2 {
minutes, _ := strconv.ParseFloat(parts[0], 64)
secs, _ := strconv.ParseFloat(parts[1], 64)
seconds += minutes*60 + secs
} else if len(parts) == 3 {
hours, _ := strconv.ParseFloat(parts[0], 64)
minutes, _ := strconv.ParseFloat(parts[1], 64)
secs, _ := strconv.ParseFloat(parts[2], 64)
seconds += hours*3600 + minutes*60 + secs
} else if len(parts) == 4 {
days, _ := strconv.ParseFloat(parts[0], 64)
hours, _ := strconv.ParseFloat(parts[1], 64)
minutes, _ := strconv.ParseFloat(parts[2], 64)
secs, _ := strconv.ParseFloat(parts[3], 64)
seconds += days*86400 + hours*3600 + minutes*60 + secs
}
return seconds
}
func ExtractBareFilenameFromPath(in_path string) string {
baseName := filepath.Base(in_path)
lastDotIndex := strings.LastIndex(baseName, ".")
if lastDotIndex != -1 {
fileNameWithoutExt := baseName[:lastDotIndex]
return fileNameWithoutExt
} else {
// If there's no extension, the whole base name is the filename
return baseName
}
}
/*func RemoveElement(slice []any, s int) []any {
return append(slice[:s], slice[s+1:]...)
}*/
const (
JumpmapTypeSoldier = 0
JumpmapTypeDemoman = 1
)
func GetFileNameFromJumpmapRunTypeEnum(run_type int) string {
if run_type == JumpmapTypeSoldier {
return "soldier"
}
if run_type == JumpmapTypeDemoman {
return "demoman"
}
panic("unknown jumpmap run types!")
// return "unknown"
}
func StringToJumpmapRunType(jump_map_wanted_type string) int {
switch jump_map_wanted_type {
case "s":
fallthrough
case "soldier":
return JumpmapTypeSoldier
case "d":
fallthrough
case "demoman":
return JumpmapTypeDemoman
default:
}
panic("Trying to convert to jumpmap run type (soldier/demo) with incorrect string data!")
}
var mapinfo_database_location = filepath.Join("database", "map_info")
func ReadDatabaseFile(jump_map_name string) JumpDatabaseMapEntry {
data, error := os.ReadFile(filepath.Join(mapinfo_database_location, jump_map_name+".json"))
check(error)
var jump_map JumpDatabaseMapEntry
if err := json.Unmarshal(data, &jump_map); err != nil {
check(err)
}
return jump_map
}
func ReadRunRecordFile(jump_map_type int, jump_map_name string) JumpmapRunTimeEntry {
data, error := os.ReadFile(filepath.Join("personal", "completed."+GetFileNameFromJumpmapRunTypeEnum(jump_map_type), jump_map_name+".json"))
check(error)
var jump_map JumpmapRunTimeEntry
if err := json.Unmarshal(data, &jump_map); err != nil {
check(err)
}
return jump_map
}
func PrintRecord(printable_record any) string {
data, error := json.MarshalIndent(printable_record, "", "\t")
check(error)
return string(data)
}
func FindMapNameFromPartial(partial_jump_map_name string) string {
if !strings.HasPrefix(partial_jump_map_name, "jump_") {
partial_jump_map_name = "jump_" + partial_jump_map_name
}
found_files, err := filepath.Glob(filepath.Join(mapinfo_database_location, partial_jump_map_name+"*.json"))
check(err)
found_file_count := len(found_files)
if found_file_count > 1 {
found_map := filepath.Join(filepath.Join(mapinfo_database_location, partial_jump_map_name+".json"))
if _, err := os.Stat(found_map); err == nil {
return ExtractBareFilenameFromPath(found_map)
} else {
fmt.Printf("There are too many maps that match your request: \"%s\"\n", partial_jump_map_name)
if found_file_count < 5 {
fmt.Println("The following maps match your request:")
fmt.Println(strings.Join(found_files, "\n"))
}
os.Exit(2)
}
}
return ExtractBareFilenameFromPath(found_files[0])
}
func GetPrettyMapTime(jump_map_type int, jump_map_name string) string {
run_record := ReadRunRecordFile(jump_map_type, jump_map_name)
duration := time.Duration(run_record.Duration) * time.Second
hours := int(duration.Hours())
minutes := int(duration.Minutes()) % 60
seconds := int(duration.Seconds()) % 60
return fmt.Sprintf("%dh %dm %ds", hours, minutes, seconds)
}
func GetMapList() []string {
found_files, err := filepath.Glob(filepath.Join(mapinfo_database_location, "*.json"))
check(err)
for index, fle := range found_files {
found_files[index] = ExtractBareFilenameFromPath(fle)
}
return found_files
}
func GetCompletedMapList(jump_map_type int) []string {
found_files, err := filepath.Glob(filepath.Join("personal", "completed."+GetFileNameFromJumpmapRunTypeEnum(jump_map_type), "*.json"))
check(err)
for index, fle := range found_files {
found_files[index] = ExtractBareFilenameFromPath(fle)
}
return found_files
}
func GetMapTier(jump_map_type int, jump_map_name string) int {
map_info := ReadDatabaseFile(jump_map_name)
tier := func() int {
switch jump_map_type {
case JumpmapTypeDemoman:
return map_info.Tiers.Demoman
case JumpmapTypeSoldier:
return map_info.Tiers.Soldier
default:
panic("unselectable JumpmapType enum option other than solly or demo")
}
}()
return tier
}
func IsOnMapIgnorelist(jump_map_name string) bool {
if jump_map_name == "base_jump" {
return true
}
return false
}
func PrintMapUsageInfo() {
fmt.Println("usage: " + os.Args[0] + "[operation] <MAP_NAME> <SOLDIER/DEMOMAN(s/d)>")
}
/*func PrintClassUsageInfo() {
fmt.Println("usage: " + os.Args[0] + "[operation] <SOLDIER/DEMOMAN(s/d)>")
}*/
func PrintUsageInfo() {
fmt.Println("usage: " + os.Args[0] + " [operation] <Additional Args>")
fmt.Println("try running \"" + os.Args[0] + " help\" for additional info on operations")
}
func PrintHelpInfo() {
PrintUsageInfo()
fmt.Println("help - Get the help you are currently seeing")
fmt.Println("get-time - Get a personal maps completion time")
fmt.Println("list-tier - Get the difficulty tier of a particular map")
fmt.Println("list-tiers - Get all of the difficulty tiers listed out, useful for combining with grep!")
fmt.Println("list-maps - Get a list of all maps available")
fmt.Println("convert-time - Convert a time from HH:MM:SS.MS to seconds, for use in storing times in the database")
fmt.Println("get-curtime - Get the current unix date, time in seconds")
}
func main() {
argument_length := len(os.Args)
if argument_length < 2 {
PrintUsageInfo()
os.Exit(42)
}
if os.Args[1] == "help" {
PrintHelpInfo()
os.Exit(0)
}
if os.Args[1] == "get-time" {
if argument_length < 3 {
fmt.Println("Error: No map name provided")
PrintMapUsageInfo()
os.Exit(1)
}
wanted_class := JumpmapTypeSoldier
if argument_length < 4 {
fmt.Println("Warning: No classname provided, using soldier as default")
} else {
wanted_class = StringToJumpmapRunType(os.Args[3])
}
// try {
wanted_map := FindMapNameFromPartial(os.Args[2])
//} catch
fmt.Println("Run time for " + wanted_map + ": " + GetPrettyMapTime(wanted_class, wanted_map))
os.Exit(0)
}
if os.Args[1] == "list-tier" {
if argument_length < 3 {
fmt.Println("Error: No map name provided")
PrintMapUsageInfo()
os.Exit(1)
}
wanted_class := JumpmapTypeSoldier
if argument_length < 4 {
fmt.Println("Warning: No classname provided, using soldier as default")
} else {
wanted_class = StringToJumpmapRunType(os.Args[3])
}
wanted_map := FindMapNameFromPartial(os.Args[2])
fmt.Printf("%s: T%d\n", wanted_map, GetMapTier(wanted_class, wanted_map))
os.Exit(0)
}
if os.Args[1] == "list-tiers" {
wanted_class := JumpmapTypeSoldier
if argument_length < 3 {
fmt.Println("Warning: No classname provided, using soldier as default")
} else {
wanted_class = StringToJumpmapRunType(os.Args[3])
}
map_list := GetMapList()
fmt.Println("Found map tiers:")
for _, mapp := range map_list {
fmt.Printf("%s: T%d\n", mapp, GetMapTier(wanted_class, mapp))
}
os.Exit(0)
}
if os.Args[1] == "list-completed-tiers" {
wanted_class := JumpmapTypeSoldier
if argument_length < 3 {
fmt.Println("Warning: No classname provided, using soldier as default")
} else {
wanted_class = StringToJumpmapRunType(os.Args[3])
}
map_list := GetCompletedMapList(wanted_class)
fmt.Println("Found completed map tiers:")
for _, mapp := range map_list {
if !IsOnMapIgnorelist(mapp) {
fmt.Printf("%s: T%d\n", mapp, GetMapTier(wanted_class, mapp))
}
}
os.Exit(0)
}
if os.Args[1] == "list-uncompleted-tiers" {
wanted_class := JumpmapTypeSoldier
if argument_length < 3 {
fmt.Println("Warning: No classname provided, using soldier as default")
} else {
wanted_class = StringToJumpmapRunType(os.Args[3])
}
map_list := GetMapList()
completed_map_list := GetCompletedMapList(wanted_class)
fmt.Println("Found uncompleted map tiers:")
for _, mapp := range map_list {
if !slices.Contains(completed_map_list, mapp) {
fmt.Printf("%s: T%d\n", mapp, GetMapTier(wanted_class, mapp))
}
}
os.Exit(0)
}
if os.Args[1] == "list-maps" {
map_list := GetMapList()
fmt.Println("Found maps:")
for _, mapp := range map_list {
fmt.Println(mapp)
}
os.Exit(0)
}
if os.Args[1] == "convert-time" {
if argument_length < 3 {
fmt.Println("Error: No time to convert into seconds???...???")
os.Exit(1)
}
fmt.Printf("Converted-Time-To-Seconds: %f\n", ConvertPrettyTimeToSeconds(os.Args[2]))
os.Exit(0)
}
if os.Args[1] == "get-curtime" {
currentTime := time.Now()
fractional_nanosecond := float64(currentTime.UnixNano())
unix_seconds := fractional_nanosecond / 1e9
fmt.Printf("Curtime: %f\n", unix_seconds)
os.Exit(0)
}
PrintUsageInfo()
}