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

Salia Nahshal_snn2126 #604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 8 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,22 @@

@app.route('/')
def show_scoreboard():
return render_template('scoreboard.html', scoreboard = scoreboard)
return render_template('scoreboard.html', scoreboard=scoreboard)

@app.route('/increase_score', methods=['GET', 'POST'])
@app.route('/increase_score', methods=['POST'])
def increase_score():
global scoreboard

json_data = request.get_json()
team_id = json_data["id"]
json_data = request.get_json()
team_id = json_data["id"]

for team in scoreboard:
if team["id"] == team_id:
team["score"] += 1

# Sort the scoreboard by score in descending order
scoreboard = sorted(scoreboard, key=lambda x: x["score"], reverse=True)

return jsonify(scoreboard=scoreboard)


Expand Down
52 changes: 28 additions & 24 deletions static/scoreboard.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
function display_scoreboard(scoreboard){
function display_scoreboard(scoreboard) {
$("#teams").empty();
$.each(scoreboard, function(index, team){
$.each(scoreboard, function (index, team) {
addTeamView(team.id, team.name, team.score);
});
}

function addTeamView(id, name, score){
var team_template = $("<div class = row></div>");
var name_template = $("<div class = col-md-5></div>");
var score_template = $("<div class = col-md-2></div>");
var button_template = $("<div class = col-md-2></div>");
var increase_button = $("<button class = increase-button>+</button>");
$(increase_button).click(function(){
function addTeamView(id, name, score) {
var team_template = $("<div class='row'></div>");
var name_template = $("<div class='col-md-5'></div>");
var score_template = $("<div class='col-md-2'></div>");
var button_template = $("<div class='col-md-2'></div>");
var increase_button = $("<button class='increase-button'>+</button>");

$(increase_button).click(function () {
increase_score(id);
});

name_template.text(name);
score_template.text(score);
button_template.append(increase_button);
Expand All @@ -23,26 +25,28 @@ function addTeamView(id, name, score){
$("#teams").append(team_template);
}

function increase_score(id){
var team_id = {"id": id}
function increase_score(id) {
var team_id = { id: id };

$.ajax({
type: "POST",
url: "increase_score",
dataType : "json",
url: "increase_score",
dataType: "json",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(team_id),
success: function(result){

data: JSON.stringify(team_id),
success: function (result) {
// Re-render the scoreboard with the updated data
display_scoreboard(result.scoreboard);
},
error: function (request, status, error) {
console.log("Error");
console.log(request);
console.log(status);
console.log(error);
},
error: function(request, status, error){
console.log("Error");
console.log(request)
console.log(status)
console.log(error)
}
});
}

$(document).ready(function(){
$(document).ready(function () {
display_scoreboard(scoreboard);
})
});
80 changes: 43 additions & 37 deletions templates/scoreboard.html
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script type="text/javascript" src="{{ url_for('static', filename = 'scoreboard.js') }}"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<script>
var scoreboard = {{scoreboard|tojson}}
</script>
<style>
#title{
font-size: 30px;
font-weight: bold;
color: red;
padding: 50px;
margin-bottom: 30px;
}
#reams-input{
vertical-align: middle;
}
#submit-button{
vertical-align: middle;
}
#error-field{
color: red;
}
</style>
</head>
<body>
<div class="container">
<div id="title">
2020 NHL Scoreboard
</div>
<div class="contents">
<div id="teams">

</div>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script type="text/javascript" src="{{ url_for('static', filename = 'scoreboard.js') }}"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<script>
var scoreboard = {{ scoreboard| tojson}}
</script>
<style>
#title {
font-size: 30px;
font-weight: bold;
color: red;
padding: 50px;
margin-bottom: 30px;
}

#reams-input {
vertical-align: middle;
}

#submit-button {
vertical-align: middle;
}

#error-field {
color: red;
}
</style>
</head>

<body>
<div class="container">
<div id="title">
2020 NHL Scoreboard
</div>
<div class="contents">
<div id="teams">

</div>
</div>
</body>
</div>
</body>

</html>