Skip to content

Commit

Permalink
scoreboard
Browse files Browse the repository at this point in the history
  • Loading branch information
qchateau committed Sep 17, 2020
1 parent 25e3948 commit 2053afb
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
build/
.history/
.vs-code/
.vscode/
.clangd/


.env
compile_commands.json
18 changes: 14 additions & 4 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@

<body style="margin: 0px">
<span style="font-family: 'Roboto Mono'" id="preloadfont">.</span>

<canvas id="canvas"> </canvas>

<div id="container" class="unselectable">
<span id="spacer-canvas"></span>

<div id="details">
<div id="name-container">
<div>Name:</div>
<input type="text" id="input-name" />
<div>
<div id="name-flex-container">
<div id="input-name-label">Name</div>
<input type="text" id="input-name" />
</div>
</div>

<div id="scoreboard"></div>
<div id="scoreboard">
<div class="scoreboard-line">
<span class="scoreboard-title">SCOREBOARD</span>
</div>
<div id="scoreboard-body"></div>
</div>
</div>
</div>
</body>
Expand Down
4 changes: 3 additions & 1 deletion client/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class Input {
this.onInput = onInput;
this.maxDragLength = maxDragLength;

element.addEventListener("touchstart", this.onTouchStart.bind(this));
element.addEventListener("touchstart", this.onTouchStart.bind(this), {
passive: false,
});
element.addEventListener("touchend", this.onTouchEnd.bind(this));
element.addEventListener(
"touchmove",
Expand Down
70 changes: 51 additions & 19 deletions client/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ body {
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
font-size: 175%;
font-family: "Roboto Mono";
color: white;
}

/* hide the element used to preload the fonts */
Expand All @@ -17,46 +20,75 @@ body {
position: absolute;
}

#container {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}

#canvas {
position: absolute;
z-index: -1;
}

#inner-canvas {
width: 100px;
height: 100%;
#container {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}

#details {
width: 100%;
height: 100%;
padding: 20px;
box-sizing: border-box;
color: white;
font-size: 125%;
font-family: "Roboto Mono";
}

#name-flex-container {
display: flex;
justify-content: center;
}

#input-name-label {
padding-right: 20px;
font-size: 100%;
}

#input-name {
width: 100%;
max-width: 400px;
color: white;
font-size: 100%;
font-family: "Roboto Mono";
background-color: black;
border: 2px solid rgb(255, 255, 255);
padding: 2px;
background-color: rgba(0, 0, 0, 0);
border: 1px solid rgb(255, 255, 255);
border-radius: 2px;
color: white;
font-size: 90%;
}

#scoreboard {
margin-top: 20px;
background-color: rgb(50, 50, 50);
width: 100%;
max-width: 500px;
min-height: 100px;
margin: 20px auto 0px auto;
box-sizing: border-box;
border-radius: 2px;
font-size: smaller;
}

.scoreboard-title {
font-weight: bold;
padding-left: 20px;
}

.scoreboard-line {
overflow: hidden;
display: flex;
}

.scoreboard-name {
width: 75%;
overflow: hidden;
text-align: left;
text-overflow: ellipsis;
}

.scoreboard-score {
width: 25%;
text-align: right;
}

.unselectable {
Expand Down
42 changes: 42 additions & 0 deletions client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ class GameEngine {
this.inputRefY = null;
this.gameIsOver = false;
this.canvas = canvasManager;
this.scoreboardSize = 5;
this.createScoreboard();

this.sock = new WebSocket(url);

Expand Down Expand Up @@ -286,6 +288,7 @@ class GameEngine {
return;
}

this.updateScoreboard(msg.scoreboard);
this.canvas.clear();
this.canvas.drawTicksPerSecond();
this.canvas.drawLimits();
Expand Down Expand Up @@ -328,6 +331,45 @@ class GameEngine {
this.gameIsOver = false;
this.send({ command: { respawn: true } });
}

createScoreboard() {
let parent = document.getElementById("scoreboard-body");
parent.innerHTML = "";

for (let idx = 0; idx < this.scoreboardSize; ++idx) {
let row = document.createElement("div");
row.classList.add("scoreboard-line");

let name = document.createElement("div");
name.classList.add("scoreboard-name");
name.id = "scoreboard-player-name-" + idx;
row.appendChild(name);

let score = document.createElement("div");
score.classList.add("scoreboard-score");
score.id = "scoreboard-player-score-" + idx;
row.appendChild(score);

parent.appendChild(row);
}
}

updateScoreboard(scoreboard) {
function update(elementId, value) {
let element = document.getElementById(elementId);
if (element.innerText != value) {
element.innerText = value;
}
}
for (
let idx = 0;
idx < Math.min(this.scoreboardSize, scoreboard.length);
++idx
) {
update("scoreboard-player-name-" + idx, scoreboard[idx].name);
update("scoreboard-player-score-" + idx, scoreboard[idx].score.toFixed());
}
}
}

class GameManager {
Expand Down
1 change: 1 addition & 0 deletions server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if (NOT CONAN_ONLY)
session.cpp
player.cpp
world.cpp
scoreboard.cpp
)
target_link_libraries(
server
Expand Down
1 change: 1 addition & 0 deletions server/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class session_t;
class listener_t;
class world_t;
class player_t;
class scoreboard_t;

using player_id_t = boost::uuids::uuid;
using player_handle_t = std::unique_ptr<player_t, std::function<void(player_t*)>>;
Expand Down
4 changes: 3 additions & 1 deletion server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <spdlog/spdlog.h>

#include "listener.h"
#include "scoreboard.h"
#include "world.h"

using namespace sd;
Expand Down Expand Up @@ -38,9 +39,10 @@ int main(int argc, char* argv[])
// The io_context is required for all I/O
net::io_context ioc{1};

auto scoreboard = std::make_shared<scoreboard_t>();
std::vector<std::shared_ptr<world_t>> worlds;
for (int i = 0; i < nworlds; ++i) {
worlds.emplace_back(std::make_shared<world_t>(ioc));
worlds.emplace_back(std::make_shared<world_t>(ioc, scoreboard));
worlds.back()->run();
}
std::make_shared<listener_t>(ioc, std::move(worlds), tcp::endpoint{address, port})
Expand Down
62 changes: 62 additions & 0 deletions server/scoreboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "scoreboard.h"
#include "player.h"

namespace sd {

const score_t& scoreboard_t::lowest_score() const
{
return scores_.back();
}

void scoreboard_t::push_score(const score_t& score)
{
if (!scores_.empty() && score.score <= lowest_score().score) {
return;
}

bool inserted = false;
for (auto it = begin(scores_); it < end(scores_); ++it) {
if (!inserted) {
if (score.score > it->score) {
inserted = true;

if (score.id == it->id) {
// just replace and bail out
*it = score;
break;
}
else {
// insert, shift the other scores
scores_.insert(it, score);
}
}
if (score.id == it->id) {
// same player already has a better score
break;
}
}
else {
if (score.id == it->id) {
// remove previous highest score
scores_.erase(it--);
}
}
}

if (!inserted && scores_.size() < max_size) {
scores_.push_back(score);
}

while (scores_.size() > max_size) {
scores_.pop_back();
}
}

void scoreboard_t::push_score(const player_t& player)
{
if (scores_.empty() || player.score() > lowest_score().score) {
return push_score({player.id(), player.name(), player.score()});
}
}

} // sd
30 changes: 30 additions & 0 deletions server/scoreboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <string>
#include <vector>

#include "config.h"

namespace sd {

struct score_t {
player_id_t id;
std::string name;
double score;
};

class scoreboard_t {
public:
static constexpr auto max_size = 10;

const std::vector<score_t>& scores() const { return scores_; }
void push_score(const score_t& score);
void push_score(const player_t& player);

private:
const score_t& lowest_score() const;

std::vector<score_t> scores_;
};

} // sd
Loading

0 comments on commit 2053afb

Please sign in to comment.