-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrubymud.rb
136 lines (123 loc) · 2.88 KB
/
rubymud.rb
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'socket'
require 'thread'
require 'curses'
require 'readline'
require './util.rb'
# Threaded mud client with support for aliases via aliases.conf file
Curses.init_screen
Curses.curs_set(1)
class Connection
def initialize
# @win1 = Curses::Window.new(Curses.lines / 1 - 2, Curses.cols / 1 - 1, 0, 0)
# @win1.setpos(1, 2)
# @win1.refresh
@input_panel = Curses::Window.new(2, Curses.cols, Curses.lines - 2, 1)
# @input_panel.box("|", "-")
@input_panel.refresh
@input_panel.setpos(1, 1)
# Server parameters
@hostname, @port = ARGV
# @hostname = 'realmsofdespair.com'
# @port = 4000
# Setup aliases
@aliases = Hash[File.read('aliases.conf').split("\n").map{|i|i.split(':')}]
# Initialize basic params
@buffersize = 20000
@connected = false
@input = ""
end
def connect
# Initialize connection
begin
@s = TCPSocket.open(@hostname, @port)
puts "Connection established to #{@hostname}:#{@port}"
@connected = true
rescue
puts "Connection failed."
@connected = false
end
output
end
def output
# Output stream from the server
sleep(0.5)
while @connected == true
# print "\n"
@response = @s.recvfrom(@buffersize)
puts @response
# promptbar
if @input == "quit"
sleep(1)
@connected = false
@s.close
exit
end
@input_panel.setpos(1, 1)
@input_panel.refresh
@input_panel << @input
end
end
def promptbar
@rows, @cols = winsize
@bar = "#" * @input_panel.maxx
@input_panel << @bar.bg_blue.blue
end
def prompt
# User input
loop do
# puts @input_panel
begin
@input_panel << @input = Readline.readline("> ", true)
if @input == "quit"
exit
else
@input_panel.refresh
# p Readline::HISTORY.to_a
# print("-> ", buf, "\n")
end
rescue Interrupt => e
@connected = false
puts " - Quitting"
puts "\r" + ("\e[A\e[K"*3)
$STDOUT.flush
exit
rescue StandardError => e
@connected = false
raise
end
process_input(@input)
# @input_panel.setpos(1, 1)
end
end
def process_input(i)
# Now splits aliases with multiple commands delimited by ;
if @aliases.key?(i)
if @aliases[i].include? ";"
@aliases[i].split(";").each do |a|
puts a
sleep(0.5)
@s.puts(a)
end
else
puts "\r" + ("\e[A\e[K"*3)
# STDOUT.flush
puts @aliases[i]
@s.puts(@aliases[i])
end
else
# puts "\r" + ("\e[A\e[K"*3)
# $STDOUT.flush
@s.puts(@input)
end
@input_panel.setpos(1, 1)
end
def close
# Close connection
@s.close
end
end
mud = Connection.new
t1 = Thread.new { mud.connect }
t2 = Thread.new { mud.prompt }
t1.join
t2.join