<commit_before>﻿#include "GrailState.h"
#include "GameGrail.h"
#include <Windows.h>
#include <algorithm>
#include <cstdlib> 
#include "DBServices.h"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/format.hpp"


int StateWaitForEnter::handle(GameGrail* engine)
{
	if(engine->isAllStartReady())
	{
		engine->popGameState();
		engine->pushGameState(new StateSeatArrange);		
	}
	Sleep(1000);
	return GE_SUCCESS;
}

int StateSeatArrange::handle(GameGrail* engine)
{
	ztLoggerWrite(ZONE, e_Debug, "[Table %d] Enter StateSeatArrange", engine->getGameId());
	
	int m_maxPlayers = engine->getGameMaxPlayers();
	
	// 直接将随机结果保存到engine中
	GameInfo& game_info = engine->room_info;

	if(!isSet)
	{
		srand ((unsigned int)time(NULL));
		assignTeam(engine);
		vector< int > colors = assignColor(engine->m_seatMode, m_maxPlayers);

		SinglePlayerInfo *player_info;

		for(int i = 0; i < m_maxPlayers; i++){
			player_info = game_info.add_player_infos();
			int color = colors[i];
			int id;
			if(color){
				id = red.back();
				red.pop_back();
			}
			else{
				id = blue.back();
				blue.pop_back();
			}
			player_info->set_id(id);
			player_info->set_team(color);
			player_info->set_nickname(engine->getPlayerContext(id)->getName());
		}
		game_info.set_is_started(true);
		for(int i = 0; i < m_maxPlayers; i++){
			messages[i] = &game_info;
		}
		engine->resetReady();
		isSet = true;
	}
	if(engine->waitForAll(MSG_GAME, (void**)messages, false)){
		engine->popGameState();
		engine->playing = true;
		return engine->setStateRoleStrategy();
	}
	else{
		return GE_TIMEOUT;
	}
}

void StateSeatArrange::assignTeam(GameGrail* engine)
{
	int m_maxPlayers = engine->getGameMaxPlayers();
	list< int > red_l = engine->teamA;
	list< int > blue_l = engine->teamB;

	if(rand() % 2)
		red_l.swap(blue_l);

	vector< int > ids;
	for (int i = 0; i < m_maxPlayers; i++)
		ids.push_back(i);
	std::random_shuffle(ids.begin(), ids.end());
	
	for(int i = 0; i < m_maxPlayers; i++){
		int chosen = ids[i];
		if(red_l.end() == std::find(red_l.begin(), red_l.end(), chosen) && blue_l.end() == std::find(blue_l.begin(), blue_l.end(), chosen)){
			if(red_l.size() < m_maxPlayers/2)
				red_l.push_back(chosen);
			else if (blue_l.size() < m_maxPlayers / 2)
				blue_l.push_back(chosen);
		}
	}
	vector< int > red_v( red_l.begin(), red_l.end() );
	vector< int > blue_v( blue_l.begin(), blue_l.end() );
	std::random_shuffle (red_v.begin(), red_v.end());
	std::random_shuffle (blue_v.begin(), blue_v.end());
	
	red = red_v;
	blue = blue_v;
}
 
vector< int > StateSeatArrange::assignColor(int mode, int playerNum)
{
	vector< int > colors;
	switch(mode)
	{
	case SEAT_MODE_RANDOM:
		for(int i = 0; i < playerNum/2; i++){
			colors.push_back(1);
			colors.push_back(0);
		}
		//首位必红
		std::random_shuffle (++colors.begin(), colors.end());	
		break;
	case SEAT_MODE_2COMBO:
	{
		//间隔后，一对前后交换就是二连
		for(int i = 0; i < playerNum; i += 2){
			colors.push_back(1);
			colors.push_back(0);
		}
		//首位必红，在2~(n-1)中随机取一与前一个交换
		int chosen = rand() % (playerNum -2)+ 2;
		int swapped = (chosen-1 + playerNum) % playerNum;
		int temp = colors[chosen];
		colors[chosen] = colors[swapped];
		colors[swapped] = temp;
	}
		break;
	case SEAT_MODE_3COMBO:
	{
		int colors_t[] = {1, 1, 1, 0, 0, 0};
		//首位必红
		int firstRed = rand() % 3;
		int count = 0;		
		for(int i = firstRed; i < 6; i++){
			colors.push_back(colors_t[i]);
		}
		for(int i = 0; i < firstRed; i++){
			colors.push_back(colors_t[i]);
		}
    }
		break;
	case SEAT_MODE_INTERLACE:
		for(int i = 0; i < playerNum/2; i++){
			colors.push_back(1);
			colors.push_back(0);
		}
		break;
	case SEAT_MODE_RBBRRB:
		int colors_t[] = {1, 0, 0, 1, 1, 0};
		for(int i = 0; i < 6; i++){
			colors.push_back(colors_t[i]);
		}
		break;
	}
	return colors;
}

int StateLeaderElection::handle(GameGrail* engine)
{
	ztLoggerWrite(ZONE, e_Debug, "[Table %d] Enter StateLeaderElection", engine->getGameId());
	void* reply;
	int maxPlayer = engine->getGameMaxPlayers();
	GameInfo& game_info = engine->room_info;
	bool isTimeOut = !engine->waitForAll(network::MSG_BECOME_LEADER_REQ, (void**)messages);

	for (int i = 0; i < maxPlayer; i++) {
		if (GE_SUCCESS == engine->getReply(i, reply)) {
			BecomeLeaderResponse *respond = (BecomeLeaderResponse*)reply;
			if (respond->yes()) {
				SinglePlayerInfo* player_it = engine->getPlayerInfo(i);				
				if (player_it->team() == RED) {
					red.push_back(i);
				}
				else {
					blue.push_back(i);
				}
			}			
		}
	}

	if (red.empty()) {
		for (int i = 0; i < maxPlayer; i++) {
			SinglePlayerInfo player_it = game_info.player_infos().Get(i);
			if (player_it.team() == RED) {
				red.push_back(i);
			}
		}
	}
	if (blue.empty()) {
		for (int i = 0; i < maxPlayer; i++) {
			SinglePlayerInfo player_it = game_info.player_infos().Get(i);
			if (player_it.team() == BLUE) {
				blue.push_back(i);
			}<commit_msg>修正队长选择<commit_after>﻿#include "GrailState.h"
#include "GameGrail.h"
#include <Windows.h>
#include <algorithm>
#include <cstdlib> 
#include "DBServices.h"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/format.hpp"


int StateWaitForEnter::handle(GameGrail* engine)
{
	if(engine->isAllStartReady())
	{
		engine->popGameState();
		engine->pushGameState(new StateSeatArrange);		
	}
	Sleep(1000);
	return GE_SUCCESS;
}

int StateSeatArrange::handle(GameGrail* engine)
{
	ztLoggerWrite(ZONE, e_Debug, "[Table %d] Enter StateSeatArrange", engine->getGameId());
	
	int m_maxPlayers = engine->getGameMaxPlayers();
	
	// 直接将随机结果保存到engine中
	GameInfo& game_info = engine->room_info;

	if(!isSet)
	{
		srand ((unsigned int)time(NULL));
		assignTeam(engine);
		vector< int > colors = assignColor(engine->m_seatMode, m_maxPlayers);

		SinglePlayerInfo *player_info;

		for(int i = 0; i < m_maxPlayers; i++){
			player_info = game_info.add_player_infos();
			int color = colors[i];
			int id;
			if(color){
				id = red.back();
				red.pop_back();
			}
			else{
				id = blue.back();
				blue.pop_back();
			}
			player_info->set_id(id);
			player_info->set_team(color);
			player_info->set_nickname(engine->getPlayerContext(id)->getName());
		}
		game_info.set_is_started(true);
		for(int i = 0; i < m_maxPlayers; i++){
			messages[i] = &game_info;
		}
		engine->resetReady();
		isSet = true;
	}
	if(engine->waitForAll(MSG_GAME, (void**)messages, false)){
		engine->popGameState();
		engine->playing = true;
		return engine->setStateRoleStrategy();
	}
	else{
		return GE_TIMEOUT;
	}
}

void StateSeatArrange::assignTeam(GameGrail* engine)
{
	int m_maxPlayers = engine->getGameMaxPlayers();
	list< int > red_l = engine->teamA;
	list< int > blue_l = engine->teamB;

	if(rand() % 2)
		red_l.swap(blue_l);

	vector< int > ids;
	for (int i = 0; i < m_maxPlayers; i++)
		ids.push_back(i);
	std::random_shuffle(ids.begin(), ids.end());
	
	for(int i = 0; i < m_maxPlayers; i++){
		int chosen = ids[i];
		if(red_l.end() == std::find(red_l.begin(), red_l.end(), chosen) && blue_l.end() == std::find(blue_l.begin(), blue_l.end(), chosen)){
			if(red_l.size() < m_maxPlayers/2)
				red_l.push_back(chosen);
			else if (blue_l.size() < m_maxPlayers / 2)
				blue_l.push_back(chosen);
		}
	}
	vector< int > red_v( red_l.begin(), red_l.end() );
	vector< int > blue_v( blue_l.begin(), blue_l.end() );
	std::random_shuffle (red_v.begin(), red_v.end());
	std::random_shuffle (blue_v.begin(), blue_v.end());
	
	red = red_v;
	blue = blue_v;
}
 
vector< int > StateSeatArrange::assignColor(int mode, int playerNum)
{
	vector< int > colors;
	switch(mode)
	{
	case SEAT_MODE_RANDOM:
		for(int i = 0; i < playerNum/2; i++){
			colors.push_back(1);
			colors.push_back(0);
		}
		//首位必红
		std::random_shuffle (++colors.begin(), colors.end());	
		break;
	case SEAT_MODE_2COMBO:
	{
		//间隔后，一对前后交换就是二连
		for(int i = 0; i < playerNum; i += 2){
			colors.push_back(1);
			colors.push_back(0);
		}
		//首位必红，在2~(n-1)中随机取一与前一个交换
		int chosen = rand() % (playerNum -2)+ 2;
		int swapped = (chosen-1 + playerNum) % playerNum;
		int temp = colors[chosen];
		colors[chosen] = colors[swapped];
		colors[swapped] = temp;
	}
		break;
	case SEAT_MODE_3COMBO:
	{
		int colors_t[] = {1, 1, 1, 0, 0, 0};
		//首位必红
		int firstRed = rand() % 3;
		int count = 0;		
		for(int i = firstRed; i < 6; i++){
			colors.push_back(colors_t[i]);
		}
		for(int i = 0; i < firstRed; i++){
			colors.push_back(colors_t[i]);
		}
    }
		break;
	case SEAT_MODE_INTERLACE:
		for(int i = 0; i < playerNum/2; i++){
			colors.push_back(1);
			colors.push_back(0);
		}
		break;
	case SEAT_MODE_RBBRRB:
		int colors_t[] = {1, 0, 0, 1, 1, 0};
		for(int i = 0; i < 6; i++){
			colors.push_back(colors_t[i]);
		}
		break;
	}
	return colors;
}

int StateLeaderElection::handle(GameGrail* engine)
{
	ztLoggerWrite(ZONE, e_Debug, "[Table %d] Enter StateLeaderElection", engine->getGameId());
	void* reply;
	int maxPlayer = engine->getGameMaxPlayers();
	GameInfo& game_info = engine->room_info;
	bool isTimeOut = !engine->waitForAll(network::MSG_BECOME_LEADER_REQ, (void**)messages);

	for (int i = 0; i < maxPlayer; i++) {
		if (GE_SUCCESS == engine->getReply(i, reply)) {
			BecomeLeaderResponse *respond = (BecomeLeaderResponse*)reply;
			if (respond->yes()) {
				SinglePlayerInfo* player_it = engine->getPlayerInfo(i);				
				if (player_it->team() == RED) {
					red.push_back(i);
				}
				else {
					blue.push_back(i);
				}
			}			
		}
	}

	if (red.empty()) {
		for (int i = 0; i < maxPlayer; i++) {
			SinglePlayerInfo player_it = game_info.player_infos(i);
			if (player_it.team() == RED) {
				red.push_back(player_it.id());
			}
		}
	}
	if (blue.empty()) {
		for (int i = 0; i < maxPlayer; i++) {
			SinglePlayerInfo player_it = game_info.player_infos(i);
			if (player_it.team() == BLUE) {
				blue.push_back(player_it.id());
			}
{'fraction_non_alphanumeric': 0.17386841835163708, 'fraction_numerical': 0.010469054705942728, 'mean_word_length': 3.9436834094368343, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2618937', 'n_tokens_mistral': 4460, 'n_tokens_neox': 3996, 'n_words': 756}