Example #1
0
StringBuilder *MatchRecorder::export_str_player() {

	int flag = 0; // 什么也不做
/*	if (_penalty_count > 0) // 点球大战计数器
	{
		_reverse = 0; // 方向进攻方向

		if (_penalty_count % 2 == 0) {
			flag = 1; // player_id 取反
		}
	}*/

	StringBuilder *str = new StringBuilder(40*1024); // 压缩前长度

	MsgMatchPlayerRoundData msg;//= new MsgMatchRoundData();
	msg.mid = 1;
	msg.player_count = RECORDER_PLAYER_COUNT;
	str->append((const char*) &msg, sizeof(msg) - sizeof(void*));

	for (int i = 0; i < RECORDER_PLAYER_COUNT; ++i) {
		PlayerRoundInfo info;
		info.time_sacle_numbers = _players_frames[i].size();
		info.pid = (*_players_frames[i].begin())->get_player_id();
		info.cond = (*_players_frames[i].begin())->get_player_cond();
		str->append((const char*) &info, sizeof(info) - sizeof(void*));
		save_data(_players_frames[i].begin(), _players_frames[i].end(), str,
				_reverse, _is_half, flag);
	}
	((MsgMatchPlayerRoundData*) str->get_buffer())->msg_size = str->len();
	return str;
}
Example #2
0
StringBuilder *MatchRecorder::export_str_ball() {

	int string_len = 100;

	string_len += _ball_frames.size() * 20;
	StringBuilder *str = new StringBuilder(string_len); // 压缩前长度

	MsgGameBallRoundData msg;
	msg.mid =1;
	msg.time_counts = _ball_frames.size();
	str->append((const char*)&msg,sizeof(msg) - sizeof(void*));

	save_data(_ball_frames.begin(), _ball_frames.end(), str, _reverse, _is_half, 0);
	((MsgGameBallRoundData*)str->get_buffer())->msg_size = str->len();

	return str;
}
Example #3
0
/**
 * Reads the bytes in the buffer. Interprets messages as
 *  it detects them.
 *
 * Preconditions:
 * -serialInBox has been initialized.
 * -MAX_MESSAGE_SIZE number of bytes of memory availiable
 *   on the stack.
 * -Serial.begin() has been called successfully.
 * -serialInBox may need more memory on the heap.
 *
 * Post Conditions:
 * -serialInBox may have additional bytes from buffer if the proper
 *   begginning and ending chars are received and the message is
 *   properly formatted.

 * @param buffer is the array of characters to read.
 * @param numOfBytes is the length of the array.
 */
void readBytes( char buffer[], int numOfBytes ) {
  static boolean foundMessage = false;
  // Discard any previous messages.
  serialInBox.reset();
  // Handle each byte.
    for( int i = 0; i < numOfBytes; ++i ) {
      char chr = buffer[ i ];
      switch( chr ) {
        case MSG_START:
        {
          foundMessage = true;
          //XXX Clear inbox.
        }
        break;
        case MSG_TERMINATOR:
        {
          if ( serialInBox.len() > 0 ) {
              // What to do once we have the message?
            int messageID;
            bool goodMessage = false;
            // Get message type.
            char messageType = *(const char*)serialInBox;
            serialOutBox.appends( "\"" );
            serialOutBox.appends( (const char*)serialInBox );
            serialOutBox.appends( "\"" );

            switch( messageType ) {
              case TIME_REQUEST:
              {
                int delayAmount;
                if ( sscanf( (const char*)serialInBox + 1, "%d/%d", &messageID, &delayAmount ) == 2 ) {
                  goodMessage = true;
                  timeRequestEndTime = millis() + delayAmount;
                }
              }
              break;
              default:
              {
                // Unknown request.
              }
            }
              // Read ID.
              // sscanf returns the number of variables read.
            if ( goodMessage ) {
              // Only new messages.
              if ( lastIDReceived < messageID ) {
                // Write confirmation message.`
                char tempOutString[MAX_MESSAGE_SIZE];
                sprintf( tempOutString, "%cC%d%c", MSG_START, messageID, MSG_TERMINATOR );
                Serial.write( tempOutString );
                serialOutBox.appends( tempOutString );
                lastIDReceived = messageID;
              }
            }
            else {
              char tempOutString[ MAX_MESSAGE_SIZE ];
              sprintf( tempOutString, "%cE%d\"%s\"Bad Message Format%c", MSG_START, messageID, (const char*)serialInBox, MSG_TERMINATOR );
              serialOutBox.appends( tempOutString );
            }
            foundMessage = false;
            serialInBox.reset();
          }
        }
        break;
        default:
        {
          if ( foundMessage )
            serialInBox.append( chr );
        }
        break;
      }
    }
}