forked from endgame/MudCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.lua
More file actions
57 lines (51 loc) · 1.5 KB
/
boot.lua
File metadata and controls
57 lines (51 loc) · 1.5 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
55
56
57
-- This program is free software. It comes without any warranty, to
-- the extent permitted by applicable law. You can redistribute it
-- and/or modify it under the terms of the Do What The Fuck You Want
-- To Public License, Version 2, as published by Sam Hocevar. See
-- http://sam.zoy.org/wtfpl/COPYING for more details.
-- Map player names to their descriptor.
PLAYERS = {}
-- Remove leading/trailing whitespace.
function string:trim()
return (self:gsub('^%s*(.-)%s*$', '%1'))
end
-- Read in a loop until a valid name has been read.
function mud.descriptor:read_name()
local name = self:read():trim():lower()
if name:match '^%l+$' then
if PLAYERS[name] then
self:send 'That name is already taken. Choose another.\r\n'
return self:read_name()
else
return name
end
else
self:send 'Invalid name. Choose another.\r\n'
return self:read_name()
end
end
function mud.descriptor:on_close()
if self.name then
PLAYERS[self.name] = nil
end
end
-- The chat mainloop.
function mud.descriptor:chat()
self.prompt = 'chat> '
while true do
local line = self:read()
local message = '<' .. self.name .. '> ' .. line .. '\r\n'
for _, d in pairs(PLAYERS) do
if d ~= self then
d:send(message)
end
end
end
end
function mud.descriptor:on_open()
self.prompt = '> '
self:send 'By what name do you wish to be known?\r\n'
self.name = self:read_name()
PLAYERS[self.name] = self
self:chat()
end