level0_1.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-"
  3. """
  4. NOINIW 2051 - by psy (epsylon@riseup.net)
  5. """
  6. import pygame
  7. import sys, os, random
  8. import data.levels.shell as shell
  9. import data.menu as menu
  10. import data.levels.level0.tree as tree
  11. def level0_shell(surface, WINDOW_SIZE, set_language, ACCESS_LEVEL, NETWORK_MACHINE_NUMBER, COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_GRAY, COLOR_YELLOW, COLOR_ORANGE, COLOR_GREEN):
  12. init(surface, WINDOW_SIZE, set_language, ACCESS_LEVEL, NETWORK_MACHINE_NUMBER, COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_GRAY, COLOR_YELLOW, COLOR_ORANGE, COLOR_GREEN)
  13. def init(surface, WINDOW_SIZE, set_language, ACCESS_LEVEL, NETWORK_MACHINE_NUMBER, COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_GRAY, COLOR_YELLOW, COLOR_ORANGE, COLOR_GREEN):
  14. CURRENT_LEVEL = 0 # set current level
  15. CURRENT_LANGUAGE = set_language # set current language
  16. ACCESS_LEVEL = str(ACCESS_LEVEL)
  17. NETWORK_MACHINE_NUMBER = str(NETWORK_MACHINE_NUMBER)
  18. width= WINDOW_SIZE[0]
  19. height = WINDOW_SIZE[1]
  20. fullLine = 18
  21. font_SHELL_type = "data/fonts/Vera.ttf"
  22. font_SHELL_size = 15
  23. myFont = pygame.font.Font(font_SHELL_type, font_SHELL_size) # create font object for SHELL
  24. listColor = [COLOR_WHITE, COLOR_BLUE, COLOR_GREEN, COLOR_YELLOW, COLOR_ORANGE]
  25. changeColor = 0
  26. showCur = 0
  27. colorCur = listColor.pop(listColor.index(random.choice(listColor)))
  28. content = []
  29. contentDisplay = []
  30. listCommand = []
  31. indexListCommand = 0
  32. contentLineCurrent = ''
  33. contentLineCurrentDisplay = '|'
  34. posCursor = 0
  35. currentLine = True
  36. root = ACCESS_LEVEL+'@'+NETWORK_MACHINE_NUMBER+':~% '
  37. camTop = 0
  38. camBot = 0
  39. line = 0
  40. mac_uname, mac_root, starting_position, files_tree, macs_tree = tree.init() # extracting basics from shell: uname, root, starting position, trees...
  41. current_position = starting_position
  42. def displayText(text, at, x, y, color, bg=None):
  43. if not ACCESS_LEVEL+'@'+NETWORK_MACHINE_NUMBER+':~% ' in text:
  44. label = myFont.render(text, at, COLOR_WHITE, bg)
  45. surface.blit(label, (x, y))
  46. else:
  47. labelUser = myFont.render(ACCESS_LEVEL+'@'+NETWORK_MACHINE_NUMBER, at, (COLOR_ORANGE), bg)
  48. labelColon = myFont.render(':', at, (COLOR_WHITE), bg)
  49. labelNS = myFont.render('%', at, (COLOR_WHITE), bg)
  50. labelTilde = myFont.render('~', at, (COLOR_BLUE), bg)
  51. surface.blit(labelUser, (0, y))
  52. surface.blit(labelColon, (105, y))
  53. surface.blit(labelTilde, (113, y))
  54. surface.blit(labelNS, (125, y))
  55. text = text.replace(ACCESS_LEVEL+'@'+NETWORK_MACHINE_NUMBER+':~%', '')
  56. labelText = myFont.render(text, at, color, bg)
  57. surface.blit(labelText, (135, y))
  58. def readChar():
  59. if event.key == pygame.K_BACKSPACE:
  60. return 'backspace'
  61. elif event.key == pygame.K_PAGEUP:
  62. return 'pageup'
  63. elif event.key == pygame.K_PAGEDOWN:
  64. return 'pagedown'
  65. elif event.key == pygame.K_TAB:
  66. return 'tab'
  67. elif event.key == pygame.K_RETURN:
  68. return 'enter'
  69. elif event.key == pygame.K_ESCAPE:
  70. return 'esc'
  71. elif event.key in (pygame.K_RSHIFT, pygame.K_LSHIFT):
  72. return 'shift'
  73. elif event.key in (pygame.K_RCTRL, pygame.K_LCTRL):
  74. return 'control'
  75. elif event.key == pygame.K_RIGHT:
  76. return 'kright'
  77. elif event.key == pygame.K_LEFT:
  78. return 'kleft'
  79. elif event.key == pygame.K_UP:
  80. return 'kup'
  81. elif event.key == pygame.K_DOWN:
  82. return 'kdown'
  83. elif event.key == pygame.K_CAPSLOCK:
  84. return None
  85. elif event.key == 283:
  86. return 'begincur'
  87. elif event.key == 284:
  88. return 'endcur'
  89. elif event.key == 285:
  90. return 'delall'
  91. else:
  92. return event.unicode
  93. def progressCommand(cmd, current_position):
  94. cmd = cmd.strip()
  95. if cmd == '':
  96. return []
  97. elif cmd == 'clear': # clear
  98. return []
  99. elif cmd == 'help': # help
  100. current_help = helpCommand()
  101. return current_position, current_help, True
  102. elif cmd == 'exit': # exit
  103. menu.main() # return to login menu
  104. elif cmd == 'pwd': # pwd
  105. current_path = shell.getPWD(current_position)
  106. return current_position, current_path, False
  107. elif cmd == 'ls' or cmd == 'dir': # ls/dir
  108. current_tree = shell.getList(mac_root, current_position, files_tree)
  109. return current_position, current_tree, False
  110. elif cmd == 'id': # id
  111. user_id = shell.getId(ACCESS_LEVEL)
  112. return current_position, user_id, False
  113. elif cmd == 'whois': # whois
  114. whois = shell.getId(ACCESS_LEVEL)
  115. return current_position, whois, False
  116. elif cmd == 'hostname': # hostname
  117. hostname = shell.getHostName(NETWORK_MACHINE_NUMBER)
  118. return current_position, hostname, False
  119. elif cmd == 'uname': # uname
  120. uname = shell.getUname(mac_uname)
  121. return current_position, uname, False
  122. elif cmd == "cd ..": # cd_up
  123. current_path = shell.getCdUP(mac_root, current_position)
  124. current_position = current_path
  125. return current_position, current_path, False
  126. elif cmd.startswith('cd '):
  127. direc = cmd[3:]
  128. direc = direc.strip()
  129. current_path = shell.changePWD(mac_root, current_position, files_tree, direc)
  130. current_position = current_path
  131. return current_position, current_path, False
  132. elif cmd.startswith('cat '):
  133. file_target = cmd[3:]
  134. file_target = file_target.strip()
  135. file_text = shell.getCAT(CURRENT_LANGUAGE, CURRENT_LEVEL, current_position, files_tree, file_target)
  136. return current_position, file_text, False
  137. elif cmd.startswith('install '):
  138. file_target = cmd[7:]
  139. file_target = file_target.strip()
  140. file_text = shell.installProgram(current_position, files_tree, file_target)
  141. return current_position, file_text, False
  142. elif cmd.startswith('connect '):
  143. file_target = cmd[7:]
  144. file_target = file_target.strip()
  145. file_text = shell.connectMac(current_position, macs_tree, file_target, surface, WINDOW_SIZE, CURRENT_LANGUAGE)
  146. return current_position, file_text, False
  147. elif cmd.startswith('disconnect'):
  148. menu.main() # return to login menu
  149. else:
  150. return current_position, 'y-shell: command not found: "'+ str(cmd)+'"', False
  151. def helpCommand():
  152. lstHelp = []
  153. m = open("data/levels/help.txt", "r")
  154. shell_help = m.readlines()
  155. m.close
  156. lstHelp.append("SHELL: "+str(shell.VERSION))
  157. lstHelp.append(" ")
  158. for text in shell_help:
  159. if CURRENT_LANGUAGE == "0": # English
  160. if text.startswith("ENGLISH"):
  161. lstHelp.append(str(text.split(":")[1].replace("\n","")))
  162. else: # Spanish
  163. if text.startswith("SPANISH"):
  164. lstHelp.append(str(text.split(":")[1].replace("\n","")))
  165. lstHelp.append(" ")
  166. return lstHelp
  167. # starting main() loop
  168. while 1:
  169. fullLine = (height - 120) // 20
  170. if currentLine:
  171. if camBot - camTop == (fullLine - 1):
  172. camBot = len(contentDisplay)
  173. camTop = camBot - (fullLine - 1)
  174. for event in pygame.event.get():
  175. if event.type == pygame.KEYDOWN:
  176. newChar = readChar()
  177. if newChar not in ('delall', 'begincur', 'endcur', 'backspace', 'tab', 'enter', 'esc', 'pageup', 'pagedown',\
  178. 'shift', 'control', None, 'kright', 'kleft', 'kup', 'kdown'):
  179. try:
  180. contentLineCurrent = list(contentLineCurrent)
  181. contentLineCurrent.insert(posCursor, newChar)
  182. contentLineCurrent = ''.join(contentLineCurrent)
  183. posCursor += 1
  184. lstChar = list(contentLineCurrent)
  185. lstChar.insert(posCursor, '|')
  186. contentLineCurrentDisplay = ''.join(lstChar)
  187. except:
  188. posCursor = 0
  189. if camBot - camTop == (fullLine - 1):
  190. camTop += 1
  191. camBot += 1
  192. content.append([root])
  193. contentLineCurrent = ''
  194. contentLineCurrentDisplay = '|'
  195. indexListCommand = 0
  196. showCur = 0
  197. currentLine = True
  198. elif newChar == 'delall':
  199. contentLineCurrent = ''
  200. posCursor = 0
  201. contentLineCurrentDisplay = '|'
  202. currentLine = True
  203. elif newChar == 'begincur':
  204. posCursor = 0
  205. contentLineCurrentDisplay = '|' + contentLineCurrent
  206. currentLine = True
  207. elif newChar == 'endcur':
  208. posCursor = len(contentLineCurrent)
  209. contentLineCurrentDisplay = contentLineCurrent + '|'
  210. currentLine = True
  211. elif newChar == 'pageup':
  212. if not len(listCommand) == 0:
  213. if -len(listCommand) != indexListCommand:
  214. indexListCommand -= 1
  215. contentLineCurrent = listCommand[indexListCommand]
  216. contentLineCurrentDisplay = contentLineCurrent + '|'
  217. posCursor = len(contentLineCurrent)
  218. currentLine = True
  219. showCur = 0
  220. elif newChar == 'pagedown':
  221. if not len(listCommand) == 0:
  222. if indexListCommand < -1:
  223. indexListCommand += 1
  224. contentLineCurrent = listCommand[indexListCommand]
  225. contentLineCurrentDisplay = contentLineCurrent + '|'
  226. posCursor = len(contentLineCurrent)
  227. currentLine = True
  228. showCur = 0
  229. elif newChar == 'kup':
  230. if camTop != 0:
  231. if camBot - camTop == (fullLine - 1):
  232. camBot -= 1
  233. camTop -= 1
  234. currentLine = False
  235. showCur = 0
  236. elif newChar == 'kdown':
  237. if camBot < len(contentDisplay):
  238. camBot += 1
  239. camTop += 1
  240. showCur = 0
  241. elif newChar == 'kright':
  242. if not len(contentLineCurrent) == posCursor:
  243. posCursor += 1
  244. lstChar = list(contentLineCurrent)
  245. lstChar.insert(posCursor, '|')
  246. contentLineCurrentDisplay = ''.join(lstChar)
  247. currentLine = True
  248. showCur = 0
  249. elif newChar == 'kleft':
  250. if posCursor != 0:
  251. posCursor -= 1
  252. lstChar = list(contentLineCurrent)
  253. lstChar.insert(posCursor, '|')
  254. contentLineCurrentDisplay = ''.join(lstChar)
  255. currentLine = True
  256. showCur = 0
  257. elif newChar == 'backspace':
  258. if len(contentLineCurrent) != 0 and posCursor != 0:
  259. try:
  260. contentLineCurrent = list(contentLineCurrent)
  261. wordPoped = contentLineCurrent.pop(posCursor - 1)
  262. contentLineCurrent = ''.join(contentLineCurrent)
  263. posCursor -= 1
  264. except:
  265. contentLineCurrent = contentLineCurrent[1:]
  266. posCursor = len(contentLineCurrent)
  267. lstChar = list(contentLineCurrent)
  268. lstChar.insert(posCursor, '|')
  269. contentLineCurrentDisplay = ''.join(lstChar)
  270. currentLine = True
  271. showCur = 0
  272. elif newChar == 'enter':
  273. currentLine = True
  274. indexListCommand = 0
  275. if camBot - camTop == (fullLine - 1):
  276. camTop += 1
  277. camBot += 1
  278. content.append([root + contentLineCurrent])
  279. if contentLineCurrent.strip() == 'clear':
  280. camTop = 0
  281. camBot = 0
  282. posCursor = 0
  283. content = []
  284. contentLineCurrent = ''
  285. contentLineCurrentDisplay = '|'
  286. else:
  287. current_position, contentAppend, help_mode = progressCommand(contentLineCurrent, current_position)
  288. if help_mode == False:
  289. content.append(contentAppend)
  290. else:
  291. for eachLine in contentAppend:
  292. if camBot - camTop == (fullLine - 1):
  293. camTop += 1
  294. camBot += 1
  295. content.append(eachLine)
  296. help_mode = False
  297. if len(contentLineCurrent.strip(' ')) != 0:
  298. listCommand.append(contentLineCurrent)
  299. posCursor = 0
  300. contentLineCurrent = ''
  301. contentLineCurrentDisplay = '|'
  302. showCur = 0
  303. surface.fill(COLOR_BLACK)
  304. changeColor += 1
  305. showCur += 1
  306. contentDisplay = []
  307. for i in range(len(content)):
  308. text = ''.join(content[i])
  309. if len(text) * 8 > width:
  310. while len(text) * 8 > width:
  311. textMini = text[:width // 8 - 1]
  312. contentDisplay.append(textMini)
  313. text = text[width // 8 - 1:]
  314. contentDisplay.append(text)
  315. else:
  316. contentDisplay.append(text)
  317. if currentLine:
  318. if len(contentDisplay) >= fullLine:
  319. camBot = len(contentDisplay)
  320. camTop = camBot - (fullLine - 1)
  321. else:
  322. camBot = len(contentDisplay)
  323. camTop = 0
  324. for i in range(camTop, camBot):
  325. displayText(contentDisplay[i], 1, 0, line * 20, COLOR_WHITE)
  326. line += 1
  327. if camBot == len(contentDisplay):
  328. if showCur < 500:
  329. displayText(root + contentLineCurrentDisplay, 1, 0, line * 20, COLOR_WHITE)
  330. else:
  331. displayText(root + contentLineCurrent, 1, 0, line * 20, COLOR_WHITE)
  332. if showCur > 1000:
  333. showCur = 0
  334. else:
  335. displayText(''.join(contentDisplay[camBot]), 1, 0, line * 20, COLOR_WHITE)
  336. line = 0
  337. pygame.display.flip()