-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.lua
More file actions
54 lines (46 loc) · 1.33 KB
/
script.lua
File metadata and controls
54 lines (46 loc) · 1.33 KB
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
chatbot_port = 9005
steam_ids = {}
tick = 0
function onTick()
tick = tick + 1
if tick % 30 == 0 then
server.httpGet(chatbot_port, "/getmsgs")
server.httpGet(chatbot_port, "/setplayers?p="..#server.getPlayers()-1)
end
end
function onCreate(is_world_create)
for _, player in pairs(server.getPlayers()) do
steam_ids[player.id] = tostring(player.steam_id)
end
end
function httpReply(port, url, response_body)
if url == "/getmsgs" and response_body ~= "OK" then
pos = string.find(response_body,"\r")
if pos ~= nil then
name = string.sub(response_body,0,pos)
msg = string.sub(response_body,pos,string.len(response_body))
server.announce(name, msg)
end
end
end
function onPlayerJoin(steam_id, name, peer_id, admin, auth)
steam_ids[peer_id] = tostring(steam_id)
server.httpGet(chatbot_port, "/join?sid="..steam_id)
end
function onPlayerLeave(steam_id, name, peer_id, is_admin, is_auth)
server.httpGet(chatbot_port, "/leave?sid="..steam_id)
end
function onChatMessage(user_peer_id, sender_name, message)
server.httpGet(chatbot_port, "/sendmsg?msg="..encode(message).."&sid="..encode(steam_ids[user_peer_id]))
end
function encode(str)
if str == nil then
return ""
end
str = string.gsub(str, "([^%w _ %- . ~])", cth)
str = str:gsub(" ", "%%20")
return str
end
function cth(c)
return string.format("%%%02X", string.byte(c))
end