[英]Retrieve hash KEY from hash VALUE (in Ruby)
我正在尝试设置服务器以进行战争游戏。 我想我已经将套接字和播放器放在一个散列中,其中键是我的套接字,而播放器是我的值。
现在,我只为客户端类编写一个检索函数就很好了,因为它们只需要从服务器中拉出即可,但是我需要具体说明从哪个客户端进行输入,但是我无法弄清楚如何获得一个我程序中的哈希值中的哈希键。
Mudasobwa,我要通过的测试是
def test_server_capture_output_from_client
在底部。 谢谢,这真的困扰我。
#SERVER CLASS -- Server starts up a TCP, and starts up the game and players and deal the cards
#SERVER CLASS -- Server starts up a TCP, and starts up the game and players and deal the cards
require 'minitest/autorun'
require 'socket'
require_relative 'WarGame_Class.rb'
require_relative 'ModifiedPlayer_Class.rb'
require_relative 'DeckClass.rb'
class WarServer
def initialize(host, port)
@socket_server = TCPServer.new(host, port)
@players = [Player.new, Player.new]
@deck = CardDeck.new
@deck.deal_cards(@players[0].cards, @players[1].cards)
game = WarGame.new
@clients = {} # keys are sockets, values are players
end
def read_client_keys(keys)
@clients.key[keys]
end
def close
@socket_server.close
end
def capture_input(player) ##input client to get what they wrote
@input = @clients.keys[0].read_nonblock(1000) # arbitrary max number of bytes
end
def accept_client
#Hash here to link client to player? (or game?)
client = @socket_server.accept
@clients[client] = @players[@clients.size]
# puts "clients key 0: #{@clients.keys[0]}"
puts
# puts "clients values: #{@clients.values}"
if @clients.size == 2
start_game#####################!!!! Starts game if two clients can put client messages in start game
end
end
def start_game ##############!!!
@clients.keys[0].puts "Welcome to War. Please press enter to play your card"
@clients.keys[1].puts "Welcome to War. Please press enter to play your card"
end
end
class MockWarClient
def initialize
@socket = TCPSocket.new('localhost', 2012)
end
def output
@output
end
def capture_output #need to add (socket)? How else read from specific socket?
@output = @socket.read_nonblock(1000) # arbitrary max number of bytes
rescue
@output = "capture_output error."
end
def capture_input
end
end
class WarServerTest < MiniTest::Unit::TestCase
def setup #This would be like our INITIALIZE Function
#anything is available through out all tests (i.e., instance vars)
@war_server = WarServer.new('localhost', 2012)
end
def teardown
@war_server.close
end
def test_have_1_port_listening
socket_1 = TCPSocket.open('localhost', 2012)
pass('connection in place')
rescue Errno::ECONNREFUSED
flunk('Port 1 error 1st test')
end
def test_connecting_two_clients
client_1 = MockWarClient.new
@war_server.accept_client
client_2 = MockWarClient.new
@war_server.accept_client
client_1.capture_output
refute(client_1.output.empty?)
#client2.capture_output
#refute(client_2.output.empty?)
end
def test_server_capture_output_from_client
client_1 = MockWarClient.new
@war_server.accept_client
client_2 = MockWarClient.new
@war_server.accept_client
#can output @war_server.read_client_keys, though, if I take out the argument to pass in.
puts "Test_Server_output @client keys #{@war_server.read_client_keys(player)}" #cient_1?
@warserver.capture_input
refute(@war_server.input.empty)
end
end
感谢您的任何帮助!
通常,有一个Hash#key
方法可按值检索密钥:
h = {'a' => 1, 'b' => 2}
puts h.key(2)
# ⇒ 'b'
在您的情况下,这似乎已足够。 但是要注意不同密钥可能具有相同值的情况:
h = {'a' => 1, 'b' => 2, 'c' => 1}
puts h.key(1)
# ⇒ 'c'
在这里,您获得了该值的最后一个键。 无论您怀疑不同键的值是否相同,都可能使用Hash#keys
方法或Hash#each
迭代器并手动处理迭代。
例如,以数组的形式获取给定值的键:
h = {'a' => 1, 'b' => 2, 'c' => 1}
puts h.select { |k,v| v == 1}.keys
# ⇒ ['a', 'c']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.