177 lines
4.6 KiB
Ruby
177 lines
4.6 KiB
Ruby
# Parser for client commands.
|
|
# Translates incoming JSON requests into game actions.
|
|
|
|
|
|
# Handles the parsing of incoming JSON commands from clients.
|
|
class CommandParser
|
|
def initialize
|
|
end
|
|
|
|
# Parses a command based on its type.
|
|
|
|
# @param client [TCPSocket] The client socket.
|
|
|
|
# @param command [Hash] The command hash.
|
|
|
|
# @return [Hash] The response hash.
|
|
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
|
|
|
|
# Parses a "hello" type command.
|
|
|
|
# @param client [TCPSocket] The client socket.
|
|
|
|
# @param command [Hash] The command hash.
|
|
|
|
# @return [Hash] The response hash.
|
|
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
|
|
|
|
# Parses a "query" type command.
|
|
|
|
# @param command [Hash] The command hash.
|
|
|
|
# @return [Hash] The response hash.
|
|
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
|
|
|
|
# Parses a "command" type command.
|
|
|
|
# @param command [Hash] The command hash.
|
|
|
|
# @return [Hash] The response hash.
|
|
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 'build'
|
|
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 'build'
|
|
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 'build'
|
|
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
|
|
when 'build'
|
|
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
|
|
when 'build'
|
|
else
|
|
raise CommandParserError, "Unable to parse the type command, domain player, key '#{key}'"
|
|
end
|
|
end
|
|
return response
|
|
end
|
|
end
|
|
|
|
# Handles the parsing of incoming JSON commands from clients.
|
|
# Exception raised when a command cannot be parsed.
|
|
class CommandParserError < StandardError
|
|
end
|