rcpd/swat-codegen/codegen.sh

107 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
loc=$(dirname $(readlink -f "$0"))
# TOKEN MANAGEMENT
function migrate
{
curl "https://swataftermath.com:8443/account/$1/activate-migrated" --json '{"currentPassword":"'"$3"'","emailAddress":"'"$2"'","newPassword":"'"$3"'"}'
curl "https://swataftermath.com:8443/account/$1/verify-email" --json '{"verificationCode":"'"$(cat)"'"}'
login "$1" "$3"
}
function login
{
[ -n "$2" ] || { echo "Please provide a username and password, in that order, when calling this"; exit; }
curl https://swataftermath.com:8443/login --json '{"username":"'"$1"'","password":"'"$2"'"}' > ~/.config/rcpd.cfg
}
function get_token
{
cat ~/.config/rcpd.cfg 2>/dev/null || { echo "Please login first" >&2; exit; }
}
# CORE API CALLS
function get
{
TOKE=`get_token`
curl -H "Authorization:Bearer $TOKE" https://swataftermath.com:9443/account/$1
}
function post
{
TOKE=`get_token`
curl -H "Authorization:Bearer $TOKE" https://swataftermath.com:9443/account/$1 --json "$2"
}
function delete_code
{
TOKE=`get_token`
curl -H "Authorization:Bearer $TOKE" https://swataftermath.com:9443/account/rank-code/$1 -X DELETE
}
# UTILS
function lookup
{
w=
matches=`grep -ic "${*:2}" $loc/$1`
if [ "$matches" -ne 1 ]; then
echo "$1 '${*:2}' is ambiguous (got $matches matches), trying whole-word match" >&2
# Attempt whole-word match and see if we get exactly one result (and then remember it has happened!!!)
w=w
word_matches=`grep -ic$w "${*:2}" $loc/$1`
if [ "$word_matches" -ne 1 ]; then echo "still ambiguous (got $word_matches matches)" >&2; return 1; fi
fi
read $1 garbage <<< "`grep -i$w "${*:2}" $loc/$1`"
}
# USER-FACING COMMANDS
function builds
{
if [ -n "$1" ]; then filter='.[] | select(.buildName=="'"$*"'")'; fi
get saved-builds | jq $filter
}
function save
{
if [ -n "$2" ]; then name=',"buildName":"'"${*:2}"'"'; fi
res=`post rank-code '{"rankCode":"'$1'"'"$name"'}'`
jq <<< "$res"
if [ -z "`jq -r '.buildName // ""' <<< $"res"`" ]; then delete_code $1; fi # Don't bother storing any build that doesn't have a name. Assume it's just logging the results of a game.
}
function generate
{
# mfw I actually have to do work
IFS=/ read -a hero <<< "$*"
length=${#hero[*]}
if [ $length -eq 6 ]; then IFS=/ read class gun armour trait spec talent <<< "$*"
elif [ $length -eq 5 ]; then IFS=/ read class armour trait spec talent <<< "$*"
elif [ $length -eq 4 ]; then IFS=/ read class trait spec talent <<< "$*"; fi # Probably a borg or a wm.
if [ -z "$gun" ]; then
if [ "${class%wm}" != "$class" ]; then gun=${class%wm}; class=wm
elif [ "${class%mav}" != "$class" ]; then gun=${class%mav}; class=mav; fi
fi
res=0
lookup class "$class"
res=$((res+$?))
if [ -z "$gun" ]; then gun=$class; fi # Do this after qualifying class so I don't have to nest the full class table in the gun table.
lookup gun "$gun"
res=$((res+$?))
if [ -z "$armour" ]; then armour=$class; fi
lookup armour "$armour"
res=$((res+$?))
lookup trait "$trait"
res=$((res+$?))
lookup spec "$spec"
res=$((res+$?))
lookup talent "$talent"
res=$((res+$?))
if [ $res -gt 0 ]; then echo "Please correct above errors and try again"; exit; fi
echo $class $gun $armour $trait $spec $talent
post generate '{"swatClass":"'$class'","gun":"'$gun'","armor":"'$armour'","trait":"'$trait'","specialization":"'$spec'","talent":"'$talent'"}' | jq '.rankCode//.'
}
# DO SHIT
"$@"