Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match Recovery #19

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2de1aa7
Create game_serialization.py
ochan1 Oct 24, 2019
e1c3d29
test push
conormora Oct 24, 2019
edd03c6
serialization functions created
ochan1 Nov 21, 2019
e63a5ef
page route for match recovery and some styling
akshitdewan Nov 21, 2019
f023991
lcm added
ochan1 Dec 5, 2019
5221814
Game Data Load (w/o lcm & header added)
ochan1 Dec 5, 2019
6069735
lcm setup
akshitdewan Dec 5, 2019
6950794
merge
akshitdewan Dec 5, 2019
8a4aecc
game data is sent from backend when match recovery page is opened and…
akshitdewan Jan 29, 2020
1574aa5
1 field and 2 field basic setup
Jan 29, 2020
094dbdb
update json function w/ individual field reading and basic alliance s…
akshitdewan Jan 29, 2020
ff8c93b
updated 4 fields
Jan 29, 2020
0407cc2
merge
Jan 29, 2020
7ff58b4
also added things formatted differently than 'ALLIANCES'
akshitdewan Jan 29, 2020
3a74337
able to send data from frontend to backend. does not update yet becau…
akshitdewan Jan 29, 2020
9e1640c
id setting
Jan 29, 2020
b2e43d3
updated generate alliance fields with DOM
Jan 30, 2020
ef928d1
Merge branch 'shepherd/game_serialization' of https://github.com/pion…
Jan 30, 2020
1aeb446
updated for distinct names in alliances
Feb 6, 2020
675e213
updated for order
Feb 6, 2020
cdb5507
adjusted upload JSON method
Feb 13, 2020
f5e3755
finalizing changes to match recovery
Feb 15, 2020
ce5f01b
im not sure
akshitdewan Feb 15, 2020
253af2e
merged with tims final work (hopefully)
akshitdewan Feb 15, 2020
a01e62a
quick update JSON bug fix
Feb 15, 2020
1bc320d
gitignore and merging tims changes
akshitdewan Feb 15, 2020
3e540a1
gitignore updated2 and merging getting score
akshitdewan Feb 15, 2020
f786cf3
added score
akshitdewan Feb 20, 2020
a4a2f75
the state is now being loaded properly. fixed update json and updated…
akshitdewan Feb 20, 2020
b91d232
finally delete all but shepherd
akshitdewan Jun 13, 2020
98c0b45
mrege with master
akshitdewan Aug 22, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,7 @@ hibike/virtual_devices.txt
/dawn-*
/artifacts
.vagrant

# Game State Log
shepherd/game_data.json

11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.7"
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 0 additions & 82 deletions README.md

This file was deleted.

25 changes: 12 additions & 13 deletions shepherd/Alliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from Timer import *
from LCM import *


class Alliance:
"""This is the Alliance class, which holds the state values used to track
the scores and states of the alliances
Expand Down Expand Up @@ -39,31 +40,29 @@ def change_score(self, amount):
"""
self.score += amount
lcm_send(LCM_TARGETS.SCOREBOARD, SCOREBOARD_HEADER.SCORE,
{"alliance" : self.name, "score" : math.floor(self.score)})
{"alliance": self.name, "score": math.floor(self.score)})

def reset(self):
self.score = 0
self.team_1_connection = False
self.team_2_connection = False
lcm_send(LCM_TARGETS.SCOREBOARD, SCOREBOARD_HEADER.SCORE,
{"alliance" : self.name, "score" : math.floor(self.score)})
#TODO: Send info to sensors about reset
#TODO: Send info to UI about reset
#TODO: Move score sends to shepherd.py
{"alliance": self.name, "score": math.floor(self.score)})
# TODO: Send info to sensors about reset
# TODO: Send info to UI about reset
# TODO: Move score sends to shepherd.py
self.recipe_times = []
self.recipe_count = 0
self.penalties = []

# __dict__ is a Class Built-In to get the instance variable values of instance {VARIABLE, VALUE}

def __str__(self):
return ("<alliance: " + self.name + "> <teams: " + self.team_1_name + " " +
str(self.team_1_number) + ", " + self.team_2_name + " " + str(self.team_2_number) +
"> <score: " + str(self.score) + ">")

def increment(self, _time):
#TODO: Add the time to the recipe_times
self.recipe_times.append(_time)
self.recipe_count += 1

def penalty(self, _time):
#TODO: Add penalites
self.penalties.append(_time)
def __repr__(self):
return ("<alliance: " + self.name + "> <teams: " + self.team_1_name + " " +
str(self.team_1_number) + ", " + self.team_2_name + " " + str(self.team_2_number) +
"> <score: " + str(self.score) + ">")
112 changes: 104 additions & 8 deletions shepherd/Shepherd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import Sheet
import bot
import audio
import game_serialization


clients = RuntimeClientManager((), ())
Expand All @@ -35,11 +36,21 @@ def start():
EVENTS = queue.Queue()
lcm_start_read(LCM_TARGETS.SHEPHERD, EVENTS)
while True:
print("GAME STATE OUTSIDE: ", GAME_STATE)

# # Will not be used (same as LAST_HEADER), {"LAST_HEADER": LAST_HEADER, "payload": payload}
# No need "EVENTS"
# "GAME_STATE": GAME_STATE
# "MATCH_NUMBER": MATCH_NUMBER
# "STARTING_SPOTS": STARTING_SPOTS
# "MASTER_ROBOTS": MASTER_ROBOTS # Variables will evaluate (the dot stuff)
# "BUTTONS": BUTTONS
# "CODES_USED": CODES_USED

print("GAME STATE: ", GAME_STATE)
time.sleep(0.1)
payload = EVENTS.get(True)
LAST_HEADER = payload
print(payload)
print("payload ", payload)
if GAME_STATE == STATE.SETUP:
func = SETUP_FUNCTIONS.get(payload[0])
if func is not None:
Expand Down Expand Up @@ -122,6 +133,8 @@ def to_setup(args):
print({"blue_score" : ALLIANCES[ALLIANCE_COLOR.BLUE].score,
"gold_score" : ALLIANCES[ALLIANCE_COLOR.GOLD].score})

save_game()

def to_auto(args):
'''
Move to the autonomous stage where robots should begin autonomously.
Expand All @@ -131,6 +144,9 @@ def to_auto(args):
#pylint: disable= no-member
global GAME_STATE
global clients

save_game()

try:
alternate_connections = (ALLIANCES[ALLIANCE_COLOR.BLUE].team_1_custom_ip,
ALLIANCES[ALLIANCE_COLOR.BLUE].team_2_custom_ip,
Expand Down Expand Up @@ -165,6 +181,9 @@ def to_wait(args):
Some years, there might be methods that can be called once in the wait stage
'''
global GAME_STATE

save_game()

GAME_STATE = STATE.WAIT
lcm_send(LCM_TARGETS.SCOREBOARD, SCOREBOARD_HEADER.STAGE, {"stage": GAME_STATE})
disable_robots()
Expand All @@ -176,6 +195,9 @@ def to_teleop(args):
By the end, should be in teleop state and the teleop match timer should be started.
'''
global GAME_STATE

save_game()

GAME_STATE = STATE.TELEOP
lcm_send(LCM_TARGETS.SCOREBOARD, SCOREBOARD_HEADER.STAGE, {"stage": GAME_STATE})

Expand All @@ -193,6 +215,9 @@ def to_end(args):
and final score adjustments can be made.
'''
global GAME_STATE

save_game()

lcm_send(LCM_TARGETS.UI, UI_HEADER.SCORES,
{"blue_score" : math.floor(ALLIANCES[ALLIANCE_COLOR.BLUE].score),
"gold_score" : math.floor(ALLIANCES[ALLIANCE_COLOR.GOLD].score)})
Expand Down Expand Up @@ -224,6 +249,9 @@ def reset(args=None):
BUTTONS['gold_2'] = False
BUTTONS['blue_1'] = False
BUTTONS['blue_2'] = False
MASTER_ROBOTS['blue'] = ""
MASTER_ROBOTS['gold'] = ""
CODES_USED = []
lcm_send(LCM_TARGETS.TABLET, TABLET_HEADER.RESET)
lcm_send(LCM_TARGETS.DAWN, DAWN_HEADER.RESET)
print("RESET MATCH, MOVE TO SETUP")
Expand Down Expand Up @@ -394,6 +422,65 @@ def send_connections(args):
# "b_2_connection" : ALLIANCES[ALLIANCE_COLOR.BLUE].team_2_connection}
# lcm_send(LCM_TARGETS.UI, UI_HEADER.CONNECTIONS, msg)

def load_game(args):
"""
Load the game since last game
"""
game_serialization.load_json()
lcm_send(LCM_TARGETS.UI, UI_HEADER.SCORES,
{"blue_score" : None,
"gold_score" : None})

def load_game_data(args):
print("inside load game data with data ", args)
global GAME_STATE
global MATCH_NUMBER
global STARTING_SPOTS
global MASTER_ROBOTS
global BUTTONS
global CODES_USED
global ALLIANCES

GAME_STATE = args["GAME_STATE"]
MATCH_NUMBER = args["MATCH_NUMBER"]
STARTING_SPOTS = args["STARTING_SPOTS"]
MASTER_ROBOTS = args["MASTER_ROBOTS"]
BUTTONS = args["BUTTONS"]
CODES_USED = args["CODES_USED"]
ALLIANCES = args["ALLIANCES"]

for key in ALLIANCES:
if ALLIANCES[key] is not None:
param_data = ALLIANCES[key]
print("param_data", param_data)
ALLIANCES[key] = Alliance(param_data["name"], param_data["team_1_name"], param_data["team_1_number"], \
param_data["team_2_name"], param_data["team_2_number"], param_data["team_1_custom_ip"], \
param_data["team_2_custom_ip"])
ALLIANCES[key].score = param_data["score"]
print("inside load game data with alliances ",ALLIANCES)
print("Game state",GAME_STATE)
print("match number",MATCH_NUMBER)
print("starting spots",STARTING_SPOTS)
print("master robots",MASTER_ROBOTS)
print("buttons",BUTTONS)
print("codes_used",CODES_USED)
print("alliances",ALLIANCES)
save_game()

def save_game():

alliance_processed = dict(ALLIANCES)
print("alliance processed:",alliance_processed)
for key in alliance_processed:
if alliance_processed[key] is not None:
alliance_processed[key] = alliance_processed[key].__dict__
print("in save game")
robot_order = ["blue_1", "blue_2", "gold_1", "gold_2"]
starting_spots_dict = {robot_order[i]: STARTING_SPOTS[i] for i in range(4)}

game_serialization.create_json({"GAME_STATE": GAME_STATE, "MATCH_NUMBER": MATCH_NUMBER, "STARTING_SPOTS": STARTING_SPOTS, \
"MASTER_ROBOTS": MASTER_ROBOTS, "BUTTONS": BUTTONS, "CODES_USED": CODES_USED, "ALLIANCES": alliance_processed})

###########################################
# Event to Function Mappings for each Stage
###########################################
Expand All @@ -402,7 +489,9 @@ def send_connections(args):
SHEPHERD_HEADER.SETUP_MATCH: to_setup,
SHEPHERD_HEADER.SCORE_ADJUST : score_adjust,
SHEPHERD_HEADER.GET_MATCH_INFO : get_match,
SHEPHERD_HEADER.START_NEXT_STAGE: to_auto
SHEPHERD_HEADER.START_NEXT_STAGE: to_auto,
SHEPHERD_HEADER.REQUEST_LATEST_DATA: load_game,
SHEPHERD_HEADER.UPDATE_SHEPHERD_DATA: load_game_data
}

AUTO_FUNCTIONS = {
Expand All @@ -412,8 +501,9 @@ def send_connections(args):
SHEPHERD_HEADER.ROBOT_OFF : disable_robot,
#SHEPHERD_HEADER.CODE_RETRIEVAL : bounce_code,
SHEPHERD_HEADER.ROBOT_CONNECTION_STATUS: set_connections,
SHEPHERD_HEADER.REQUEST_CONNECTIONS: send_connections

SHEPHERD_HEADER.REQUEST_CONNECTIONS: send_connections,
SHEPHERD_HEADER.REQUEST_LATEST_DATA: load_game,
SHEPHERD_HEADER.UPDATE_SHEPHERD_DATA: load_game_data
}

WAIT_FUNCTIONS = {
Expand All @@ -422,7 +512,9 @@ def send_connections(args):
SHEPHERD_HEADER.GET_SCORES : get_score,
SHEPHERD_HEADER.START_NEXT_STAGE : to_teleop,
SHEPHERD_HEADER.ROBOT_CONNECTION_STATUS: set_connections,
SHEPHERD_HEADER.REQUEST_CONNECTIONS: send_connections
SHEPHERD_HEADER.REQUEST_CONNECTIONS: send_connections,
SHEPHERD_HEADER.REQUEST_LATEST_DATA: load_game,
SHEPHERD_HEADER.UPDATE_SHEPHERD_DATA: load_game_data
}

TELEOP_FUNCTIONS = {
Expand All @@ -432,7 +524,9 @@ def send_connections(args):
SHEPHERD_HEADER.ROBOT_OFF : disable_robot,
#SHEPHERD_HEADER.CODE_RETRIEVAL : bounce_code,
SHEPHERD_HEADER.ROBOT_CONNECTION_STATUS: set_connections,
SHEPHERD_HEADER.REQUEST_CONNECTIONS: send_connections
SHEPHERD_HEADER.REQUEST_CONNECTIONS: send_connections,
SHEPHERD_HEADER.REQUEST_LATEST_DATA: load_game,
SHEPHERD_HEADER.UPDATE_SHEPHERD_DATA: load_game_data

}

Expand All @@ -444,7 +538,9 @@ def send_connections(args):
SHEPHERD_HEADER.GET_MATCH_INFO : get_match,
SHEPHERD_HEADER.FINAL_SCORE : final_score,
SHEPHERD_HEADER.ROBOT_CONNECTION_STATUS: set_connections,
SHEPHERD_HEADER.REQUEST_CONNECTIONS: send_connections
SHEPHERD_HEADER.REQUEST_CONNECTIONS: send_connections,
SHEPHERD_HEADER.REQUEST_LATEST_DATA: load_game,
SHEPHERD_HEADER.UPDATE_SHEPHERD_DATA: load_game_data
}

###########################################
Expand Down
Loading