Skip to content

Commit

Permalink
Merge pull request #198 from ucsd-cse125-sp24/fix/timer-drift
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Lentz authored Jun 5, 2024
2 parents bf2aa1b + 1617106 commit 145f0d4
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 76 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"game": {
"maze": {
"directory": "maps",
"procedural": false,
"procedural": true,
"maze_file": "test/itemRoom.maze"
}
},
Expand Down
6 changes: 3 additions & 3 deletions include/server/game/servergamestate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,11 @@ class ServerGameState {
MatchPhase matchPhase;

/**
* @brief Amount of time, in timesteps, left until the end of the match
* This value only becomes relevant when matchPhase is set to
* @brief epoch timestamp of when the match will end
* This value only becomes set when matchPhase is set to
* MatchPhase::RelayRace
*/
unsigned int timesteps_left;
time_t relay_finish_time;

/**
* @brief Player victory is by default false - only becomes true if a Player
Expand Down
6 changes: 2 additions & 4 deletions include/shared/game/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

/* Game phase information */
// Time limit initially set to 5 minutes
#define TIME_LIMIT_MS std::chrono::milliseconds(300000)
//#define TIME_LIMIT_MS std::chrono::milliseconds(5 * 60 * 1000)
#define TIME_LIMIT_S std::chrono::seconds(300)

/* Number of player deaths to update match state to MatchState::RelayRace */
#define PLAYER_DEATHS_TO_RELAY_RACE 5
//#define PLAYER_DEATHS_TO_RELAY_RACE 15
#define PLAYER_DEATHS_TO_RELAY_RACE 3
8 changes: 4 additions & 4 deletions include/shared/game/sharedgamestate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ struct SharedGameState {

MatchPhase matchPhase;

unsigned int timesteps_left;
time_t relay_finish_time;

bool playerVictory;

Expand All @@ -188,7 +188,7 @@ struct SharedGameState {
this->timestep = FIRST_TIMESTEP;
this->lobby.max_players = MAX_PLAYERS;
this->matchPhase = MatchPhase::MazeExploration;
this->timesteps_left = TIME_LIMIT_MS / TIMESTEP_LEN;
this->relay_finish_time = 0;
this->playerVictory = false;
this->numPlayerDeaths = 0;
}
Expand All @@ -201,14 +201,14 @@ struct SharedGameState {
this->lobby.max_players = config.server.max_players;
this->lobby.name = config.server.lobby_name;
this->matchPhase = MatchPhase::MazeExploration;
this->timesteps_left = TIME_LIMIT_MS / TIMESTEP_LEN;
this->relay_finish_time = 0;
this->playerVictory = false;
this->numPlayerDeaths = 0;
}

DEF_SERIALIZE(Archive& ar, const unsigned int version) {
ar & objects & timestep & lobby & phase & matchPhase
& timesteps_left & playerVictory & numPlayerDeaths;
& relay_finish_time & playerVictory & numPlayerDeaths;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion include/shared/utilities/time.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

long long getMsSinceEpoch();
long long getMsSinceEpoch();

long long getSecSinceEpoch();
8 changes: 4 additions & 4 deletions maps/rooms/20x20/crush_axis.hard
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#..![#########..!..#
.......|XXXX|.......
.......|XXXX|.......
#..!..########..!..#
#..#....|..|....#..#
#..#....|..|....#..#
#..!..###[####..!..#
#..#....|..|....#..#
#..#....|..|....#..#
#..{....|..|....#..#
#..#....|..|....}..#
{..#....|..|....#..#
#..#....|..|....#..#
#..!..########..!..#
#..!..####]###..!..#
.......|XXXX|.......
.......|XXXX|.......
#..!##[##########..#
Expand Down
8 changes: 4 additions & 4 deletions maps/rooms/20x20/dark_halls_1.hard
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
####..########..####
####..########..####
####..########..####
####..########..####
##[#..########..####
.......X............
............X.......
####..###..###..####
####..###.X###.X####
####..###.X##{.X####
####X.###..###..####
####..###..###..####
####..###..###..####
####..###X.###..####
####..}##X.###..####
####.X###..###..####
####..###..###X.####
.......X............
.............X......
####..########..####
####..########..#]##
####..########..####
####..########..####
####..########..####
8 changes: 4 additions & 4 deletions maps/rooms/20x20/dark_halls_2.hard
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
####..########..####
####..########..####
####..###vv###..####
####..###..###..####
#[##..###..###..##[#
....................
....................
####..###..###..####
####..###..###..####
##>..............<##
####..###..###..####
####..#]#..###..####
####..###..###..####
##>..............<##
####..###..###..####
####..###..#]#..####
####..###..###..####
....................
.............X......
####..###..###..####
#]##..###..###..##]#
####..###^^###..####
####..########..####
####..########..####
8 changes: 4 additions & 4 deletions maps/rooms/20x20/dark_room_1.hard
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
....................
####............####
####.............<##
##>.............####
####.............<##
##>.............####
####.............<##
##>......]!.....####
####....!##!.....<##
##>.....!##!....####
####.....![......<##
##>.............####
####............####
....................
Expand Down
20 changes: 10 additions & 10 deletions maps/rooms/20x20/dark_room_2.hard
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
####..########..####
####..########..####
####..########..####
....X....X..........
..............X.....
####..X..X......####
####........X...####
....X....##.........
.........##...X.....
####..X..##.....####
###{.....##.X...}###
####.X....X.....####
####..........X.####
####..X....X....F.p#
#######......#######
#######....X.##F..p#
####...X.....X..####
####............####
####..X....X....####
....................
..............X.....
###{.....##.....}###
####..X..##X....####
.........##.........
.........##...X.....
####..########..####
####..########..####
####..########..####
Expand Down
10 changes: 5 additions & 5 deletions maps/rooms/20x20/pillar_maze_bottom_1.orb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
####################
####################
####################
###[############]###
##................##
##..!.!!!.!.!.!.!.##
##........!...!...<#
##..!.!*!.!.!.!.!.##
##..!.!*!.!.}.!.!.##
##....!...........##
##..!.!!!.!.!!!.!.##
#>........!.......##
##..!.!.!.!.!.!.!.##
#>........!.......}#
##..!.!.{.!.!.!.!.##
##............!...##
##..!!!.!.!.!.!.!.##
##................<#
#{................<#
##..!.!.!.!.!.!.!.##
##..........!.....##
####..########..####
Expand Down
4 changes: 2 additions & 2 deletions maps/rooms/20x20/pillar_maze_bottom_2.orb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
##w...............##
##!!!.!!!.!.!.!.!.##
##.p......!.s.!...##
##.!!.!.![!.!.!.!.##
##.!!.!.![!.!.[.!.##
##....!...........##
##!]!.!.!.!.!!.!!.##
##..!.....!...*...##
##..!.!.!.!.!!.!!.##
##............!...##
##.!!!!.!.!.!.!.!.##
##.!![!.!.!.!.!.!.##
##................##
##..!.!.!.!.!.].!.##
##....!.!...!....p##
Expand Down
8 changes: 4 additions & 4 deletions maps/rooms/20x20/pillar_maze_top_1.orb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
##................##
##..!.!!!.!.!.!.!.##
#{........!.s.!...<#
##..!.!.!.!.!.!.!.##
##..!.!.[.!.!.!.!.##
##..p.!...........##
##..!.!.!.!.!]!.!.##
#>........!.......##
#>........!.......}#
##..!.!.!!!.!.!.!.##
##............!.w.##
##..![!.!!!.!.!.!.##
##......!*........<#
##..![!.!]!.!.!.!.##
##{.....!*........<#
##..!.!.!.!.!.}.!.##
##..p.......!.....##
####################
Expand Down
10 changes: 5 additions & 5 deletions maps/rooms/20x20/pillar_maze_top_2.orb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
###{..}######{..}###
####..########..####
##................##
##..!.!!!.!.!.!.!.##
##..!.!!!.].!.!.!.##
#{..p.....!...!...<#
##..!.![!.!.!.!.!.##
##....!.......!...##
##..!.!.!.!.!]!.!.##
#>....!.s.!.......##
##..!.!.!.!.!.!.!.##
##..........!.!.w.##
##!!![!.!!!.!.!.!.##
##..!.!.!.!.!.!.!.}#
#{..........!.!.w.##
##!!![!.!!!.[.!.!.##
##..*!...!........<#
##.!!!!.!!!.!.}.!.##
##..........!...p.##
#{..........!...p.##
####################
####################
####################
Expand Down
28 changes: 14 additions & 14 deletions maps/rooms/20x20/teleporter_hall.hard
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
####..########..####
####..##[##[##..####
####........T...####
###!...T........!###
...........T........
....T.........T.....
##....T..T..T.....##
##.................#
####.....##.....####
######...##...######
.........##T........
....T....##...T.....
##....T..##.T.....##
##.......!!........!
#{.T....T...T...T.1#
##....T...T...T....#
#..T..............##
##....T...T...T....!
!..T..............##
#2....T....T..T...}#
#..............T..##
##.T..T...T......T##
.............T......
........T...........
###!............!###
####...T...T....####
!........!!....T..##
##.T..T..##......T##
.........##..T......
........T##.........
######...##...######
####.....##.....####
####..##]##]##..####
####..########..####
2 changes: 1 addition & 1 deletion src/client/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ void GUI::_layoutGameHUD() {
// Add timer string
if (client->gameState.matchPhase == MatchPhase::RelayRace) {
std::string timerString = "Time Left: ";
int timerSeconds = client->gameState.timesteps_left * ((float)TIMESTEP_LEN.count()) / 1000;
auto timerSeconds = client->gameState.relay_finish_time - getSecSinceEpoch();
timerString += std::to_string(timerSeconds);

timerString += (timerSeconds > 1) ? " seconds" : " second";
Expand Down
9 changes: 4 additions & 5 deletions src/server/game/servergamestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ServerGameState::ServerGameState(GameConfig config) {
// Initialize game instance match phase data
// Match begins in MazeExploration phase (no timer)
this->matchPhase = MatchPhase::MazeExploration;
this->timesteps_left = TIME_LIMIT_MS / TIMESTEP_LEN;
this->relay_finish_time = 0;
// Player victory is by default false (need to collide with an open exit
// while holding the Orb to win, whereas DM wins on time limit expiration)
this->playerVictory = false;
Expand Down Expand Up @@ -116,7 +116,7 @@ std::vector<SharedGameState> ServerGameState::generateSharedGameState(bool send_
curr_update.lobby = this->lobby;
curr_update.phase = this->phase;
curr_update.matchPhase = this->matchPhase;
curr_update.timesteps_left = this->timesteps_left;
curr_update.relay_finish_time = this->relay_finish_time;
curr_update.playerVictory = this->playerVictory;
curr_update.numPlayerDeaths = this->numPlayerDeaths;
return curr_update;
Expand Down Expand Up @@ -786,9 +786,7 @@ void ServerGameState::update(const EventList& events) {
// Countdown timer if the Orb has been picked up by a Player and the match
// phase is now RelayRace
if (this->matchPhase == MatchPhase::RelayRace) {
this->timesteps_left--;

if (this->timesteps_left == 0) {
if (std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()) > this->relay_finish_time) {
// Dungeon Master won on time limit expiration
this->phase = GamePhase::RESULTS;
}
Expand Down Expand Up @@ -1570,6 +1568,7 @@ void ServerGameState::transitionToRelayRace() {

this->matchPhase = MatchPhase::RelayRace;

this->relay_finish_time = getSecSinceEpoch() + TIME_LIMIT_S.count();
// Open all exits!
for (int i = 0; i < this->objects.getExits().size(); i++) {
Exit* exit = this->objects.getExits().get(i);
Expand Down
2 changes: 1 addition & 1 deletion src/shared/game/sharedgamestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void SharedGameState::update(const SharedGameState& other) {
this->phase = other.phase;
this->timestep = other.timestep;
this->matchPhase = other.matchPhase;
this->timesteps_left = other.timesteps_left;
this->relay_finish_time = other.relay_finish_time;
this->playerVictory = other.playerVictory;
this->numPlayerDeaths = other.numPlayerDeaths;

Expand Down
6 changes: 6 additions & 0 deletions src/shared/utilities/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ long long getMsSinceEpoch() {
std::chrono::system_clock::now().time_since_epoch()
).count();
}

long long getSecSinceEpoch() {
return std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
}

0 comments on commit 145f0d4

Please sign in to comment.