Kaylee NRC python

From EG1004 Lab Manual
Jump to: navigation, search

Python Programming

It's expected, but not necessary, that students who wish to do python coding have previous experience working with the software. If the whole group wishes to use it but no one in your group has experience, that's okay, but this will present your group with additional challenges and you will NOT be given any special treatment in terms of Benchmarking or Commisioning.

Python Basics

Bellow are a few logic statements and Boolean expressions that may be useful for coding NRC, more statements and examples can be found online, these are not the only logic statements that can be used.

A Boolean expressions can be true or false, there are a variety of statements that can be used to receive a true (1) or false (0) value.

Figure 1: Boolean

Boolean expressions can be integrated into complex case checking by putting together multiple statements. An "and" statement is true if both conditions are true. An "or" statement is true if AT LEAST one condition is true. A "not" statement is true if the condition is NOT true.

Figure 2: And, Or, Not

An "if" statement is a conditional statement in front of a code that will run a certain block of code only if the statement is true. Both simple and complex Boolean statements can be used.

Figure 3: If statement

An "if else" statement is a conditional statement in front of a code that will run a certain block of code only if the statement is true. And if the conditions are false then the block of code in the "else" portion will be ran.

Figure 4: if else statement

An "elif" can be added to include additional conditions to check for, but the if statements will be ran through in order and once a condition is met the rest of the if else statement is skipped over.

Figure 5: elif statement

A "for loop" will run through a specific set of code (within the for loop) until the condition set at the beginning of the code is met.

Figure 6: for loop

A "while loop" will run through a specific set of coded continuously until the Boolean statement is not longer true.

Figure 7: while loop

NRC API Specifications

Simple coding and commands for NRC are as follows. Examples of how this works in Snap is shown after the following information. Coding is completed in this format so that the code for the nanorobot can properly interact with the main code, the code that holds the diseases and viruses, the main code will be refereed to as "Jesse".

There are two types of request commands that are used here:

  • "GET" requests are used to pull data from the server.
  • "POST" requests can also pull data BUT something is changed on the server side (that's you!), so after putting an input into the server feedback is received from the server based on that input.

The API Specification for NRC is as follows:

All URLs for NRC follow the following format:<command-family>/<username>/<secret>/<action>/<index>
        The base URL is the IP address of the Raspberry Pi running the webserver

        There are two command families currently in use, login and game.
        login -> handles logging in
        game -> handles game interaction

        These fields are used to "authenticate" your game.
        username -> your netid
        secret -> a string set during login.  This CAN be different between games, it is set by
            the user, not the game itself

        These fields are the actual interaction with the game.  Unless otherwise
        specified, they are POST requests that return a json object with a returned status.

        Some actions require you to specify WHICH thing to take.  This is done by providing a numerical index.

Logging in:
    Make a POST request to<username>/<secret>
    Where username is your NetID and secret is a secret of your choosing.

<action> list: 
        POST request.
        Progresses the game forward by one unit of time, here called a tick.
        Returns a one item json object with the status of the tick

        POST request.
        Causes the player character to "Hold." The player will temporarily stop moving through the graph.
            Holding has a limited length of time it can be applied.

        POST request.
        Causes the player character to release a possible "Hold".  The player character will begin moving again.

        POST request.
        The next time the player character can choose their next path they will go down the path in the list with
            the specified index.  Indices begin at 0.  If the selected index is greater than the length of the list
            the player character will go down the last path in the list.  This is NOT automatically cleared and will
            remain until manually reset.
        Returns a one item json object confirming the direction selected.

        POST request.
        Resets the game so that other users can log in.  MUST be done after EVERY run.

        GET request.
        Returns all of the available status for the player character in a json object.  The schema is specified below.

status json object schema:
    "player_postition"-> the name of the node the player character is currently in
    "options"-> the list of nodes the player character can go to next
    "number_of_options"-> the length of the options list
    "score"-> the current score
    "round_number"-> the round number/number of ticks that have occurred
    "rounds_remaining"-> the number of ticks left in the game
    "holding"-> True if the player is holding, otherwise False
    "holding_time_remaining"-> The number of rounds the player could hold
    "bot_visible"-> True if the bot character is within sight of the player character, False otherwise
    "bot_location"-> The name of the bot's current location if it is visible

Logging into Jesse

The following Python code follows the format described in the API specifications above using the format needed to log into Jesse. As action and index are not included for logging in they were left blank, although they can just be omitted from the code.

Figure 8: Login Code

Directions Sample Code

The following Python code follows the format described in the API specifications above using the format needed to give a simple direction to your nanorobot.

Figure 9: Directions Sample Code