Advertisement
Guest User

serwer 0.1e

a guest
Jul 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.33 KB | None | 0 0
  1. -- Edytor Strony/Serwer wersja 0.1e
  2.  
  3. ver = "0.1e"
  4.  
  5. if fs.exists("FreshApi") then
  6. os.unloadAPI("FreshApi")
  7. os.loadAPI("FreshApi")
  8. else
  9. error("Zainstaluj FreshAPI")
  10. end
  11.  
  12. function sTable(file, table)
  13. f = fs.open(file, "w")
  14. f.write(textutils.serialize(table))
  15. f.close()
  16. end
  17.  
  18. function lTable(file)
  19. f = fs.open(file, "r")
  20. table = textutils.unserialize(f.readAll())
  21. f.close()
  22. return table
  23. end
  24.  
  25. function ping()
  26. received = {}
  27. test = {}
  28. n2 = {}
  29. a = 0
  30. if config[3] == 2 then
  31. sleep(0.5)
  32. end
  33. while true do
  34. a = a + 1
  35. received[a] = {}
  36. event, id, txt, xd = os.pullEvent("rednet_message")
  37. if txt:find("BTSHello") then
  38. n = textutils.unserialize(txt)
  39. n2[a] = n[2]
  40. received[a][1] = id
  41. received[a][2] = n2[a]
  42. received[a][3] = xd
  43. end
  44. end
  45. end
  46.  
  47. function timer()
  48. if config[3] == 2 then
  49. timer1 = os.startTimer(5)
  50. else
  51. timer1 = os.startTimer(0.2)
  52. end
  53. while true do
  54. event, timer2 = os.pullEvent("timer")
  55. if event == "timer" and timer2 == timer1 then
  56. break
  57. end
  58. end
  59. end
  60.  
  61. function getBTS()
  62. rednet.broadcast("HelloBTS")
  63. parallel.waitForAny(timer, ping)
  64. return received
  65. end
  66.  
  67. function inf (gt)
  68. term.clear()
  69. term.setCursorPos(1,1)
  70. FreshApi.centerPrint("| Edytor Strony ".. ver.. " |")
  71. term.setCursorPos(1,2)
  72. if gt then
  73. textutils.slowPrint(FreshApi.line(), 100)
  74. else
  75. FreshApi.drawLine()
  76. end
  77. term.setCursorPos(1,3)
  78. term.clearLine()
  79. end
  80.  
  81. function signal(dis)
  82. dis = tonumber(dis)
  83. sig = "[ ]"
  84. if dis == nil then
  85. dis = 513
  86. sig = "[ ]"
  87. end
  88. if dis < 113 then
  89. sig = "[*****]"
  90. end
  91. if dis < 201 then
  92. if dis > 113 then
  93. sig = "[**** ]"
  94. end
  95. end
  96. if dis < 301 then
  97. if dis > 201 then
  98. sig = "[*** ]"
  99. end
  100. end
  101. if dis < 401 then
  102. if dis > 301 then
  103. sig = "[** ]"
  104. end
  105. end
  106. if dis < 513 then
  107. if dis > 401 then
  108. sig = "[* ]"
  109. end
  110. end
  111. return sig
  112. end
  113.  
  114. function connect()
  115. getBTS()
  116. our = {}
  117. r = "false"
  118. output = "false"
  119. p = 0
  120. for i=1, #received do
  121. if n2[i] == config[2] then
  122. p = p + 1
  123. our[p] = received[i][1]
  124. end
  125. end
  126. for i=1, #our do
  127. rednet.send(our[i], "connectBTS")
  128. parallel.waitForAny(timer, receive)
  129. if txt == "true" then
  130. output = id2
  131. name = config[2]
  132. break
  133. end
  134. end
  135. if output == "false" then
  136. sleep(1)
  137. for i=1, #received do
  138. for o=1, #config[4] do
  139. if n2[i] == config[4][o] then
  140. rednet.send(received[i][1], "connectBTS")
  141. parallel.waitForAny(timer, receive)
  142. if txt == "true" then
  143. name = config[4][o]
  144. r = "true"
  145. output = id2
  146. break
  147. end
  148. end
  149. end
  150. end
  151. end
  152. return output, name, r
  153. end
  154.  
  155. function receive()
  156. ot = "false"
  157. while true do
  158. event, id2, txt, dis = os.pullEvent("rednet_message")
  159. if event == "rednet_message" then
  160. ot = "true"
  161. break
  162. end
  163. return id2, txt, dis, ot
  164. end
  165. end
  166.  
  167. function wysSig(id, name, r)
  168. while true do
  169. if id == "false" then
  170. FreshApi.centerText("Brak Polaczenia...")
  171. while true do
  172. id = connect()
  173. if id == "false" then
  174. else
  175. break
  176. end
  177. end
  178. end
  179. rednet.send(id, "HelloBTS")
  180. parallel.waitForAny(timer, receive)
  181. nm = ""
  182. if ot then
  183. nm = " ".. tostring(name)
  184. else
  185. nm = "Brak Polaczenia"
  186. end
  187. term.clear()
  188. term.setCursorPos(1,1)
  189. print(nm)
  190. if r == "true" then
  191. term.setCursorPos(39, 1)
  192. print("[R] ".. tostring(signal(dis)))
  193. else
  194. term.setCursorPos(43, 1)
  195. print(tostring(signal(dis)))
  196. end
  197. term.setCursorPos(1,1)
  198. FreshApi.centerPrint(" | Edytor Strony ".. "| ")
  199. term.setCursorPos(1,2)
  200. FreshApi.drawLine()
  201. term.setCursorPos(1,3)
  202. term.clearLine()
  203. if ot then
  204. break
  205. else
  206. FreshApi.centerText("Brak Polaczenia...")
  207. while true do
  208. id = connect()
  209. if id == "false" then
  210. else
  211. break
  212. end
  213. end
  214. end
  215. end
  216. end
  217.  
  218. function edit(site)
  219. if fs.exists("edytor/".. site) then
  220. cal = lTable("edytor/".. site)
  221. tab = cal[1]
  222. tec = cal[2]
  223. else
  224. tab = {"Link przenies na inna strone|zmien w wlasc. ln.)", "Wysrodkuj (w wlasc. ln.)", "tekst (aby usunac wcisnij enter 2 razy)", "Zapisz wciskajac F3, wyjdz wciskajac F4" }
  225. tec = {}
  226. end
  227. t = 1
  228. d = 5
  229. wybor = 7
  230. w,h = term.getSize()
  231. scr = 0
  232. wysSig(id, name, r)
  233. term.setCursorPos(1,3)
  234. term.write("Edit:")
  235. term.setCursorPos(1,3)
  236. FreshApi.centerPrint(site)
  237. term.setCursorPos(1,4)
  238. FreshApi.drawLine()
  239. while true do
  240. term.setCursorPos(1,3)
  241. term.clearLine()
  242. term.write("Edit:")
  243. term.setCursorPos(1,3)
  244. FreshApi.centerPrint(site)
  245. term.setCursorPos(1,4)
  246. FreshApi.drawLine()
  247. c,v = term.getSize()
  248. --if #tab > 10 then
  249. tabL = 12
  250. --else
  251. -- tabL = #tab
  252. --end
  253. a = 0
  254. ln = "Ln ".. wybor-6
  255. term.setCursorPos(50-#ln,18)
  256. term.clearLine()
  257. term.setCursorPos(1, 18)
  258. term.write("Enter - Wpisz tekst F1 - Wlasciwosci ln.")
  259. term.setCursorPos(1, 17)
  260. term.clearLine()
  261. term.write("F3 - Zapisz F4 - Wyjdz")
  262. term.setCursorPos(50-#ln,18)
  263. term.write(ln)
  264. for i=1,tabL do
  265. a = a + 1
  266. term.setCursorPos(t, d + a - 1)
  267. term.clearLine()
  268. if a+scr == wybor-6 then
  269. if tab[i+scr] == nil then
  270. write(">")
  271. else
  272. write(">".. tostring(tab[i+scr]))
  273. end
  274. else
  275. if tab[i+scr] == nil then
  276. write("")
  277. else
  278. write(" ".. tostring(tab[i+scr]))
  279. end
  280. end
  281. end
  282. event, key = os.pullEvent("key")
  283. if key == 200 and wybor-6>1 then
  284. if wybor > 12 then
  285. scr = scr - 1
  286. end
  287. wybor = wybor - 1
  288. elseif key == 208 then
  289. wybor = wybor + 1
  290. if wybor > 12 then
  291. scr = scr + 1
  292. end
  293. elseif key == 59 then
  294. elseif key == 61 then
  295. cal = {}
  296. cal[1] = tab
  297. cal[2] = tec
  298. sTable("edytor/".. site, cal)
  299. term.setCursorPos(1, 3)
  300. term.clearLine()
  301. FreshApi.centerPrint("Zapisano.")
  302. sleep(1)
  303. elseif key == 62 then
  304. break
  305. elseif key == 28 then
  306. term.setCursorPos(1, 18)
  307. term.clearLine()
  308. term.write('Wpisz "Anuluj123" aby anulowac')
  309. term.setCursorPos(1, 3)
  310. term.clearLine()
  311. FreshApi.centerPrint("Wpisz tekst")
  312. term.setCursorPos(1,17)
  313. term.clearLine()
  314. term.write(">")
  315. term.setCursorPos(2,17)
  316. input = read()
  317. if #input > 48 then
  318. term.setCursorPos(1,17)
  319. term.clearLine()
  320. FreshApi.centerPrint("Tekst nie moze przekraczac 48 znakow.")
  321. sleep(1.5)
  322. wysSig(id, name, r)
  323. else
  324. if input == "Anuluj123" or input == '"Anuluj123"' then
  325. else
  326. tab[wybor-6] = input
  327. end
  328. end
  329. end
  330. end
  331. end
  332.  
  333. function server()
  334. txtT = {}
  335. p = {}
  336. k = {}
  337. u = {}
  338. for i=1, #config[6] do
  339. k[i] = config[6][i]
  340. u[i] = config[5].. ".mc/".. config[6][i]
  341. end
  342. k[#k+1] = config[5]
  343. u[#u+1] = config[5].. ".mc"
  344. while true do
  345. while true do
  346. event1, id3, txt1, dis1 = os.pullEvent("rednet_message")
  347. if event == "rednet_message" then
  348. break
  349. end
  350. end
  351. --local ok, err = pcall(function() txtT = textutils.unserialize(txt) end)
  352. --if not ok then
  353. -- print("No table")
  354. --else
  355. txtT = textutils.unserialize(txt1)
  356. if txtT == nil then
  357. else
  358. for i=1, #u do
  359. if txtT[2] == u[i] then
  360. if fs.exists("edytor/".. k[i]) then
  361. idD = txtT[1]
  362. p[1] = txtT[1]
  363. p[2] = lTable("edytor/".. k[i])
  364. p1 = textutils.serialize(p)
  365. print("wyslalo")
  366. rednet.send(id3, p1)
  367. end
  368. end
  369. end
  370. end
  371. end
  372. end
  373. --end
  374.  
  375. function sConnect ()
  376. while true do
  377. term.clear()
  378. id, name, r = connect()
  379. wysSig(id, name, r)
  380. sleep(10)
  381. end
  382. end
  383.  
  384. sides = rs.getSides()
  385. config = {}
  386. term.clear()
  387. inf()
  388.  
  389. if fs.exists("edytor/Settings") then
  390. config = lTable("edytor/Settings")
  391. rednet.open(config[1])
  392. else
  393. term.setCursorPos(1,3)
  394. FreshApi.centerPrint("Pierwsza konfiguracja.")
  395. sleep(1.5)
  396. term.setCursorPos(1,3)
  397. term.clearLine()
  398. FreshApi.centerPrint("Wybierz rodzaj polaczenia.")
  399. s = FreshApi.drawMenu({"Modem", "Kabel"}, 2, 5)
  400. config[3] = s
  401. term.setCursorPos(1,3)
  402. term.clearLine()
  403. inf(slow)
  404. if config[3] == 1 then
  405. FreshApi.centerPrint("Wybierz strone z ktorej jest modem.")
  406. else
  407. FreshApi.centerPrint("Wybierz strone z ktorej jest kabel.")
  408. end
  409. s = FreshApi.drawMenu(sides, 2, 5)
  410. config[1] = sides[s]
  411. rednet.open(config[1])
  412. inf(slow)
  413. term.setCursorPos(1, 3)
  414. term.clearLine()
  415. getBTS()
  416. FreshApi.centerPrint("Lista dostepnych operatorow. Wybierz jednego.")
  417. list = {}
  418. wys = {}
  419. for i=1, #n2 do
  420. if n2[i] == nil then
  421. else
  422. for i2=i+1, #n2 do
  423. if n2[i] == n2[i2] then
  424. table.remove(n2, i2)
  425. table.remove(received, i2)
  426. end
  427. end
  428. end
  429. end
  430. for i=1, #n2 do
  431. if not n2[i] == nil then
  432. else
  433. list[i] = n2[i]
  434. zm = n2[i]
  435. if config[3] == 1 then
  436. wys[i] = " ".. zm.. " ".. tostring(signal(received[i][3])).. " "
  437. else
  438. wys[i] = " ".. zm.. " "
  439. end
  440. end
  441. end
  442. wbr = FreshApi.drawMenu(wys, 2, 5)
  443. config[2] = list[wbr]
  444. y = 1
  445. w = {}
  446. list2 = {}
  447. for i=1, #list do
  448. list2[i] = " ".. list[i].. " "
  449. end
  450. list2[#list2+1] = "Dalej"
  451. inf(slow)
  452. term.setCursorPos(1, 3)
  453. term.clearLine()
  454. FreshApi.centerPrint("Lista dostepnych operatorow. Wybierz rumuningi.")
  455. while true do
  456. inf(false)
  457. term.setCursorPos(1, 3)
  458. term.clearLine()
  459. FreshApi.centerPrint("Lista dostepnych operatorow. Wybierz rumuningi.")
  460. l = FreshApi.drawMenu(list2, 2, 5)
  461. if l == #list2 then
  462. break
  463. else
  464. finding = list2[l]
  465. if finding:find("[R]") then
  466. list2[l] = " ".. list[l].. " "
  467. else
  468. list2[l] = " ".. list2[l].. " [R] "
  469. end
  470. end
  471. end
  472. for i=1, #list2 do
  473. finding = list2[i]
  474. if finding:find("[R]") then
  475. w[y] = list[i]
  476. y = y + 1
  477. end
  478. end
  479. config[4] = w
  480. m = {"Wpisz nazwe", "Ustaw nazwe"}
  481. gID = os.getComputerID()
  482. while true do
  483. inf()
  484. term.setCursorPos(1,3)
  485. term.clearLine()
  486. FreshApi.centerPrint("Podaj nazwe strony.")
  487. s = FreshApi.drawMenu(m, 2, 5)
  488. if s == 1 then
  489. term.setCursorPos(2,2)
  490. nazwa = tostring(read())
  491. m[3] = nazwa.. ".mc"
  492. end
  493. if s == 2 then
  494. a = "true"
  495. if #nazwa > 31 then
  496. term.setCursorPos(1,3)
  497. FreshApi.centerPrint("Nazwa nie moze przekraczac 30 znakow.")
  498. a = "false"
  499. sleep(1.5)
  500. end
  501. if #nazwa < 1 then
  502. term.setCursorPos(1,3)
  503. FreshApi.centerPrint("Podaj nazwe.")
  504. a = "false"
  505. sleep(1.5)
  506. end
  507. if a == "true" then
  508. config[5] = nazwa
  509. break
  510. end
  511. end
  512. end
  513. config[6] = {}
  514. fs.makeDir("edytor")
  515. sTable("edytor/Settings", config)
  516. FreshApi.centerText("Skonfigurowano pomyslnie.")
  517. sleep(1)
  518. term.clear()
  519. end
  520.  
  521. while true do
  522. term.clear()
  523. FreshApi.centerText("Laczenie...")
  524. id, name, r = connect()
  525. term.clear()
  526. wysSig(id, name, r)
  527. term.setCursorPos(1,3)
  528. FreshApi.centerPrint("Wybierz opcje.")
  529. term.setCursorPos(1,4)
  530. FreshApi.drawLine()
  531. wbr = FreshApi.drawMenu({"Edytuj strone", "Podglag strony", "Uruchom serwer", "Ustawienia", "Informacje"}, 2, 6)
  532. if wbr == 3 then
  533. --parallel.waitForAny(sConnect, server)
  534. server()
  535. end
  536. if wbr == 1 then
  537. wysSig(id, name, r)
  538. term.setCursorPos(1,3)
  539. FreshApi.centerPrint("Wybierz opcje.")
  540. term.setCursorPos(1,4)
  541. FreshApi.drawLine()
  542. ps = FreshApi.drawMenu({"Edytuj stone/podstrone", "Utworz podstrone", "Usun podstrone", "Wstecz"}, 2, 6)
  543. if ps == 3 then
  544. wb = {}
  545. wb[1] = "Wstecz"
  546. for i=1, #config[6] do
  547. wb[i+1] = config[5].. ".mc/".. config[6][i]
  548. end
  549. wysSig(id, name, r)
  550. term.setCursorPos(1,3)
  551. FreshApi.centerPrint("Wybierz podstrone do usuniecia.")
  552. term.setCursorPos(1,4)
  553. FreshApi.drawLine()
  554. kl = FreshApi.drawMenu(wb, 2, 6)
  555. if kl == 1 then
  556. else
  557. config[6][kl-1] = nil
  558. for i=1, #config[6] do
  559. if config[6][i] == nil then
  560. table.remove(config[6], i)
  561. end
  562. end
  563. sTable("edytor/Settings", config)
  564. end
  565. end
  566. if ps == 1 then
  567. wb = {}
  568. wb[1] = "Wstecz"
  569. wb[2] = config[5].. ".mc"
  570. for i=1, #config[6] do
  571. wb[i+2] = config[5].. ".mc/".. config[6][i]
  572. end
  573. wysSig(id, name, r)
  574. term.setCursorPos(1,3)
  575. FreshApi.centerPrint("Wybierz strone do edytowania.")
  576. term.setCursorPos(1,4)
  577. FreshApi.drawLine()
  578. kl = FreshApi.drawMenu(wb, 2, 6)
  579. if kl == 1 then
  580. end
  581. if kl == 2 then
  582. edit(config[5])
  583. end
  584. if kl > 2 then
  585. edit(config[6][kl-2])
  586. end
  587. end
  588. if ps == 2 then
  589. nazwa = ""
  590. m = {"Wpisz nazwe", "Ustaw nazwe", "Wstecz"}
  591. gID = os.getComputerID()
  592. while true do
  593. wysSig(id, name, r)
  594. term.setCursorPos(1,3)
  595. term.clearLine()
  596. FreshApi.centerPrint("Podaj nazwe podstrony.")
  597. term.setCursorPos(1,4)
  598. FreshApi.drawLine()
  599. s = FreshApi.drawMenu(m, 2, 6)
  600. if s == 3 then
  601. break
  602. end
  603. if s == 1 then
  604. term.setCursorPos(2,2)
  605. nazwa = tostring(read())
  606. m[4] = config[5].. ".mc".. "/".. nazwa
  607. end
  608. if s == 2 then
  609. a = "true"
  610. if #nazwa > 34 - #config[5] then
  611. term.setCursorPos(1,3)
  612. FreshApi.centerPrint("Nazwa nie moze przekraczac ".. 34 - #config[5] " znakow.")
  613. a = "false"
  614. sleep(1.5)
  615. end
  616. for i=1, #config[6] do
  617. if nazwa == #config[6][i] then
  618. term.setCursorPos(1,3)
  619. FreshApi.centerPrint("Nazwa nie moze przekraczac ".. 34 - #config[5] " znakow.")
  620. a = "false"
  621. sleep(1.5)
  622. end
  623. end
  624. if #nazwa < 1 then
  625. term.setCursorPos(1,3)
  626. FreshApi.centerPrint("Podaj nazwe.")
  627. a = "false"
  628. sleep(1.5)
  629. end
  630. if a == "true" then
  631. wysSig(id, name, r)
  632. config[6][#config[6]+1] = nazwa
  633. sTable("edytor/Settings", config)
  634. break
  635. end
  636. end
  637. end
  638. end
  639. end
  640. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement