function routinecheck()
{
    global $s_message, $s_videoid, $s_taboo, $s_matched, $s_sessionscore, $s_opponentstatus, $s_entity;
    $userid = $_POST['userid'];
    $rs = mysql_query("SELECT status FROM g1_players where playerid = '{$userid}'");
    $row = mysql_fetch_array($rs);
    $status = $row['status'];
    //error_log("routine check status".$userid.' '.$status );
    mysql_query("UPDATE g1_players SET lastaccessedtime= current_timestamp() where playerid = '{$userid}'");
    switch ($status) {
        case 'WAITING':
            //error_log("routine check waiting ".$userid );
            $s_message = 'WAITING';
            sendMessage();
            break;
        case 'START':
            // send start, video id, taboo words, session score
            //error_log("routine check start ".$userid );
            mysql_query("UPDATE g1_players SET status='PLAYING' where playerid = '{$userid}'");
            $gameid = GetGameId($userid);
            $rs = mysql_query("SELECT videoid,taboowords FROM g2_gameadmin where gameid = '{$gameid}'");
            $row = mysql_fetch_array($rs);
            $s_message = 'START';
            $s_videoid = $row['videoid'];
            $s_taboo = $row['taboowords'];
            $rs = mysql_query("SELECT sessionscore FROM g1_players where playerid = '{$userid}'");
            $row = mysql_fetch_array($rs);
            $s_sessionscore = $row['sessionscore'];
            //error_log("routine check start ".$userid );
            sendMessage();
            break;
        case 'ANOTHERGAME':
            //error_log("routine check another game ".$userid );
            $s_message = 'ANOTHERGAME';
            $opponentid = GetOpponentId($userid);
            $rs = mysql_query("SELECT status FROM g1_players WHERE playerid = '{$opponentid}'");
            $row = mysql_fetch_array($rs);
            $s_opponentstatus = $row['status'];
            sendMessage();
            break;
        case 'PROFILEYES':
            //error_log("routine check profile eyes ".$userid );
            $s_message = 'PROFILEYES';
            $opponentid = GetOpponentId($userid);
            $rs = mysql_query("SELECT status FROM g1_players WHERE playerid = '{$opponentid}'");
            $row = mysql_fetch_array($rs);
            $s_opponentstatus = $row['status'];
            sendMessage();
            break;
        case 'PROFILEINVITE':
            //error_log("routine check profile invite ".$userid );
            $s_message = 'PROFILEINVITE';
            $opponentid = GetOpponentId($userid);
            $rs = mysql_query("SELECT status FROM g1_players WHERE playerid = '{$opponentid}'");
            $row = mysql_fetch_array($rs);
            $s_opponentstatus = $row['status'];
            sendMessage();
            break;
        case 'SHOWPROFILE':
            //error_log("routine check showprofile ".$userid );
            $s_message = 'SHOWPROFILE';
            $s_entity = GetOpponentId($userid);
            mysql_query("UPDATE g1_players SET status='ENDSESSION' where playerid = '{$userid}'");
            sendMessage();
            break;
            //	case 'ENDSESSION':
            //		$s_message 			= 	'ENDSESSION';
            //		sendMessage();
            //		break;
        //	case 'ENDSESSION':
        //		$s_message 			= 	'ENDSESSION';
        //		sendMessage();
        //		break;
        case 'PLAYING':
            //return score and opponent's status, matched words
            //error_log("routine check play ".$userid );
            $s_message = 'PLAY';
            $opponentid = GetOpponentId($userid);
            $rs = mysql_query("SELECT status FROM g1_players WHERE playerid = '{$opponentid}'");
            $row = mysql_fetch_array($rs);
            $s_opponentstatus = $row['status'];
            $rs = mysql_query("SELECT sessionscore FROM g1_players WHERE playerid = '{$userid}'");
            $row = mysql_fetch_array($rs);
            $s_sessionscore = $row['sessionscore'];
            $gameid = GetGameId($userid);
            $rs = mysql_query("SELECT wordsmatched FROM g2_gameadmin WHERE gameid = '{$gameid}'");
            $row = mysql_fetch_array($rs);
            $s_entity = $row['wordsmatched'];
            // matched words
            $s_entity = substr($s_entity, 0, -1);
            //error_log("routine check play ".$userid );
            sendMessage();
            break;
        case 'PASS':
            //return score and opponent's status, matched words
            //error_log("routine check pass ".$userid );
            $s_message = 'PLAY';
            $opponentid = GetOpponentId($userid);
            $rs = mysql_query("SELECT status FROM g1_players WHERE playerid = '{$opponentid}'");
            $row = mysql_fetch_array($rs);
            $s_opponentstatus = $row['status'];
            $rs = mysql_query("SELECT sessionscore FROM g1_players WHERE playerid = '{$userid}'");
            $row = mysql_fetch_array($rs);
            $s_sessionscore = $row['sessionscore'];
            $gameid = GetGameId($userid);
            $rs = mysql_query("SELECT wordsmatched FROM g2_gameadmin WHERE gameid = '{$gameid}'");
            $row = mysql_fetch_array($rs);
            $s_entity = $row['wordsmatched'];
            // matched words
            $s_entity = substr($s_entity, 0, -1);
            sendMessage();
            break;
        case 'QUITGAME':
            $s_message = 'QUITGAME';
            sendMessage();
            clearrecord($userid);
            break;
        case 'DENIED':
            mysql_query("DELETE FROM g1_players WHERE playerid = '{$userid}'");
            $s_message = 'DENIED';
            sendMessage();
            break;
            //	echo 'Just one more step! Life is like that !';
    }
    // function over
}
function videoover()
{
    $userid = $_POST['userid'];
    $decision = $_POST['decision'];
    $opponent = GetOpponentId($userid);
    $rs = mysql_query("select status from g1_players where playerid = '{$opponent}'");
    $row = mysql_fetch_array($rs);
    $opp_status = $row['status'];
    mysql_query("UPDATE g1_players SET status='{$decision}' where playerid = '{$userid}'");
    if ($decision == 'ENDSESSION' && $opp_status == 'ENDSESSION') {
        clearrecord($userid);
        // delete, clear everything
        // no more messages will be sent from any of them.
        exit;
    }
    switch ($decision) {
        case 'ANOTHERGAME':
            switch ($opp_status) {
                case 'ANOTHERGAME':
                    $gameid = GetGameId($userid);
                    // store game information in G3 and update the fields in G1, G2
                    ExtractGist($gameid);
                    callvideoagent($userid, $opponent);
                    //call VA ( $userid, $opponent )
                    break;
                case 'PROFILEYES':
                    mysql_query("UPDATE g1_players SET status='PROFILEINVITE' where playerid = '{$userid}'");
                    break;
            }
            break;
        case 'PROFILEYES':
            switch ($opp_status) {
                case 'ANOTHERGAME':
                    mysql_query("UPDATE g1_players SET status='PROFILEINVITE' where playerid = '{$opponent}'");
                    break;
                case 'PROFILEYES':
                    mysql_query("UPDATE g1_players SET status='SHOWPROFILE' where playerid = '{$userid}'");
                    mysql_query("UPDATE g1_players SET status='SHOWPROFILE' where playerid = '{$opponent}'");
                    break;
            }
            break;
    }
    // function over.
}