SHOW:
|
|
- or go back to the newest paste.
| 1 | shell.run("sha1")
| |
| 2 | chat = peripheral.wrap("left")
| |
| 3 | function split(str, pat) | |
| 4 | local t = {} -- NOTE: use {n = 0} in Lua-5.0
| |
| 5 | local fpat = "(.-)" .. pat | |
| 6 | local last_end = 1 | |
| 7 | local s, e, cap = str:find(fpat, 1) | |
| 8 | while s do | |
| 9 | if s ~= 1 or cap ~= "" then | |
| 10 | table.insert(t,cap) | |
| 11 | end | |
| 12 | last_end = e+1 | |
| 13 | s, e, cap = str:find(fpat, last_end) | |
| 14 | end | |
| 15 | if last_end <= #str then | |
| 16 | cap = str:sub(last_end) | |
| 17 | table.insert(t, cap) | |
| 18 | end | |
| 19 | return t | |
| 20 | end | |
| 21 | modem=peripheral.wrap("bottom")
| |
| 22 | modem.open(1) | |
| 23 | function makeWallet(hash,owner) | |
| 24 | if not fs.exists("/wallets/"..hash) then
| |
| 25 | fs.makeDir("/wallets/"..hash)
| |
| 26 | n = fs.open("/wallets/"..hash.."/owner","w")
| |
| 27 | n.write(owner) | |
| 28 | n.close() | |
| 29 | fi=fs.open("/wallets/"..hash.."/inf/","w")
| |
| 30 | fi.write("0")
| |
| 31 | fi.close() | |
| 32 | return true | |
| 33 | else | |
| 34 | return false | |
| 35 | end | |
| 36 | end | |
| 37 | function readOwner(hash) | |
| 38 | n = fs.open("/wallets/"..hash.."/owner","r")
| |
| 39 | owner = n.readAll() | |
| 40 | n.close() | |
| 41 | return owner | |
| 42 | end | |
| 43 | function checkWallet(hash) | |
| 44 | return fs.exists("/wallets/"..hash)
| |
| 45 | end | |
| 46 | function delWallet(hash) | |
| 47 | fs.delete("/wallets/"..hash)
| |
| 48 | end | |
| 49 | function nameToWH(name) | |
| 50 | return SHA1(name) | |
| 51 | end | |
| 52 | function setBal(hash,bal) | |
| 53 | if fs.exists("/wallets/"..hash.."/inf/") then
| |
| 54 | fs.delete("/wallets/"..hash.."/inf/")
| |
| 55 | end | |
| 56 | fi=fs.open("/wallets/"..hash.."/inf/","w")
| |
| 57 | fi.write(tostring(bal)) | |
| 58 | fi.close() | |
| 59 | end | |
| 60 | function getBal(hash) | |
| 61 | fi=fs.open("/wallets/"..hash.."/inf/","r")
| |
| 62 | bal=tonumber(fi.readAll()) | |
| 63 | fi.close() | |
| 64 | return bal | |
| 65 | end | |
| 66 | ownerName="justync7" | |
| 67 | term.clear() | |
| 68 | term.setCursorPos(1,1) | |
| 69 | print("CraftCoin Alpha 1.0 Master Server")
| |
| 70 | if not fs.exists("/wallets/") then
| |
| 71 | fs.makeDir("/wallets/")
| |
| 72 | end | |
| 73 | ownerWallet=SHA1(ownerName) | |
| 74 | walletCount=0 | |
| 75 | for i,v in pairs(fs.list("/wallets/")) do
| |
| 76 | walletCount=walletCount+1 | |
| 77 | end | |
| 78 | pi=3.14159265358979323846264338327950288419716939937510 | |
| 79 | maxCoin=walletCount*1500000/(pi*2) | |
| 80 | print("Max CraftCoins is "..maxCoin)
| |
| 81 | while true do | |
| 82 | event, player, message, reply, msg = os.pullEventRaw() | |
| 83 | if event == "chat" then | |
| 84 | messagep = split(message," ") | |
| 85 | if messagep[1] == ".echo" then | |
| 86 | chat.say(message:sub(7)) | |
| 87 | elseif messagep[1] == ".cc" then | |
| 88 | if messagep[2] == "wallet" then | |
| 89 | if messagep[3] == "new" then | |
| 90 | t = makeWallet(nameToWH(player),player) | |
| 91 | if t == true then | |
| 92 | chat.tell(player,"Sucessfully created your new wallet!") | |
| 93 | else | |
| 94 | chat.tell(player,"Failed to create wallet, already exists or error, contact justync7.") | |
| 95 | end | |
| 96 | elseif messagep[3] == "check" then | |
| 97 | ts = checkWallet(messagep[4]) | |
| 98 | if ts == true then | |
| 99 | chat.tell(player,"True: Wallet exists.") | |
| 100 | else | |
| 101 | chat.tell(player,"False: Wallet does not exist.") | |
| 102 | end | |
| 103 | elseif messagep[3] == "bal" then | |
| 104 | if messagep[4] == "user" then | |
| 105 | chat.tell(player,tostring(getBal(nameToWH(messagep[5])))) | |
| 106 | elseif messagep[4] == "wallet" then | |
| 107 | chat.tell(player,tostring(getBal(messagep[5]))) | |
| 108 | end | |
| 109 | elseif messagep[3] == "pay" then | |
| 110 | if messagep[4] == "user" then | |
| 111 | rec=getBal(nameToWH(messagep[5])) | |
| 112 | sen=getBal(nameToWH(player)) | |
| 113 | sen=sen-tonumber(messagep[6]) | |
| 114 | rec=rec+tonumber(messagep[6]) | |
| 115 | setBal(nameToWH(messagep[5]),rec) | |
| 116 | setBal(nameToWH(player),sen) | |
| 117 | elseif messagep[4] == "wallet" then | |
| 118 | rec=getBal(messagep[5]) | |
| 119 | sen=getBal(nameToWH(player)) | |
| 120 | sen=sen-tonumber(messagep[6]) | |
| 121 | rec=rec+tonumber(messagep[6]) | |
| 122 | setBal(messagep[5],rec) | |
| 123 | setBal(nameToWH(player),sen) | |
| 124 | end | |
| 125 | end | |
| 126 | elseif messagep[2] == "mine" then | |
| 127 | modem.transmit(1,1,"startmine "..nameToWH(player)) | |
| 128 | chat.tell(player,"You are now mining for 1 CraftCoin!") | |
| 129 | elseif messagep[2] == "lua" then | |
| 130 | pcall(message:sub(9)) | |
| 131 | end | |
| 132 | end | |
| 133 | elseif event == "modem_message" then | |
| 134 | messagep = split(msg," ") | |
| 135 | if messagep[1] == "mined" then | |
| 136 | if messagep[2] then | |
| 137 | bal = getBal(messagep[2]) | |
| 138 | bal = bal + 1 | |
| 139 | setBal(messagep[2],bal) | |
| 140 | chat.tell(readOwner(messagep[2]),"You mined a coin! +1 CraftCoin") | |
| 141 | end | |
| 142 | end | |
| 143 | end | |
| 144 | end |