aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2024-08-18 17:01:32 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2024-08-18 17:01:32 -0400
commitd158be2aea4300234d1c1c31aad041d66919960b (patch)
tree00ad8e8acfd81ef7b6b3b22e087d6c0ab638d10c
parentc840e6608751f1e15a2cd6347924186b27fbe842 (diff)
add useful scripts
-rwxr-xr-xscripts/csview6
-rwxr-xr-xscripts/discord34
-rwxr-xr-xscripts/roll-die70
3 files changed, 110 insertions, 0 deletions
diff --git a/scripts/csview b/scripts/csview
new file mode 100755
index 0000000..688938f
--- /dev/null
+++ b/scripts/csview
@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+if [ "$#" -eq 0 ] || [ "$1" = "-" ]; then
+ column -ts, | less -S
+else
+ column -ts, "$1" | less -S
+fi
diff --git a/scripts/discord b/scripts/discord
new file mode 100755
index 0000000..948c59e
--- /dev/null
+++ b/scripts/discord
@@ -0,0 +1,34 @@
+#!/usr/bin/env bash
+DOWNLOAD_URL="https://discord.com/api/download?platform=linux&format=tar.gz"
+INSTALL_DIR="$HOME/src/Discord"
+
+update(){
+ pidof Discord > /dev/null 2>&1 && pkill Discord
+ curl -L "$DOWNLOAD_URL" > /tmp/discord.tar.gz &&
+ rm -rf "$INSTALL_DIR" &&
+ tar -xzf /tmp/discord.tar.gz -C "$HOME/src"
+}
+
+
+# check for updates
+if [ -n "$1" ]; then
+ case "$1" in
+ "update")
+ echo "Updating Discord"
+ update
+ ;;
+ *)
+ echo "Unknown argument. Usage $0 [update]"
+ exit 1
+ ;;
+ esac
+else
+ # install if not installed
+ [ -x "$INSTALL_DIR/Discord" ] || {
+ echo "Installing Discord"
+ update
+ }
+
+ # start discord
+ "$INSTALL_DIR/Discord" > /dev/null 2>&1 & disown
+fi
diff --git a/scripts/roll-die b/scripts/roll-die
new file mode 100755
index 0000000..8135b0b
--- /dev/null
+++ b/scripts/roll-die
@@ -0,0 +1,70 @@
+#!/usr/bin/env python3
+
+import random
+import sys
+from pathlib import Path
+import re
+
+
+def roll_die(num_dice: int, dice_type: int, modifier: int) -> [int]:
+ rolls = []
+ for i in range(num_dice):
+ rolls.append(random.randint(1, dice_type) + modifier)
+
+ return rolls
+
+
+def parse_dice_notation(dice_notation) -> (int, int, int):
+ # Define the regular expression pattern
+ pattern = re.compile(r'(\d+)d(\d+)([+-]\d+)?')
+
+ # Search the pattern in the dice notation string
+ match = pattern.match(dice_notation)
+
+ if not match:
+ raise ValueError(f"Invalid dice notation: {dice_notation}")
+
+ # Extract the groups from the match
+ groups = match.groups()
+
+ # Convert the groups to integers and handle the optional modifier
+ num_dice = int(groups[0])
+ dice_type = int(groups[1])
+ modifier = int(groups[2]) if groups[2] else 0
+
+ return num_dice, dice_type, modifier
+
+
+def summary(rolls: [int]) -> None:
+ print(rolls)
+ print(f'Min: {min(rolls)}')
+ print(f'Max: {max(rolls)}')
+ if len(rolls) > 1:
+ print(f'Total: {sum(rolls)}')
+ print(f'Avg: {sum(rolls)/len(rolls):.2f}')
+
+
+def main():
+ if len(sys.argv) != 2:
+ path = Path(sys.argv[0]).parts[-1]
+ print(f"Usage: {path} xdy[(+|y)z]")
+ print("Rolls x number of y sided die, optionally using a modifier z")
+ print()
+ print("Examples:")
+ print("\t2d4+2 Rolls two four sided die adding 2 to each roll")
+ print("\t1d20-1 Rolls a single twenty and subtracts 1 from the roll")
+ sys.exit(1)
+
+ dice_notation = sys.argv[1]
+
+ try:
+ num_die, die_type, modifier = parse_dice_notation(dice_notation)
+ except ValueError as e:
+ print(e)
+
+ rolls = roll_die(num_die, die_type, modifier)
+ summary(rolls)
+
+
+if __name__ == "__main__":
+ main()