-
Notifications
You must be signed in to change notification settings - Fork 0
/
chem
executable file
·96 lines (81 loc) · 1.71 KB
/
chem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Print usage informations
function printUsage {
echo "Usage"
echo
echo "chem -h, chem --help, chem ? - show this info"
echo "chem - show statistics and known elements"
echo "chem <el1> <el2>, chem <el1> + <el2> - combine elements"
}
# Craft new element
# Expecting two parameters (element1 and element2)
function craftElement {
if [ $(isKnown $1) -ne 1 ]; then
echo "I don't know element $1!"
exit 1
fi
if [ $(isKnown $2) -ne 1 ]; then
echo "I don't know element $2!"
exit 1
fi
ELEMENT=$(cat db/combos|grep -e " $1+$2" -e " $2+$1"|head -1|cut -d"=" -f1)
if [ "$ELEMENT" != "" ]; then
if [ $(isKnown $ELEMENT) -ne 1 ]; then
echo "Congratz! You just found $ELEMENT ;)"
setKnown $ELEMENT
else
echo "You made $ELEMENT (already known)"
fi
else
echo "Nothing happen ..."
fi
}
# Set element as known
# Expecting one parameter (element name)
function setKnown {
echo $1 >> ~/.chem
}
# Check if element is known
# Expecting one parameter (element name)
function isKnown {
if cat ~/.chem|grep -q $1; then
echo 1
else
echo 0
fi
}
# Print all known elements
function getKnown {
echo "Already known elements: "
cat ~/.chem|sort
}
# Initialize .chem db file, if not exists
function initGame {
if [ ! -f ~/.chem ]; then
echo -e "fire\nwater\nair\nearth" > ~/.chem
echo "Initialized new game"
fi
}
## Main game ##
initGame
case "$1" in
-h|--help) printUsage; exit 0 ;;
esac
if [ $# -eq 0 ]; then
getKnown
exit 0
fi
if [ $# -lt 2 ]; then
printUsage
exit 1
fi
ELEMENT1=$1
ELEMENT2=$2
if [ "$2" == "+" ]; then
if [ $# -lt 3 ]; then
printUsage
exit 1
fi
ELEMENT2=$3
fi
echo $(craftElement $ELEMENT1 $ELEMENT2)