Fixed tests. Need to check for damage from AI code
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
# 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.
|
||||
# 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']
|
||||
@@ -24,6 +29,13 @@ class CommandParser
|
||||
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']
|
||||
@@ -36,6 +48,11 @@ class CommandParser
|
||||
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']
|
||||
@@ -50,6 +67,11 @@ class CommandParser
|
||||
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']
|
||||
@@ -69,6 +91,7 @@ class CommandParser
|
||||
response = {}
|
||||
payload.keys.each do |key, value|
|
||||
case key
|
||||
when 'build'
|
||||
when 'connect'
|
||||
game.connect_player(client, id, value)
|
||||
when 'faction'
|
||||
@@ -85,6 +108,7 @@ class CommandParser
|
||||
response = {}
|
||||
payload.keys.each do |key, value|
|
||||
case key
|
||||
when 'build'
|
||||
when 'time'
|
||||
game.send_time(id, value)
|
||||
when 'size'
|
||||
@@ -101,6 +125,7 @@ class CommandParser
|
||||
response = {}
|
||||
payload.keys.each do |key, value|
|
||||
case key
|
||||
when 'build'
|
||||
when 'resources'
|
||||
game.get_player_resources(id, value)
|
||||
when 'planets'
|
||||
@@ -123,6 +148,7 @@ class CommandParser
|
||||
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
|
||||
@@ -135,6 +161,7 @@ class CommandParser
|
||||
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
|
||||
@@ -143,5 +170,7 @@ class CommandParser
|
||||
end
|
||||
end
|
||||
|
||||
# Handles the parsing of incoming JSON commands from clients.
|
||||
# Exception raised when a command cannot be parsed.
|
||||
class CommandParserError < StandardError
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user