Beispiel #1
0
static void company_info(tmdb_options_t *opts)
{
	tmdb_configuration_t	*cfg;
	tmdb_company_t		*company;
	tmdb_company_t		*company_p;

	company = tmdb_company_info(opts->id);
	print_company_info(company);
	cfg = tmdb_configuration();
	company_p = company;
	while (company_p) {
		tmdb_save_image_generic(cfg, company_p->logo_path,
					"./images/logos");
		company_p = company_p->next;
	}
	tmdb_clean_configuration(cfg);
	tmdb_clean_company_type(company);
}
int main(int argc, char const* argv[]) {


	int sock, error;
	uint32_t get_data;
	uint32_t stock_data[ELEM];
	uint32_t default_data[ELEM];

	char host[16];
	char port[16];
	strcpy(host, argv[1]);
	strcpy(port, argv[2]);

	sock = makesock(port);
	printf("sock : %d\n", sock);

	int i;

	// game start
	while(true) {
		get_data = getData(&sock, stock_data);
		if(stock_data[1] == LIST) {
			printf("can connect to game.\n");
			break;
		}
	}

	bool list_flag = false;
	uint32_t math;
	uint32_t buy_id;
	uint32_t code = stock_data[1];
	uint32_t value;
	uint32_t key = stock_data[0];
	int turn = 0;
	uint32_t budget = 10000;

	// company and my_stock
	uint32_t my_stock[COMPANY];
	struct Company company[COMPANY];
	struct Company tmp_company[COMPANY];
	int j;
	for(j = 0; j < COMPANY; j++) {
		my_stock[j] = 0;
		default_data[j] = stock_data[j];
		company[j].id = j;
		company[j].price = stock_data[3+j*2];
		company[j].ave = stock_data[3+j*2];
		company[j].pattern = 0;
		company[j].prev_pattern = 0;
	}
	print_company_info(company);

	printf("\n");
	printf("game start.\n");
	printf("begin *** budget: %u, key: %u, code: %x\n", budget, key, stock_data[1]);

	int read_len;
	uint32_t tmp_data[ELEM];
	int s;
	bool is_minus = true;
	bool is_math = true;

	// doing game
	for(s = 0; s < TURNS; s++) {

		int a;
		read_len = -1;
		turn++;
		int n = 0;

		//list_flag = true;
		printf("\n ====== begin turn %d. before request. ======\n", s+1);
		for(i = 0; i < 5; i++) {
			
			if(key == 0 && code == END) {
				printf("my_lank is ....... %u\n", tmp_data[2]);
				exit(-1);
				break;
			}



			bool check_flag = false;

			if(turn == 1) {
				math = 10;
				buy_id = i;
				code = PURCHASE;
				check_flag = true;
			} else if(turn >= 59) {
					math = my_stock[i+n];
					buy_id = i+n;
					code = SALE;
					check_flag = true;
					n = 5;
			} else {
				int c;
				uint32_t m;
				for(c = n; c < COMPANY; c++) {
					
					m = 100;
					if(company[i].pattern < company[i].prev_pattern) {
						while(m <= 0) {
							if(is_minus_budget(budget, value, m) == true) {
								break;
							}
							m--;
						}
						if(m == 0) {
							continue;
						}
						math = m;
						buy_id = i;
						code = PURCHASE;
						check_flag = true;
						n = c;
						printf("buy check: company_id: %u, math: %u\n", buy_id, math);
						break;
					}
					if(company[i].pattern > company[i].prev_pattern) {
						math = my_stock[i];
						buy_id = i;
						code = SALE;
						check_flag = true;
						n = c;
						printf("sale check: company_id: %u, math: %u\n", buy_id, math);
						break;
					}
				}
			}

			if(check_flag == false) {
				continue;
			}

		
			request(&sock, &key, &math, &buy_id, &code);
			int l = getData(&sock, tmp_data);
			check_code(&tmp_data[1]);
			value = company[buy_id].price;
			if(tmp_data[1] != ACCEPT) {
				printf("\nnot accepted request.\n");
			} else {
				printf("\naccepted request.\n");
				status(&budget, my_stock, &turn, &buy_id, &code, &math, &value);
				//status(&budget, my_stock, &turn, &math, &buy_id, &code, &value);
			}
		}

		printf("\n ------- stayng ------\n");

		//while(list_flag == true) {
		while(1) {

			read_len = getData(&sock, tmp_data);
			check_code(&tmp_data[1]);
			code = tmp_data[1];
			if(code == ACCEPT || code == ERR_CODE || code == ERR_KEY || code == ERR_REQ || code == ERR_ID || code == ERR_PUR || code == ERR_SAL) {
				continue;
			}
			if(code != LIST) {
				continue;
			} else {
				dump_data(tmp_data);
				key = tmp_data[0];
				code = tmp_data[1];
				set_company_info(tmp_data, company, &turn);
				print_company_info(company);
				//list_flag = false;
				break;
			}
		}
		
		// read list
		printf("result -> budget: %u, turn: %d, key: %u, code: %x\n", budget, turn, tmp_data[0], tmp_data[1]);
		printf("turn %d end\n", s + 1);
	}
	
	return 0;
}