CashWorld/CashWorld.cpp

401 lines
16 KiB
C++
Raw Normal View History

2018-06-17 10:00:43 +08:00
<EFBFBD><EFBFBD>// CashWorld Client Program
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <ctime>
#include "GSock/gsock.h"
#include "GSock/gsock_helper.h"
2018-06-17 12:12:32 +08:00
#include "ui_helper.h"
2018-06-17 10:00:43 +08:00
using namespace std;
2018-06-17 12:12:32 +08:00
const string SERVER_IP = "123.206.86.73";
2018-06-17 10:00:43 +08:00
const int PORT_CHECK = 59401;
const int PORT_LOGIN = 59402;
2018-06-17 12:12:32 +08:00
const int PORT_GAME = 59402;
2018-06-17 10:00:43 +08:00
char _xtime[256];
const char* _update_xtime()
{
time_t t = time(NULL);
strftime(_xtime, 256, "%Y-%m-%d %H:%M:%S", localtime(&t));
return _xtime;
}
FILE* _log_f = NULL;
#define xlog(fmt,...) _update_xtime();fprintf(_log_f,"<%s | %s> " fmt "\n",_xtime,__func__,##__VA_ARGS__);fflush(_log_f)
#define start_xlog() _log_f=fopen("log.txt","a");fprintf(_log_f,"==============================\n");fflush(_log_f)
2018-06-17 12:12:32 +08:00
std::string user_cert_code;
2018-06-17 10:00:43 +08:00
int update_check()
{
sock s;
if (s.connect(SERVER_IP, PORT_CHECK) < 0)
{
xlog("Cannot connect to server");
return -1;
}
sock_helper sh(s);
string response;
if (sh.sendline("get_current_version") <= 0 ||
sh.recvline(response)<0 )
{
xlog("Failed to querying version");
return -2;
}
istringstream istr(response);
string code, version;
if (!(istr >> code >> version))
{
xlog("Failed to parse version code. Response: %s", response.c_str());
return -3;
}
if (code != "current_version")
{
xlog("Wrong response header. Header: %s", code.c_str());
return -4;
}
2018-06-17 12:12:32 +08:00
xlog("Server response latest version is: %s", version.c_str());
2018-06-17 10:00:43 +08:00
return 0;
}
2018-06-17 12:12:32 +08:00
int user_login(const char* uname,const char* upass)
2018-06-17 10:00:43 +08:00
{
2018-06-17 12:12:32 +08:00
sock s;
if (s.connect(SERVER_IP, PORT_LOGIN) < 0)
{
xlog("Cannot connect to server");
return -1;
}
sock_helper sh(s);
if (sh.sendline("user_login") < 0 ||
sh.sendline(uname) < 0 ||
sh.sendline(upass) < 0)
{
xlog("Failed to send data");
return -2;
}
string response, code;
if (sh.recvline(response) <= 0 || sh.recvline(code) < 0)
{
xlog("Failed to recv data.");
return -3;
}
if (response != "login_ok")
{
xlog("Failed to login.");
return 1;
}
user_cert_code = code;
2018-06-17 10:00:43 +08:00
return 0;
}
2018-06-17 12:12:32 +08:00
void readline(char* outbuff, int size)
{
memset(outbuff, 0, size);
fgets(outbuff, size, stdin);
int sz = strlen(outbuff);
if (outbuff[sz - 1] == '\n') outbuff[sz - 1] = 0;
}
int ui_user_login()
{
while (true)
{
title("(u7b{vU_");
printf("(u7b T:"); fflush(stdout);
char uname[1024];
readline(uname, 1024);
printf("<00>[x:"); fflush(stdout);
char upass[1024];
readline(upass, 1024);
title("(u7b{vU_");
printf("ck(W{vU_...\n");
int ret = user_login(uname, upass);
if (ret < 0)
{
printf("Q<51>~<19><><EFBFBD>. <00>e<EFBFBD>lN g<EFBFBD>RhV<EFBFBD>^<EFBFBD>z<EFBFBD><EFBFBD><EFBFBD>c.\n");
return -1;
}
else if (ret > 0)
{
printf("(u7b Tb<EFBFBD>[x<EFBFBD><EFBFBD><EFBFBD>. <EFBFBD><EFBFBD>͑Ջ.\n");
WaitPause();
}
else return 0;
}
}
int do_get_my_profile(char* uname,int& cash)
{
sock s;
if (s.connect(SERVER_IP, PORT_GAME) < 0)
{
xlog("Cannot connect to server");
return -1;
}
sock_helper sh(s);
if (sh.sendline("get_my_profile") < 0 ||
sh.sendline(user_cert_code) < 0)
{
return -2;
}
string response;
if (sh.recvline(response) < 0)
{
return -3;
}
if (response != "my_profile_ok")
{
return -4;
}
string user_name;
string cash_str;
if (sh.recvline(user_name) <= 0 ||
sh.recvline(cash_str) < 0)
{
return -5;
}
strcpy(uname, user_name.c_str());
sscanf(cash_str.c_str(), "%d", &cash);
return 0;
}
void ui_view_my_profile()
{
title("*N<>ND<4E><44>e");
printf("ck(W<><57><EFBFBD>S*N<>ND<4E><44>e...\n");
char uname[1024] = { 0 };
int cash;
if (do_get_my_profile(uname, cash) < 0)
{
title("*N<>ND<4E><44>e");
printf("<00>e<EFBFBD>l<EFBFBD><6C><EFBFBD>S*N<>ND<4E><44>e.\n");
}
else
{
title("*N<>ND<4E><44>e");
printf("(u7b T: %s\n", uname);
printf("ё^peϑ: %d\n", cash);
}
WaitPause();
return;
}
int ui_game_guessnumber_input_gate(int& val)
{
title("speW[-<00>beQ");
char uname[1024] = { 0 };
int cash = -1;
do_get_my_profile(uname, cash);
printf("
\le<EFBFBD>v%s,<EFBFBD>`S_MRYO<EFBFBD><EFBFBD>:N%dCQ\n", uname, cash);
printf("<00><><EFBFBD><EFBFBD>eQ`O<><4F><EFBFBD>beQ<65><76><D191>peϑ,Yuzzԏ<7A>V.\n");
while (true)
{
char xbuf[1024] = { 0 };
readline(xbuf, 1024);
if (strlen(xbuf) < 1) return 0;
if (sscanf(xbuf, "%d", &val) != 1 || val < 1)
{
printf("<00><><EFBFBD><EFBFBD>eQcknx<6E>v<EFBFBD>beQё<51><D191>.\n");
ClearInput();
}
else break;
}
return 1;
}
int do_game_guessnumber(int val, int low, int high, int guess, int& result)
{
sock s;
if (s.connect(SERVER_IP, PORT_GAME) < 0)
{
xlog("Cannot connect to server");
return -1;
}
sock_helper sh(s);
char xbuf[1024] = { 0 };
sprintf(xbuf, "%d %d %d %d", val, low, high, guess);
if (sh.sendline("game_guess_number") < 0 ||
sh.sendline(user_cert_code) < 0 ||
sh.sendline(xbuf) < 0)
{
return -2;
}
string response;
string data;
if (sh.recvline(response) < 0)
{
return -3;
}
if (response != "guess_number_ok")
{
return -4;
}
if (sh.recvline(data) < 0)
{
return -5;
}
if (data == "guessed")
{
result = 1;
return 0;
}
else
{
result = 0;
return 0;
}
}
void ui_game_guessnumber_classA()
{
int val;
if (ui_game_guessnumber_input_gate(val) == 0)
{
return;
}
int c;
while (true)
{
title("speW[");
printf("<00><><EFBFBD><EFBFBD>eQ`O<><4F>s<>vpeW[ (1-9):\n");
if (scanf("%d", &c) != 1 || c < 1 || c>9)
{
printf("<00><><EFBFBD><EFBFBD>eQcknx<6E>vpeW[.\n");
ClearInput();
}
else break;
}
int result;
if (do_game_guessnumber(val, 1, 9, c, result) < 0)
{
title("speW[");
printf("Q<51>~_8^.\n");
WaitPause();
return;
}
if (result)
{
title("speW[");
printf("m`<60>U`Os-N<>N!\n");
WaitPause();
}
else
{
title("speW[");
printf("<00>_<EFBFBD>bIk,`O<>l<EFBFBD><6C>s-N!\n");
WaitPause();
}
}
void ui_game_guessnumber_classB()
{
int val;
if (ui_game_guessnumber_input_gate(val) == 0)
{
return;
}
}
void ui_game_guessnumber()
{
while (true)
{
title("speW[");
printf("<00><> <09><>b<EFBFBD><62><EFBFBD>^, <00><><EFBFBD>^<5E><>'YVY<56>R<EFBFBD><52>0N<30>S.\n");
printf("[1] -NVY<56>s10% s-NVY<56>R: 1.1\n[2] -NVY<56>s1% s-NVY<56>R: 2.0\n[3] ԏ<>V\n");
int ss = GetChoice(3);
switch (ss)
{
case 1:
ui_game_guessnumber_classA();
break;
case 2:
ui_game_guessnumber_classB();
break;
case 3:
return;
}
}
}
void ui_online_chat()
{
}
int ui_main_game()
{
while (true)
{
title("8nb'Y<>S");
printf("[1] <00>g wD<77><44>e\n[2] _<>Y8nb: speW[\n[3] J<>)Y<>[\n[4] <00><>Q\n");
int ss = GetChoice(4);
switch (ss)
{
case 1:
ui_view_my_profile();
break;
case 2:
ui_game_guessnumber();
break;
case 3:
ui_online_chat();
break;
case 4:
return 0;
}
}
}
2018-06-17 10:00:43 +08:00
int main()
{
start_xlog();
xlog("Program Started");
2018-06-17 12:12:32 +08:00
title("<00>h<EFBFBD>g<EFBFBD>f<EFBFBD>e");
printf("<00>h<EFBFBD>g<EFBFBD>f<EFBFBD>e-N,<00><> zI{...\n");
if (update_check() < 0)
{
printf("<00>e<EFBFBD>l<EFBFBD>h<EFBFBD>g<EFBFBD>f<EFBFBD>e. <00><><EFBFBD>g w`O<>vQ<51><>c.\n");
WaitPause();
//return 0;
}
if (ui_user_login() < 0)
{
xlog("Invalid return from user login");
return 0;
}
xlog("User logged in. Cert code: %s\n", user_cert_code.c_str());
ui_main_game();
2018-06-17 10:00:43 +08:00
xlog("Program Finished Successfully.");
return 0;
}