148 lines
3.9 KiB
Ruby
148 lines
3.9 KiB
Ruby
# 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
|