gameoverseer 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dc1d90c8155b62e40a08e8c8987686264990b2e7
4
+ data.tar.gz: 1d2d48e384a4c599fb75bb93c6698aeaaaf5a7c8
5
+ SHA512:
6
+ metadata.gz: 520d1046beb53d18155082fad0c6c1a53997af2775f1d705343a6d9b58ad11f0b687e8a9ef25ae0976c5caaa06d113ea5ffa5df26bf23d437f324f5bd83f7f4d
7
+ data.tar.gz: 63649b30b5d5fdf85050a20aa5df3f059a6636336f495434c1fb9549bd2b58dc69d2da7a7fa5baf9c2c7658e43224bee8010c8561a270bf03f89cf5a00d3021b
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
- log-*.txt
2
- protocol.md
3
- *.gem
1
+ log-*.txt
2
+ .yardoc/*
3
+ protocol.md
4
+ *.gem
data/Gemfile CHANGED
@@ -1,10 +1,3 @@
1
- source "https://rubygems.org"
2
-
3
- # gemspec
4
-
5
- gem "gosu"
6
- gem "nio4r", git: "https://github.com/MagLev/nio4r.git", branch: "johnnyt/recursive-lock-fix"
7
- gem "celluloid"
8
- gem "renet"# use rubygems version for now, git: "https://github.com/jvranish/rENet.git"
9
- gem "multi_json"
10
- gem "net-ssh"
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,34 +1,30 @@
1
- GIT
2
- remote: https://github.com/MagLev/nio4r.git
3
- revision: d22d0b9da828462f0ab14043f476388888430eb2
4
- branch: johnnyt/recursive-lock-fix
5
- specs:
6
- nio4r (1.0.1)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- celluloid (0.16.0)
12
- timers (~> 4.0.0)
13
- gosu (0.8.7.2)
14
- gosu (0.8.7.2-x86-mingw32)
15
- hitimes (1.2.2-java)
16
- hitimes (1.2.2-x86-mingw32)
17
- multi_json (1.10.1)
18
- net-ssh (2.9.2)
19
- renet (0.1.14)
20
- renet (0.1.14-x86-mingw32)
21
- timers (4.0.1)
22
- hitimes
23
-
24
- PLATFORMS
25
- java
26
- x86-mingw32
27
-
28
- DEPENDENCIES
29
- celluloid
30
- gosu
31
- multi_json
32
- net-ssh
33
- nio4r!
34
- renet
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gameoverseer (0.1.2)
5
+ concurrent-ruby
6
+ gosu (= 0.10.6)
7
+ multi_json
8
+ renet
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ concurrent-ruby (1.0.1)
14
+ concurrent-ruby (1.0.1-java)
15
+ gosu (0.10.6)
16
+ gosu (0.10.6-x64-mingw32)
17
+ gosu (0.10.6-x86-mingw32)
18
+ multi_json (1.11.3)
19
+ renet (0.2.1)
20
+
21
+ PLATFORMS
22
+ java
23
+ x64-mingw32
24
+ x86-mingw32
25
+
26
+ DEPENDENCIES
27
+ gameoverseer!
28
+
29
+ BUNDLED WITH
30
+ 1.11.2
data/README.md CHANGED
@@ -1,48 +1,51 @@
1
- # GameOverseer
2
- A game server, designed to be able to play host to up to 4 players in Planet Wars.
3
-
4
- This repo is a complete rewrite of [GameOverseer](https://github.com/cyberarm/gameoverseer).
5
-
6
- # Status
7
- In development.
8
- Any examples given are subject to be outdated at anytime.
9
-
10
- # Install
11
- ### Using bundler
12
- Add this to your Gemfile
13
- ``` ruby
14
- gem "gameoverseer"
15
- ```
16
- and run to install.
17
- ```
18
- bundle install
19
- ```
20
-
21
- ### Or using RubyGems
22
- ```
23
- gem install gameoverseer
24
- ```
25
-
26
- # Usage
27
- ``` ruby
28
- require 'gameoverseer'
29
-
30
- # Write a service for your game
31
- class GameWorld < GameOverseer::Service
32
- def setup
33
- channel_manager.register_channel('game_world', self)
34
- end
35
-
36
- def process(data)
37
- # Do stuff with the data hash.
38
- end
39
-
40
- def version
41
- "1.3.75"
42
- end
43
- end
44
-
45
- host = "localhost"
46
- port = 56789
47
- GameOverseer.activate(host, port)
48
- ```
1
+ [![Code Climate](https://codeclimate.com/github/cyberarm/rewrite-gameoverseer/badges/gpa.svg)](https://codeclimate.com/github/cyberarm/rewrite-gameoverseer)
2
+ [![Inline docs](http://inch-ci.org/github/cyberarm/rewrite-gameoverseer.svg?branch=master)](http://inch-ci.org/github/cyberarm/rewrite-gameoverseer)
3
+
4
+ # GameOverseer
5
+ A game server, designed to be able to play host to up to 4 players in Planet Wars.
6
+
7
+ This repo is a complete rewrite of [GameOverseer](https://github.com/cyberarm/gameoverseer).
8
+
9
+ # Status
10
+ In development.
11
+ Any examples given are subject to be outdated at anytime.
12
+
13
+ # Install
14
+ ### Using bundler
15
+ Add this to your Gemfile
16
+ ``` ruby
17
+ gem "gameoverseer"
18
+ ```
19
+ and run to install.
20
+ ```
21
+ bundle install
22
+ ```
23
+
24
+ ### Or using RubyGems
25
+ ```
26
+ gem install gameoverseer
27
+ ```
28
+
29
+ # Usage
30
+ ``` ruby
31
+ require 'gameoverseer'
32
+
33
+ # Write a service for your game
34
+ class GameWorld < GameOverseer::Service
35
+ def setup
36
+ channel_manager.register_channel('game_world', self)
37
+ end
38
+
39
+ def process(data)
40
+ # Do stuff with the data hash.
41
+ end
42
+
43
+ def version
44
+ "1.3.75"
45
+ end
46
+ end
47
+
48
+ host = "localhost"
49
+ port = 56789
50
+ GameOverseer.activate(host, port)
51
+ ```
data/gameoverseer.gemspec CHANGED
@@ -16,8 +16,8 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib", "bin"]
18
18
 
19
- s.add_runtime_dependency "gosu"
20
- s.add_runtime_dependency "celluloid"
19
+ s.add_runtime_dependency "gosu", "0.10.6"
20
+ s.add_runtime_dependency "concurrent-ruby"
21
21
  s.add_runtime_dependency "renet"
22
22
  s.add_runtime_dependency "multi_json"
23
23
  end
@@ -1,36 +1,49 @@
1
- module GameOverseer
2
- class ChannelManager
3
- CHAT = 0
4
- WORLD= 1
5
- HANDSHAKE = 2
6
- FAULT=3
7
- def initialize
8
- @channels = {}
9
- ChannelManager.instance = self
10
- end
11
-
12
- def register_channel(channel, service)
13
- _channel = channel.downcase
14
- unless @channels[_channel]
15
- @channels[_channel] = service
16
- GameOverseer::Console.log("ChannelManager> mapped '#{_channel}' to '#{service.class}'.")
17
- else
18
- raise "Could not map channel '#{_channel}' because '#{@channels[data[_channel]].class}' is already using it."
19
- end
20
- end
21
-
22
- def send_to_service(data, client_id)
23
- GameOverseer::Console.log("ChannelManager> sent '#{data}' to '#{@channels[data['channel']].class}'.")
24
- @channels[data['channel']].client_id = client_id
25
- @channels[data['channel']].process(data)
26
- end
27
-
28
- def self.instance
29
- @instance
30
- end
31
-
32
- def self.instance=_instance
33
- @instance = _instance
34
- end
35
- end
36
- end
1
+ module GameOverseer
2
+
3
+ # Handles routing packets to the services that subscribe to channels
4
+ class ChannelManager
5
+ CHAT = 0
6
+ WORLD= 1
7
+ HANDSHAKE = 2
8
+ FAULT=3
9
+
10
+ def initialize
11
+ @channels = {}
12
+ ChannelManager.instance = self
13
+ end
14
+
15
+ # Enables a service to subscribe to a channel
16
+ # @param channel [String]
17
+ # @param service [Service]
18
+ def register_channel(channel, service)
19
+ _channel = channel.downcase
20
+ unless @channels[_channel]
21
+ @channels[_channel] = service
22
+ GameOverseer::Console.log("ChannelManager> mapped '#{_channel}' to '#{service.class}'.")
23
+ else
24
+ raise "Could not map channel '#{_channel}' because '#{@channels[data[_channel]].class}' is already using it."
25
+ end
26
+ end
27
+
28
+ # Routes packet to {Service}
29
+ # @param client_id [Integer] ID of client that sent the packet
30
+ # @param data [Hash] data from packet
31
+ def send_to_service(client_id, data)
32
+ GameOverseer::Console.log("ChannelManager> sent '#{data}' to '#{@channels[data['channel']].class}'.")
33
+ @channels[data['channel']].client_id = client_id
34
+ @channels[data['channel']].process(data)
35
+ end
36
+
37
+ # Returns instance of active {ChannelManager}
38
+ # @return [ChannelManager]
39
+ def self.instance
40
+ @instance
41
+ end
42
+
43
+ # Sets instance of {ChannelManager} that is used through out the system
44
+ # @param _instance [ChannelManager]
45
+ def self.instance=_instance
46
+ @instance = _instance
47
+ end
48
+ end
49
+ end
@@ -1,50 +1,66 @@
1
- module GameOverseer
2
- class ClientManager
3
- attr_accessor :clients
4
-
5
- def initialize
6
- ClientManager.instance = self
7
- @clients = []
8
- end
9
-
10
- def add(client_id, ip_address)
11
- @clients << {client_id: client_id, ip_address: ip_address}
12
- GameOverseer::Services.client_connected(client_id, ip_address)
13
- end
14
-
15
- def update(client_id, key, value)
16
- @clients.each do |hash|
17
- if hash[:client_id] == client_id
18
- hash[key] = value
19
- end
20
- end
21
- end
22
-
23
- def get(client_id)
24
- _hash = @clients.detect do |hash|
25
- if hash[:client_id] == client_id
26
- true
27
- end
28
- end
29
-
30
- return _hash
31
- end
32
-
33
- def remove(client_id)
34
- @clients.each do |hash|
35
- if hash[:client_id] == client_id
36
- @clients.delete(hash)
37
- GameOverseer::Services.client_disconnected(client_id)
38
- end
39
- end
40
- end
41
-
42
- def self.instance
43
- @instance
44
- end
45
-
46
- def self.instance=_instance
47
- @instance = _instance
48
- end
49
- end
50
- 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
+ end
19
+
20
+ # Store client specific data in a {Hash}
21
+ # @param client_id [Integer] ID of client
22
+ # @param key [String|Symbol]
23
+ # @param value What the key should equal
24
+ def update(client_id, key, value)
25
+ @clients.each do |hash|
26
+ if hash[:client_id] == client_id
27
+ hash[key] = value
28
+ end
29
+ end
30
+ end
31
+
32
+ # Gets client data
33
+ # @param client_id [Integer]
34
+ # @return [Hash] hash containing client data
35
+ def get(client_id)
36
+ _hash = @clients.detect do |hash|
37
+ if hash[:client_id] == client_id
38
+ true
39
+ end
40
+ end
41
+
42
+ return _hash
43
+ end
44
+
45
+ # Removes client data and disconnects client
46
+ # @param client_id [Integer] ID of client
47
+ def remove(client_id)
48
+ @clients.each do |hash|
49
+ if hash[:client_id] == client_id
50
+ @clients.delete(hash)
51
+ GameOverseer::Services.client_disconnected(client_id)
52
+ end
53
+ end
54
+ end
55
+
56
+ # @return [ClientManager]
57
+ def self.instance
58
+ @instance
59
+ end
60
+
61
+ # @param _instance [ClientManager]
62
+ def self.instance=_instance
63
+ @instance = _instance
64
+ end
65
+ end
66
+ end
@@ -1,207 +1,206 @@
1
- module GameOverseer
2
- class Console < Gosu::Window
3
- include Celluloid
4
- # TODO: Use Gosu::Window.record to lower number of objects that need to be drawn
5
-
6
- PENDING_LOG = []
7
- def initialize
8
- GameOverseer::Console.instance = self
9
- super(720, 480, false)
10
- $window = self
11
- $window.caption = "GameOverseer Console"
12
- $window.text_input = Gosu::TextInput.new
13
-
14
- @default_text_instance = Gosu::Font.new($window, 'Consolas', 18)
15
- @messages = []
16
- setup_ui
17
- end
18
-
19
- def needs_cursor?
20
- true
21
- end
22
-
23
- def draw
24
- @ui_image.draw(0,0,0) if defined?(@ui_image)
25
- end
26
-
27
- def update
28
- update_ui
29
- @ui_image = $window.record(720, 480) {draw_ui}
30
- end
31
-
32
- def setup_ui
33
- @current_text = text_instance
34
- @current_text_x = 4
35
-
36
- # Required first message
37
- @messages << {
38
- text: '',
39
- instance: text_instance,
40
- color: Gosu::Color::WHITE,
41
- x: 4,
42
- y: 480-26-18,
43
- z: 1
44
- }
45
-
46
- submit_text("#{Time.now.strftime('%c')}", false)
47
- end
48
-
49
- def draw_ui
50
- draw_rect(0,0, 720, 26, Gosu::Color.rgb(200, 75, 25))
51
- draw_rect(0,454, 720, 480, Gosu::Color::WHITE)
52
- text_instance.draw("GameOverSeer Console. GameOverseer version #{GameOverSeer::VERSION} #{GameOverSeer::RELEASE_NAME} #{@messages.count}", 4, 4, 3)
53
- @current_text.draw("#{$window.text_input.text}", @current_text_x, 458, 3, 1, 1, Gosu::Color::BLACK)
54
- draw_rect(@caret+@current_text_x, 456, 2.0+@caret+@current_text_x, 474, Gosu::Color::BLUE, 4) if defined?(@caret) && @render_caret
55
-
56
- @messages.each do |message|
57
- message[:instance].draw(message[:text],message[:x],message[:y],message[:z], 1, 1, message[:color])
58
- p message[:color] unless message[:color] == Gosu::Color::WHITE
59
- end
60
- end
61
-
62
- def update_ui
63
- PENDING_LOG.each do |log_message|
64
- submit_text(log_message, false) if log_message.strip.length > 0
65
- PENDING_LOG.delete(log_message)
66
- end
67
-
68
- @caret = @current_text.text_width($window.text_input.text[0...$window.text_input.caret_pos])
69
-
70
- @caret_tick = 0 unless defined?(@caret_tick)
71
- @render_caret = true if @caret_tick < 15
72
- @render_caret = false if @caret_tick > 30
73
-
74
- @caret_tick = 0 unless @caret_tick < 45
75
- @caret_tick+=1
76
-
77
- value = @current_text.text_width($window.text_input.text)+@current_text_x
78
- if value >= 720
79
- @current_text_x-=4
80
- elsif value <= 715
81
- @current_text_x+=4 unless @current_text_x >= 4
82
- end
83
- end
84
-
85
- def text_instance
86
- @default_text_instance
87
- end
88
-
89
- def draw_rect(x1,y1, x2,y2, color = Gosu::Color::GRAY, z = 2)
90
- $window.draw_quad(x1, y1, color, x2, y1, color, x1, y2, color, x2, y2, color, z)
91
- end
92
-
93
- def scroll(direction)
94
- case direction
95
- when :down
96
- if @messages.last[:y] >= 480 - 26 - 18
97
- @messages.each do |message|
98
- message[:y]-=18
99
- end
100
- end
101
- when :up
102
- if @messages.first[:y] <= 26#<= 480 - 26 - 32
103
- @messages.each do |message|
104
- message[:y]+=18
105
- end
106
- end
107
- end
108
- end
109
-
110
- def clean_messages(count)
111
- if @messages.count >= count
112
- @messages.delete(@messages.first)
113
- end
114
- end
115
-
116
- def button_up(id)
117
- case id
118
- when 41 # Escape
119
- # Quit?
120
- when 40 # Enter
121
- submit_text($window.text_input.text)
122
- when 88 # Numpad Enter
123
- submit_text($window.text_input.text)
124
- when 259 # Mouse wheel
125
- scroll(:up)
126
- when 260 # Mouse wheel
127
- scroll(:down)
128
- end
129
- end
130
-
131
- def self.instance
132
- @instance
133
- end
134
-
135
- def self.instance=_instance
136
- @instance = _instance
137
- end
138
-
139
- def self.log(string)
140
- self.log_it(string) if string.strip.length > 0
141
- begin
142
- GameOverseer::Console.instance.submit_text(string, false)
143
- rescue NoMethodError
144
- self.defer_log(string)
145
- end
146
- end
147
-
148
- def self.log_with_color(string, color = Gosu::Color::WHITE)
149
- self.log_it(string) if string.strip.length > 0
150
- GameOverseer::Console.instance.submit_text(string, false, color)
151
- end
152
-
153
- def self.defer_log(string)
154
- PENDING_LOG << string
155
- end
156
-
157
- def self.log_it(string)
158
- puts string
159
- retry_limit = 0
160
- begin
161
- @log_file = File.open("#{Dir.pwd}/logs/log-#{Time.now.strftime('%B-%d-%Y')}.txt", 'a+') unless defined? @log_file
162
- @log_file.write "[#{Time.now.strftime('%c')}] #{string}\n"
163
- rescue Errno::ENOENT
164
- Dir.mkdir("#{Dir.pwd}/logs") unless File.exist?("#{Dir.pwd}/logs") && File.directory?("#{Dir.pwd}/logs")
165
- retry_limit+=1
166
- retry unless retry_limit >= 2
167
- end
168
- end
169
-
170
- protected
171
- def submit_text(text, from_console = true, color = Gosu::Color::WHITE)
172
- if text.strip.length > 0
173
- clean_messages(300)
174
- text = "Console> #{text}" if from_console
175
- GameOverseer::Console.log_it(text)
176
- if text.length > 83
177
- temp_text = text[0..83]
178
- @messages.each do |message|
179
- message[:y]-=18
180
- end
181
- @messages << {
182
- text: temp_text,
183
- instance: text_instance,
184
- color: color,
185
- x: 4,
186
- y: @messages.last[:y] + 18,
187
- z: 1
188
- }
189
- submit_text(text[83..text.length], false)
190
- else
191
- @messages.each do |message|
192
- message[:y]-=18
193
- end
194
- @messages << {
195
- text: text,
196
- instance: text_instance,
197
- color: color,
198
- x: 4,
199
- y: @messages.last[:y] + 18,
200
- z: 1
201
- }
202
- end
203
- $window.text_input = Gosu::TextInput.new if from_console
204
- end
205
- end
206
- end
207
- 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