Skip to content

Commit

Permalink
* Sample MyStrategy.py now generates minimal amount of pylint warnings
Browse files Browse the repository at this point in the history
* debug_client.py now generates no pylint warnings
* DebugClient can now be set to accept coordinates in "tiles" and auto-converts them
* Added drawing of waypoints in sample MyStrategy
  • Loading branch information
Vasily committed Nov 16, 2015
1 parent 587157e commit 6ee0bba
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 24 deletions.
33 changes: 24 additions & 9 deletions clients/python2/MyStrategy.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
from model.Car import Car
from model.Game import Game
from model.Move import Move
from model.World import World
'''
Main stragegy module
'''

from model.Car import Car #pylint: disable=unused-import
from model.Game import Game #pylint: disable=unused-import
from model.Move import Move #pylint: disable=unused-import
from model.World import World #pylint: disable=unused-import

try:
from debug_client import Color
except ImportError:
pass

class MyStrategy:
class MyStrategy: #pylint: disable=old-style-class, too-few-public-methods
'''
Main class defining the strategy
'''
def __init__(self):
try:
from debug_client import DebugClient
except ImportError: # no debug module, maybe running on the russianaicup.ru server
self.debug = None
else:
self.debug = DebugClient()
self.GREEN = Color(r=0.0, g=1.0, b=0.0)
self.green = Color(r=0.0, g=1.0, b=0.0)

def move(self, me, world, game, move):
def move(self, me, world, game, move): #pylint: disable=invalid-name
"""
@type me: Car
@type world: World
@type game: Game
@type move: Move
"""
if self.debug:
self.debug.use_tile_coords(game)
with self.debug.pre() as dbg:
dbg.fill_circle(100.0, 100.0, 50.0, (1.0, 0.0, 0.0))
# show the waypoints
for idx, waypoint in enumerate(world.waypoints, 1):
wayX, wayY = waypoint
dbg.fill_rect(wayX, wayY, wayX + 1, wayY + 1, (0.9, 0.9, 0.9))
dbg.text(wayX + 0.5, wayY + 0.5, idx, (0.2, 0.5, 0.2))

self.debug.use_tile_coords(False)
with self.debug.post() as dbg:
dbg.circle(game.world_width * game.track_tile_size - 100,
game.world_height * game.track_tile_size - 100,
50.0, self.GREEN)
dbg.text(me.x, me.y, 'wooo!', (0.0, 0.0, 1.0))
50.0, self.green)
dbg.text(me.x, me.y, 'wooo woo', (0.0, 0.0, 1.0))

move.engine_power = 1.0
move.throw_projectile = True
Expand Down
81 changes: 66 additions & 15 deletions clients/python2/debug_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'''
Python client for visualizer plugin for Russian AI Cup
'''

import socket
import collections

Color = collections.namedtuple('Color', 'r g b')

class DebugClient:
class DebugClient(object):
'''
Main class for controlling the plugin
'''
DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = 13579

Expand All @@ -15,13 +22,22 @@ def __init__(self, host=None, port=None):
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
self.socket.connect((host or self.DEFAULT_HOST, port or self.DEFAULT_PORT))
self.mode = self.MODE_UNKNOWN
self.convert_tile_coords = None

def pre(self):
'''
Method to create a pre-drawing context, that is, to draw things that should be drawn
*before* the field is drawn by local runner (i.e. they will appear "beneath" the field)
'''
assert self.mode == self.MODE_UNKNOWN
self.mode = self.MODE_PRE
return self

def post(self):
'''
Method to create a post-drawing context, that is, to draw things that should be drawn
*after* the field is drawn by local runner (i.e. they will appear "above" the field)
'''
assert self.mode == self.MODE_UNKNOWN
self.mode = self.MODE_POST
return self
Expand All @@ -37,35 +53,70 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.mode = self.MODE_UNKNOWN

@staticmethod
def __make_color(color):
def __make_color(color): #pylint: disable=missing-docstring
if isinstance(color, Color):
return color
if isinstance(color, (tuple, list)) and len(color) >= 3:
return Color(r=color[0], g=color[1], b=color[2])
return color

def __send_command(self, cmd, color, args, pattern=None):
def use_tile_coords(self, game=None):
'''
Turns on or off coordinates convertion from tiles to absolute; to turn convertion on
pass actual in game object, to turn off pass None
'''
if game:
self.convert_tile_coords = game.track_tile_size
else:
self.convert_tile_coords = None

def __convert_coords(self, *coords): #pylint: disable=missing-docstring
if self.convert_tile_coords is not None:
return tuple(coord * self.convert_tile_coords for coord in coords)
return coords

def __send_command(self, cmd, color, args, pattern=None): #pylint: disable=missing-docstring
assert self.mode != self.MODE_UNKNOWN
color = self.__make_color(color)
if not pattern:
pattern = '%s' + (' %f' * len(args)) + ' %f %f %f\n'
self.socket.sendall(pattern % ((cmd,) + args + color))

def circle(self, x0, y0, r0, color):
self.__send_command('circle', color, args=(x0, y0, r0))
def circle(self, x0, y0, r0, color): #pylint: disable=invalid-name
'''
Draws a non-filled circle at (x0, y0) with radius "r0" and color "color"
'''
self.__send_command('circle', color, args=self.__convert_coords(x0, y0, r0))

def fill_circle(self, x0, y0, r0, color):
self.__send_command('fill_circle', color, args=(x0, y0, r0))
def fill_circle(self, x0, y0, r0, color): #pylint: disable=invalid-name
'''
Draws a filled circle at (x0, y0) with radius "r0" and color "color"
'''
self.__send_command('fill_circle', color, args=self.__convert_coords(x0, y0, r0))

def rect(self, x0, y0, x1, y1, color):
self.__send_command('rect', color, args=(x0, y0, x1, y1))
def rect(self, x0, y0, x1, y1, color): #pylint: disable=invalid-name, too-many-arguments
'''
Draws a non-filled rect with top-left at (x0, y0) and bottom-right at (x1, y1)
with color "color"
'''
self.__send_command('rect', color, args=self.__convert_coords(x0, y0, x1, y1))

def fill_rect(self, x0, y0, x1, y1, color):
self.__send_command('fill_rect', color, args=(x0, y0, x1, y1))
def fill_rect(self, x0, y0, x1, y1, color): #pylint: disable=invalid-name, too-many-arguments
'''
Draws a filled rect with top-left at (x0, y0) and bottom-right at (x1, y1)
with color "color"
'''
self.__send_command('fill_rect', color, args=self.__convert_coords(x0, y0, x1, y1))

def line(self, x0, y0, x1, y1, color):
self.__send_command('line', color, args=(x0, y0, x1, y1))
def line(self, x0, y0, x1, y1, color): #pylint: disable=invalid-name, too-many-arguments
'''
Draws a line from (x0, y0) to (x1, y1) with color "color"
'''
self.__send_command('line', color, args=self.__convert_coords(x0, y0, x1, y1))

def text(self, x0, y0, msg, color):
self.__send_command('text', color, args=(x0, y0, msg),
def text(self, x0, y0, msg, color): #pylint: disable=invalid-name
'''
Shows a text message "msg" at (x0, y0) with color "color"
'''
self.__send_command('text', color, args=self.__convert_coords(x0, y0) + (msg,),
pattern='%s %f %f %s %f %f %f\n')

0 comments on commit 6ee0bba

Please sign in to comment.