-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.main.py
357 lines (331 loc) · 10.8 KB
/
.main.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#! /usr/bin/python
## imports
import os, sys
from subprocess import Popen
from decimal import *
from textwrap import fill
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
## file operations
def readPrice(url):
return open('.prices/' + url, 'r').read().splitlines()
def writeReport():
with open('.report/index.html','w') as f:
reportPrint = dict(stockInv, **{
"gmp" : stockInv["gm"] * stockPrice["gm"],
"gep" : ((stockInv["ge"]) * (stockPrice["ge"])),
"kop" : stockInv["ko"] * stockPrice["ko"],
"inn" : stockInv["in"] * stockPrice["in"],
"ptp" : stockInv["pt"] * stockPrice["pt"],
"money" : money,
"day" : cal[date]
})
reportPrint["assets"] = (reportPrint["gep"] + reportPrint["gmp"] + reportPrint["kop"] + reportPrint["inn"] + reportPrint["ptp"])
reportPrint["total"] = (reportPrint["assets"] + money)
f.write("""
<html>
<link rel="stylesheet" href="css/style.css">
<head><title>Report</title>Stock Report for {day}<p></head>
<body>Inventory:
<ul style="list-style-type:none">
<li>GE: {ge} = {gep}</li>
<li>GM: {gm} = {gmp}</li>
<li>KO: {ko} = {kop}</li>
<li>IN: {in} = {inn}</li>
<li>PT: {pt} = {ptp}</li>
</ul>
Money: {money}<p>
Assets: {assets}<p>
<div class="imageContainer">Total: {total}</div>
</body>
</html>""".format(**reportPrint))
def writeNews():
global money
with open('.resources/.days/%s' % date,'r') as k:
data = k.read().splitlines()
loc = data[1]
dat = cal[date]
ln = data[2]
price = data[3]
money -= Decimal(int(filter(str.isdigit, price))) / 100
with open('.resources/.days/%s.ar' % date, 'r') as w:
art = w.read().splitlines()
j = ""
for i in art:
indv = {
"title" : i.split("#")[0],
"author" : "By " + i.split("#")[1],
"text": i.split("#")[2],
}
j += """<div class="collumn">
<div class="head"><span class="headline hl3">{title}</span><p><span class="headline hl4">{author}</span></p></div>
 {text}</p></div>\n""".format(**indv)
j += """<div class="collumn">
<div class="head"><span class="headline hl3">The Stock Report</span><p><span class="headline hl4">Polly Esther</span></p></div>
<table style="width:100%">
<tr>
<th><b>Stock Symbol</th>
<th>Company Name</th>
<th>Price</b></th>
</tr>
<tr>
<td>GE</td>
<td>General Electric</td>
<td>{ge}</td>
</tr>
<tr>
<td>GM</td>
<td>General Motors</td>
<td>{gm}</td>
</tr>
<tr>
<td>KO</td>
<td>Coca-Cola</td>
<td>{ko}</td>
</tr>
<tr>
<td>IN</td>
<td>Invincible Oil</td>
<td>{in}</td>
</tr>
<tr>
<td>PT</td>
<td>Pittsburgh Coal</td>
<td>{pt}</td>
</tr>
</table>
</p></div>\n""".format(**stockPrice)
fmt_dict = {
"title": "Nation Daily",
"square": "The best news ever",
"loc" : loc,
"date" : dat,
"length" : ln,
"art": j,
"price": price,
}
text = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{title}</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<html>
<head>
<title>{title}</title>
<meta name="viewport" content="width=device-width">
</head>
<body>
<div class="head">
<div class="headerobjectswrapper">
<header>{title}</header>
</div>
<div class="subhead">{loc} - {date} - {length}<span class="price">{price}</div><div style="clear: both;"></div>
</div>
<div class="content">
<div class="collumns">
{art}
</div>
</div>
</body>
</html>
""".format(**fmt_dict)
with open('.news/index.html', 'w') as f:
f.write(text)
return data[0]
def display(url):
os.system("open " + url)
def dispnews():
display('.news/index.html')
def updateStocks():
global stockPrice
with open('.resources/.days/%s' % date,'r') as k:
dat = k.read().splitlines()
stockPrice = {"ge" : int(dat[4]),"gm" : int(dat[5]),"ko" : int(dat[6]),"in" : int(dat[7]),"pt" : int(dat[8])}
## init
with open('.resources/calendar', 'r') as f:
cal = f.readlines()
q = False
run = True
money = Decimal(500.00)
date = 0
stockInv = {
"ge" : 0,
"gm" : 0,
"ko" : 0,
"in" : 0,
"pt" : 0,
}
stockPrice = {
"ge" : 0,
"gm" : 0,
"ko" : 0,
"in" : 0,
"pt" : 0,
}
nd = False
def play(sound):
os.spawnlp(os.P_NOWAIT, 'afplay', 'afplay', '.resources/music/%s' % sound)
def clear():
os.system('clear')
def save():
sav = "%s\n%s\n" % (money, date)
for j in stockInv.values():
sav += j + "\n"
print(j)
def quit():
## save()
sys.exit("Goodbye!")
def report():
writeReport()
play('type.mp3')
display('.report/index.html')
def buy():
global stockInv
print("Ask broker to buy what?")
sel = raw_input().lower()
if sel in stockInv:
print("How many shares? (Max %s)" % (int(money / stockPrice[sel])))
num = raw_input()
try:
if int(num) < 0:
print("You can't buy negative shares!")
return
if stockPrice[sel] * int(num) <= money:
stockInv[sel] += int(num)
changeMoney((money - stockPrice[sel] * int(num)), "%s shares of %s, giving you a total of %s shares" % (num, sel, stockInv[sel]))
else:
print("You don't have enough money!")
except ValueError:
print("I don't understand what you mean. Please use positive integers.")
else:
print("I'm sorry, I don't know what you mean. Please use the stock symbols found in the newspaper.")
def sell():
global stockInv
print("Ask broker to sell what?")
sel = raw_input().lower()
if sel in stockInv:
print("How many shares? (Max %s)" % (stockInv[sel]))
num = raw_input()
try:
if int(num) < 0:
print("You can't sell negative shares!")
return
if int(num) <= stockInv[sel]:
stockInv[sel] -= int(num)
changeMoney((money + stockPrice[sel] * int(num)), "%s shares of %s, giving you a total of %s shares" % (num, sel, stockInv[sel]))
else:
print("You don't have enough shares!")
except ValueError:
print("I don't understand what you mean. Please use positive integers.")
else:
print("I'm sorry, I don't know what you mean. Please use the stock symbols found in the newspaper.")
def changeMoney(newVal, reason):
global money
if newVal > money:
print("You got %s dollars from %s. Your new balance is %s." % (newVal - money, reason, newVal))
else:
print("You spent %s dollars on %s. Your new balance is %s." % (money - newVal, reason, newVal))
money = Decimal(newVal.quantize(Decimal('.01'), rounding=ROUND_DOWN))
def cPaper():
global q
play("operator.mp3")
play("phone.aiff")
if not q:
q = True
print("What is your answer?")
with open('.resources/.days/%s' % date,'r') as k:
answer = k.read().splitlines()[9]
if raw_input().lower() == answer:
print("You got it right! Your prize is 50 dollars!")
changeMoney(money + 50, "the newspaper")
else:
print("I'm sorry, the correct answer was %s. Better luck next time!" % answer)
else:
print("You've already called us today! Only one attempt allowed per day.")
def endDay():
global nd
nd = True
options = {
0 : quit,
1 : report,
2 : buy,
3 : sell,
4 : dispnews,
5 : cPaper,
6 : endDay,
}
## user interface
def name():
print("Please enter your first name: ")
user = raw_input().lower()
return user
def menu():
print
print(u"\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 ")
print(u"\u2502" + color.BOLD + " Operation Option " + color.END + u"\u2502")
print(u"\u2502 View report 1 \u2502")
print(u"\u2502 Buy 2 \u2502")
print(u"\u2502 Sell 3 \u2502")
print(u"\u2502 Read paper 4 \u2502")
print(u"\u2502 Call paper 5 \u2502")
print(u"\u2502 Go to sleep 6 \u2502")
print(u"\u2502 Quit 0 \u2502")
print(u"\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518")
sel = raw_input()
if sel.isdigit():
if (int(sel) < 7) and (int(sel) > -1):
clear()
return int(sel)
clear()
print("Please enter a valid selection...")
return menu()
## execution
clear()
name = name()
clear()
try:
while run:
play("alarm.mp3")
updateStocks()
print("Today is %s." % cal[date].replace("\n",""))
print
print(fill(writeNews()))
while not nd:
options[menu()]()
print("You call it a day and hit the hay.")
print("(Press enter to continue)")
raw_input()
nd = False
q = False
date += 1
except IndexError:
print(color.RED + "Today is Tuesday, October 29th, 1929." + color.END)
tuesday = """After work, you heard on the radio that the market has crashed. You don't feel good. You're not only worried about your stocks, but your future as a whole. You eventually fall asleep.
"""
print(color.RED + tuesday + color.END)
print(color.RED + "Press enter to continue." + color.END)
raw_input()
print(color.RED + "Today is Wednesday, October 29th, 1929." + color.END)
wednesday = """You aren't able to get in touch with your broker. You visit his office, his favorite coffee shop, everywhere, but you can't find him. You hear rumors at work that your branch may be shut down. You could be losing your job soon.
"""
print(color.RED + wednesday + color.END)
print(color.RED + "Press enter to continue." + color.END)
raw_input()
wednesday = """Your broker killed himself after the crash. The market dove again today. You're afraid to look. Your entire portfolio is basically worthless now. You liquidate your assets, as you will be needing all the cash you can get your hands on.
"""
print(color.RED + wednesday + color.END)
print(color.RED + "Things look bleak now, but who knows what the future holds?" + color.END)
raw_input()