summaryrefslogtreecommitdiff
path: root/sample/biorhythm.rb
blob: 1c6d6fbad1204d49ddb72e483c549360f990d7f0 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/local/bin/ruby
#
#               biorhythm.rb - 
#                       $Release Version: $
#                       $Revision$
#                       $Date$
#                       by Yasuo OHBA(STAFS Development Room)
#
# --
#
#       
#

# probably based on:
#
# Newsgroups: comp.sources.misc,de.comp.sources.os9
# From: fkk@stasys.sta.sub.org (Frank Kaefer)
# Subject: v41i126:  br - Biorhythm v3.0, Part01/01
# Message-ID: <1994Feb1.070616.15982@sparky.sterling.com>
# Sender: kent@sparky.sterling.com (Kent Landfield)
# Organization: Sterling Software
# Date: Tue, 1 Feb 1994 07:06:16 GMT
#
# Posting-number: Volume 41, Issue 126
# Archive-name: br/part01
# Environment: basic, dos, os9

include Math
require "date.rb"
require "parsearg.rb"
require "parsedate.rb"

def usage()
  print "Usage:\n"
  print "biorhythm.rb [options]\n"
  print "  options...\n"
  print "    -D YYYYMMDD(birthday)     : use default values.\n"
  print "    --sdate | --date YYYYMMDD : use system date; use specified date.\n"
  print "    --birthday YYYYMMDD       : specifies your birthday.\n"
  print "    -v | -g                   : show values or graph.\n"
  print "    --days DAYS               : graph range (only in effect for graphs).\n"
  print "    --help                    : help\n"
end
$USAGE = 'usage'

def printHeader(y, m, d, p, w)
  print "\n>>> Biorhythm <<<\n"
  printf "The birthday %04d.%02d.%02d is a %s\n", y, m, d, w
  printf "Age in days: [%d]\n\n", p
end

def getPosition(z)
  pi = Math::PI
  z = Integer(z)
  phys = (50.0 * (1.0 + sin((z / 23.0 - (z / 23)) * 360.0 * pi / 180.0))).to_i
  emot = (50.0 * (1.0 + sin((z / 28.0 - (z / 28)) * 360.0 * pi / 180.0))).to_i
  geist =(50.0 * (1.0 + sin((z / 33.0 - (z / 33)) * 360.0 * pi / 180.0))).to_i
  return phys, emot, geist
end

def parsedate(s)
  ParseDate::parsedate(s).select(0, 1, 2)
end

def name_of_week(date)
  Date::DAYNAMES[date.wday]
end

#
# main program
#
parseArgs(0, nil, "vg", "D:", "sdate", "date:", "birthday:", "days:")

if $OPT_D
  dd = Date.today
  bd = Date.new(*parsedate($OPT_D))
  ausgabeart = "g"
else
  if $OPT_birthday
    bd = Date.new(*parsedate($OPT_birthday))
  else
    printf(STDERR, "Birthday                      (YYYYMMDD) : ")
    if (si = STDIN.gets.chop) != ""
      bd = Date.new(*parsedate(si))
    end
  end
  if !bd
    printf STDERR, "BAD Input Birthday!!\n"
    exit()
  end
  
  if $OPT_sdate
    dd = Date.today
  elsif $OPT_date
    dd = Date.new(*parsedate($OPT_date))
  else
    printf(STDERR, "Date        [<RETURN> for Systemdate] (YYYYMMDD) : ")
    if (si = STDIN.gets.chop) != ""
      dd = Date.new(*parsedate(si))
    end
  end
  if !dd
    dd = Date.today
  end

  if $OPT_v
    ausgabeart = "v"
  elsif $OPT_g
    ausgabeart = "g"
  else
    printf(STDERR, "Values for today or Graph  (v/g) [default g] : ")
    ausgabeart = STDIN.gets.chop
  end
end
if (ausgabeart == "v")
  printHeader(bd.year, bd.month, bd.day, dd - bd, name_of_week(bd))
  print "\n"
  
  phys, emot, geist = getPosition(dd - bd)
  printf "Biorhythm:   %04d.%02d.%02d\n", dd.year, dd.month, dd.day
  printf "Physical:    %d%%\n", phys
  printf "Emotional:   %d%%\n", emot
  printf "Mental:      %d%%\n", geist
  print "\n"
else
  if $OPT_days
    display_period = $OPT_days.to_i
  elsif $OPT_D
    display_period = 9
  else
    printf(STDERR, "Graph for how many days     [default 10] : ")
    display_period = STDIN.gets.chop
    if (display_period == "")
      display_period = 9
    else
      display_period = display_period.to_i - 1
    end
  end

  printHeader(bd.year, bd.month, bd.day, dd - bd, name_of_week(bd))
  print "                     P=physical, E=emotional, M=mental\n"
  print "             -------------------------+-------------------------\n"
  print "                     Bad Condition    |    Good Condition\n"
  print "             -------------------------+-------------------------\n"
  
  (dd - bd).step(dd - bd + display_period) do |z|
    phys, emot, geist = getPosition(z)
    
    printf "%04d.%02d.%02d : ", dd.year, dd.month, dd.day
    p = (phys / 2.0 + 0.5).to_i
    e = (emot / 2.0 + 0.5).to_i
    g = (geist / 2.0 + 0.5).to_i
    graph = "." * 51
    graph[25] = ?|
    graph[p] = ?P
    graph[e] = ?E
    graph[g] = ?M
    print graph, "\n"
    dd = dd + 1
  end
  print "             -------------------------+-------------------------\n\n"
end