Skip to content

Commit 8b8afd1

Browse files
committed
Server more or less done
1 parent ee50375 commit 8b8afd1

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

events.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
void Event::serialize_args(std::stringstream &ss) {}
44

5-
char Event::serialize(char c)
5+
std::string Event::serialize(char c)
66
{
7-
return c;
7+
return std::string{c};
88
}
99

1010
std::string Event::serialize(std::string str)
@@ -34,7 +34,7 @@ std::string Event::PlayerEliminated::serialize()
3434

3535
std::string Event::GameOver::serialize()
3636
{
37-
return std::string{1, Event::serialize(type)};
37+
return Event::serialize(type);
3838
}
3939

4040
std::string Event::ClientEvent::serialize()

events.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Event {
1010
IntegerType hn = bswap(n);
1111
return std::string(reinterpret_cast<char *>(&hn), sizeof(hn));
1212
}
13-
char serialize(char);
13+
std::string serialize(char);
1414
std::string serialize(std::string);
1515

1616
void serialize_args(std::stringstream &ss);

game_state.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,11 @@ void GameState::update_game_state_on_player_message()
189189
//with the conditions above it means some round has finished recently
190190
if (std::get<0>(round.is_active())) {
191191
if (round.game_over_raised) {
192+
std::cerr << "Prepare for new round" << std::endl;
192193
round.game_over_raised = false;
193194
for (auto &p : players) {
194195
p.pressed_arrow = false;
196+
p.lurking = true;
195197
}
196198
}
197199
}
@@ -201,7 +203,8 @@ void GameState::update_game_state_on_player_message()
201203
continue;
202204
}
203205
counter++;
204-
if (counter >= 1) {
206+
if (counter >= REQUIRED_PLAYERS) {
207+
std::cerr << "Starting new game" << std::endl;
205208
start_new_round();
206209
return;
207210
}
@@ -238,6 +241,9 @@ void GameState::start_new_round()
238241
p.snake_id = j;
239242
p.lurking = false;
240243
}
244+
decltype(pending_queue) empty_pendign_queue;
245+
std::swap(pending_queue, empty_pendign_queue);
246+
pending.clear();
241247
round = Round{board, eager};
242248
}
243249

@@ -295,8 +301,8 @@ void Round::event(Event::SerializableEvent &e)
295301
uint32_t crc = crc32(0, reinterpret_cast<unsigned char *>(&s[0]), s.length());
296302
s.resize(s.length() + 4);
297303
*reinterpret_cast<uint32_t *>(&s[s.length() - 4]) = crc;
298-
events_positions.push_back(events_history.size());
299304
events_history.insert(events_history.end(), s.begin(), s.end());
305+
events_positions.push_back(events_history.size());
300306
}
301307

302308
void Round::new_game()
@@ -357,10 +363,10 @@ void Round::register_move(Position const &new_position, Snake &s, size_t player)
357363
void Round::move(Snake &s, size_t player)
358364
{
359365
auto old_position = s.position();
360-
s.direction += s.last_turn_direction * board.turning_speed;
366+
s.direction += 360 + s.last_turn_direction * board.turning_speed;
361367
s.direction %= 360;
362-
s.x += sin(M_PI * s.direction / 180.);
363-
s.y += cos(M_PI * s.direction / 180.);
368+
s.x += cos(M_PI * s.direction / 180.);
369+
s.y += sin(M_PI * s.direction / 180.);
364370
auto new_position = s.position();
365371
if (old_position != new_position) {
366372
register_move(new_position, s, player);
@@ -371,7 +377,10 @@ void Round::cycle()
371377
{
372378
size_t player = 0;
373379
for (auto &snake: snakes) {
374-
move(snake, player++);
380+
if (!snake.eliminated) {
381+
move(snake, player);
382+
}
383+
player++;
375384
}
376385
}
377386

@@ -382,7 +391,8 @@ Board::Board(uint32_t gs, uint32_t ts, uint32_t mx, uint32_t my)
382391

383392
Round::Snake::Snake(std::string name, int32_t direction, Board const &board)
384393
: eliminated{false}, x{r.next() % board.maxx + 0.5}, y{r.next() % board.maxy + 0.5},
385-
direction{r.next() % 360}, last_turn_direction{direction}, name{name}
394+
direction{static_cast<int32_t>(r.next() % 360)}, last_turn_direction{direction},
395+
name{name}
386396
{
387397

388398
};

game_state.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ uint32_t const TWOTO16 = 65536;
4444
uint64_t const TWOTO32 = 4294967296L;
4545
size_t const MAX_PLAYERS = 42;
4646
uint64_t const INACTIVITY_TOLERANCE = 2000;
47+
uint32_t const REQUIRED_PLAYERS = 2;
4748

4849
extern Generator r;
4950

@@ -60,7 +61,7 @@ class Round {
6061
struct Snake {
6162
bool eliminated;
6263
double x, y; // current position on board
63-
uint32_t direction; //current direction snake will move in
64+
int32_t direction; //current direction snake will move in
6465
int32_t last_turn_direction; //last valid turn_direction:{-1,0,1} submitted by player
6566
std::string name; //associated player name
6667

server.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,11 @@ int main(int argc, char *argv[])
194194
sockaddr_storage rec_addr;
195195
auto events_no = gs.next_datagram(buffer, rec_addr);
196196
if (events_no > 0) {
197-
/*for (auto &c : buffer) {
197+
/*std::cout << "SO it begins " << events_no << ": ";
198+
for (auto &c : buffer) {
198199
std::cout << (uint32_t)((uint8_t)c) << " ";
199-
}*/
200-
//std::cout << std::endl;
200+
}
201+
std::cout << std::endl;*/
201202
auto len = sendto(sock.fd, &buffer[0], buffer.size(), 0,
202203
reinterpret_cast<sockaddr *>(&rec_addr), sizeof(rec_addr));
203204
if (len >= 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {

0 commit comments

Comments
 (0)