gat_ruby 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTQwNTg1MDdjY2U0OTY1MzEzYzA5MTI5YzhjNDJiYTUxYThmODczYQ==
5
+ data.tar.gz: !binary |-
6
+ YzEyMzJmYzdiYjE2NTdmMmU4OGNkOGJmYjAzODVkNmE2N2UwYTlkZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MGQ0NjkwMDhiZmM3YzhjNWRiODNkNTNmNjc5ZWU0YzZmM2Q4Mzg5ZTBhNGU5
10
+ M2QzNDIwMTEzZWZhN2I2NDdmMTUyMTAwYzc0NjczMTQwNzc0NGU3NDcwM2M5
11
+ NWI5ZmU4NmM0ZWI0ZTEwOTJkYmQ4ODYxNjFlM2Y5OTMyNmE0YjQ=
12
+ data.tar.gz: !binary |-
13
+ NjUxZGMyYzk1NmNhMmJhN2RlNjNlNDdlNWIzYjVmN2Q2NzI3MTVjMjQwOTY2
14
+ YTA2OWUwMTY0NDc1ZWZmYzlkNWQwZTFjNzVmNjQ4OWI0ZjdjNjI4NzUwMzdk
15
+ MjQ5ZDllODZjNjNmZGY2OTJjZjkxOWRkYjIxNzk5NjUxNDFhYWM=
@@ -0,0 +1,26 @@
1
+ MIT license: http://opensource.org/licenses/MIT
2
+
3
+ Copyright (c) <2013> <Game Algorithms Tournament - http://gatournament.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
11
+
12
+ Apache 2.0
13
+
14
+ Copyright [2013] [Game Algorithms Tournament - http://gatournament.com]
15
+
16
+ Licensed under the Apache License, Version 2.0 (the "License");
17
+ you may not use this file except in compliance with the License.
18
+ You may obtain a copy of the License at
19
+
20
+ http://www.apache.org/licenses/LICENSE-2.0
21
+
22
+ Unless required by applicable law or agreed to in writing, software
23
+ distributed under the License is distributed on an "AS IS" BASIS,
24
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
+ See the License for the specific language governing permissions and
26
+ limitations under the License.
@@ -0,0 +1,2 @@
1
+ require "gat_ruby/game_algorithm"
2
+ require "gat_ruby/truco_algorithm"
@@ -0,0 +1,103 @@
1
+ require "logger"
2
+ require "socket"
3
+ require "json"
4
+
5
+ # algorithm = GameAlgorithm.new
6
+ # algorithm.listen
7
+ class GameAlgorithm
8
+ def initialize
9
+ @logger = Logger.new(STDOUT)
10
+ config_log
11
+ end
12
+
13
+ def config_log
14
+ @logger.formatter = proc do |severity, datetime, progname, msg|
15
+ "#{msg}\n"
16
+ end
17
+ @logger.level = Logger::INFO
18
+ custom_log_level = nil
19
+ custom_log_level = ARGV[1].to_i if ARGV.count > 1
20
+ if custom_log_level
21
+ if custom_log_level == 10
22
+ @logger.level = Logger::DEBUG
23
+ elsif custom_log_level == 20
24
+ @logger.level = Logger::INFO
25
+ elsif custom_log_level == 30
26
+ @logger.level = Logger::WARN
27
+ elsif custom_log_level == 40
28
+ @logger.level = Logger::ERROR
29
+ elsif custom_log_level == 50
30
+ @logger.level = Logger::FATAL
31
+ end
32
+ end
33
+ end
34
+
35
+ def log(message, log_level=Logger::INFO)
36
+ @logger.add(log_level) { "[GATRuby] #{message}" }
37
+ end
38
+
39
+ def listen(host='localhost', port=nil)
40
+ unless port
41
+ port = ARGV[0].to_i if ARGV.count > 0
42
+ end
43
+ log "Random port: #{port}", Logger::DEBUG
44
+ @server = TCPServer.open(port)
45
+ log "Listening on port #{port}", Logger::DEBUG
46
+ @client = @server.accept
47
+ log "Client connected", Logger::DEBUG
48
+
49
+ @stopped = false
50
+ while not @stopped
51
+ begin
52
+ read_incoming_message
53
+ rescue Exception => e
54
+ log e, Logger::ERROR
55
+ send_error(e)
56
+ stop
57
+ close
58
+ raise e
59
+ end
60
+ end
61
+ close
62
+ end
63
+
64
+ def stop
65
+ @stopped = true
66
+ end
67
+
68
+ def close
69
+ @client.close if @client
70
+ @server.close if @server
71
+ end
72
+
73
+ def read_incoming_message
74
+ message = @client.gets
75
+ message.chomp! if message
76
+ if not message or message == 'stop'
77
+ stop
78
+ else
79
+ message = JSON.parse(message, :symbolize_names => true)
80
+ process_message(message)
81
+ end
82
+ end
83
+
84
+ def process_message(message)
85
+ if message[:action] == "play"
86
+ play(message['context'])
87
+ end
88
+ end
89
+
90
+ def play(context)
91
+ end
92
+
93
+ def send_response(message)
94
+ message = JSON.dump(message)
95
+ message = "#{message}\n"
96
+ @client.puts message
97
+ end
98
+
99
+ def send_error(error_message)
100
+ error = {'error' => error_message}
101
+ send_response(error)
102
+ end
103
+ end
@@ -0,0 +1,33 @@
1
+
2
+ class TrucoAlgorithm < GameAlgorithm
3
+ def process_message(message)
4
+ if message[:action] == "play"
5
+ play(message[:context])
6
+ elsif message[:action] == "accept_truco"
7
+ accept = accept_truco(message[:context])
8
+ if accept
9
+ send_response({:action => :accept_truco})
10
+ else
11
+ send_response({:action => :giveup_truco})
12
+ end
13
+ end
14
+ end
15
+
16
+ def play(context)
17
+ end
18
+
19
+ def accept_truco(context)
20
+ end
21
+
22
+ def can_truco(context)
23
+ return context[:round_can_truco] == true
24
+ end
25
+
26
+ def upcard(card)
27
+ send_response({:action => :upcard, :hand_card => card})
28
+ end
29
+
30
+ def truco()
31
+ send_response({:action => :truco})
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module GATRuby
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,28 @@
1
+ require "gat_ruby"
2
+
3
+ class YourAlgorithm < TrucoAlgorithm
4
+ # You must decide which card of your hand you want to upcard in the table.
5
+ def play(context)
6
+ # to see all information you have to take your decision
7
+ # puts context
8
+
9
+ randomDecisionToTruco = Random.rand(1..10)
10
+ if self.can_truco(context) and randomDecisionToTruco > 5
11
+ return self.truco() # only call this method if self.can_truco(context) returns True
12
+ else
13
+ hand = context[:hand][:cards]
14
+ option = Random.rand(0...hand.count)
15
+ random_card = hand[option]
16
+ return self.upcard(random_card)
17
+ end
18
+ end
19
+
20
+ # * *Returns* : True or False
21
+ def accept_truco(context)
22
+ return Random.rand(0..1) == 1
23
+ end
24
+ end
25
+
26
+ # Required:
27
+ algorithm = YourAlgorithm.new
28
+ algorithm.listen()
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+ require 'gat_ruby'
3
+
4
+ class GATGameAlgorithmTest < Test::Unit::TestCase
5
+ def test_first
6
+ assert_equal "hi", "hi"
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gat_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - GATournament.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Game Algorithms Tournament Ruby SDK
14
+ email: contact@gatournament.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/gat_ruby/game_algorithm.rb
20
+ - lib/gat_ruby/truco_algorithm.rb
21
+ - lib/gat_ruby/version.rb
22
+ - lib/gat_ruby.rb
23
+ - lib/sample/truco.rb
24
+ - LICENSE.txt
25
+ - test/test_gat_ruby.rb
26
+ homepage: http://rubygems.org/gems/gat_ruby
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.0.3
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: GAT Ruby SDK
49
+ test_files:
50
+ - test/test_gat_ruby.rb