CYLGame package¶
Submodules¶
CYLGame.Comp module¶
- class CYLGame.Comp.MultiplayerComp(bots, room_size, default_bot_class, run_factor=10)[source]¶
Bases:
object
- class CYLGame.Comp.MultiplayerCompRunner(interval, gamedb, game, compiler, debug=False)[source]¶
Bases:
multiprocessing.context.Process
- class CYLGame.Comp.RollingMultiplayerComp(room_size, default_bot_class, rolling_n)[source]¶
Bases:
object
- class CYLGame.Comp.RollingMultiplayerCompRunner(interval, gamedb, game, compiler, rolling_n=100, batch_size=4, debug=False)[source]¶
Bases:
multiprocessing.context.Process
CYLGame.Database module¶
- class CYLGame.Database.GameDB(root_dir)[source]¶
Bases:
object
This is the database that stores all persisted data. This database is a file based DB.
/ => Root game directory /data => Should be named something like “users”. This stores user related data. /data/TOKEN => A directory containing data for the user with the token TOKEN. /data/TOKEN/avg_score => File containing a single float representing the average score for the user. /data/TOKEN/name => File containing the name for the user. /data/TOKEN/code => Directory containing all code submitted by the user. /data/TOKEN/code/CTIME_HASH => The code submitted or played at CTIME. /data/TOKEN/code/CTIME_HASH/code.lp /data/TOKEN/code/CTIME_HASH/options.mp.gz /data/TOKEN/games => A directory related games. /data/TOKEN/games/GTOKEN => An empty file representing that the users bot was used in game GTOKEN. /games => Stores game related data. /schools => Stores school related data. /competitions => Stores competition related data. /www => Stores static and template files for the server. This is a cache that is deleted and
recreated on each restart.
- ACTIVE_CODE_KEY = 'active_code'¶
- CODE_DIR = 'code'¶
- CODE_FILENAME = 'code.lp'¶
- OPTIONS_FILENAME = 'options.mp.gz'¶
- TOKEN_LEN = 8¶
- get_code_by_hash(code_hash_prefix)[source]¶
Returns the code for the code hash or a list of hashes if the code hash prefix matches multiple codes.
- save_avg_score(token, score)[source]¶
Save a user’s average score.
- Parameters
token (str) – The user’s token.
score (int) – The user’s average score.
- save_code(token, code, options=None, set_as_active=True)[source]¶
Save a user’s code under their token.
- Parameters
token (str) – The user’s token.
code (str) – The user’s code.
options (json-able object) – The user’s options.
set_as_active (bool) – Whether to set the saved code as the active code for the token.
- save_name(token, name)[source]¶
Save a user’s name under their token.
- Parameters
token (str) – The user’s token.
name (str) – The user’s name.
- save_value(token, key, value)[source]¶
- Save a key value pair to a tokens directory. If a value has been saved under the same key it will be
overwritten by the new value passed in. The value can be looked up using the get_value function.
- Parameters
token (str) – Any valid token.
key (str) – The key to store the value under.
value (str or int or float) – The value to be stored.
CYLGame.Display module¶
- class CYLGame.Display.CharSet(image_filepath: pathlib.Path, char_width: int, char_height: int, char_rows: int, char_columns: int)[source]¶
Bases:
CYLGame.Sprite.SpriteSet
- char_columns: int¶
- char_height: int¶
- char_rows: int¶
- char_width: int¶
- image_filepath: pathlib.Path¶
- class CYLGame.Display.Display(width, height, title=None)[source]¶
Bases:
object
- get_keys()[source]¶
Gets the keys that were press since last time you called this function.
- Returns
Key
- update(frame_buffer)[source]¶
Updates the display with the buffer given.
- Parameters
frame_buffer (FrameBuffer) – The buffer to use when redrawing the screen.
- class CYLGame.Display.PyGameDisplay(width, height, title=None)[source]¶
Bases:
CYLGame.Display.Display
- get_keys()[source]¶
Gets the keys that were press since last time you called this function.
- Returns
Key
- update(frame_buffer)[source]¶
Updates the display with the buffer given.
- Parameters
frame_buffer (FrameBuffer) – The buffer to use when redrawing the screen.
CYLGame.Frame module¶
- class CYLGame.Frame.GameFrame[source]¶
Bases:
CYLGame.Frame.FrameBuffer
- class CYLGame.Frame.GridFrameBuffer(width, height, charset=None, init_value='\x00')[source]¶
Bases:
CYLGame.Frame.FrameBuffer
CYLGame.Game module¶
- class CYLGame.Game.Game(random)[source]¶
Bases:
object
- GAME_TITLE = ''¶
- MULTIPLAYER = False¶
- OPTIONS: Optional[str] = None¶
- SCREEN_HEIGHT = 0¶
- SCREEN_WIDTH = 0¶
- WEBONLY = True¶
- create_new_player(prog)[source]¶
This creates n new objects that inherits from the Player class.
- Returns
n new objects that inherit from the Player class with the given program.
- static default_prog_for_computer()[source]¶
This method is only for multi-player games to implement. :returns: type[Prog]
- static get_number_of_players()[source]¶
This method is only for multi-player games to implement. :returns: The number of players needed to play the game. :rtype: int
- class CYLGame.Game.GameRunner(game_class: Type[CYLGame.Game.Game])[source]¶
Bases:
object
- init_game(seed: int) CYLGame.Game.PlayGameState [source]¶
- static move_game(state: CYLGame.Game.PlayGameState, move: str) CYLGame.Game.PlayGameState [source]¶
- class CYLGame.Game.GridGame(random)[source]¶
Bases:
CYLGame.Game.Game
- CHAR_HEIGHT = 8¶
- CHAR_SET = '/home/runner/work/CYLGame/CYLGame/CYLGame/data/fonts/terminal8x8_gs_ro.png'¶
- CHAR_WIDTH = 8¶
- GAME_TITLE = ''¶
- SCREEN_HEIGHT = 25¶
- SCREEN_WIDTH = 80¶
- WEBONLY = False¶
- draw_screen(frame_buffer)[source]¶
WARNING: There MUST NOT be any game logic in this function since it isn’t called when simulating the game during the competitions.
- class CYLGame.Game.NonGridGame(random)[source]¶
Bases:
CYLGame.Game.Game
- WEBONLY = True¶
- class CYLGame.Game.PlayGameState(game: CYLGame.Game.Game, computer_players: List[CYLGame.Player.Player], human_player: CYLGame.Player.Player, human_prog: CYLGame.Player.UserProg, seed: int, moves: str, frame: Optional[List[List[int]]])[source]¶
Bases:
object
- computer_players: List[CYLGame.Player.Player]¶
- frame: Optional[List[List[int]]]¶
- game: CYLGame.Game.Game¶
- human_player: CYLGame.Player.Player¶
- human_prog: CYLGame.Player.UserProg¶
- moves: str¶
- seed: int¶
CYLGame.Panels module¶
- class CYLGame.Panels.Map(width, height, default_char=' ')[source]¶
Bases:
object
- char_to_ps: This is a dictionary which has a character for the keys and the value for the key is a set of all the
positions where that char is located.
- p_to_char: The is a dictionary which has a position(tuple with two elements: x and y) as a key and the current
ColoredChar as the value.
- class CYLGame.Panels.MapPanel(x, y, w, h, default_char=' ', border=<CYLGame.Panels.PanelBorder object>, padding=<CYLGame.Panels.PanelPadding object>)[source]¶
Bases:
CYLGame.Panels.Panel
- class CYLGame.Panels.MessagePanel(x, y, w, h, default_char=' ', border=<CYLGame.Panels.PanelBorder object>, padding=<CYLGame.Panels.PanelPadding object>)[source]¶
Bases:
CYLGame.Panels.Panel
This panel contains messages to be displayed to the user. It acts as a scrolling text log. This means that the first message is display on the first line and some on till you have enough messages to fill the height. Then the next message added will push all the other messages up and the first message will be discarded.
Example
- A MessagePanel with height of 3. Add messages “A”, “B” and “C”. It will display like the following:
A B C
- Add message “D” and it will display like the following:
B C D
Note
The following is deprecated: >>> m = MessagePanel() >>> m += [“Hi”] You should do this instead: >>> m = MessagePanel() >>> m.add(“Hi”)
- class CYLGame.Panels.Panel(x, y, w, h, background_char=' ', border=<CYLGame.Panels.PanelBorder object>, padding=<CYLGame.Panels.PanelPadding object>)[source]¶
Bases:
CYLGame.Panels.Map
- class CYLGame.Panels.PanelBorder(sides=0, char=None)[source]¶
Bases:
object
- BOTTOM = 8¶
- DEFAULT_CHARS = {1: 'Ä', 2: '³', 3: '¿', 4: '³', 5: 'Ú', 8: 'Ä', 10: 'Ù', 12: 'À'}¶
- LEFT = 4¶
- RIGHT = 2¶
- TOP = 1¶
- static create(top=False, bottom=False, right=False, left=False, top_right=False, top_left=False, bottom_right=False, bottom_left=False)[source]¶
Use this method for an easy way to create nice borders. Set any side to true to create a border with the default characters. You can also set any side to a character which will create a border with that character instead. In order for the corners (top_right, top_left, …) to be rendered both of their respective sides must be set to True or a char.
Ex. >>> PanelBorder.create(top=True) # This will create a top border with the default char
>>> PanelBorder.create(top='$') # This will create a top border of '$'s.
>>> PanelBorder.create(top='=', left='!', top_left='?') # This will create a border looking like some thing below. ?==== ! !
- class CYLGame.Panels.PanelPadding(sides=0, size=0)[source]¶
Bases:
object
- BOTTOM = 8¶
- DEFAULT_SIZE = 0¶
- LEFT = 4¶
- RIGHT = 2¶
- TOP = 1¶
- class CYLGame.Panels.StatusPanel(x, y, w, h, default_char=' ', border=<CYLGame.Panels.PanelBorder object>, padding=<CYLGame.Panels.PanelPadding object>)[source]¶
Bases:
CYLGame.Panels.MessagePanel
CYLGame.Player module¶
- class CYLGame.Player.DefaultGridPlayer(prog: CYLGame.Player.Prog, bot_consts: CYLGame.structures.const_mapping.ConstMapping)[source]¶
Bases:
CYLGame.Player.Player
Make sure that all bot_vars are updated in game.do_turn
- class CYLGame.Player.LittlePythonProg(prog: littlepython.interpreter.LPProg, options: dict, token: Optional[str] = None, name: Optional[str] = None, code_hash: Optional[str] = None)[source]¶
Bases:
CYLGame.Player.Prog
- class CYLGame.Player.Player(prog: CYLGame.Player.Prog)[source]¶
Bases:
abc.ABC
- class CYLGame.Player.Prog(options: dict, token: Optional[str] = None, name: Optional[str] = None, code_hash: Optional[str] = None)[source]¶
Bases:
abc.ABC
- class CYLGame.Player.Room(bots: Optional[List[CYLGame.Player.Prog]] = None, seed=None)[source]¶
Bases:
object
- property rand_seeded¶
A randomly seeded room with the same bots.
- class CYLGame.Player.UserProg[source]¶
Bases:
CYLGame.Player.Prog
CYLGame.SensorGame module¶
- class CYLGame.SensorGame.SensorGame(random)[source]¶
Bases:
CYLGame.Game.NonGridGame
- NONGRID = True¶
- OPTIONS: Optional[str] = 'sensors'¶
- WEBONLY = True¶
- class CYLGame.SensorGame.SensorPlayer(prog: CYLGame.Player.Prog)[source]¶
Bases:
CYLGame.Player.Player
- add_sensor(_range, angle, width, turret)[source]¶
Adds a sensor to this player. _range: integer [0, 100], the distance that the sensor travels angle: integer [0, 360], the direction in degrees the sensor points width: integer [0, 360], the width in degrees of the sensor turret: bool, if True, the angle is relative to the turret’s angle, otherwise angle is relative to the tank’s angle
- class CYLGame.SensorGame.SensorSanitizers[source]¶
Bases:
object
- color_re = re.compile('#[0-9A-Fa-f]{6}')¶
CYLGame.Server module¶
- class CYLGame.Server.GameServer[source]¶
Bases:
flask_classful.FlaskView
- avg_game_count = None¶
- charset = None¶
- compression = None¶
- game = None¶
- gamedb = None¶
- host = None¶
- language = None¶
- play_game_cache: cachetools.LRUCache¶
- port = None¶
- route_base = '/'¶
- classmethod serve(game, host='', port=5000, compression=False, language=0, avg_game_count=10, multiplayer_scoring_interval=20, num_of_threads=None, game_data_path='temp_game', avg_game_func=<function scorer.<locals>.wrapped>, debug=False, reuse_addr=None, play_cache_size=64, error_log_file='{dbfile}/log/error.log', debug_log_file='{dbfile}/log/debug.log')[source]¶
- CYLGame.Server.serve(game, host='', port=5000, compression=False, language=0, avg_game_count=10, multiplayer_scoring_interval=20, num_of_threads=None, game_data_path='temp_game', avg_game_func=<function scorer.<locals>.wrapped>, debug=False, reuse_addr=None, play_cache_size=64, error_log_file='{dbfile}/log/error.log', debug_log_file='{dbfile}/log/debug.log')¶
CYLGame.Utils module¶
- class CYLGame.Utils.OnlineMean(i=0, mean=0, roll_after_n=- 1)[source]¶
Bases:
object
- property floored_mean¶
- CYLGame.Utils.int2base(x, base)[source]¶
This function converts an int to a str using a specific base.
- Note: This function was created to support weird bases that don’t already have a builtin method. So if you want to
convert ints to base 2, 8, 16 it is better to use bin, oct, hex method respectively.