Code Behind Yu-Gi-Oh! Backend

Let’s explore the files, classes, and methods that are responsible for Yu-Gi-Oh!’s functionality.

The Player File

The Player Class

This class handles all activity relating to the player. This includes drawing cards, summoning monsters, sending monsters to the graveyard, and other various getters/setters.

class src.player.Player(life_points: int, name: str)

Class which represents a player in a Yugioh game.

change_monster_position(field_idx: int)

Change a monster’s battle position.

Args:
field_idx: Index of the card on the field whose position will be changed.
decrease_life_points(life_points: int)

Decrease player’s life points by a specified amount.

Args:
life_points: The number of points to decrease life points by. If this value is greater than the amount of
life points the player has, the player’s life point total will be set to 0.
draw_card(quantity=1) → None

Draw a specified number cards from the top of the player’s deck.

Args:
quantity: number of cards to draw. If not specified, defaults to 1
get_deck_size() → int

Getter for the number of cards in the player’s deck.

Returns:
The number of cards in the player’s deck.
get_graveyard_size() → int

Getter for the number of cards in the player’s graveyard.

Returns:
The number of cards in the player’s graveyard.
get_hand_size() → int

Getter for the number of cards in player’s hand.

Returns:
The number of cards in the player’s hand.
normal_summon(hand_idx: int, field_idx: int, position: str)

Removes a monster card from the player’s hand and adds it to the field at a specific location.

Args:
hand_idx: Index of the card in the player’s hand to be summoned. field_idx: Index on the field to place the card. position: Position to summon monster in.

Returns:

send_card_to_graveyard(field_idx: int, hand_idx: int) → bool
Get card from either hand or field and send it to the graveyard. A -1 index means that no card from the
hand/field should be sent to the graveyard
Params:
hand_index:int The card in the player’s hand to be sent to the graveyard field_index:int The location of the card on the field to be sent to the graveyard
:return True if a card was successfully sent to the graveyard, False otherwise. A return value of False
means that both parameters had values of -1.
tribute_summon(hand_idx: int, tribute1_idx: int, tribute2_idx: int, position: str)

Removes a monster card from the player’s hand and adds it to the field at a specific location.

Args:
hand_idx: Index of the card in the player’s hand to be summoned. tribute1_idx: Index of the first card to tribute. tribute2_idx: Index of the second card to tribute, may not be used. position: Position to summon monster in.

The Card File

There are two types of card classes related to this version of Yu-Gi-Oh!. The first class is a parent class for a generic card. The second of the two is a more specific Monster Card.

The Generic Card Class

class src.card.Card(name: str, description: str)

A class representing a Yu-Gi-Oh! Card.

display_card() → dict
Gets the information for the current card.
Returns:
display_info:
Dictionary containing the instance variables of the Card class.

The Monster Card Class

class src.card.Monster(name: str, description: str, attribute: str, monster_type: str, level: int, attack_points: int, defense_points: int)

A class representing a Yu-Gi-Oh! Monster Card.

Note: This class only supports Normal monsters in the current build.

display_card() → dict
Gets the information for the current monster card.
Returns:
display_info:
a dict containing the instance variables for the monster card.

The Extra Methods

create_card
Args: card_name: The name of the card to be created Returns: a Card type corresponding to the card name, or None if no card exists with that name
create_deck_from_array

Method that creates a deck given an array of strings of card names Args:

card_name_array: an array of strings of card names to build the deck
Returns:
a list containing Card objects
create_deck_from_preset

Method that creates a deck form a csv file of card names Args:

preset_path: the path of the file that contains a card preset in csv
Returns:
a list containing Card objects
deck_to_card_name_list
Method to convert a list of Card objects to a list of their names only :param cards: :return: a list of card names
monster_card_dict_to_card_object
Method to convert a dictionary representation of a card to a card Object :param card_dict: a dict or json representation of a card :return: a card object using the attriburts in the card_dict
monster_card_to_string
param:card: The card to convert to a string
return:A string in format {cardName} {attack}/{defense}

The Game File

The Game Controller Class

This class deals with controlling the actual functionality of the Yu-Gi-Oh! game. This controller is involved with determining the first player, changing turns, having monsters attack, determining a winner, etc.

class src.game.GameController(session_id=0)
attack_monster(attacking_monster: int, attacked_monster: int)

Conducts an attack from one of current player’s monsters to one of other player’s monsters.

Args:
attacking_monster: Index on the current player’s field of the monster that is attacking. Must be in attack
position.

attacked_monster: Index on the other player’s field of the monster that is being attacked.

attack_player(attacking_monster)

Conducts an attack from one of current player’s monsters directly towards the other player’s life points.

Args:
attacking_monster: Index on the current player’s field of the monster that is attacking.
change_turn()

Changes player turn.

determine_first_player()

Sets starting turn order.

get_winner()

Returns the player that won the game or None if the game was tied.

Should only be called after is_there_winner returns True.

Returns:
Player that won or None if there was a tie
is_there_winner()

Checks if either player has won. A player has won if their opponent’s life_points have reached 0.

Returns:
True if either player’s life point total is 0, False otherwise.
normal_summon(hand_idx: int, position: str)

Summons monster from current_players’s hand onto current_player’s field.

Args:
hand_idx: index in current_player’s hand of monster to summon position: Position to summon monster in.
tribute_summon_monster(hand_idx: int, tribute1_idx: int, tribute2_idx: int, position: str)

Tribute summon monster from current player’s hand onto current player’s field.

Args:

hand_idx: index in current_player’s hand of monster to summon tribute1_idx: index in current_player’s field of first tribute monster tribute2_idx: index in current_player’s field of second tribute monster, only needed when summoning a

level 7 or higher monster

position: Position to summon monster in.

The Yugioh file

The yugioh class

This class is in charge of creating, storing, presenting, and updating a Yu-Gi-Oh! game session.

class src.yugioh.Yugioh

Yugioh is the yugioh_game interface in which the yugioh yugioh_game can be played by CRUD.

create_game(request: dict) → Union[dict, bytes]
Parameters:request – dictionary containing key-value pairs. session_id: session id associated with the

yugioh_game session. ‘player_name’: name of the player ‘deck’ a list of strings of card names to create the deck with ‘get_pickle’ optional false. If true, instead returns a pickled version of the game :return: dict version of Game

delete_game(request: dict) → Union[dict, bytes]
Args:
request: dictionary containing a single key-value pair. The key is “session_id”. The value is a integer unique to all ongoing yugioh_game sessions.
Returns:
reply: dictionary containing a single key-value pair. The key is “session_id”. The value is a integer unique to all ongoing yugioh_game sessions.
read_game(request: dict) → Union[dict, bytes]
Args:
request: dictionary containing single key-value pair. The key is “session_id”. The value is a integer unique to all ongoing yugioh_game sessions.
Returns:
reply: dictionary containing a several key-value pairs that fully describe the yugioh_game’s state.
update_game(request: dict) → dict
Args:
request: dictionary describing the “move” to be made in the yugioh_game keys: :arg: “session_id”: value associated with a yugioh game session “player”: player to make the move as. 1 for player 1 or 2 for player 2 “move”: Move to take in the yugioh game. Values can be , “summon_monster”, “change_turn”, “attack”, “tribute_summon”. “args”: a list of arguments needed to make the move
Returns:
reply: dictionary describing the yugioh_game’s new state.

Code Behind Yu-Gi-Oh! Server

The following is the code for setting up the Yu-Gi-Oh! server:

The Server File

The Server Class

This class is in charge of setting up the server for the game.
class src.server.YugiohServer(server_ip: str, port: int)
threaded_client(session_id: int)

Thread that is associated with a client and receives data from it. It :param client_sock: Socket that corresponds to the connected client :param session_id: Game id that client is associated with :param get_pickle: True if the client requested to get data by pickle :return: None

The Init Server Method

initialize_server

The Network File

The Network Class

This class is in charge of dealing with all network related activities for the server
class src.network.Network
recv_data(sock: socket.socket)

Recieve data until have enough data in buffer to reconstruct

Code Behind Yu-Gi-Oh! CLI

The Account Menu File

The Account Menu Class

This class is in charge of setting up the original CLI and allows the user to deck build, start games, and so on.

The Client File

The Client Class

This class is in charge of connecting to the server and running the game
class src.client.NetworkCli
display_board()

Displays the yugioh_game board and the current player’s hand to the command line :return: None

send_data_and_update_game(data: dict)

Method that sends data to the server and recieves data back to update :param data: data to send to the server :return: None

start_game()

Starts an instance of a yugioh yugioh_game on the command line :return: None

Code Behind Yu-Gi-Oh! Database

The Database File

The User Class

In charge of logging all database user information

The Card Class

In charge of logging all database card information

The Database Functions File

This file contains multiple methods for interacting with the database login

register
user_exists
read_cards_into_db
get_user_stats
create_monster_db_entry
create_spell_db_entry
get_card_info
get_deck_from_db
print_user_decks
save_user_deck
update_win_loss_draw