79 lines
		
	
	
	
		
			2.5 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
	
		
			2.5 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
| target="${args[target]}"
 | |
| 
 | |
| ts_clients=$(teamspeak-query-lib clients --uid)
 | |
| ts_online=$(grep ~/.config/ts-control/steam_map.txt -Ff <(echo "$ts_clients" | awk '{print $1}'))
 | |
| friend_ids=$(echo "$ts_online" | awk '{print $1}' | paste -sd ',')
 | |
| 
 | |
| friends=$(curl -s "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$STEAM_KEY&steamids=$friend_ids" \
 | |
|   | jq '.response.players[] | {steamid, gameid, gameextrainfo}')
 | |
| 
 | |
| 
 | |
| steamid_to_tsname() {
 | |
|   local steamid="$1"
 | |
|   ts_uid=$(grep -F "$steamid" ~/.config/ts-control/steam_map.txt | awk '{print $2}')
 | |
|   echo "$ts_clients" | grep -F "$ts_uid" | cut -f2- -d' '
 | |
| }
 | |
| 
 | |
| create_groups() {
 | |
|   echo "$friends" | jq -r '.gameid' | sort -u | while read -r gameid
 | |
|   do
 | |
| 
 | |
|     if [ "$gameid" = "null" ]; then
 | |
|       continue
 | |
|     fi
 | |
| 
 | |
|     game_name=$(echo $friends | jq -r --arg gameid "$gameid" 'select(.gameid == $gameid) | .gameextrainfo' | head -n 1)
 | |
|     players=$(echo $friends | jq -r --arg gameid "$gameid" 'select(.gameid == $gameid) | .steamid' | paste -sd ',')
 | |
|     jq -cn --argjson players "[$players]" --arg name "$game_name" --arg id "$gameid" '{id: $id, name: $name, players: $players}'
 | |
|   done
 | |
| 
 | |
|   players=$(echo "$friends" | jq -r 'select(.gameid == null) | .steamid' | paste -sd ',')
 | |
|   jq -cn --argjson players "[$players]" '{id: null, name: "No game", players: $players}'
 | |
| }
 | |
| 
 | |
| create_prompt() {
 | |
|   local groups="$1"
 | |
|   index=0
 | |
|   echo "$groups" | while read -r group
 | |
|   do
 | |
|     echo "$index $(echo "$group" | jq -r '.name')"
 | |
|     echo "$group" | jq -r '.players[]' | while read -r steamid
 | |
|     do
 | |
|       ts_name=$(steamid_to_tsname "$steamid")
 | |
|       echo "$index   $ts_name"
 | |
|     done
 | |
|     index=$((index + 1))
 | |
|   done
 | |
| }
 | |
| 
 | |
| groups=$(create_groups)
 | |
| prompt=$(create_prompt "$groups")
 | |
| 
 | |
| filter=""
 | |
| 
 | |
| if [ -n "$target" ]
 | |
| then
 | |
|   filter="^.  *$target$" # START INDEX SPACES TARGET (broken for index > 9)
 | |
| fi
 | |
| 
 | |
| selected=$(_ts_control_single_or_dmenu "$prompt" "$filter")
 | |
| if [ -z "$selected" ]; then
 | |
|   exit 1
 | |
| fi
 | |
| 
 | |
| target_channel=""
 | |
| if [ -z "$target" ] # ask for channel if no target is specified
 | |
| then
 | |
|   target_channel=$(teamspeak-query-lib channels | $DMENU --prompt "Channel" || true)
 | |
| fi
 | |
| 
 | |
| if [ -z "$target_channel" ] # use first empty channel if no channel is specified
 | |
| then
 | |
|   target_channel=$(teamspeak-query-lib channels --only-empty | head -n 1)
 | |
| fi
 | |
| 
 | |
| selected_index=$(echo "$selected" | awk '{print $1}')
 | |
| prefix="$selected_index   "
 | |
| 
 | |
| readarray -t clients < <(echo "$prompt" | grep "^$prefix" | awk '{print $2}')
 | |
| teamspeak-query-lib move --strict-client --strict-channel "$target_channel" "${clients[@]}"
 |