head	1.23;
access;
symbols
	tonic_4:1.23
	tonic_3:1.23
	tonic_2:1.23
	demo:1.23.0.2
	design_report:1.20
	oral_presentation_freeze:1.5.0.2
	DAVEG_CDP:1.3.0.4
	template_integ:1.3.0.2;
locks; strict;
comment	@ * @;


1.23
date	95.12.12.23.29.06;	author hartmans;	state Exp;
branches;
next	1.22;

1.22
date	95.12.12.22.21.30;	author daveg;	state Exp;
branches;
next	1.21;

1.21
date	95.12.12.21.17.00;	author hartmans;	state Exp;
branches;
next	1.20;

1.20
date	95.12.11.20.39.27;	author hartmans;	state Exp;
branches;
next	1.19;

1.19
date	95.12.11.19.28.05;	author jered;	state Exp;
branches;
next	1.18;

1.18
date	95.12.11.18.49.07;	author hartmans;	state Exp;
branches;
next	1.17;

1.17
date	95.12.10.05.14.27;	author hartmans;	state Exp;
branches;
next	1.16;

1.16
date	95.12.10.01.35.55;	author jered;	state Exp;
branches;
next	1.15;

1.15
date	95.12.09.19.52.41;	author hartmans;	state Exp;
branches;
next	1.14;

1.14
date	95.12.08.04.56.19;	author jered;	state Exp;
branches;
next	1.13;

1.13
date	95.12.08.04.37.47;	author hartmans;	state Exp;
branches;
next	1.12;

1.12
date	95.12.08.04.35.12;	author jered;	state Exp;
branches;
next	1.11;

1.11
date	95.12.08.04.20.59;	author hartmans;	state Exp;
branches;
next	1.10;

1.10
date	95.12.07.22.42.03;	author jered;	state Exp;
branches;
next	1.9;

1.9
date	95.12.03.19.57.04;	author hartmans;	state Exp;
branches;
next	1.8;

1.8
date	95.12.02.09.43.25;	author hartmans;	state Exp;
branches;
next	1.7;

1.7
date	95.12.01.05.31.33;	author jered;	state Exp;
branches;
next	1.6;

1.6
date	95.11.30.09.44.54;	author hartmans;	state Exp;
branches;
next	1.5;

1.5
date	95.11.30.06.31.53;	author hartmans;	state Exp;
branches
	1.5.2.1;
next	1.4;

1.4
date	95.11.30.04.26.48;	author hartmans;	state Exp;
branches;
next	1.3;

1.3
date	95.11.22.07.06.27;	author daveg;	state Exp;
branches;
next	1.2;

1.2
date	95.11.22.05.39.37;	author daveg;	state Exp;
branches;
next	1.1;

1.1
date	95.11.21.11.24.42;	author daveg;	state Exp;
branches;
next	;

1.5.2.1
date	95.11.30.10.22.44;	author hartmans;	state Exp;
branches;
next	;


desc
@@


1.23
log
@Update players so they don't display stuff that isn't needed when the other person displays them.
@
text
@//  This is -*- c++ -*- code

#include <player.h>
#include <ps5/url_ops.h>
#include <ps5/error_response.h>

player::player(string name, long total_time) {
    check(total_time > 0, "Invalid total_time passed to player constructor");
    player_name = name;
    player_time = total_time;
    g = NULL;
    clock_is_running = false;
}

void player::associate( game *gm) {
    g = gm;
}    

bool player::is_associated() const {
    return (g != NULL);
}

bool player::get_position (string &p) const {
    if (player_position == "")
	return false;
    p = player_position;
    return true;
}

void player::set_position (string p) {
    player_position = p;
}

void player::set_time(long t) {
    check(t >= 0,"player::set_time passed negative time");
    player_time = t;
}


// Note, this is just a quick hack to have something here.  We need to
// decide the protocol that this will be based upon.
http_response *player::get(string request, extensions *ext)
{
string first;
if (!unescape_uri(first_component(request), first))
    return new error_response(http_response::BAD_REQUEST,
			      "Unable to unescape request; go away.");

if (first == "set_name") {
map <string,string> *q = parse_query(extract_query(request));
string name;
if (q->find("name",name)) {
if (name.length() == 0)
    return new error_response(http_response::BAD_REQUEST, "No, you need to specify a name when you're setting it!");
set_name(name);
return get_game()->display(ext,get_game()->player_index(this));
}
else return new error_response(http_response::BAD_REQUEST,
			       "You must specify the name you set.");
}
else if (first == "set_time") {
    map <string,string> *q = parse_query(extract_query(request));
    string value;
    if (q->find("msecs", value)) {
	long tval = atoi(value.get_chars());
	if (tval >= 0 )
	    set_time(tval);
	else get_game()->message("You cannot set time to a negative number.");
    }
    else if (q->find("time", value)) {
	long tval = 1000*atoi(value.get_chars());
	if (tval >= 0 )
	    set_time(tval);
	else get_game()->message("You cannot set time to a negative number.");
    }
    else  return new error_response(http_response::BAD_REQUEST,
				   "Sorry, but you need to tell me either time or msecs to do that.");
    return new error_response(http_response::OK,
			      "Time set.");

}

 return new error_response(http_response::NOT_FOUND,"I don't know how to do that, boss.");
}




void player::you_moved (move &) {
  stop_clock();
  consider_forfeiting();
}

// Ok, this needs some more added to it.
void player::opponent_moved (move &,int ) {
  stop_clock();
  consider_forfeiting();
}


void player::state_changed(void) {
  stop_clock();
  consider_forfeiting();
}

void player::consider_forfeiting(void) {
    if ((player_time <= 0) &&
	is_associated()&&
	((get_game()->winner() < 0 )||
	(get_game()->get_player(get_game()->winner()) != this)))
	g->forfeit(g->player_index(this));
}

void player::start_clock() {
    check(clock_is_running == false,
	   "Clock was already running upon call to start_clock()");
    clock_is_running = true;
    gettimeofday(&clock_started_when, NULL); 
}

void player::stop_clock(){
    struct timeval current_time;
    if (clock_is_running) {
      clock_is_running = false;
      gettimeofday(&current_time, NULL);
      long msecs_past = 1000*(current_time.tv_sec-clock_started_when.tv_sec);
      msecs_past += (current_time.tv_usec-clock_started_when.tv_usec)/1000;
      
if (msecs_past < 0) 
  cerr << "Your OS is confused: I'm about to subtract " <<msecs_past<<" from the time left, but don't think I will.\n";
else player_time -= msecs_past;
    }
}



void player::set_name(string newname) {
check(newname.length() > 0 , "name never specified");
player_name = newname;
}
@


1.22
log
@fixed forfeit bug
@
text
@d76 1
a76 1
    else return new error_response(http_response::BAD_REQUEST,
d78 3
@


1.21
log
@* game: implement message
* player: fix set_time so it works
* machine_player: move if you can when can_go is set to true
@
text
@d88 1
d94 1
a99 1
  // This is probably reasonable
a128 1
      consider_forfeiting();
d130 1
a130 1
  }
@


1.20
log
@fix forfeit
@
text
@d58 3
a79 3
else     return new error_response(http_response::BAD_REQUEST, "No, you need to specify a name when you're setting it!");

}
@


1.19
log
@More display modifications.
@
text
@d105 2
a106 2
	(get_game()->winner() >= 0 )&&
	(get_game()->get_player(get_game()->winner()) != this))
@


1.18
log
@Timing fixes, mainly for human_player
@
text
@d106 1
a106 1
	(get_game()->get_player(get_game()->winner()) != this)
@


1.17
log
@Fix compilation problems,
Add several methods to deal with backward
compatibility protocol.
@
text
@d103 4
a106 1
    if (player_time <= 0)
@


1.16
log
@Fixed many bogosities.
@
text
@d80 1
a80 1
else return new error_response(http_response::NOT_FOUND,"I don't know how to do that, boss.");
d86 1
a86 1
void player::you_moved (move &m) {
d91 1
a91 1
void player::opponent_moved (move &m,int p) {
@


1.15
log
@Implement set_time and set (set?can_go=foo)
Fix error in detach handling.


l
@
text
@d60 1
a60 1
strinv value;
@


1.14
log
@Removed some debugging.
@
text
@d44 5
a48 1
string first = first_component(request);
d58 19
@


1.13
log
@Don't stop clock unless it is running
@
text
@a97 2
      cout << "clock_started: "<<clock_started_when.tv_sec<<" "<<clock_started_when.tv_usec<<"\n";
      cout << "current_time: "<<current_time.tv_sec<<" " <<current_time.tv_usec<<"\n";
@


1.12
log
@Fixed a typo
@
text
@d93 8
a100 8
    
    clock_is_running = false;
    gettimeofday(&current_time, NULL);
    long msecs_past = 1000*(current_time.tv_sec-clock_started_when.tv_sec);
msecs_past += (current_time.tv_usec-clock_started_when.tv_usec)/1000;
    cout << "clock_started: "<<clock_started_when.tv_sec<<" "<<clock_started_when.tv_usec<<"\n";
    cout << "current_time: "<<current_time.tv_sec<<" " <<current_time.tv_usec<<"\n";
    
d102 1
a102 1
    cerr << "Your OS is confused: I'm about to subtract " <<msecs_past<<" from the time left, but don't think I will.\n";
d104 3
a106 2
consider_forfeiting();
}
@


1.11
log
@Insert clock debugging code.
Fix resync URI.
@
text
@d99 1
a99 1
    cout << "current_time: "<<current_time.tv_sec<<" "current_time.tv_usec<<"\n";
@


1.10
log
@Lots of display fixes and changes.
@
text
@d98 3
@


1.9
log
@Fix timing in player,
add get to human_player,
fix error in game::apply
@
text
@d64 1
a64 1
    stop_clock();
d69 1
d74 3
a76 2
    // This is probably reasonable
    consider_forfeiting();
@


1.8
log
@Implement player::get
@
text
@d4 2
d52 1
a52 1
return get_game()->display(ext,get_game->player_index(this));
d95 1
a95 1
msecs_past = (current_time.tv_usec-clock_started_when.tv_usec)/1000;
@


1.7
log
@gettimeofday, filenode fixes.
@
text
@d40 11
a50 4
http_response *player::get(string request, extensions *ext) {
    //    string name = request.substring(0,10);
    //    string position = request.substring(10, 20);
    return new normal_response(true);
d52 1
d54 6
@


1.6
log
@change stuff
@
text
@d70 1
a70 2
gettimeofday(&clock_started_when);
    
d77 1
a77 1
    gettimeofday(&current_time);
@


1.5
log
@Fix bugs so it builds
@
text
@d88 5
@


1.5.2.1
log
@And the same on this branch
@
text
@a87 5

void player::set_name(string newname) {
check(newname.length() > 0 , "name never specified");
player_name = newname;
}
@


1.4
log
@player:  Deal with clock stuff
game: fix argument bogosity
@
text
@d52 1
a52 1
void player::opponent_moved (move &m, player *p) {
d78 1
a78 1
gettimeofday(&current_time)
d80 1
a80 1
msecs_past = (current_time.tv_usecs-clock_started_when.tv_usec)/1000;
@


1.3
log
@a few more bug fixes
@
text
@a9 1
    penalty = 0;
a36 3
void player::apply_penalty(long size) {
    penalty += size;
}
a52 3
    start_clock();
    // Well, we should really do something about making a move of our
    // own here :-)
d55 1
a55 1
// This needs work too.
a58 4
    // Then, check if we have valid moves.
    // board *b = g->get_board();
    // if (b->num_valid_moves <= 0)
    //       return;
d63 1
a63 1
	g->forfeit(this);
d70 2
a71 1
    running_time = time((long *) 0);
d75 1
a75 1
    time_t current_time;
a76 2
    check(clock_is_running == true,
	   "Clock was not running upon call to stop_clock()");
d78 7
a84 5
    current_time = time((long *) 0);
    // Ok, casts are bad, but time_t should already be a long.  I just
    // put the cast in case it is something else on some platform.
    player_time -= (long)(current_time - running_time);
    consider_forfeiting();
d87 1
@


1.2
log
@Version 1.0.  All but opponent_moved and state_changed implemented
@
text
@a2 1
#include <6170.h>
d6 1
a6 1
    checks(s > 0, "Invalid total_time passed to player constructor");
d8 1
a8 1
    time = total_time;
d34 2
a35 2
    checks(t >= 0,"player::set_time passed negative time");
    time = t;
d45 3
a47 4
    http_response *response = new http_response;
    string name = request.substring(0,10);
    string position = request.substring(10, 20);
    returns response;
d73 1
a73 1
    if (time <= 0)
d78 1
a78 1
    checks(clock_is_running == false,
d87 1
a87 1
    checks(clock_is_running == true,
d93 1
a93 1
    time -= (long)(current_time - running_time);
@


1.1
log
@Version .7 of player implementation
@
text
@d43 2
d52 1
a54 1
    
d57 1
d60 2
a61 1
    
d64 1
d66 6
d81 1
a81 1
	   "Clock was already running upon call to start_clock() \n");
d90 1
a90 1
	   "Clock was not running upon call to stop_clock() \n");
d93 2
d96 1
a96 2
    if (time <= 0)
	consider_forfeiting();
@
