game_dig 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5292bd7b7c7bddd3fa91f1081e6b9dbe2497f9036680047e55d14f4e25f5752b
4
- data.tar.gz: f51410f39c2b62206bf3e2437e22793116ea04e48d8f24ac00b571b952f61688
3
+ metadata.gz: 1606417a646f32a9d14cf9c257d469059ec2cee23135750b869e1347224e61bd
4
+ data.tar.gz: 79f5027b3737ee03265083ada485ce45a4cd1998550076b10bc802116f33a9bd
5
5
  SHA512:
6
- metadata.gz: c5eb259271be2e071f5f31b6002465c21492c3bb43faa3cd5f3f22426b6d6339fd20a0ffb3ee8cf9b2e06aec9e69c8a6c44c0ddaf3e465c76e8572ac8932e77c
7
- data.tar.gz: 8a08df836cec0cdfbff4308d961c3c6e29a6026897a6c815dcd96b6747805ea6582fba323071c899673f8cdcd2f0745252b88ab5074cd17dcf721d3779b7d11f
6
+ metadata.gz: 8003e414913f228ea043a64904f0a477dac28fb2881086f5ac89bbf6d52d6f3b0c5dc1dd8c2f594d48dbc126fddb4dfa88767d77c0beb965ad907e150fdf3858
7
+ data.tar.gz: acf5a2889986077010cc105cdf8324d692284fd91db41deaac8e7d1a383c219779d9e8e56c94a08ca6a5713e9286022072185da066e662764a7eb5f8fa800720
data/.gitignore CHANGED
@@ -12,4 +12,10 @@
12
12
  *.gem
13
13
 
14
14
  # rspec failure tracking
15
- .rspec_status
15
+ .rspec_status
16
+
17
+
18
+ /lib/node/node_modules
19
+ /lib/node/yarn.lock
20
+ /lib/node/package-lock.json
21
+ /playground.rb
data/README.md CHANGED
@@ -69,8 +69,11 @@ This will just run the gamedig cli and return its result.
69
69
  p data
70
70
  ```
71
71
 
72
- ### Service wrapper (not implemented yet!)
73
- Behaves the same to the user, but runs a tiny node webservice in the background to avoid startup times.
72
+ ### Service wrapper (experimental, only did some testing on linux)
73
+
74
+ Only the following parameters are implemented for the service wrapper: `type, host, protocol` (version 0.1.0)
75
+
76
+ Behaves the same to the user, but runs a tiny node webservice in the background to avoid startup times when called more than one time.
74
77
 
75
78
  ```
76
79
  your-ruby-app
data/Rakefile CHANGED
@@ -19,4 +19,12 @@ task :cli do |t|
19
19
  require_relative 'cli/pry'
20
20
  end
21
21
 
22
+ desc "start service cli to play with the environment"
23
+ task :cli_service do |t|
24
+ puts
25
+ puts %{ Starting CLI ... }.red
26
+ puts
27
+ require_relative 'cli/pry_service'
28
+ end
29
+
22
30
  RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,5 @@
1
+
2
+ require_relative '../lib/game_dig/service'
3
+
4
+ require 'pry'
5
+ binding.pry
@@ -0,0 +1,68 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ class GameDigHelper
5
+
6
+ #----------------------------------------------------------------------------------------------------
7
+
8
+ def self.node_service_running?
9
+ port_open? '127.0.0.1', 24445
10
+ end
11
+
12
+ #----------------------------------------------------------------------------------------------------
13
+
14
+ def self.stop_node_service
15
+ begin
16
+ uri = URI("http://127.0.0.1:24445/exit")
17
+ res = Net::HTTP.get_response(uri)
18
+ rescue Errno::ECONNREFUSED => e
19
+ # will be refused, as /exit closes the server and makes no more response
20
+ end
21
+ end
22
+
23
+ #----------------------------------------------------------------------------------------------------
24
+
25
+ #
26
+ # Check if gamedig cli is installed globally
27
+ #
28
+ # @return [Boolean]
29
+ def self.cli_installed?()
30
+ !!(which 'gamedig')
31
+ end
32
+
33
+ #----------------------------------------------------------------------------------------------------
34
+
35
+ def self.which(cmd)
36
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
37
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
38
+ exts.each do |ext|
39
+ exe = File.join(path, "#{cmd}#{ext}")
40
+ return exe if File.executable?(exe) && !File.directory?(exe)
41
+ end
42
+ end
43
+ nil
44
+ end
45
+
46
+ #----------------------------------------------------------------------------------------------------
47
+
48
+ private
49
+
50
+ #----------------------------------------------------------------------------------------------------
51
+
52
+ def self.port_open?(ip, port, seconds = 0.5)
53
+ # => checks if a port is open or not on a remote host
54
+ Timeout::timeout(seconds) do
55
+ begin
56
+ TCPSocket.new(ip, port).close
57
+ true
58
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError
59
+ false
60
+ end
61
+ end
62
+ rescue Timeout::Error
63
+ false
64
+ end
65
+
66
+ #----------------------------------------------------------------------------------------------------
67
+
68
+ end
@@ -0,0 +1,38 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'benchmark'
4
+
5
+ require_relative 'game_dig_helper'
6
+
7
+ node_js_app_base_path = File.expand_path(File.dirname(__FILE__) + '/../node/')
8
+ node_js_app_js_path = File.expand_path(node_js_app_base_path + '/gamedig-service.js')
9
+
10
+ unless Dir.exist?(node_js_app_base_path + '/node_modules')
11
+ puts "GameDig Service: Install missing dependencies ..."
12
+ if !!GameDigHelper.which('yarn')
13
+ `cd "#{node_js_app_base_path}" && yarn install`
14
+ elsif !!GameDigHelper.which('npm')
15
+ `cd "#{node_js_app_base_path}" && npm install`
16
+ else
17
+ raise "Neither 'yarn' nor 'npm' found!"
18
+ end
19
+ puts "done"
20
+ end
21
+
22
+
23
+ unless GameDigHelper.node_service_running?
24
+ node_js_thread = Thread.new do |t|
25
+ system %Q(cd "#{node_js_app_base_path}" && node ./gamedig-service.js)
26
+ at_exit do
27
+ node_js_thread.exit
28
+ end
29
+ end
30
+ end
31
+
32
+ ENV['GAMEDIG_SERVICE'] = 'true'
33
+
34
+ at_exit do
35
+ GameDigHelper.stop_node_service
36
+ end
37
+
38
+ require_relative '../game_dig'
@@ -1,3 +1,3 @@
1
1
  module GameDig
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
data/lib/game_dig.rb CHANGED
@@ -1,5 +1,9 @@
1
- require 'mkmf'
2
- require 'game_dig/version'
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ require_relative 'game_dig/version'
6
+ require_relative 'game_dig/game_dig_helper'
3
7
  require_relative 'custom_errors/game_dig_error'
4
8
  require_relative 'custom_errors/game_dig_cli_not_found'
5
9
 
@@ -10,6 +14,7 @@ require_relative 'custom_errors/game_dig_cli_not_found'
10
14
  module GameDig
11
15
 
12
16
  DEBUG_MESSAGE_END = 'Q#0 Query was successful'
17
+ @@node_service_up = false
13
18
 
14
19
  #----------------------------------------------------------------------------------------------------
15
20
 
@@ -25,7 +30,21 @@ module GameDig
25
30
  # @param [Boolean] debug
26
31
  # @param [Boolean] request_rules
27
32
  def self.query(type:, host:, port: nil, max_attempts: nil, socket_timeout: nil, attempt_timeout: nil, given_port_only: nil, debug: nil, request_rules: nil)
28
- throw GameDigCliNotFound unless cli_installed?
33
+ if ENV['GAMEDIG_SERVICE'] == 'true'
34
+ perform_service_query(type: type, host: host, port: port, max_attempts: max_attempts, socket_timeout: socket_timeout, attempt_timeout: attempt_timeout, given_port_only: given_port_only, debug: debug, request_rules: request_rules)
35
+ else
36
+ perform_cli_query(type: type, host: host, port: port, max_attempts: max_attempts, socket_timeout: socket_timeout, attempt_timeout: attempt_timeout, given_port_only: given_port_only, debug: debug, request_rules: request_rules)
37
+ end
38
+ end
39
+
40
+ #----------------------------------------------------------------------------------------------------
41
+
42
+ private
43
+
44
+ #----------------------------------------------------------------------------------------------------
45
+
46
+ def self.perform_cli_query(type:, host:, port: nil, max_attempts: nil, socket_timeout: nil, attempt_timeout: nil, given_port_only: nil, debug: nil, request_rules: nil)
47
+ throw GameDigCliNotFound unless GameDigHelper.cli_installed?
29
48
  command = "gamedig --type #{type}"
30
49
  command += " --port #{port}" if port
31
50
  command += " --maxAttempts #{max_attempts}" if max_attempts
@@ -35,7 +54,6 @@ module GameDig
35
54
  command += " --debug #{debug}" if debug
36
55
  command += " --requestRules" if request_rules
37
56
  command += " #{host}"
38
- puts command
39
57
  begin
40
58
  output = `#{command}`
41
59
  json = if debug
@@ -58,27 +76,33 @@ module GameDig
58
76
 
59
77
  #----------------------------------------------------------------------------------------------------
60
78
 
61
- private
62
-
63
- #
64
- # Check if gamedig cli is installed globally
65
- #
66
- # @return [Boolean]
67
- def self.cli_installed?()
68
- !!(which 'gamedig')
79
+ def self.perform_service_query(type:, host:, port: nil, max_attempts: nil, socket_timeout: nil, attempt_timeout: nil, given_port_only: nil, debug: nil, request_rules: nil)
80
+ ensure_node_service_is_up
81
+ hostname = host
82
+ hostname += ":#{port}" if port
83
+ uri = URI("http://127.0.0.1:24445/#{type}/#{hostname}")
84
+ res = Net::HTTP.get_response(uri)
85
+ res.body
69
86
  end
70
87
 
71
88
  #----------------------------------------------------------------------------------------------------
72
89
 
73
- def self.which(cmd)
74
- exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
75
- ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
76
- exts.each do |ext|
77
- exe = File.join(path, "#{cmd}#{ext}")
78
- return exe if File.executable?(exe) && !File.directory?(exe)
90
+ def self.ensure_node_service_is_up
91
+ max_counter = 100
92
+ counter = 0
93
+ unless @@node_service_up
94
+ loop do
95
+ counter += 1
96
+ if GameDigHelper.node_service_running?
97
+ break
98
+ elsif counter >= max_counter
99
+ raise "Node gamedig service did not boot up properly ..."
100
+ break
101
+ end
102
+ sleep 0.1
79
103
  end
104
+ @@node_service_up = true
80
105
  end
81
- nil
82
106
  end
83
107
 
84
108
  #----------------------------------------------------------------------------------------------------
@@ -0,0 +1,46 @@
1
+ const Gamedig = require('gamedig');
2
+ const Hapi = require('@hapi/hapi');
3
+
4
+ const server = Hapi.server({
5
+ port: 24445,
6
+ host: 'localhost'
7
+ });
8
+
9
+ const init = async () => {
10
+
11
+ server.route({
12
+ method: 'GET',
13
+ path: '/exit',
14
+ handler: async (request, h) => {
15
+ server.stop();
16
+ process.exit(0);
17
+ return '';
18
+ }
19
+ });
20
+
21
+ server.route({
22
+ method: 'GET',
23
+ path: '/{type}/{ip}',
24
+ handler: async (request, h) => {
25
+
26
+ const { type, ip } = request.params;
27
+ const [ host, port ] = ip.split(':')
28
+
29
+ const serverData = await Gamedig.query({
30
+ type: type,
31
+ host,
32
+ port
33
+ });
34
+ return serverData;
35
+ }
36
+ });
37
+
38
+ await server.start();
39
+ };
40
+
41
+ process.on('unhandledRejection', (err) => {
42
+ console.error(err);
43
+ });
44
+
45
+ init();
46
+
@@ -0,0 +1,9 @@
1
+ {
2
+ "scripts": {
3
+ "start": "node ./gamedig-service.js"
4
+ },
5
+ "dependencies": {
6
+ "@hapi/hapi": "^21.1.0",
7
+ "gamedig": "^4.0.5"
8
+ }
9
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: game_dig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthäus Beyrle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-07 00:00:00.000000000 Z
11
+ date: 2022-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,11 +97,16 @@ files:
97
97
  - bin/console
98
98
  - bin/setup
99
99
  - cli/pry.rb
100
+ - cli/pry_service.rb
100
101
  - game_dig.gemspec
101
102
  - lib/custom_errors/game_dig_cli_not_found.rb
102
103
  - lib/custom_errors/game_dig_error.rb
103
104
  - lib/game_dig.rb
105
+ - lib/game_dig/game_dig_helper.rb
106
+ - lib/game_dig/service.rb
104
107
  - lib/game_dig/version.rb
108
+ - lib/node/gamedig-service.js
109
+ - lib/node/package.json
105
110
  homepage: https://github.com/magynhard/ruby-game_dig
106
111
  licenses:
107
112
  - MIT