picking up project again. Fixing rspec errors first

This commit is contained in:
2026-05-28 10:13:28 -06:00
parent eb161e0f2e
commit a1f31a89b2
5 changed files with 268 additions and 23 deletions
+147
View File
@@ -0,0 +1,147 @@
# Nathan Hinton 12 Jan 2026
#
# Parse a command from the client. This somehow will end up connecting with the
# game. This is because it will need to be able to communicate with the objects
# in the game and have game level access.
class CommandParser
def initialize
end
def parse(client, command)
response = {'request_id' => command['request_id']}
case command['type']
when 'hello'
parse_hello(client, command)
when 'query'
parse_query(command)
when 'command'
parse_command(command)
else
raise CommandParserError, "Unable to parse unknown type of '#{commad['type']}'"
end
return response
end
def parse_hello(client, command)
response = {'type' => command['type']}
id = command['player_id']
case command['domain']
when 'player'
hello_player(client, id, command['payload'])
else
raise CommandParserError, "Unable to parse the hello type, domain '#{command['domain']}'"
end
return response
end
def parse_query(command)
response = {'type' => command['type']}
id = command['player_id']
case command['domain']
when 'game'
query_game(id, command['payload'])
when 'player'
query_player(id, command['payload'])
else
raise CommandParserError, "Unable to parse the query type, domain '#{command['domain']}'"
end
return response
end
def parse_command(command)
response = {'type' => command['type']}
id = command['player_id']
case command['domain']
when 'game'
command_game(id, command['payload'])
when 'player'
command_player(id, command['payload'])
else
raise CommandParserError, "Unable to parse the command type, domain '#{command['domain']}'"
end
return response
end
# Perform the actions for the type of hello and domain of player.
def hello_player(client, id, payload)
response = {}
payload.keys.each do |key, value|
case key
when 'connect'
game.connect_player(client, id, value)
when 'faction'
game.set_player_faction(id, value)
else
raise CommandParserError, "Unable to parse the type hello, domain player, key '#{key}'"
end
end
return response
end
# Perform the actions for the type of query and the domain of game
def query_game(id, payload)
response = {}
payload.keys.each do |key, value|
case key
when 'time'
game.send_time(id, value)
when 'size'
game.send_size(id, value)
else
raise CommandParserError, "Unable to parse the type query, domain game, key '#{key}'"
end
end
return response
end
# Perform the actions for the type of query and the domain of player
def query_player(id, payload)
response = {}
payload.keys.each do |key, value|
case key
when 'resources'
game.get_player_resources(id, value)
when 'planets'
game.get_player_planets(id, value)
when 'buildings'
game.get_player_buildings(id, value)
when 'ships'
game.get_player_ships(id, value)
when 'construction'
game.get_player_construction(id, value)
else
raise CommandParserError, "Unable to parse the type query, domain player, key '#{key}'"
end
end
return response
end
# Perform the actions for the type command and the domain of game
def command_game(id, payload)
response = {}
payload.keys.each do |key, value|
case key
else
raise CommandParserError, "Unable to parse the type command, domain game, key '#{key}'"
end
end
return response
end
# Perform the actions for the type command and the domain of player
def command_player(id, payload)
response = {}
payload.keys.each do |key, value|
case key
else
raise CommandParserError, "Unable to parse the type command, domain player, key '#{key}'"
end
end
return response
end
end
class CommandParserError < StandardError
end
+15 -16
View File
@@ -176,19 +176,17 @@ module Game
# The entry point for all messages that arrive from a client. The
# payload must be a hash decoded from JSON and contain a `type` field.
#
# @param client [TCPSocket] The socket that the client used.
# @param command [Hash] The parsed client command
#
# @return [String] JSON-encoded response
#
def parse_command(command)
def parse_command(client, command)
return error_response('Player not registered!') unless command['player_id']
case command['type']
when 'dbg'
# Debugger - do nothing in production
{ 'type' => 'ok', 'payload' => 'debugging' }.to_json
when 'hello'
error_response('Hello command should already have been handled.')
parse_hello(client, command)
when 'query'
parse_query(command)
when 'command'
@@ -233,7 +231,18 @@ module Game
# @return [String] JSON-encoded error response
#
def error_response(request_id, msg)
{ 'type' => 'error', 'request_id', => id, 'payload' => msg }.to_json
{ 'type' => 'error', 'request_id' => id, 'payload' => msg }.to_json
end
##
# Return an update response to the client
def update_response(request_id, domain, payload)
{'type' => 'update', 'domain' => domain, 'request_id' => request_id, 'payload' => payload}.to_json
end
# Return an event response to the client
def event_response(request_id, domain, payload)
{'type' => 'event', 'domain' => domain, 'request_id' => request_id, 'payload' => payload}.to_json
end
##
@@ -367,16 +376,6 @@ module Game
@players.find { |p| p.player_id == player_id } || false
end
# --------------------------------------------------------------------
# PRIVATE HELPERS - kept simple for the test-suite
# --------------------------------------------------------------------
private
def error_response(msg)
{ 'type' => 'error', 'payload' => msg }.to_json
end
def load_players(ids)
ids.map { |id| Player.load_from_id(id) }
end