Skip to content

Commit

Permalink
Final cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawk777 committed Oct 9, 2012
1 parent f273d35 commit b1a9e42
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 413 deletions.
4 changes: 2 additions & 2 deletions dialog_gameover.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#include "gameinfo.h"

Dialog_Gameover::Dialog_Gameover(Gtk::Window& parent) :
dialog(parent, u8"", false /*use markup*/, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true /*modal*/)
dialog(parent, u8"Game over", false /*use markup*/, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true /*modal*/)
{
}

void Dialog_Gameover::update_from_gameinfo(const GameInfo& gameinfo)
{
dialog.set_message(Glib::ustring::compose(u8"Game over.\nTime taken: %1.", format_time_seconds(gameinfo.data.time_taken)));
dialog.set_message(u8"Game over.");

dialog.set_secondary_text(Glib::ustring::compose(u8"%1 (yellow):\n\t%2 Goals\n\t%3 Red cards\n%4 (blue):\n\t%5 Goals\n\t%6 Red cards",
gameinfo.data.teamnames[Yellow], gameinfo.data.goals[Yellow], gameinfo.data.redcards[Yellow],
Expand Down
70 changes: 24 additions & 46 deletions gamecontrol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,16 @@
#include <glibmm.h>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sstream>
#include <utility>

#ifndef WIN32
# include <arpa/inet.h> //for htonl()
#include <arpa/inet.h> //for htonl()
#else
# include <winsock.h>
#include <winsock.h>
#endif



#define MAX_LINE 256

#define CHOOSETEAM(t, blue, yel) (((t) == Blue) ? (blue) : (yel))


Expand All @@ -71,13 +63,19 @@ GameControl::GameControl(const std::string& configfile, const std::string& logfn
try {
broadcast.set_destination(mc_addr, mc_port);
}
catch (UDP_Broadcast::IOError& e)
catch (const UDP_Broadcast::IOError& e)
{
log.add(Glib::ustring::compose(u8"Ethernet failed: %1", e.what()));
}

// a little user output
print();
std::cout << "Game Settings\n";
std::cout << "\ttimelimits : First Half " << format_time_seconds(gameinfo.data.timelimits[FIRST_HALF]) << '\n';
std::cout << "\t\tHalf time " << format_time_seconds(gameinfo.data.timelimits[HALF_TIME]) << '\n';
std::cout << "\t\tSecond half " << format_time_seconds(gameinfo.data.timelimits[SECOND_HALF]) << '\n';
std::cout << "\t\tOvertime " << format_time_seconds(gameinfo.data.timelimits[OVER_TIME1]) << '\n';
std::cout << "\t\tOvertime " << format_time_seconds(gameinfo.data.timelimits[OVER_TIME2]) << '\n';
std::cout << "\ttimeouts : number " << gameinfo.data.nrtimeouts[0] << ", total time " << format_time_seconds(gameinfo.data.timeouts[0]) << '\n';

// intialize the timer
gameinfo.resetTimer();
Expand All @@ -89,17 +87,6 @@ GameControl::GameControl(const std::string& configfile, const std::string& logfn
}
}

void GameControl::print()
{
std::cout << "Game Settings\n";
std::cout << "\ttimelimits : First Half " << format_time_seconds(gameinfo.data.timelimits[FIRST_HALF]) << '\n';
std::cout << "\t\tHalf time " << format_time_seconds(gameinfo.data.timelimits[HALF_TIME]) << '\n';
std::cout << "\t\tSecond half " << format_time_seconds(gameinfo.data.timelimits[SECOND_HALF]) << '\n';
std::cout << "\t\tOvertime " << format_time_seconds(gameinfo.data.timelimits[OVER_TIME1]) << '\n';
std::cout << "\t\tOvertime " << format_time_seconds(gameinfo.data.timelimits[OVER_TIME2]) << '\n';
std::cout << "\ttimeouts : number " << gameinfo.data.nrtimeouts[0] << ", total time " << format_time_seconds(gameinfo.data.timeouts[0]) << '\n';
}

/////////////////////////////
// send commands
// log commands, send them over UDP and change game state
Expand All @@ -112,18 +99,18 @@ void GameControl::sendCommand(char cmd, const Glib::ustring& msg)
log.add(Glib::ustring::compose(u8"Sending '%1': %2", cmd, msg));
gameinfo.logCommand(cmd, msg);

ethernetSendCommand(cmd);
ethernetSendPacket();
}


/////////////////////////////
// send command to ethernet clients.
void GameControl::ethernetSendCommand(const char cmd)
void GameControl::ethernetSendPacket()
{
unsigned int rem = std::chrono::duration_cast<std::chrono::duration<unsigned int, std::ratio<1>>>(gameinfo.timeRemaining()).count();

uint8_t p[6];
p[0] = cmd;
p[0] = lastCommand;
p[1] = static_cast<uint8_t>(lastCommandCounter);
p[2] = static_cast<uint8_t>(gameinfo.data.goals[Blue]);
p[3] = static_cast<uint8_t>(gameinfo.data.goals[Yellow]);
Expand All @@ -146,8 +133,6 @@ void GameControl::stepTime()
std::chrono::high_resolution_clock::duration dt = tnew - tlast;
tlast = tnew;

// printf("game state %i\n", gameinfo.data.state);

// save restore file
gameinfo.save(savename);

Expand All @@ -160,8 +145,7 @@ void GameControl::stepTime()
stopTimeout();
}
} else {
if ((gameinfo.data.stage == HALF_TIME) ||
!gameinfo.isHalted()) {
if ((gameinfo.data.stage == HALF_TIME) || !gameinfo.isHalted()) {
gameinfo.data.time_taken += dt;
for (int x = 0; x < NUM_TEAMS; ++x) {
if (gameinfo.data.timepenalty[x] > std::chrono::high_resolution_clock::duration::zero()) {
Expand All @@ -170,12 +154,11 @@ void GameControl::stepTime()
gameinfo.data.timepenalty[x] = std::chrono::high_resolution_clock::duration::zero();
}
}

}
}

// repeat last command (if someone missed it)
ethernetSendCommand(lastCommand);
ethernetSendPacket();
}


Expand Down Expand Up @@ -244,7 +227,7 @@ bool GameControl::beginFirstHalf()
if (gameinfo.data.stage != PREGAME)
return (false);

// send the first half signal but we do not oficially begin
// send the first half signal but we do not officially begin
// until start signal is sent
gameinfo.data.stage = PREGAME;
setHalt();
Expand Down Expand Up @@ -310,8 +293,7 @@ bool GameControl::beginOvertime2()
bool GameControl::beginPenaltyShootout()
{
if (enabled) {
if ((gameinfo.data.stage != OVER_TIME2) &&
(gameinfo.data.stage != SECOND_HALF)) {
if ((gameinfo.data.stage != OVER_TIME2) && (gameinfo.data.stage != SECOND_HALF)) {
return (false);
}

Expand Down Expand Up @@ -344,7 +326,7 @@ bool GameControl::setReady()
sendCommand(COMM_READY, "STarting robots");
gameinfo.setRunning();

// progress into teh first half upon the start signal
// progress into the first half upon the start signal
switch (gameinfo.data.stage) {
case PREGAME:
gameinfo.data.stage = FIRST_HALF;
Expand Down Expand Up @@ -375,12 +357,11 @@ bool GameControl::setStart()
if (!enabled) {
sendCommand(COMM_START, "Starting robots");
} else {
// if (!gameinfo.isTimeout() && gameinfo.isStopped()) {
if (!gameinfo.isTimeout()) {
sendCommand(COMM_START, "Starting robots");
gameinfo.setRunning();

// progress into teh first half upon the start signal
// progress into the first half upon the start signal
switch (gameinfo.data.stage) {
case PREGAME:
gameinfo.data.stage = FIRST_HALF;
Expand Down Expand Up @@ -436,11 +417,12 @@ bool GameControl::setCancel()
else
{
// reset yellow card if it is canceled
for (int x = 1; x < NUM_TEAMS; ++x)
for (unsigned int x = 1; x < NUM_TEAMS; ++x) {
if (gameinfo.data.timepenalty[x] > std::chrono::high_resolution_clock::duration::zero() && gameinfo.data.timepenalty[x] > gameinfo.data.timepenalty[x-1])
gameinfo.data.timepenalty[x] = std::chrono::high_resolution_clock::duration::zero();
else if (gameinfo.data.timepenalty[x-1] > std::chrono::high_resolution_clock::duration::zero())
gameinfo.data.timepenalty[x-1] = std::chrono::high_resolution_clock::duration::zero();
}
}
return (true);
}
Expand All @@ -453,9 +435,8 @@ bool GameControl::beginTimeout(Team team)
Glib::ustring::compose(u8"Timeout %1", str_Team[team]));

if (enabled) {
if ((gameinfo.nrTimeouts(team) <= 0) || (gameinfo.timeoutRemaining(team) <= std::chrono::high_resolution_clock::duration::zero())) {
if ((gameinfo.nrTimeouts(team) <= 0) || (gameinfo.timeoutRemaining(team) <= std::chrono::high_resolution_clock::duration::zero()))
return (false);
}
if (!gameinfo.isStopped() && !gameinfo.isHalted())
return (false);

Expand All @@ -473,7 +454,6 @@ bool GameControl::stopTimeout()
{
sendCommand(COMM_TIMEOUT_END, "End Timeout");


if (enabled) {
if (gameinfo.data.state != TIMEOUT)
return (false);
Expand All @@ -493,8 +473,7 @@ bool GameControl::goalScored(Team team)
Glib::ustring::compose(u8"Goal scored %1", str_Team[team]));

if (enabled) {
if ( (!gameinfo.isStopped() && !gameinfo.isHalted()) ||
(gameinfo.data.stage == PREGAME))
if ( (!gameinfo.isStopped() && !gameinfo.isHalted()) || (gameinfo.data.stage == PREGAME))
return (false);

if (gameinfo.data.stage == PENALTY_SHOOTOUT) {
Expand All @@ -513,8 +492,7 @@ bool GameControl::removeGoal(Team team)
Glib::ustring::compose(u8"Goal removed %1", str_Team[team]));

if (enabled) {
if ( (!gameinfo.isStopped() && !gameinfo.isHalted()) ||
(gameinfo.data.stage == PREGAME))
if ( (!gameinfo.isStopped() && !gameinfo.isHalted()) || (gameinfo.data.stage == PREGAME))
return (false);

if (gameinfo.data.stage == PENALTY_SHOOTOUT) {
Expand Down
8 changes: 3 additions & 5 deletions gamecontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,13 @@ class GameControl {
unsigned int lastCommandCounter;

// log commands, send them over UDP and change game state
void sendCommand(const char cmd, const Glib::ustring& msg);
void ethernetSendCommand(const char cmd);
void sendCommand(char cmd, const Glib::ustring& msg);
void ethernetSendPacket();

bool enabled;


public:
// debugging printout
void print();

// get the info to display
const GameInfo &getGameInfo() {
return (gameinfo);
Expand All @@ -265,6 +262,7 @@ class GameControl {
enabled = !enabled;
log.add(Glib::ustring::compose(u8"enabled %1", enabled));
}

void setEnable(bool en = true) {
enabled = en;
log.add(Glib::ustring::compose(u8"setting enabled %1", enabled));
Expand Down
7 changes: 5 additions & 2 deletions getopt.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma GCC diagnostic ignored "-Wold-style-cast"

/*
* The next part is from the Microsoft Windows 2000 professional Resource Kit
* with some modifications from me (gloye@inf.fu-berlin.de)
Expand Down Expand Up @@ -40,6 +42,7 @@
static char sccsid[] = "@(#)getopt.c 4.13 (Berkeley) 2/23/91";
#endif /* LIBC_SCCS and not lint */

#include "getopt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -50,7 +53,7 @@ static char sccsid[] = "@(#)getopt.c 4.13 (Berkeley) 2/23/91";
int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */
optopt; /* character checked for validity */
char *optarg; /* argument associated with option */
const char *optarg; /* argument associated with option */

#define BADCH (int)'?'
#define EMSG ""
Expand All @@ -61,7 +64,7 @@ char *rindex(char *s, int c);
int
getopt(int nargc, char * const *nargv, char *ostr)
{
static char *place = EMSG; /* option letter processing */
static const char *place = EMSG; /* option letter processing */
register char *oli; /* option letter list index */
char *p;

Expand Down
2 changes: 1 addition & 1 deletion getopt.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef GETOPT_H
#define GETOPT_H

extern char *optarg;
extern const char *optarg;
int getopt(int nargc, char * const *nargv, char* ostr);

#endif
Expand Down
9 changes: 4 additions & 5 deletions logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
#include <iomanip>
#include <iostream>

Logging::Logging()
Logging::Logging() :
time_startup(std::chrono::system_clock::now())
{
std::time(&time_startup);
}


void Logging::add(const Glib::ustring& message)
{
std::time_t now;
std::time(&now);
std::cout << Glib::ustring::format(std::setw(5), now - time_startup) << "s: " << Glib::locale_from_utf8(message) << '\n';
std::chrono::system_clock::duration diff = std::chrono::system_clock::now() - time_startup;
std::cout << Glib::ustring::format(std::setw(5), std::chrono::duration_cast<std::chrono::duration<unsigned int, std::ratio<1>>>(diff).count()) << "s: " << message << '\n';
}

4 changes: 2 additions & 2 deletions logging.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef LOGGING_H
#define LOGGING_H

#include <ctime>
#include <chrono>
#include <glibmm.h>

class Logging
Expand All @@ -11,7 +11,7 @@ class Logging
void add(const Glib::ustring& message);

protected:
std::time_t time_startup;
std::chrono::system_clock::time_point time_startup;
};


Expand Down
Loading

0 comments on commit b1a9e42

Please sign in to comment.