Skip to content

Commit

Permalink
Merge pull request #166 from battlecode/webinfra-scrim-score
Browse files Browse the repository at this point in the history
Webinfra scrim score
  • Loading branch information
opheez authored Dec 29, 2020
2 parents e8d6430 + a71dbb0 commit db39781
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 16 deletions.
18 changes: 18 additions & 0 deletions backend/api/migrations/0019_scrimmage_winscore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.13 on 2020-12-27 02:03

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0018_auto_20200416_2354'),
]

operations = [
migrations.AddField(
model_name='scrimmage',
name='winscore',
field=models.IntegerField(null=True),
),
]
18 changes: 18 additions & 0 deletions backend/api/migrations/0020_scrimmage_losescore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.13 on 2020-12-27 17:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0019_scrimmage_winscore'),
]

operations = [
migrations.AddField(
model_name='scrimmage',
name='losescore',
field=models.IntegerField(null=True),
),
]
2 changes: 2 additions & 0 deletions backend/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class Scrimmage(models.Model):

# Match-running (completed by match runner)
status = models.TextField(choices=SCRIMMAGE_STATUS_CHOICES, default='pending')
winscore = models.IntegerField(null=True)
losescore = models.IntegerField(null=True)
replay = models.TextField(blank=True)

# Metadata
Expand Down
2 changes: 1 addition & 1 deletion backend/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ScrimmageSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Scrimmage
fields = ('url', 'id', 'league', 'red_team', 'red_mu', 'blue_team', 'blue_mu', 'ranked',
'status', 'replay', 'requested_by', 'requested_at', 'started_at', 'updated_at', 'tournament_id')
'status', 'winscore', 'losescore', 'replay', 'requested_by', 'requested_at', 'started_at', 'updated_at', 'tournament_id')
read_only_fields = ('url', 'requested_at', 'started_at', 'updated_at')


Expand Down
22 changes: 17 additions & 5 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,16 +1038,28 @@ def set_outcome(self, request, league_id, team, pk=None):
scrimmage = Scrimmage.objects.all().get(pk=pk)
except:
return Response({'message': 'Scrimmage does not exist.'}, status.HTTP_404_NOT_FOUND)

# return Response(request.data, status.HTTP_200_OK)
if 'status' in request.data:
sc_status = request.data['status']
sc_status = request.data['status']
if sc_status == "redwon" or sc_status == "bluewon":

if 'winscore' in request.data and 'losescore' in request.data:
sc_winscore = request.data['winscore']
sc_losescore = request.data['losescore']
else:
return Response({'message': 'Must include both winscore and losescore in request.'},
status.HTTP_400_BAD_REQUEST)

if int(sc_winscore) < (float(sc_winscore) + float(sc_losescore))/2.0:
return Response({'message': 'Scores invalid. Winscore must be at least half of total games.'}, status.HTTP_400_BAD_REQUEST)
scrimmage.status = sc_status
scrimmage.winscore = sc_winscore
scrimmage.losescore = sc_losescore

# if tournament, then return here
if scrimmage.tournament_id is not None:
scrimmage.save()
return Response({'status': sc_status}, status.HTTP_200_OK)
return Response({'status': sc_status, 'winscore': sc_winscore, 'losescore': sc_losescore}, status.HTTP_200_OK)

# update rankings using elo
# get team info
Expand Down Expand Up @@ -1081,12 +1093,12 @@ def set_outcome(self, request, league_id, team, pk=None):
lost.save()

scrimmage.save()
return Response({'status': sc_status}, status.HTTP_200_OK)
return Response({'status': sc_status, 'winscore': sc_winscore, 'losescore': sc_losescore}, status.HTTP_200_OK)
elif sc_status == "error":
scrimmage.status = sc_status

scrimmage.save()
return Response({'status': sc_status}, status.HTTP_200_OK)
return Response({'status': sc_status, 'winscore': None, 'losescore': None}, status.HTTP_200_OK)
else:
return Response({'message': 'Set scrimmage to pending/queued/cancelled with accept/reject/cancel api calls'}, status.HTTP_400_BAD_REQUEST)
else:
Expand Down
17 changes: 12 additions & 5 deletions backend/backend_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
response = requests.post(domain + 'auth/token/', data=data)
token = json.loads(response.text)['access']

# data = {
# 'type': 'tour_scrimmage',
# 'tournament_id': '-1',
# 'player1': '917',
# 'player2': '919'
# }
data = {
'type': 'tour_scrimmage',
'tournament_id': '-1',
'player1': '917',
'player2': '919'
'status': 'error',
'winscore': None,
'losescore': None
}
headers = {"Authorization": "Bearer " + token}

response = requests.post(domain + 'api/match/enqueue/', data=data, headers=headers)
# response = requests.post(domain + 'api/match/enqueue/', data=data, headers=headers)
response = requests.patch(domain + 'api/0/scrimmage/1/set_outcome/', data=data, headers=headers)

print(response.text)
10 changes: 9 additions & 1 deletion frontend/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,18 @@ class Api {
else if (s[i].status === 'bluewon') s[i].status = on_red ? 'lost' : 'won';

if (s[i].status !== 'lost' && s[i].status !== 'won') {

s[i].replay = undefined;
}

if (s[i].status === 'won'){
s[i].score = `${s[i].winscore} - ${s[i].losescore}`;
}else if (s[i].status === 'lost'){
s[i].score = `${s[i].losescore} - ${s[i].winscore}`;
}else{
s[i].score = ' - ';
}

s[i].status = s[i].status.charAt(0).toUpperCase() + s[i].status.slice(1);

s[i].date = new Date(s[i].updated_at).toLocaleDateString();
Expand All @@ -531,7 +540,6 @@ class Api {
s[i].team = on_red ? s[i].blue_team : s[i].red_team;
s[i].color = on_red ? 'Red' : 'Blue';


requests.push(s[i]);
} callback(requests);
});
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/views/scrimmaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ScrimmageHistory extends Component {
<th>Date</th>
<th>Time</th>
<th>Status</th>
<th>Score</th>
<th>Team</th>
<th>Ranked</th>
<th>Replay</th>
Expand All @@ -122,6 +123,7 @@ class ScrimmageHistory extends Component {
<td>{ s.date }</td>
<td>{ s.time }</td>
{ stat_row }
<td>{ s.score }</td>
<td>{ s.team }</td>
<td>{ s.ranked ? "Ranked" : "Unranked"}</td>
{ s.replay?<td><a href={`${process.env.REACT_APP_REPLAY_URL}/replays/${s.replay}.txt`} target="_blank">Watch</a></td>:<td>N/A</td> }
Expand Down
10 changes: 6 additions & 4 deletions infrastructure/worker/app/game_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from google.cloud import storage


def game_report_result(gametype, gameid, result):
def game_report_result(gametype, gameid, result, winscore=None, losescore=None):
"""Sends the result of the run to the API endpoint"""
try:
auth_token = util.get_api_auth_token()
response = requests.patch(url=api_game_update(gametype, gameid), data={
'status': result
'status': result,
'winscore': winscore,
'losescore': losescore
}, headers={
'Authorization': 'Bearer {}'.format(auth_token)
})
Expand Down Expand Up @@ -179,9 +181,9 @@ def game_worker(gameinfo):
game_log_error(gametype, gameid, 'Could not determine winner')
else:
if wins[0] > wins[1]:
game_report_result(gametype, gameid, GAME_REDWON)
game_report_result(gametype, gameid, GAME_REDWON, wins[0], wins[1])
elif wins[1] > wins[0]:
game_report_result(gametype, gameid, GAME_BLUEWON)
game_report_result(gametype, gameid, GAME_BLUEWON, wins[1], wins[0])
else:
game_log_error(gametype, gameid, 'Ended in draw, which should not happen')

Expand Down

0 comments on commit db39781

Please sign in to comment.