gameoverseer 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,68 +1,68 @@
1
- module GameOverseer
2
-
3
- # Stores client data
4
- class ClientManager
5
- attr_accessor :clients
6
-
7
- def initialize
8
- ClientManager.instance = self
9
- @clients = []
10
- end
11
-
12
- # Add client to clients list
13
- # @param client_id [Integer]
14
- # @param ip_address [String]
15
- def add(client_id, ip_address)
16
- @clients << {client_id: client_id, ip_address: ip_address}
17
- GameOverseer::Services.client_connected(client_id, ip_address)
18
- GameOverseer::Console.log("ClientManager> client with id '#{client_id}' connected")
19
- end
20
-
21
- # Store client specific data in a {Hash}
22
- # @param client_id [Integer] ID of client
23
- # @param key [String|Symbol]
24
- # @param value What the key should equal
25
- def update(client_id, key, value)
26
- @clients.each do |hash|
27
- if hash[:client_id] == client_id
28
- hash[key] = value
29
- end
30
- end
31
- end
32
-
33
- # Gets client data
34
- # @param client_id [Integer]
35
- # @return [Hash] hash containing client data
36
- def get(client_id)
37
- _hash = @clients.detect do |hash|
38
- if hash[:client_id] == client_id
39
- true
40
- end
41
- end
42
-
43
- return _hash
44
- end
45
-
46
- # Removes client data and disconnects client
47
- # @param client_id [Integer] ID of client
48
- def remove(client_id)
49
- @clients.each do |hash|
50
- if hash[:client_id] == client_id
51
- @clients.delete(hash)
52
- GameOverseer::Services.client_disconnected(client_id)
53
- GameOverseer::Console.log("ClientManager> client with id '#{client_id}' disconnected")
54
- end
55
- end
56
- end
57
-
58
- # @return [ClientManager]
59
- def self.instance
60
- @instance
61
- end
62
-
63
- # @param _instance [ClientManager]
64
- def self.instance=_instance
65
- @instance = _instance
66
- end
67
- end
68
- end
1
+ module GameOverseer
2
+
3
+ # Stores client data
4
+ class ClientManager
5
+ attr_accessor :clients
6
+
7
+ def initialize
8
+ ClientManager.instance = self
9
+ @clients = []
10
+ end
11
+
12
+ # Add client to clients list
13
+ # @param client_id [Integer]
14
+ # @param ip_address [String]
15
+ def add(client_id, ip_address)
16
+ @clients << {client_id: client_id, ip_address: ip_address}
17
+ GameOverseer::Services.client_connected(client_id, ip_address)
18
+ GameOverseer::Console.log("ClientManager> client with id '#{client_id}' connected")
19
+ end
20
+
21
+ # Store client specific data in a {Hash}
22
+ # @param client_id [Integer] ID of client
23
+ # @param key [String|Symbol]
24
+ # @param value What the key should equal
25
+ def update(client_id, key, value)
26
+ @clients.each do |hash|
27
+ if hash[:client_id] == client_id
28
+ hash[key] = value
29
+ end
30
+ end
31
+ end
32
+
33
+ # Gets client data
34
+ # @param client_id [Integer]
35
+ # @return [Hash] hash containing client data
36
+ def get(client_id)
37
+ _hash = @clients.detect do |hash|
38
+ if hash[:client_id] == client_id
39
+ true
40
+ end
41
+ end
42
+
43
+ return _hash
44
+ end
45
+
46
+ # Removes client data and disconnects client
47
+ # @param client_id [Integer] ID of client
48
+ def remove(client_id)
49
+ @clients.each do |hash|
50
+ if hash[:client_id] == client_id
51
+ @clients.delete(hash)
52
+ GameOverseer::Services.client_disconnected(client_id)
53
+ GameOverseer::Console.log("ClientManager> client with id '#{client_id}' disconnected")
54
+ end
55
+ end
56
+ end
57
+
58
+ # @return [ClientManager]
59
+ def self.instance
60
+ @instance
61
+ end
62
+
63
+ # @param _instance [ClientManager]
64
+ def self.instance=_instance
65
+ @instance = _instance
66
+ end
67
+ end
68
+ end
@@ -1,206 +1,206 @@
1
- module GameOverseer
2
- class Console < Gosu::Window
3
- # TODO: Use Gosu::Window.record to lower number of objects that need to be drawn
4
-
5
- PENDING_LOG = []
6
- def initialize
7
- GameOverseer::Console.instance = self
8
- super(720, 480, false)
9
- $window = self
10
- $window.caption = "GameOverseer Console"
11
- $window.text_input = Gosu::TextInput.new
12
-
13
- @default_text_instance = Gosu::Font.new($window, 'Consolas', 18)
14
- @messages = []
15
- setup_ui
16
- end
17
-
18
- def needs_cursor?
19
- true
20
- end
21
-
22
- def draw
23
- @ui_image.draw(0,0,0) if defined?(@ui_image)
24
- end
25
-
26
- def update
27
- update_ui
28
- @ui_image = $window.record(720, 480) {draw_ui}
29
- end
30
-
31
- def setup_ui
32
- @current_text = text_instance
33
- @current_text_x = 4
34
-
35
- # Required first message
36
- @messages << {
37
- text: '',
38
- instance: text_instance,
39
- color: Gosu::Color::WHITE,
40
- x: 4,
41
- y: 480-26-18,
42
- z: 1
43
- }
44
-
45
- submit_text("#{Time.now.strftime('%c')}", false)
46
- end
47
-
48
- def draw_ui
49
- draw_rect(0,0, 720, 26, Gosu::Color.rgb(200, 75, 25))
50
- draw_rect(0,454, 720, 480, Gosu::Color::WHITE)
51
- text_instance.draw("GameOverSeer Console. GameOverseer version #{GameOverseer::VERSION} #{GameOverseer::RELEASE_NAME} #{@messages.count}", 4, 4, 3)
52
- @current_text.draw("#{$window.text_input.text}", @current_text_x, 458, 3, 1, 1, Gosu::Color::BLACK)
53
- draw_rect(@caret+@current_text_x, 456, 2.0+@caret+@current_text_x, 474, Gosu::Color::BLUE, 4) if defined?(@caret) && @render_caret
54
-
55
- @messages.each do |message|
56
- message[:instance].draw(message[:text],message[:x],message[:y],message[:z], 1, 1, message[:color])
57
- p message[:color] unless message[:color] == Gosu::Color::WHITE
58
- end
59
- end
60
-
61
- def update_ui
62
- PENDING_LOG.each do |log_message|
63
- submit_text(log_message, false) if log_message.strip.length > 0
64
- PENDING_LOG.delete(log_message)
65
- end
66
-
67
- @caret = @current_text.text_width($window.text_input.text[0...$window.text_input.caret_pos])
68
-
69
- @caret_tick = 0 unless defined?(@caret_tick)
70
- @render_caret = true if @caret_tick < 15
71
- @render_caret = false if @caret_tick > 30
72
-
73
- @caret_tick = 0 unless @caret_tick < 45
74
- @caret_tick+=1
75
-
76
- value = @current_text.text_width($window.text_input.text)+@current_text_x
77
- if value >= 720
78
- @current_text_x-=4
79
- elsif value <= 715
80
- @current_text_x+=4 unless @current_text_x >= 4
81
- end
82
- end
83
-
84
- def text_instance
85
- @default_text_instance
86
- end
87
-
88
- def draw_rect(x1,y1, x2,y2, color = Gosu::Color::GRAY, z = 2)
89
- $window.draw_quad(x1, y1, color, x2, y1, color, x1, y2, color, x2, y2, color, z)
90
- end
91
-
92
- def scroll(direction)
93
- case direction
94
- when :down
95
- if @messages.last[:y] >= 480 - 26 - 18
96
- @messages.each do |message|
97
- message[:y]-=18
98
- end
99
- end
100
- when :up
101
- if @messages.first[:y] <= 26#<= 480 - 26 - 32
102
- @messages.each do |message|
103
- message[:y]+=18
104
- end
105
- end
106
- end
107
- end
108
-
109
- def clean_messages(count)
110
- if @messages.count >= count
111
- @messages.delete(@messages.first)
112
- end
113
- end
114
-
115
- def button_up(id)
116
- case id
117
- when 41 # Escape
118
- # Quit?
119
- when 40 # Enter
120
- submit_text($window.text_input.text)
121
- when 88 # Numpad Enter
122
- submit_text($window.text_input.text)
123
- when 259 # Mouse wheel
124
- scroll(:up)
125
- when 260 # Mouse wheel
126
- scroll(:down)
127
- end
128
- end
129
-
130
- def self.instance
131
- @instance
132
- end
133
-
134
- def self.instance=_instance
135
- @instance = _instance
136
- end
137
-
138
- def self.log(string)
139
- self.log_it(string) if string.strip.length > 0
140
- begin
141
- GameOverseer::Console.instance.submit_text(string, false)
142
- rescue NoMethodError
143
- self.defer_log(string)
144
- end
145
- end
146
-
147
- def self.log_with_color(string, color = Gosu::Color::WHITE)
148
- self.log_it(string) if string.strip.length > 0
149
- GameOverseer::Console.instance.submit_text(string, false, color)
150
- end
151
-
152
- def self.defer_log(string)
153
- PENDING_LOG << string
154
- end
155
-
156
- def self.log_it(string)
157
- puts string
158
- retry_limit = 0
159
- begin
160
- @log_file = File.open("#{Dir.pwd}/logs/log-#{Time.now.strftime('%B-%d-%Y')}.txt", 'a+') unless defined? @log_file
161
- @log_file.write "[#{Time.now.strftime('%c')}] #{string}\n"
162
- rescue Errno::ENOENT
163
- Dir.mkdir("#{Dir.pwd}/logs") unless File.exist?("#{Dir.pwd}/logs") && File.directory?("#{Dir.pwd}/logs")
164
- retry_limit+=1
165
- retry unless retry_limit >= 2
166
- end
167
- end
168
-
169
- protected
170
- def submit_text(text, from_console = true, color = Gosu::Color::WHITE)
171
- if text.strip.length > 0
172
- clean_messages(300)
173
- text = "Console> #{text}" if from_console
174
- GameOverseer::Console.log_it(text)
175
- if text.length > 83
176
- temp_text = text[0..83]
177
- @messages.each do |message|
178
- message[:y]-=18
179
- end
180
- @messages << {
181
- text: temp_text,
182
- instance: text_instance,
183
- color: color,
184
- x: 4,
185
- y: @messages.last[:y] + 18,
186
- z: 1
187
- }
188
- submit_text(text[83..text.length], false)
189
- else
190
- @messages.each do |message|
191
- message[:y]-=18
192
- end
193
- @messages << {
194
- text: text,
195
- instance: text_instance,
196
- color: color,
197
- x: 4,
198
- y: @messages.last[:y] + 18,
199
- z: 1
200
- }
201
- end
202
- $window.text_input = Gosu::TextInput.new if from_console
203
- end
204
- end
205
- end
206
- end
1
+ module GameOverseer
2
+ class Console < Gosu::Window
3
+ # TODO: Use Gosu::Window.record to lower number of objects that need to be drawn
4
+
5
+ PENDING_LOG = []
6
+ def initialize
7
+ GameOverseer::Console.instance = self
8
+ super(720, 480, false)
9
+ $window = self
10
+ $window.caption = "GameOverseer Console"
11
+ $window.text_input = Gosu::TextInput.new
12
+
13
+ @default_text_instance = Gosu::Font.new($window, 'Consolas', 18)
14
+ @messages = []
15
+ setup_ui
16
+ end
17
+
18
+ def needs_cursor?
19
+ true
20
+ end
21
+
22
+ def draw
23
+ @ui_image.draw(0,0,0) if defined?(@ui_image)
24
+ end
25
+
26
+ def update
27
+ update_ui
28
+ @ui_image = $window.record(720, 480) {draw_ui}
29
+ end
30
+
31
+ def setup_ui
32
+ @current_text = text_instance
33
+ @current_text_x = 4
34
+
35
+ # Required first message
36
+ @messages << {
37
+ text: '',
38
+ instance: text_instance,
39
+ color: Gosu::Color::WHITE,
40
+ x: 4,
41
+ y: 480-26-18,
42
+ z: 1
43
+ }
44
+
45
+ submit_text("#{Time.now.strftime('%c')}", false)
46
+ end
47
+
48
+ def draw_ui
49
+ draw_rect(0,0, 720, 26, Gosu::Color.rgb(200, 75, 25))
50
+ draw_rect(0,454, 720, 480, Gosu::Color::WHITE)
51
+ text_instance.draw("GameOverSeer Console. GameOverseer version #{GameOverseer::VERSION} #{GameOverseer::RELEASE_NAME} #{@messages.count}", 4, 4, 3)
52
+ @current_text.draw("#{$window.text_input.text}", @current_text_x, 458, 3, 1, 1, Gosu::Color::BLACK)
53
+ draw_rect(@caret+@current_text_x, 456, 2.0+@caret+@current_text_x, 474, Gosu::Color::BLUE, 4) if defined?(@caret) && @render_caret
54
+
55
+ @messages.each do |message|
56
+ message[:instance].draw(message[:text],message[:x],message[:y],message[:z], 1, 1, message[:color])
57
+ p message[:color] unless message[:color] == Gosu::Color::WHITE
58
+ end
59
+ end
60
+
61
+ def update_ui
62
+ PENDING_LOG.each do |log_message|
63
+ submit_text(log_message, false) if log_message.strip.length > 0
64
+ PENDING_LOG.delete(log_message)
65
+ end
66
+
67
+ @caret = @current_text.text_width($window.text_input.text[0...$window.text_input.caret_pos])
68
+
69
+ @caret_tick = 0 unless defined?(@caret_tick)
70
+ @render_caret = true if @caret_tick < 15
71
+ @render_caret = false if @caret_tick > 30
72
+
73
+ @caret_tick = 0 unless @caret_tick < 45
74
+ @caret_tick+=1
75
+
76
+ value = @current_text.text_width($window.text_input.text)+@current_text_x
77
+ if value >= 720
78
+ @current_text_x-=4
79
+ elsif value <= 715
80
+ @current_text_x+=4 unless @current_text_x >= 4
81
+ end
82
+ end
83
+
84
+ def text_instance
85
+ @default_text_instance
86
+ end
87
+
88
+ def draw_rect(x1,y1, x2,y2, color = Gosu::Color::GRAY, z = 2)
89
+ $window.draw_quad(x1, y1, color, x2, y1, color, x1, y2, color, x2, y2, color, z)
90
+ end
91
+
92
+ def scroll(direction)
93
+ case direction
94
+ when :down
95
+ if @messages.last[:y] >= 480 - 26 - 18
96
+ @messages.each do |message|
97
+ message[:y]-=18
98
+ end
99
+ end
100
+ when :up
101
+ if @messages.first[:y] <= 26#<= 480 - 26 - 32
102
+ @messages.each do |message|
103
+ message[:y]+=18
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ def clean_messages(count)
110
+ if @messages.count >= count
111
+ @messages.delete(@messages.first)
112
+ end
113
+ end
114
+
115
+ def button_up(id)
116
+ case id
117
+ when 41 # Escape
118
+ # Quit?
119
+ when 40 # Enter
120
+ submit_text($window.text_input.text)
121
+ when 88 # Numpad Enter
122
+ submit_text($window.text_input.text)
123
+ when 259 # Mouse wheel
124
+ scroll(:up)
125
+ when 260 # Mouse wheel
126
+ scroll(:down)
127
+ end
128
+ end
129
+
130
+ def self.instance
131
+ @instance
132
+ end
133
+
134
+ def self.instance=_instance
135
+ @instance = _instance
136
+ end
137
+
138
+ def self.log(string)
139
+ self.log_it(string) if string.strip.length > 0
140
+ begin
141
+ GameOverseer::Console.instance.submit_text(string, false)
142
+ rescue NoMethodError
143
+ self.defer_log(string)
144
+ end
145
+ end
146
+
147
+ def self.log_with_color(string, color = Gosu::Color::WHITE)
148
+ self.log_it(string) if string.strip.length > 0
149
+ GameOverseer::Console.instance.submit_text(string, false, color)
150
+ end
151
+
152
+ def self.defer_log(string)
153
+ PENDING_LOG << string
154
+ end
155
+
156
+ def self.log_it(string)
157
+ puts string
158
+ retry_limit = 0
159
+ begin
160
+ @log_file = File.open("#{Dir.pwd}/logs/log-#{Time.now.strftime('%B-%d-%Y')}.txt", 'a+') unless defined? @log_file
161
+ @log_file.write "[#{Time.now.strftime('%c')}] #{string}\n"
162
+ rescue Errno::ENOENT
163
+ Dir.mkdir("#{Dir.pwd}/logs") unless File.exist?("#{Dir.pwd}/logs") && File.directory?("#{Dir.pwd}/logs")
164
+ retry_limit+=1
165
+ retry unless retry_limit >= 2
166
+ end
167
+ end
168
+
169
+ protected
170
+ def submit_text(text, from_console = true, color = Gosu::Color::WHITE)
171
+ if text.strip.length > 0
172
+ clean_messages(300)
173
+ text = "Console> #{text}" if from_console
174
+ GameOverseer::Console.log_it(text)
175
+ if text.length > 83
176
+ temp_text = text[0..83]
177
+ @messages.each do |message|
178
+ message[:y]-=18
179
+ end
180
+ @messages << {
181
+ text: temp_text,
182
+ instance: text_instance,
183
+ color: color,
184
+ x: 4,
185
+ y: @messages.last[:y] + 18,
186
+ z: 1
187
+ }
188
+ submit_text(text[83..text.length], false)
189
+ else
190
+ @messages.each do |message|
191
+ message[:y]-=18
192
+ end
193
+ @messages << {
194
+ text: text,
195
+ instance: text_instance,
196
+ color: color,
197
+ x: 4,
198
+ y: @messages.last[:y] + 18,
199
+ z: 1
200
+ }
201
+ end
202
+ $window.text_input = Gosu::TextInput.new if from_console
203
+ end
204
+ end
205
+ end
206
+ end