repl.it
Python

loc 1255, counting blanks

fork
loading
Files
  • main.py
  • keys
  • bignums.py
  • bonus.py
  • canvas.py
  • cookie.txt
  • game.py
  • server.py
  • units.py
  • upgrades.py
  • util.py
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
import threading
import time

import keys.reader
from game import Game
from units import UNITS, MAX_UNIT_NAME_LEN, MAX_UNIT_DESC_LEN
from upgrades import UPGRADES, MAX_UPGRADE_NAME_LEN, MAX_UPGRADE_DESC_LEN
from canvas import Canvas, FG, BG
import util
import bonus
import server

# COOKIE CLICKER: TERMINAL EDITION
# its a stupid idea but w/e

TITLE_POS = 10, 3
TITLE = "Cookie Clicker: Terminal Edition"

COOKIES_POS = 15, 4
COOKIES_LEN = 10

BIG_COOKIE_POS = 13, 10

SELECT_FG = 0  | FG # black
SELECT_BG = 15 | BG # white

UNIT_POS = 70, 5
UNIT_MAX_LEN = 25
UNIT_HEADER = "-" * 35
UNIT_SPACING = 3

UNIT_NAME_POS = 70, 3
UNIT_DESC_POS = 70, 4

UNIT_BUY_Y = 3
UNIT_BUY_X_START = 59
UNIT_BUY_X_END = 62
UNIT_BUY_TEXT = "Buy"

UNIT_SELL_Y = 3
UNIT_SELL_X_START = 64
UNIT_SELL_X_END = 68
UNIT_SELL_TEXT = "Sell"

UPGRADE_POS = 10, 30
UPGRADE_SHOW_MAX = 5
UPGRADE_HEIGHT = 3
UPGRADE_SPACING = 4

UPGRADE_NAME_POS = 10, 28
UPGRADE_DESC_POS = 10, 29
MAX_UPGRADE_COST_LEN = 10

UPGRADE_BUY_Y = 34
UPGRADE_BUY_X_START = 10
UPGRADE_BUY_X_END = 13
UPGRADE_BUY_TEXT = "Buy"

SAVE_BUTTON_Y = 40
SAVE_BUTTON_X_START = 10
SAVE_BUTTON_X_END = 14
SAVE_BUTTON_TEXT = "Save"

SAVE_LINE_POS = 10, 41
SAVE_URL_POS = 10, 42

CLICK_RATE_THRESHOLD = 0.07

COOKIE = "🍪"

def show_cookies(game):
    with canvas.lock:
      canvas.write_at(*COOKIES_POS, (util.prettify_num(game.cookies.val) + COOKIE).ljust(COOKIES_LEN))
      canvas.finish_op()

def draw_unit_widget(x, y, unit, selected):
  canvas.write_at(x, y, UNIT_HEADER)

  name = unit.unit.name.ljust(UNIT_MAX_LEN)
  count = str(unit.unit_count.val)
  name_count = (name + count).ljust(len(UNIT_HEADER))
  if selected:
    name_count = canvas.colorize(name_count, SELECT_FG, SELECT_BG)
  canvas.write_at(x, y + 1, name_count)

  cost = (util.prettify_num(unit.cost.val) + COOKIE)
  benefit = f"{util.prettify_num(unit.total_income.val)}{COOKIE} ({util.prettify_num(unit.unit_income.val, False)})"
  cost_benefit = cost + benefit.rjust(len(UNIT_HEADER) - len(cost))
  if selected:
    cost_benefit = canvas.colorize(cost_benefit, SELECT_FG, SELECT_BG)
  canvas.write_at(x, y + 2, cost_benefit)

  if selected:
    canvas.write_at(*UNIT_NAME_POS, unit.unit.name.ljust(MAX_UNIT_NAME_LEN))
    canvas.write_at(*UNIT_DESC_POS, unit.unit.desc.ljust(MAX_UNIT_DESC_LEN))

def get_unit_pos(ind):
  return UNIT_POS[0], UNIT_POS[1] + ind * UNIT_SPACING

def draw_unit_ind(units, ind, selected):
    draw_unit_widget(*get_unit_pos(ind), units[ind], selected)

def draw_all_unit_widgets(units, selected=0):
  with canvas.lock:
    for ind, _ in enumerate(units):
      draw_unit_ind(units, ind, selected == ind)
    canvas.finish_op()

def reselect_unit_widget(units, from_, to):
  with canvas.lock:
    draw_unit_ind(units, from_, False)
    draw_unit_ind(units, to, True)
    canvas.finish_op()

def redraw_unit_widget(units, ind, selected):
  with canvas.lock:
    draw_unit_ind(units, ind, selected)
    canvas.finish_op()

def draw_upgrade_widget(x, y, upgrade, selected):
  boundary = "+--+"
  wall = "|"
  if selected:
    boundary = canvas.colorize(boundary, SELECT_FG, SELECT_BG)
    wall = canvas.colorize(wall, SELECT_FG, SELECT_BG)
  
  show = canvas.colorize(upgrade.show + " ", upgrade.color)

  canvas.write_at(x, y, boundary)
  canvas.write_at(x, y + 1, wall + show + wall)
  canvas.write_at(x, y + 2, boundary)

  if selected:
    cost = util.prettify_num(upgrade.cost) + COOKIE
    name_line = upgrade.name.ljust(MAX_UPGRADE_NAME_LEN) + "  " + cost.ljust(MAX_UPGRADE_COST_LEN)
    canvas.write_at(*UPGRADE_NAME_POS, name_line)
    canvas.write_at(*UPGRADE_DESC_POS, upgrade.desc.ljust(MAX_UPGRADE_DESC_LEN))

def all_upgrades_bought():
  canvas.write_at(*UPGRADE_NAME_POS, "Gotta catch em all".ljust(MAX_UPGRADE_NAME_LEN + MAX_UPGRADE_COST_LEN + 2))
  canvas.write_at(*UPGRADE_DESC_POS, "wait no you already did".ljust(MAX_UPGRADE_DESC_LEN))
  canvas.write_at(UPGRADE_BUY_X_START, UPGRADE_BUY_Y, " " * len(UPGRADE_BUY_TEXT))

def get_upgrade_pos(ind):
  return UPGRADE_POS[0] + ind * UPGRADE_SPACING, UPGRADE_POS[1]

def clear_upgrade_widget(x, y):
  for i in range(3):
    canvas.write_at(x, y + i, " " * 4)

def draw_upgrade_ind(upgrades, ind, selected):
  draw_upgrade_widget(*get_upgrade_pos(ind), upgrades[-ind - 1], selected)

def clear_upgrade_ind(ind):
  clear_upgrade_widget(*get_upgrade_pos(ind))

def reselect_upgrade_widget(upgrades, from_, to):
  with canvas.lock:
    draw_upgrade_ind(upgrades, from_, False)
    draw_upgrade_ind(upgrades, to, True)
    canvas.finish_op()

def draw_all_upgrade_widgets(upgrades, selected):
  with canvas.lock:
    for ind, _ in enumerate(upgrades[:-UPGRADE_SHOW_MAX - 1:-1]):
      draw_upgrade_ind(upgrades, ind, selected == ind)

    if len(upgrades) < UPGRADE_SHOW_MAX:
      for ind in range(len(upgrades), UPGRADE_SHOW_MAX):
        clear_upgrade_ind(ind)

    canvas.finish_op()

class KeyBindingHandler():
  def __init__(self, game):
    self.unit_ind = 0
    self.upgrade_ind = 0

    self.last_click = 0

    self.game = game

    canvas.write_at(UNIT_BUY_X_START, UNIT_BUY_Y, UNIT_BUY_TEXT)
    canvas.write_at(UNIT_SELL_X_START, UNIT_SELL_Y, UNIT_SELL_TEXT)
    canvas.write_at(UPGRADE_BUY_X_START, UPGRADE_BUY_Y, UPGRADE_BUY_TEXT)
    canvas.write_at(SAVE_BUTTON_X_START, SAVE_BUTTON_Y, SAVE_BUTTON_TEXT)

    self.draw_indicators()

    self.add_callbacks()

    with open("cookie.txt") as cookie, canvas.lock:
      x, y = BIG_COOKIE_POS
      for line in cookie:
        line = line.rstrip("\n")
        if not line:
          continue
        canvas.write_at(x, y, line)
        y += 1
  
  def draw_indicators(self):
    show_cookies(self.game)
    draw_all_unit_widgets(self.game.units, self.unit_ind)
    draw_all_upgrade_widgets(self.game.upgrades, self.upgrade_ind)
  
  def add_callbacks(self):
    bonus.ChangeCallback(self.game.cookies, lambda _: show_cookies(self.game))

    for ind, unit in enumerate(self.game.units):
      redraw = lambda _, ind=ind: redraw_unit_widget(self.game.units, ind, self.unit_ind == ind)
      bonus.ChangeCallback(unit.unit_count, redraw)
      bonus.ChangeCallback(unit.unit_income, redraw)
      bonus.ChangeCallback(unit.cost, redraw)

  def handle_keys(self):
    paste_mode = False
    with keys.reader.set_raw():
      for key in keys.reader.read_keys(keys.reader.get_keycodes()):
        if paste_mode:
          if key == "PASTE-END":
            paste_mode = False
            self.paste("".join(pasted))
          else:
            pasted.append(key)
            continue
        if key.startswith("CLICK"):
          self.click(key)
        elif key == "PASTE-START":
          paste_mode = True
          pasted = []
        elif key in self.handlers:
          self.handlers[key](self)
    
  def enter(self):
    if (time.time() - self.last_click) > CLICK_RATE_THRESHOLD:
      self.game.click_cookie()
    self.last_click = time.time()
  
  def click(self, key):
    x, y = key[6:-1].split(",")
    x, y = int(x), int(y)
    if (UNIT_POS[0] <= x < UNIT_POS[0] + len(UNIT_HEADER)
        and UNIT_POS[1] <= y < UNIT_POS[1] + UNIT_SPACING * len(self.game.units)):
      chosen_unit = (y - UNIT_POS[1]) // UNIT_SPACING
      reselect_unit_widget(self.game.units, self.unit_ind, chosen_unit)
      self.unit_ind = chosen_unit
    
    elif (UPGRADE_POS[1] <= y < UPGRADE_POS[1] + UPGRADE_HEIGHT
          and UPGRADE_POS[0] <= x < UPGRADE_POS[0] + UPGRADE_SPACING * UPGRADE_SHOW_MAX):
      chosen_upgrade = (x - UPGRADE_POS[0]) // UPGRADE_SPACING
      if chosen_upgrade < len(self.game.upgrades):
        reselect_upgrade_widget(self.game.upgrades, self.upgrade_ind, chosen_upgrade)
        self.upgrade_ind = chosen_upgrade
    
    elif y == UNIT_BUY_Y and UNIT_BUY_X_START <= x < UNIT_BUY_X_END:
      self.right()
    
    elif y == UNIT_SELL_Y and UNIT_SELL_X_START <= x < UNIT_SELL_X_END:
      self.left()
    
    elif y == UPGRADE_BUY_Y and UPGRADE_BUY_X_START <= x < UPGRADE_BUY_X_END:
      self.k()
    
    elif y == SAVE_BUTTON_Y and SAVE_BUTTON_X_START <= x < SAVE_BUTTON_X_END:
      self.save()

    else:
      self.game.click_cookie()
  
  def up(self):
    new_unit_ind = (self.unit_ind - 1) % len(self.game.units)
    reselect_unit_widget(self.game.units, self.unit_ind, new_unit_ind)
    self.unit_ind = new_unit_ind
  
  def down(self):
    new_unit_ind = (self.unit_ind + 1) % len(self.game.units)
    reselect_unit_widget(self.game.units, self.unit_ind, new_unit_ind)
    self.unit_ind = new_unit_ind
  
  def right(self):
    self.game.buy_unit(self.game.units[self.unit_ind])
  
  def left(self):
    self.game.sell_unit(self.game.units[self.unit_ind])
  
  def j(self):
    new_upgrade_ind = self.upgrade_ind - 1
    new_upgrade_ind %= min(UPGRADE_SHOW_MAX, len(self.game.upgrades))
    reselect_upgrade_widget(self.game.upgrades, self.upgrade_ind, new_upgrade_ind)
    self.upgrade_ind = new_upgrade_ind

  def l(self):
    new_upgrade_ind = self.upgrade_ind + 1
    new_upgrade_ind %= min(UPGRADE_SHOW_MAX, len(self.game.upgrades))
    reselect_upgrade_widget(self.game.upgrades, self.upgrade_ind, new_upgrade_ind)
    self.upgrade_ind = new_upgrade_ind
  
  def k(self):
    if not self.game.upgrades:
      return
    upgrade_to_buy = self.game.upgrades[-self.upgrade_ind - 1]
    if self.game.buy_upgrade(upgrade_to_buy):
      self.game.upgrades.pop(-self.upgrade_ind - 1)
      draw_all_upgrade_widgets(self.game.upgrades, self.upgrade_ind)
    
    if not self.game.upgrades:
      all_upgrades_bought()
  
  def save(self):
    with canvas.lock:
      canvas.write_at(*SAVE_LINE_POS, "Saving...")
      canvas.finish_op()
    saved = server.save(self.game.save())
    with canvas.lock:
      canvas.write_at(*SAVE_LINE_POS, "Save data at (right click + paste to load):")
      canvas.write_at(*SAVE_URL_POS, saved)
      canvas.finish_op()
    
  def paste(self, text):
    self.game = Game.load(UNITS, UPGRADES, text)
    self.draw_indicators()
    self.add_callbacks()
  
  handlers = {
    "ENTER": enter,
    "CLICK": click,
    "UP": up,
    "DOWN": down,
    "LEFT": left,
    "RIGHT": right,
    "j": j,
    "l": l,
    "k": k,
    "s": save,
    "PASTE": paste,
  }

game = Game(UNITS, UPGRADES)

canvas = Canvas()

keys.reader.enable_clicks()
print("\x1b[?2004h") # bracketed paste for save data

canvas.write_at(*TITLE_POS, TITLE)
canvas.finish_op()

key_handler = KeyBindingHandler(game)

key_thread = threading.Thread(target=key_handler.handle_keys)
key_thread.start()

while True:
  key_handler.game.tick()
  show_cookies(key_handler.game)
  time.sleep(1)
?