Skip to content

Commit

Permalink
server testing continues; find seems to be working I think
Browse files Browse the repository at this point in the history
  • Loading branch information
ixchow committed Oct 5, 2023
1 parent 397cdda commit 2f7e0c1
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 57 deletions.
214 changes: 206 additions & 8 deletions Scoreboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

const uint32_t IdleTimeout = 60;
const uint32_t CloseTimeout = 20;
const uint32_t FindWait = 20;
const uint32_t FindWait = 2; //6;

const float MoveWait = 0.25f;
const float GemWait = 10.0f;
const size_t GemMax = 20;

const float FindStarDecay = 1.4f;

Scoreboard::Scoreboard(Server &server_, std::vector< std::string > const &maze_, std::vector< Player > const &players_) : maze(maze_), players(players_), server(server_) {
}
Scoreboard::~Scoreboard() {
Expand All @@ -27,6 +29,7 @@ bool Scoreboard::handle_event(SDL_Event const &, glm::uvec2 const &window_size)
}

void Scoreboard::update(float elapsed) {
timestamp += 1;

server.poll([this](Connection *connection, Connection::Event evt) {
if (evt == Connection::OnOpen) {
Expand Down Expand Up @@ -92,6 +95,15 @@ void Scoreboard::update(float elapsed) {
disconnect(connection, "Incorrect secret '" + secret + "'.");
break;
}

if (player.connect_wait != 0.0f) {
disconnect(connection, "On cool-down; wait " + std::to_string(int32_t(std::ceil(player.connect_wait))) + " seconds more.");
break;
}

if (player.timestamp == ~uint64_t(0)) {
player.timestamp = timestamp;
}
if (role == 'g') {
if (player.gatherer) {
disconnect(player.gatherer, "Newer gatherer connected.");
Expand Down Expand Up @@ -162,7 +174,9 @@ void Scoreboard::update(float elapsed) {
}
if (found) {
declare_found(info.player);
} else if (direction != glm::ivec2(0)) {
break;
}
if (direction != glm::ivec2(0)) {
step(&info.player->seeker_at, direction);
}
info.player->seeker_state = Player::PendingView;
Expand Down Expand Up @@ -221,7 +235,9 @@ void Scoreboard::update(float elapsed) {
if (player.gatherer && player.gatherer_state != Player::Disconnected) {
auto f = gems.find(player.gatherer_at);
if (f != gems.end()) {
uint32_t old_score = player.score();
player.gems += 1;
if (player.score() != old_score) player.timestamp = timestamp;
gems.erase(f);

//let them know:
Expand Down Expand Up @@ -267,11 +283,13 @@ void Scoreboard::update(float elapsed) {
}
}

//ask for new moves if got old moves:
//cooldowns:
for (auto &player : players) {
player.connect_wait = std::max(0.0f, player.connect_wait - elapsed);
player.gatherer_cooldown = std::max(0.0f, player.gatherer_cooldown - elapsed);
player.seeker_cooldown = std::max(0.0f, player.seeker_cooldown - elapsed);

//ask for new moves if got old moves:
if (player.gatherer && player.gatherer_state == Player::PendingView && player.gatherer_cooldown == 0.0f) {
send_view(player.gatherer, player.gatherer_at);
player.gatherer_state = Player::WaitingMove;
Expand All @@ -281,6 +299,16 @@ void Scoreboard::update(float elapsed) {
player.seeker_state = Player::WaitingMove;
}
}

//find star animations:
for (auto si = find_stars.begin(); si != find_stars.end(); /* later */) {
auto old = si;
++si;
old->second = std::max(0.0f, old->second - elapsed / FindStarDecay);
if (old->second == 0.0f) {
find_stars.erase(old);
}
}
}

void Scoreboard::draw(glm::uvec2 const &drawable_size) {
Expand Down Expand Up @@ -346,11 +374,22 @@ void Scoreboard::draw(glm::uvec2 const &drawable_size) {
}
}

//find stars:
for (auto const &fs : find_stars) {
float amt = fs.second;
float amt2 = std::min(1.0f, amt * 2.0f);
Cell &cell = get_cell(fs.first);
cell.codepoint = 0x263c;
cell.fg = glm::u8vec3(uint8_t(0x88 * amt), uint8_t(0xbb * amt), uint8_t(0x22 * amt2)) + glm::u8vec3(0x44);
cell.bg = glm::u8vec3(0x44);
}


//players in maze:
for (auto const &player : players) {
if (player.gatherer && player.gatherer_state != Player::Disconnected) {
Cell &cell = get_cell(player.gatherer_at);
cell.codepoint = player.avatar[0];
cell.codepoint = player.avatar;
cell.fg = player.color;
cell.bg = player.background;
}
Expand All @@ -365,6 +404,162 @@ void Scoreboard::draw(glm::uvec2 const &drawable_size) {
}
}

{ //draw scoreboard on display:
//01234567890123456789012345
// rr A ss (gg ff) andrewid

std::vector< Player const * > by_score;
for (auto const &player : players) {
by_score.emplace_back(&player);
}
std::sort(by_score.begin(), by_score.end(), [](Player const *a, Player const *b) {
uint32_t sa = a->score();
uint32_t sb = b->score();
if (sa != sb) return sa > sb;
else return a->timestamp < b->timestamp;
});

int32_t y = display_size.y - 1;
int32_t x = display_size.x - 29;

auto write = [&](uint32_t codepoint, glm::u8vec3 fg, glm::u8vec3 bg) {
if (y < 0 || uint32_t(y) >= display_size.y) return;
if (x < 0 || uint32_t(x) >= display_size.x) return;
Cell &cell = display[y * display_size.x + x];
cell.codepoint = codepoint;
cell.fg = fg;
cell.bg = bg;
++x;
};

auto write_number = [&](uint32_t number, glm::u8vec3 fg, glm::u8vec3 bg) {
number = std::min(99U, number);

if (number >= 10) {
write('0' + (number / 10), fg, bg);
} else {
write(' ', fg, bg);
}
write('0' + (number % 10), fg, bg);
};

auto write_string = [&](std::string string, uint32_t width, glm::u8vec3 fg, glm::u8vec3 bg) {
string = string.substr(0,width); //truncate
while (string.size() < width) string = string + ' '; //pad

for (uint32_t i = 0; i < width; ++i) {
write(string[i], fg, bg);
}
};

const glm::u8vec3 border_fg(0x00);
const glm::u8vec3 border_bg(0x22);

//header:

write(0x250C, border_fg, border_bg);
for (uint32_t i = 0; i < 27; ++i) {
write(0x2500, border_fg, border_bg);
}
write(0x2510, border_fg, border_bg);

y -= 1;
x = display_size.x - 29;

write(0x2502, border_fg, border_bg);

write(' ', glm::u8vec3(0xff), glm::u8vec3(0x22));
write('#', glm::u8vec3(0xff), glm::u8vec3(0x22));
write(' ', glm::u8vec3(0xff), glm::u8vec3(0x22));
write('A', glm::u8vec3(0xaa), glm::u8vec3(0x22));
write(' ', glm::u8vec3(0xff), glm::u8vec3(0x22));
write(' ', glm::u8vec3(0xaa), glm::u8vec3(0x22));
write('$', glm::u8vec3(0xaa), glm::u8vec3(0x22));
write(' ', glm::u8vec3(0xff), glm::u8vec3(0x22));
write('(', glm::u8vec3(0x88), glm::u8vec3(0x22));
write(' ', glm::u8vec3(0xaa), glm::u8vec3(0x22));
write(0x2666, glm::u8vec3(0x44, 0xaa, 0xff), glm::u8vec3(0x22));
write(' ', glm::u8vec3(0x88), glm::u8vec3(0x22));
write(' ', glm::u8vec3(0xaa), glm::u8vec3(0x22));
write(0x263c, glm::u8vec3(0xbb, 0xee, 0x44), glm::u8vec3(0x22));
write(')', glm::u8vec3(0x88), glm::u8vec3(0x22));

write(' ', glm::u8vec3(0xff), glm::u8vec3(0x22));

write_string("who", 8, glm::u8vec3(0xaa), glm::u8vec3(0x22));

write(' ', glm::u8vec3(0xff), glm::u8vec3(0x22));

write('g', glm::u8vec3(0xaa), glm::u8vec3(0x22));
write('s', glm::u8vec3(0xaa), glm::u8vec3(0x22));

write(0x2502, border_fg, border_bg);


//players:
for (uint32_t row = 0; row < std::min(display_size.y, uint32_t(by_score.size())); ++row) {
y -= 1;
x = display_size.x - 29;

auto const &player = *by_score[row];

write(0x2502, border_fg, border_bg);

//rank:
write_number(row+1, glm::u8vec3(0xff), glm::u8vec3(0x00));
write(' ', glm::u8vec3(0xff), glm::u8vec3(0x00));

//avatar:
write(player.avatar, player.color, player.background);
write(' ', glm::u8vec3(0xff), glm::u8vec3(0x00));

//score:
write_number(player.score(), glm::u8vec3(0xff), glm::u8vec3(0x00));
write(' ', glm::u8vec3(0xff), glm::u8vec3(0x00));

write('(', glm::u8vec3(0x88), glm::u8vec3(0x00));
write_number(player.gems, glm::u8vec3(0xaa), glm::u8vec3(0x00));
write(' ', glm::u8vec3(0x88), glm::u8vec3(0x00));
write_number(player.finds, glm::u8vec3(0xaa), glm::u8vec3(0x00));
write(')', glm::u8vec3(0x88), glm::u8vec3(0x00));


write(' ', glm::u8vec3(0xff), glm::u8vec3(0x00));

write_string(player.andrewid, 8, player.color, player.background);

write(' ', glm::u8vec3(0xff), glm::u8vec3(0x00));

if (player.connect_wait != 0.0f) {
write_number(uint32_t(std::ceil(player.connect_wait)), glm::u8vec3(0xaa), glm::u8vec3(0x00));
} else {
if (player.gatherer_state == Player::Disconnected) {
write(0x2219, glm::u8vec3(0xaa), glm::u8vec3(0x00));
} else {
write(0x2713, glm::u8vec3(0xff), glm::u8vec3(0x00));
}

if (player.seeker_state == Player::Disconnected) {
write(0x2219, glm::u8vec3(0xaa), glm::u8vec3(0x00));
} else {
write(0x2713, glm::u8vec3(0xff), glm::u8vec3(0x00));
}
}

write(0x2502, border_fg, border_bg);
}

y -= 1;
x = display_size.x - 29;

write(0x2514, border_fg, border_bg);
for (uint32_t i = 0; i < 27; ++i) {
write(0x2500, border_fg, border_bg);
}
write(0x2518, border_fg, border_bg);

}

//- - - - - - - -

glClear(GL_COLOR_BUFFER_BIT);
Expand Down Expand Up @@ -477,20 +672,23 @@ void Scoreboard::declare_found(Player *player_) {
assert(player_);
Player &player = *player_;

if (player.gatherer) find_stars[player.gatherer_at] = 1.0f;
if (player.seeker) find_stars[player.seeker_at] = 1.0f;

if (player.gatherer && player.seeker) {
if (player.gatherer_at == player.seeker_at) {
uint32_t old_score = player.score();
player.finds += 1;
if (old_score != player.score()) player.timestamp = timestamp;
disconnect(player.gatherer, "Good job, you were found by the seeker! Reconnect after " + std::to_string(FindWait) + " seconds to play again.");
disconnect(player.seeker, "Good job, you were found the gatherer! Reconnect after " + std::to_string(FindWait) + " seconds to play again.");
} else {
disconnect(player.gatherer, "Unsuccessful find attempt! Reconnect after " + std::to_string(FindWait) + " seconds to play again.");
disconnect(player.seeker, "Unsuccessful find attempt! Reconnect after " + std::to_string(FindWait) + " seconds to play again.");
}
}
if (player.gatherer && !player.seeker) {
} else if (player.gatherer && !player.seeker) {
disconnect(player.gatherer, "Unsuccessful find attempt -- no seeker(?!). Reconnect after " + std::to_string(FindWait) + " seconds to play again.");
}
if (!player.gatherer && player.seeker) {
} else if (!player.gatherer && player.seeker) {
disconnect(player.seeker, "Unsuccessful find attempt -- no gatherer(?!). Reconnect after " + std::to_string(FindWait) + " seconds to play again.");
}

Expand Down
9 changes: 8 additions & 1 deletion Scoreboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct Player {
std::string andrewid;
std::string secret;

std::string avatar;
uint32_t avatar = uint8_t('x'); //codepoint of avatar
glm::u8vec3 color;
glm::u8vec3 background;

Expand Down Expand Up @@ -44,6 +44,10 @@ struct Player {
//current score:
uint32_t gems = 0;
uint32_t finds = 0;
uint32_t score() const {
return std::min(49U, gems) + 10 * std::min(5U, finds);
}
uint64_t timestamp = ~uint64_t(0); //set to highest possible if not connected yet

//timeout after finds complete:
float connect_wait = 0.0f;
Expand Down Expand Up @@ -88,6 +92,9 @@ struct Scoreboard : Mode {
std::unordered_set< glm::ivec2 > gems;
float gem_cooldown = 0.0f;

std::unordered_map< glm::ivec2, float > find_stars;

uint64_t timestamp = 0; //to track relative ordering of events
Server &server;

//track active connections:
Expand Down
Loading

0 comments on commit 2f7e0c1

Please sign in to comment.