gamefic-mud 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a8fbce2090194a364d012ad04c933a9c1ff572f
4
+ data.tar.gz: 31c1ff56b98dd54c820092872fb379f92224c6e8
5
+ SHA512:
6
+ metadata.gz: 8b4ff2794d6b3aeb5e52c700635c7c72efee2e5f01f3d57ef0e3b12e3b76ea8734a0c532afd2b61d833c3b0647d5f8aa563e2f8951f4d5aa261b705b7fbfaf47
7
+ data.tar.gz: 2307f17548a756283f15571eab5c04da03e268ce4d86df59fd74b65a029fdd02bb858b8dc3b15f804a5988249265bf795fefc39de162f347a971a9a0001ef62a
@@ -0,0 +1,8 @@
1
+ module Gamefic
2
+
3
+ module Mud
4
+ autoload :Engine, 'gamefic-mud/engine'
5
+ autoload :User, 'gamefic-mud/user'
6
+ end
7
+
8
+ end
@@ -0,0 +1,146 @@
1
+ require 'websocket-eventmachine-server'
2
+
3
+ module Gamefic
4
+
5
+ class Mud::Engine < Gamefic::Engine::Base
6
+ def post_initialize
7
+ @web_connections = {}
8
+ @accepts = []
9
+ end
10
+
11
+ def will_accept type: :tcpsocket, host: '0.0.0.0', port: 4342
12
+ @accepts.push({ type: type, host: host, port: port })
13
+ end
14
+
15
+ def will_accept_tcpsocket host: '0.0.0.0', port: 4342
16
+ will_accept type: :tcpsocket, host: host, port: port
17
+ end
18
+
19
+ def will_accept_websocket host: '0.0.0.0', port: 4343
20
+ will_accept type: :websocket, host: host, port: port
21
+ end
22
+
23
+ def run
24
+ EM.epoll
25
+ EM.run do
26
+ trap("TERM") { stop }
27
+ trap("INT") { stop }
28
+
29
+ if @accepts.empty?
30
+ will_accept
31
+ end
32
+ @accepts.each { |a|
33
+ if a[:type] == :websocket
34
+ start_websocket host: a[:host], port: a[:port]
35
+ else
36
+ start_tcpsocket host: a[:host], port: a[:port]
37
+ end
38
+ }
39
+
40
+ EventMachine.add_periodic_timer 1 do
41
+ plot.ready
42
+ plot.update
43
+ plot.players.each { |p|
44
+ data = p.user.flush
45
+ if data != ''
46
+ p.user.update data
47
+ p.user.prompt p.prompt
48
+ end
49
+ }
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ def stop
56
+ puts "Terminating WebSocket Server"
57
+ EventMachine.stop
58
+ end
59
+
60
+ private
61
+
62
+ def start_websocket host:, port:
63
+ ::WebSocket::EventMachine::Server.start(host: host, port: port) do |ws|
64
+
65
+ ws.onopen do
66
+ puts "Client connected"
67
+ user = Gamefic::Mud::User::WebSocket.new(ws, self)
68
+ user.start Mud::User::State::Login
69
+ @web_connections[ws] = user
70
+ end
71
+
72
+ ws.onmessage do |msg, type|
73
+ @web_connections[ws].process msg
74
+ end
75
+
76
+ ws.onclose do
77
+ puts "Client disconnected"
78
+ plot.destroy @web_connections[ws].character unless @web_connections[ws].character.nil?
79
+ @web_connections.delete ws
80
+ end
81
+
82
+ ws.onerror do |e|
83
+ puts "Error: #{e}"
84
+ end
85
+
86
+ ws.onping do |msg|
87
+ puts "Received ping: #{msg}"
88
+ end
89
+
90
+ ws.onpong do |msg|
91
+ puts "Received pong: #{msg}"
92
+ end
93
+
94
+ end
95
+
96
+ puts "WebSocket server started on #{host}:#{port}"
97
+ end
98
+
99
+ module TcpHandler
100
+ attr_accessor :plot
101
+ attr_accessor :user
102
+ attr_reader :ip_addr
103
+ attr_reader :port
104
+
105
+ def post_init
106
+ port, ip = Socket.unpack_sockaddr_in(get_peername)
107
+ @port = port
108
+ @ip_addr = ip
109
+ puts "Connection received from #{ip}:#{port}"
110
+ end
111
+
112
+ def receive_data data
113
+ # HACK Convert to UTF-8 and close connection on errors
114
+ conv = ''
115
+ data.each_byte { |b|
116
+ c = b.chr.encode('UTF-8')
117
+ if c.valid_encoding?
118
+ conv += c
119
+ else
120
+ conv += '?'
121
+ end
122
+ }
123
+ conv.strip!
124
+ user.process conv
125
+ rescue Encoding::UndefinedConversionError
126
+ puts 'Throwing away garbage'
127
+ close_connection
128
+ end
129
+ def unbind
130
+ puts "Disconnecting from #{ip_addr}:#{port}"
131
+ plot.destroy user.character unless user.character.nil?
132
+ end
133
+ end
134
+
135
+ def start_tcpsocket host:, port:
136
+ EventMachine.start_server host, port, TcpHandler do |conn|
137
+ conn.plot = plot
138
+ conn.user = Gamefic::Mud::User::TcpSocket.new(conn, self)
139
+ conn.user.start Mud::User::State::Login
140
+ end
141
+
142
+ puts "Telnet server started on #{host}:#{port}"
143
+ end
144
+ end
145
+
146
+ end
@@ -0,0 +1,10 @@
1
+ module Gamefic
2
+
3
+ module Mud::User
4
+ autoload :Base, 'gamefic-mud/user/base'
5
+ autoload :State, 'gamefic-mud/user/state'
6
+ autoload :WebSocket, 'gamefic-mud/user/websocket'
7
+ autoload :TcpSocket, 'gamefic-mud/user/tcpsocket'
8
+ end
9
+
10
+ end
@@ -0,0 +1,32 @@
1
+ module Gamefic
2
+
3
+ class Mud::User::Base < Gamefic::User::Base
4
+ attr_accessor :character
5
+ attr_reader :engine
6
+
7
+ def initialize connection, engine
8
+ @connection = connection
9
+ @engine = engine
10
+ @state = Mud::User::State::Base.new(self)
11
+ end
12
+
13
+ def start user_state
14
+ @state = user_state.new(self)
15
+ @state.start
16
+ end
17
+
18
+ def process message
19
+ @state.process message
20
+ end
21
+
22
+ def update data
23
+ end
24
+
25
+ def transmit data
26
+ end
27
+
28
+ def prompt text
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,9 @@
1
+ module Gamefic
2
+
3
+ module Mud::User::State
4
+ autoload :Base, 'gamefic-mud/user/state/base'
5
+ autoload :Login, 'gamefic-mud/user/state/login'
6
+ autoload :Play, 'gamefic-mud/user/state/play'
7
+ end
8
+
9
+ end
@@ -0,0 +1,23 @@
1
+ module Gamefic
2
+
3
+ module Mud::User::State
4
+
5
+ class Base
6
+ attr_reader :user
7
+
8
+ def initialize user
9
+ @user ||= user
10
+ end
11
+
12
+ def start
13
+ puts "User started #{self.class}"
14
+ end
15
+
16
+ def process message
17
+ puts "User sent #{message} in #{self.class}"
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module Gamefic
2
+
3
+ module Mud::User::State
4
+
5
+ class Login < Base
6
+
7
+ def start
8
+ user.prompt 'Enter your name:'
9
+ end
10
+
11
+ def process message
12
+ character = user.engine.plot.make Character, name: message, description: "#{message} is your name."
13
+ character.connect user
14
+ user.character = character
15
+ user.transmit "Welcome, #{message}."
16
+ user.start Mud::User::State::Play
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Gamefic
2
+
3
+ class Mud::User::State::Play < Mud::User::State::Base
4
+ def start
5
+ user.engine.plot.introduce user.character
6
+ end
7
+
8
+ def process message
9
+ user.character.queue.push message unless message == ''
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,19 @@
1
+ require 'gamefic/text'
2
+
3
+ module Gamefic
4
+
5
+ class Mud::User::TcpSocket < Mud::User::Base
6
+ def update data
7
+ transmit "\n" + Gamefic::Text::Html::Conversions.html_to_ansi(data)
8
+ end
9
+
10
+ def transmit data
11
+ @connection.send_data data
12
+ end
13
+
14
+ def prompt text
15
+ transmit "\n#{text} "
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,17 @@
1
+ module Gamefic
2
+
3
+ class Mud::User::WebSocket < Mud::User::Base
4
+ def update data
5
+ transmit data
6
+ end
7
+
8
+ def transmit data
9
+ @connection.send data
10
+ end
11
+
12
+ def prompt text
13
+ transmit "<label class=\"prompt\">#{text}</label>"
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,5 @@
1
+ module Gamefic
2
+ module Mud
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gamefic-mud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fred Snyder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gamefic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: websocket-eventmachine-server
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.1
41
+ description: An engine for running multiplayer games with Gamefic
42
+ email: fsnyder@gamefic.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/gamefic-mud.rb
48
+ - lib/gamefic-mud/engine.rb
49
+ - lib/gamefic-mud/user.rb
50
+ - lib/gamefic-mud/user/base.rb
51
+ - lib/gamefic-mud/user/state.rb
52
+ - lib/gamefic-mud/user/state/base.rb
53
+ - lib/gamefic-mud/user/state/login.rb
54
+ - lib/gamefic-mud/user/state/play.rb
55
+ - lib/gamefic-mud/user/tcpsocket.rb
56
+ - lib/gamefic-mud/user/websocket.rb
57
+ - lib/gamefic-mud/version.rb
58
+ homepage: http://gamefic.com
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.1.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.5.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Gamefic MUD
82
+ test_files: []