-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
321 lines (261 loc) · 7.24 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <stdio.h>
#include <stdlib.h>
//specific lib inclusion
#include "client.h"
int main(int argc, char * argv[])
{
//prepare ncurses interface
setInterface();
//evaluating additional parameters
evaluateParams(argc,argv);
//initializing client network
struct client_network net;
if(argc>1){
net.target_ip = argv[1];
} else {
char * ip = malloc(11);
ip = "127.0.0.1";
net.target_ip = ip;
}
//creating the user structure that will take record of the user info and current state
struct clientuser * me = malloc(sizeof(struct clientuser));
me->logged = 0;
while(true){
//anonymous play not supported - force user to log in
while(!me->logged){
struct logininfo * logindata = NULL;
logindata = showLoginForm(); //show login form, login data is allocated, needs to be free
singleWindowMessage("logging...");
fullClientCommand(&net, "login", logindata->username, logindata->password, "", "");
struct netmessage * incoming = readClientMessage(&net);
if (areEqual(incoming->msg1, "done")){
singleWindowMessage("logged!");
// set up base client user account
me->username = copystring(logindata->username);
me->password = copystring(logindata->password);
me->loginkey = copystring(incoming->msg2);
me->currentgame = NULL;
me->logged = 1;
me->wins = 0;
me->losses = 0;
sleep(2);
} else if(areEqual(incoming->msg1, "wrongpassword")) {
singleWindowMessage("Wrong Password!");
sleep(2);
} else if(areEqual(incoming->msg1, "usernotfound")) {
singleWindowMessage("User Not Found!");
sleep(2);
} else {
singleWindowMessage("Unknown Error!");
sleep(2);
}
free(logindata->username);
free(logindata->password);
free(logindata);
}
int choice = showMainMenu();
switch (choice){
case 0: //logout
fullClientCommand(&net, "logout","","","",me->loginkey);
struct netmessage * incoming = readClientMessage(&net);
if(areEqual(incoming->msg1, "done")){
me->logged = 0;
free(me->username);
free(me->password);
free(me->loginkey);
me->currentgame = NULL;
}
break;
case 1: //join game
singleWindowMessage("Waiting for player...");
char * gameid = joinGame(&net, me);
singleWindowMessage("Game Found!");
sleep(1);
int result = playGame(&net, gameid, me);
if(result==1){
singleWindowMessage("Game Won!");
sleep(4);
} else if (result==-1) {
singleWindowMessage("Game Lost!");
sleep(4);
}
break;
}
break;
}
unsetInterface(); //unset ncurses
return 0;
}
int playGame(struct client_network * net, char * gameid, struct clientuser * me)
{
int color = 0;
int opp = 0;
fullClientCommand(net, "opponent","","",gameid,me->loginkey);
struct netmessage * incoming = readClientMessage(net);
if(areEqual(incoming->msg1, "thinking")){
color = 2;
opp = 1;
singleWindowMessage("You have blacks!");
} else if(areEqual(incoming->msg1, "yourmove")){
color = 1;
opp = 2;
singleWindowMessage("You have whites!");
}
//preparing board
struct board b;
setAllBoardEmpty(&b);
prepareBoard(&b);
while(1){
//retrieving server game status
fullClientCommand(net, "opponent","","",gameid,me->loginkey);
incoming = readClientMessage(net);
//l'avversario sta pensando
if(areEqual(incoming->msg1, "thinking")){
sleep(2);
continue;
} else if (canMove(&b, color)==-1) {
return -1;
} else if (canMove(&b, opp)==-1) {
return 1;
//tocca a te, fai la mossa
} else if (areEqual(incoming->msg1, "yourmove")) {
//perfoming locally opponent move
if(strlen(incoming->msg2)>0){
struct moveinfo * tempmove = malloc(sizeof(struct moveinfo));
tempmove->daC = fromCharToPChar(incoming->msg2[0]);
tempmove->daL = fromCharToPChar(incoming->msg2[1]);
tempmove->aC = fromCharToPChar(incoming->msg2[2]);
tempmove->aL = fromCharToPChar(incoming->msg2[3]);
int lol = nextMove( &b, tempmove, opp);
if(lol==-1){
singleWindowMessage("failed to set opponent move!");
}
clear();
refresh();
if (canMove(&b, color)==-1) {
return -1;
} else if (canMove(&b, opp)==-1) {
return 1;
} else if (color==1) {
printWhiteBoard(&b);
} else {
printBlackBoard(&b);
}
free(tempmove->daC);
free(tempmove->aC);
free(tempmove->daL);
free(tempmove->aL);
free(tempmove);
} else {
singleWindowMessage("failed to retrieve opponent move!!");
}
struct moveinfo * move;
int exitCond2 = -1;
int exitCondServer = -1;
//force player to give a correct input
while(exitCondServer==-1){
while(exitCond2==-1){
move = takeMove(&b, color);
exitCond2 = nextMove( &b, move, color);
if(exitCond2==-1){
singleWindowMessage("Mossa illegale!");
sleep(1);
}
}
char m[5];
m[0] = move->daC[0];
m[1] = move->daL[0];
m[2] = move->aC[0];
m[3] = move->aL[0];
m[4] = 0; //string terminator
fullClientCommand(net, "move",m,"",gameid,me->loginkey);
incoming = readClientMessage(net);
if(areEqual(incoming->msg1, "accepted")){
singleWindowMessage("Moved!");
sleep(1);
exitCondServer = 1; //exiting...
} else {
singleWindowMessage("Move rejected from server");
exitCond2==-1;
sleep(1);
}
}
clear();
refresh();
if (canMove(&b, color)==-1) {
return -1;
} else if (canMove(&b, opp)==-1) {
return 1;
} else if (color==1) {
printWhiteBoard(&b);
} else {
printBlackBoard(&b);
}
free(move->daC);
free(move->aC);
free(move->daL);
free(move->aL);
free(move);
}
}
}
/*
* Wait for a game and connect.
*/
char * joinGame(struct client_network * net, struct clientuser * me)
{
while(1){
fullClientCommand(net, "join","","","",me->loginkey);
struct netmessage * incoming = readClientMessage(net);
if(areEqual(incoming->msg1, "ready")){
return incoming->msg2;
} else {
sleep(1);
}
}
}
/*
* Convert from char to char *
*/
char * fromCharToPChar(char p)
{
char * s = malloc(2);
s[0] = p;
s[1] = 0;
return s;
}
/*
* Evaluate additional lauch parameters
*/
void evaluateParams(int argc, char * argv[])
{
if ((argc>1) && (areEqual("--help", argv[1]))){
printf("Dama Carla - Online Dama Client\n");
printf("Port : 2343\n");
printf("Author : Claudio Desideri <[email protected]>\n");
printf("Website: http://dama-carla.googlecode.com\n");
printf("\n");
printf("\n");
printf("Usage:\n");
printf("\n");
printf("register <nickname> <password> :\n");
printf("Register a new account on server with given <nickname> and <password>\n");
exit(0);
} else if ((argc>=4) && (areEqual("register", argv[1]))){
//initializing client network
struct client_network net;
fullClientCommand(&net, "register", argv[2], argv[3], "", "");
struct netmessage * incoming = readClientMessage(&net);
if((areEqual(incoming->msg1,"done"))){
printf("done\n");
exit(0);
} else if ((areEqual(incoming->msg1,"alreadythere"))) {
printf("user already present in database\n");
exit(0);
} else {
printf("unknown error :(\n");
printf("feel free to report a bug to http://dama-carla.googlecode.com\n");
exit(0);
}
}
}