-
-
Save SkyTheCoder/67406f2b6dc7f1ca4511 to your computer and use it in GitHub Desktop.
Codea Project Gist Created with AutoGist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Chat Room | |
-- Use this function to perform your initial setup | |
function setup() | |
print("Hello World!") | |
str = "" | |
LSTRING = "" | |
bcolor = 0 | |
l = 0 | |
t = 50 | |
r = WIDTH | |
b = 0 | |
prev = {} | |
server = "https://gist.github.com/SkyTheCoder/b09cd401cc606277e723/raw/6a58ae501cbc684e1cefd5c5dbbaf8145050edc6/ChatRoomData" | |
failFunction = function(error) | |
tPrint("Error during http request:") | |
tPrint(error) | |
end | |
http.request(server, function() | |
tPrint("Success!") | |
end, failFunction, {method = "POST", data = "Does this message appear? Oh, well..."}) | |
response = "" | |
http.request(server, function(data) | |
--prev = string.split(data, "\n") | |
end, failFunction, {method = "GET"}) | |
--prev = string.split(response, "\n") | |
end | |
-- This function gets called once every frame | |
function draw() | |
smooth() | |
background(255) | |
stroke(127) | |
strokeWidth(6) | |
fill(0) | |
font("Inconsolata") | |
fontSize(36) | |
textMode(CENTER) | |
text("Chat Room",WIDTH/2,HEIGHT-25) | |
fontSize(24) | |
text("By SkyTheCoder",WIDTH/2,HEIGHT-50) | |
fontSize(14) | |
text(os.date(),WIDTH/2,HEIGHT-75) | |
fontSize(24) | |
if bcolor == 0 then | |
fill(0) | |
stroke(127) | |
else | |
--fill(127) | |
--stroke(63) | |
end | |
rectMode(CENTER) | |
textMode(CORNER) | |
if isKeyboardShowing() then | |
rect(WIDTH / 2, (HEIGHT / 2) - 10, WIDTH, 50) | |
else | |
rect(WIDTH / 2,25,WIDTH,50) | |
end | |
rectMode(CORNER) | |
for k,v in ipairs(prev) do | |
fill(0) | |
if isKeyboardShowing() then | |
text(v, 10, ((HEIGHT / 2) + 45) + (30 * (#prev - k))) | |
else | |
text(v, 10, 55 + (30 * (#prev - k))) | |
end | |
end | |
buffer = keyboardBuffer() | |
fill(255) | |
if isKeyboardShowing() then | |
if buffer ~= nil then | |
local l = "" | |
if math.floor(((math.sin(5*ElapsedTime)+1)*0.5)+0.5) < 0.5 and isKeyboardShowing() then | |
l = "|" | |
end | |
text(buffer .. l,10,(HEIGHT/2)-25) | |
end | |
else | |
bcolor = 0 | |
--text(LSTRING,10,10) | |
end | |
rectMode(STANDARD) | |
textMode(STANDARD) | |
end | |
function touched(touch) | |
if touch.x > l and touch.x < r and touch.y > b and touch.y < t then | |
showKeyboard() | |
bcolor = 1 | |
else | |
if isKeyboardShowing() then | |
hideKeyboard() | |
bcolor = 0 | |
end | |
end | |
end | |
function keyboard(key) | |
if key == BACKSPACE then | |
buffer = string.sub(buffer, 1, string.len(buffer) - 1) | |
end | |
if key == RETURN then | |
str = buffer | |
LSTRING = str | |
hideKeyboard() | |
tPrint(str) | |
http.request(server, function(data) | |
end, failFunction, {method = "PATCH", data = "\n" .. str}) | |
end | |
end | |
function tPrint(text) | |
table.insert(prev, tostring(text)) | |
end | |
function string.replace(s, pattern, replacement) | |
return (s:gsub(pattern, replacement)) | |
end | |
function string.remove(s, pattern) | |
return string.replace(s, pattern, "") | |
end | |
function string.startsWith(s, pattern) | |
return not (not s:find("^"..pattern)) | |
end | |
function string.endsWith(s, pattern) | |
return not (not s:find(pattern .. "$")) | |
end | |
function string.split(s, pattern) | |
local t = {} | |
for w in s:gmatch("[^"..pattern.."]+") do | |
table.insert(t, w) | |
end | |
return t | |
end | |
function string.allChars(s) | |
local t = {} | |
for c in s:gmatch(".") do | |
table.insert(t, c) | |
end | |
return t | |
end | |
function string.slice(s, interval) | |
t = {} | |
for sl in s:gmatch(string.rep(".", interval)) do | |
table.insert(t, sl) | |
end | |
return t | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment