-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_server.c
1344 lines (975 loc) · 34.9 KB
/
project_server.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "project.h"
//Global Variables /////////////////////////////////////////////////////////////
Uint32 Event_ServerCommand;
int socket_fd;
struct board_info board;
int total_clients = 0;
int active_clients = 0;
struct client* client_vector;
pthread_rwlock_t* line_lock;
pthread_rwlock_t client_vector_mutex;
pthread_mutex_t fruit_mutex, total_clients_mutex;
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]){
SDL_Event event;
int done = 0;
int total_bricks, max_clients;
pthread_t accept_thr, score_thr;
total_bricks = read_file(&board);
initialize_mutexes();
max_clients = server_setup(total_bricks);
pthread_create(&accept_thr, NULL, accept_thread, &max_clients);
pthread_create(&score_thr, NULL, score_thread, NULL);
create_board_window(board.length, board.width);
paint_objects(board);
Event_ServerCommand = SDL_RegisterEvents(1);
while (!done){
while (SDL_PollEvent(&event)){
if(event.type == SDL_QUIT) {
done = SDL_TRUE;
printf("Ending the game\n");
close(socket_fd);
}
if(event.type == Event_ServerCommand){
struct move* data_ptr;
data_ptr = event.user.data1;
paint_tile((*data_ptr).place1);
paint_tile((*data_ptr).place2);
free(event.user.data1);
}
}
}
close_board_windows();
pthread_cancel(accept_thr);
pthread_cancel(score_thr);
server_data_cleanup();
return 0;
}
///////////////////////////////////////////////////////////////////////////////
int server_setup(int bricks){
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd == 0)
error_handle("Failed to socket\n");
struct sockaddr_in local_addr;
memset(&local_addr, 0, sizeof(struct sockaddr_in));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = INADDR_ANY;
local_addr.sin_port = htons(3000);
int ret_val = bind(socket_fd, (struct sockaddr*)&local_addr, sizeof(struct sockaddr_in));
if(ret_val < 0)
error_handle("Fail to bind\n");
int max_clients = (board.width*board.length - bricks +1) / 4;
ret_val = listen(socket_fd, max_clients);
if(ret_val < 0)
error_handle("Error in listen\n");
printf("Server setup successful\n");
return max_clients;
}
///////////////////////////////////////////////////////////////////////////////
void* accept_thread(void* arg){
int max_clients = *((int*)arg);
int client_fd;
struct sockaddr_in other_addr;
socklen_t len = sizeof(struct sockaddr_in);
pthread_t thread_id;
struct client my_client;
printf("Waiting for client to connect\n");
while(1){
client_fd = accept(socket_fd, (struct sockaddr*)&other_addr, &len);
if(client_fd < 0)
error_handle("Error in accept\n");
else
printf("Accepted a new client ");
if(active_clients >= max_clients){
printf("but the game is full - client kicked. Try again later\n");
close(client_fd);
continue;
}
my_client = new_client(client_fd);
if(my_client.client_fd < 0)
continue;
printf("- Player id: %d\n", my_client.player_id);
deliver_or_trim_fruit();
pthread_create(&thread_id, NULL, work_thread, &my_client);
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
struct client new_client(int client_fd){
//Reads color, assigns the pacman and monster of the client, send to the client
int free_x, free_y;
struct client new_client;
struct color client_color;
struct tile pacman_tile, monster_tile;
struct move msg;
int empty_spot = 0;
//Reads the color of the client
int bytes = read(client_fd, &client_color, sizeof(client_color));
if(bytes != sizeof(struct color)){
printf("Error reading the client's color - kicking client\n");
close(client_fd);
new_client.client_fd = -1;
return new_client;
}
new_client.client_color = client_color;
new_client.player_id = client_fd;
new_client.client_fd = client_fd;
new_client.score = 0;
//Sends the dimensions of the board
int length = board.length;
int width = board.width;
send(client_fd, &length, sizeof(int), 0);
send(client_fd, &width, sizeof(int), 0);
//Finds two empty spots for the clients' characters
find_free_tile(&free_x, &free_y);
pacman_tile.type = PACMAN;
pacman_tile.tile_color = client_color;
pacman_tile.x = free_x;
pacman_tile.y = free_y;
pacman_tile.owner = new_client.player_id;
pacman_tile.super_bites = 0;
new_client.x_pacman = free_x;
new_client.y_pacman = free_y;
pthread_rwlock_wrlock(&line_lock[pacman_tile.y]);
board.board_matrix[pacman_tile.y][pacman_tile.x] = pacman_tile;
pthread_rwlock_unlock(&line_lock[pacman_tile.y]);
find_free_tile(&free_x, &free_y);
monster_tile.type = MONSTER;
monster_tile.tile_color = client_color;
monster_tile.x = free_x;
monster_tile.y = free_y;
monster_tile.owner = new_client.player_id;
monster_tile.super_bites = 0;
new_client.x_monster = free_x;
new_client.y_monster = free_y;
pthread_rwlock_wrlock(&line_lock[monster_tile.y]);
board.board_matrix[monster_tile.y][monster_tile.x] = monster_tile;
pthread_rwlock_unlock(&line_lock[monster_tile.y]);
//Send the information of the board to the client
for(int i=0; i<board.width; i++){
pthread_rwlock_rdlock(&line_lock[i]);
for(int j=0; j<board.length; j++){
send(client_fd, &board.board_matrix[i][j], sizeof(struct tile), 0);
}
pthread_rwlock_unlock(&line_lock[i]);
}
//Send the new client its information
send(client_fd, &new_client, sizeof(struct client), 0);
//Updates the client vector
//Checks if there is a free spot on the client vector
pthread_rwlock_wrlock(&client_vector_mutex);
for(int i=0; i<total_clients; i++){
if(client_vector[i].client_fd < 0){
client_vector[i] = new_client;
active_clients++;
//pthread_rwlock_unlock(&client_vector_mutex);
empty_spot = 1;
//return new_client; //TODO este return não impede de enviar o que está à frente?
}
}
if(empty_spot == 0){
//If not, increases the vector
total_clients++;
active_clients++;
client_vector = (struct client*) realloc(client_vector, sizeof(struct client)*total_clients );
client_vector[total_clients -1] = new_client;
}
pthread_rwlock_unlock(&client_vector_mutex);
//Envia as novas personagens para os outros clientes
msg.type = REGULAR_MESSAGE;
msg.place1 = pacman_tile;
msg.place2 = monster_tile;
update_server_window(msg);
pthread_rwlock_rdlock(&client_vector_mutex);
for(int i=0; i < total_clients; i++){
if(client_vector[i].client_fd > 0 && client_vector[i].client_fd != client_fd){
send(client_vector[i].client_fd, &msg, sizeof(struct move), 0);
}
}
pthread_rwlock_unlock(&client_vector_mutex);
return new_client;
}
////////////////////////////////////////////////////////////////////////////////
void client_disconnect(int client_fd){
close(client_fd);
//Actualizar o vetor dos clientes
pthread_rwlock_wrlock(&client_vector_mutex);
active_clients--;
for(int i=0; i < total_clients; i++){
if(client_vector[i].client_fd == client_fd){
client_vector[i].client_fd = -1;
client_vector[i].player_id = -1;
}
}
pthread_rwlock_unlock(&client_vector_mutex);
//Actualizar o board
struct move tiles_to_remove;
tiles_to_remove.type = REGULAR_MESSAGE;
int characters_to_remove = 2;
int stop = 0;
for(int i=0; i<board.width && stop == 0; i++){
for(int j=0; j<board.length && stop == 0; j++){
pthread_rwlock_wrlock(&line_lock[i]);
if(board.board_matrix[i][j].owner == client_fd){
if(board.board_matrix[i][j].type == PACMAN){
board.board_matrix[i][j].type = EMPTY;
board.board_matrix[i][j].owner = -1;
tiles_to_remove.place1 = board.board_matrix[i][j];
characters_to_remove--;
}
else if(board.board_matrix[i][j].type == MONSTER){
board.board_matrix[i][j].type = EMPTY;
board.board_matrix[i][j].owner = -1;
tiles_to_remove.place2 = board.board_matrix[i][j];
characters_to_remove--;
}
}
pthread_rwlock_unlock(&line_lock[i]);
if(characters_to_remove == 0)
stop = 1;
}
}
send_to_all_clients(tiles_to_remove);
update_server_window(tiles_to_remove);
deliver_or_trim_fruit();
}
////////////////////////////////////////////////////////////////////////////////
void* work_thread(void* arg){
struct client my_client = *((struct client*) arg);
int client_fd = my_client.client_fd;
int ret_val;
struct client_intent intent;
struct move server_command;
int valid_movement;
server_command.type = REGULAR_MESSAGE;
//Para contar o max move speed
struct timespec start_pacman, start_monster, stop_pacman, stop_monster;
double interval;
int count_pacman = 0;
int count_monster = 0;
//Para o inactivity jump
pthread_t inactivity_jump_pacman;
pthread_t inactivity_jump_monster;
int active_pacman = 0;
int active_monster = 0;
struct inactivity_arg inactive_pac_arg;
inactive_pac_arg.active = &active_pacman;
inactive_pac_arg.character = PACMAN;
inactive_pac_arg.client_fd = client_fd;
struct inactivity_arg inactive_mons_arg;
inactive_mons_arg.active = &active_monster;
inactive_mons_arg.character = MONSTER;
inactive_mons_arg.client_fd = client_fd;
pthread_create(&inactivity_jump_pacman, NULL, inactivity_jump_thread, &inactive_pac_arg);
pthread_create(&inactivity_jump_monster, NULL, inactivity_jump_thread, &inactive_mons_arg);
while(1){
//Lê as intenções do cliente
ret_val = read(client_fd, &intent, sizeof(struct client_intent));
//Client disconnect
if(ret_val <= 0){
printf("Client %d disconnected\n", my_client.player_id);
client_disconnect(client_fd);
active_pacman = -1;
active_monster = -1;
sleep(3); //Isto é para a as threads do inactivity jump acabarem
return NULL;
}
//Max movement speed restriction do Pacman
if(intent.character == PACMAN){
active_pacman = 1;
if(count_pacman == 0)
clock_gettime(CLOCK_REALTIME, &start_pacman);
count_pacman++;
if(count_pacman > 1){
clock_gettime(CLOCK_REALTIME, &stop_pacman);
interval = (stop_pacman.tv_sec - start_pacman.tv_sec)+(stop_pacman.tv_nsec - start_pacman.tv_nsec)*1e-9;
if(interval < 0.5)
continue;
else{
count_pacman = 1;
clock_gettime(CLOCK_REALTIME, &start_pacman);
}
}
}
//Max movement speed restriction do Monstro
else if(intent.character == MONSTER){
active_monster = 1;
if(count_monster == 0)
clock_gettime(CLOCK_REALTIME, &start_monster);
count_monster++;
if(count_monster > 1) {
clock_gettime(CLOCK_REALTIME, &stop_monster);
interval = (stop_monster.tv_sec - start_monster.tv_sec)+(stop_monster.tv_nsec - start_monster.tv_nsec)*1e-9;
if(interval < 0.5){
continue;
}
else{
count_monster = 1;
clock_gettime(CLOCK_REALTIME, &start_monster);
}
}
}
//Determina o efeito da ação pretendida do cliente
valid_movement = resolve(intent, &server_command);
if(valid_movement){
send_to_all_clients(server_command);
update_server_window(server_command);
}
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
int resolve(struct client_intent intent, struct move* effect){
int valid;
int checked = 0;
int current_x = intent.current_x;
int current_y = intent.current_y;
int wanted_x = intent.wanted_x;
int wanted_y = intent.wanted_y;
struct tile matrix_current, matrix_target;
pthread_rwlock_rdlock(&line_lock[current_y]);
matrix_current = board.board_matrix[current_y][current_x];
pthread_rwlock_unlock(&line_lock[current_y]);
//If the target is outside the board
if(wanted_x < 0 || wanted_x >= board.length || wanted_y < 0|| wanted_y >= board.width){
//Hitting left wall
if(wanted_x < 0){
//Is the bounce location inside the board?
if(current_x + 1 < board.length){
wanted_x = current_x + 1;
}
else{
return 0; //Bounce location outside the board
}
}
//Hitting right wall
else if(wanted_x >= board.length){
//Is the bounce location inside the board?
if(current_x - 1 >= 0){
wanted_x = current_x - 1;
}
else{
return 0;
}
}
//Hitting top wall
else if(wanted_y < 0){
//Is the bounce location inside the board?
if(current_y + 1 < board.width){
wanted_y = current_y + 1;
}
else{
return 0;
}
}
//Hitting bottom wall
else if(wanted_y >= board.width){
//Is the bounce location inside the board
if(current_y - 1 >= 0){
wanted_y = current_y - 1;
}
else{
return 0;
}
}
pthread_rwlock_rdlock(&line_lock[wanted_y]);
matrix_target = board.board_matrix[wanted_y][wanted_x];
pthread_rwlock_unlock(&line_lock[wanted_y]);
if(matrix_target.type != EMPTY)
return 0;
checked = 1;
}
//If the target is inside the board
if(checked == 0){
pthread_rwlock_rdlock(&line_lock[wanted_y]);
matrix_target = board.board_matrix[wanted_y][wanted_x];
pthread_rwlock_unlock(&line_lock[wanted_y]);
}
//Target is an empty tile -> swap
if( matrix_target.type == EMPTY){
swap_tiles(&matrix_current, &matrix_target);
valid = 1;
}
//I'm a pacman and target is a monster
else if( matrix_current.type == PACMAN && matrix_target.type == MONSTER){
//Characters are from the same player -> swap
if(matrix_current.owner == matrix_target.owner){
swap_tiles(&matrix_current, &matrix_target);
valid = 1;
}
//Normal Pacman jumping into opposing Monster -> Pacman gets eaten
else if( matrix_current.super_bites == 0){
struct tile character = matrix_current;
matrix_current.type = EMPTY;
matrix_current.owner = -1;
increase_score(matrix_target.owner);
respawn_character(character);
valid = 1;
}
//SuperPacman jumping into opposing Monster -> Monster gets eaten
else{
struct tile character = matrix_target;
matrix_target.type = EMPTY;
matrix_target.owner = -1;
matrix_current.super_bites--;
increase_score(matrix_current.owner);
swap_tiles(&matrix_current, &matrix_target);
respawn_character(character);
valid = 1;
}
}
//I'm a pacman and target is a pacman
else if( matrix_current.type == PACMAN && matrix_target.type == PACMAN){
swap_tiles(&matrix_current, &matrix_target);
valid = 1;
}
//I'm a monster and target is a pacman
else if( matrix_current.type == MONSTER && matrix_target.type == PACMAN){
//Characters are from the same player -> swap
if(matrix_current.owner == matrix_target.owner){
swap_tiles(&matrix_current, &matrix_target);
valid = 1;
}
//Monster jumping into opposing pacman -> Monster eats pacman
else if(matrix_target.super_bites == 0){
struct tile character = matrix_target;
matrix_target.type = EMPTY;
matrix_target.owner = -1;
increase_score(matrix_current.owner);
swap_tiles(&matrix_current, &matrix_target);
respawn_character(character);
valid = 1;
}
//Monster jumping into Super Pacman -> Monster gets eaten
else{
struct tile character = matrix_current;
matrix_current.type = EMPTY;
matrix_current.owner = -1;
matrix_target.super_bites--;
increase_score(matrix_target.owner);
respawn_character(character);
valid = 1;
}
}
//I'm a monster and target is a monster
else if(matrix_current.type == MONSTER && matrix_target.type == MONSTER){
swap_tiles(&matrix_current, &matrix_target);
valid = 1;
}
//Target is a fruit
else if( matrix_target.type == CHERRY || matrix_target.type == LEMON){
matrix_target.type = EMPTY;
if(matrix_current.type == PACMAN)
matrix_current.super_bites = 2;
increase_score(matrix_current.owner);
swap_tiles(&matrix_current, &matrix_target);
valid = 1;
pthread_t fruit_thread_id;
pthread_create(&fruit_thread_id, NULL, new_fruit_thread, NULL);
}
//Target is a brick
else if( matrix_target.type == BRICK){
//Hitting brick from the right
if(current_x > wanted_x){
//Is the bounce location inside the board?
if(current_x + 1 < board.length){
pthread_rwlock_rdlock(&line_lock[wanted_y]);
if(board.board_matrix[wanted_y][current_x+1].type == EMPTY){ //Yes, and it's free
wanted_x = current_x + 1;
valid = 1;
}
else{
wanted_x = current_x; //Yes, but it's occupied
valid = 0;
}
pthread_rwlock_unlock(&line_lock[wanted_y]);
}
else{
wanted_x = current_x;
valid = 0; //Bounce location outside of matrix
}
}
//Hitting brick from the left
else if(current_x < wanted_x){
//Is the bounce location inside the board?
if(current_x - 1 >= 0){
pthread_rwlock_rdlock(&line_lock[wanted_y]);
if(board.board_matrix[wanted_y][current_x-1].type == EMPTY){ //Yes, and it's free
wanted_x = current_x - 1;
valid = 1;
}
else{
wanted_x = current_x; //Yes, but it's occupied
valid = 0;
}
pthread_rwlock_unlock(&line_lock[wanted_y]);
}
else{
wanted_x = current_x;
valid = 0; //Bounce location outside of matrix
}
}
//Hitting brick from below
else if(current_y > wanted_y ){
//Is the bounce location inside the board and is the place free?
if(current_y + 1 < board.width){
pthread_rwlock_rdlock(&line_lock[current_y + 1]);
if(board.board_matrix[current_y + 1][wanted_x].type == EMPTY){ //Yes, and it's free
wanted_y = current_y + 1;
valid = 1;
}
else{
wanted_y = current_y;
valid = 0; //Yes, but it's occupied
}
pthread_rwlock_unlock(&line_lock[current_y + 1]);
}
else{
wanted_y = current_y;
valid = 0; //Bounce location outside of matrix
}
}
//Hitting brick from above
else if(current_y < wanted_y ){
if(current_y - 1 >= 0){
pthread_rwlock_rdlock(&line_lock[current_y - 1]);
if(board.board_matrix[current_y - 1][wanted_x].type == EMPTY){ //Yes, and it's free
wanted_y = current_y - 1;
valid = 1;
}
else{
wanted_y = current_y; //Yes, but it's occupied
valid = 0;
}
pthread_rwlock_unlock(&line_lock[current_y - 1]);
}
else{
wanted_y = current_y;
valid = 0; //Bounce location outside of matrix
}
}
if(valid){
pthread_rwlock_rdlock(&line_lock[wanted_y]);
matrix_target = board.board_matrix[wanted_y][wanted_x];
pthread_rwlock_unlock(&line_lock[wanted_y]);
swap_tiles(&matrix_current, &matrix_target);
}
}
//¯\_(ツ)_/¯
else{
valid = 0;
return valid;
}
if(valid){
//This will be sent to the client
effect->place1 = matrix_current;
effect->place2 = matrix_target;
//This is to update the matrix
pthread_rwlock_wrlock(&line_lock[current_y]);
board.board_matrix[current_y][current_x] = matrix_current;
pthread_rwlock_unlock(&line_lock[current_y]);
pthread_rwlock_wrlock(&line_lock[wanted_y]);
board.board_matrix[wanted_y][wanted_x] = matrix_target;
pthread_rwlock_unlock(&line_lock[wanted_y]);
}
return valid;
}
////////////////////////////////////////////////////////////////////////////////
void* inactivity_jump_thread(void* arg){
struct inactivity_arg in_arg = *((struct inactivity_arg*) arg);
int* active = in_arg.active;
int client_fd = in_arg.client_fd;
int character = in_arg.character;
struct tile current;
struct move msg;
msg.type = REGULAR_MESSAGE;
int free_x, free_y, old_x, old_y;
int counter = 0;
int stop;
while(1){
sleep(1);
//Client disconnected
if(*active == -1)
return NULL;
counter++;
//If the character becomes active resets the counter
if(*active == 1){
*active = 0;
counter = 0;
continue;
}
//If the counter reaches 30 seconds
if(counter == 30){
*active = 0;
counter = 0;
//Searches for the tile with the character from this player and makes it jump
stop = 0;
for(int i=0; i<board.width && stop == 0; i++){
pthread_rwlock_wrlock(&line_lock[i]);
for(int j=0; j<board.length && stop == 0; j++){
if(board.board_matrix[i][j].owner == client_fd && board.board_matrix[i][j].type == character){
current = board.board_matrix[i][j];
old_x = j;
old_y = i;
board.board_matrix[i][j].type = EMPTY;
board.board_matrix[i][j].owner = -1;
stop = 1;
}
}
pthread_rwlock_unlock(&line_lock[i]);
}
find_free_tile(&free_x, &free_y);
current.x = free_x;
current.y = free_y;
pthread_rwlock_wrlock(&line_lock[free_y]);
board.board_matrix[free_y][free_x] = current;
msg.place1 = board.board_matrix[free_y][free_x];
pthread_rwlock_unlock(&line_lock[free_y]);
pthread_rwlock_rdlock(&line_lock[old_y]);
msg.place2 = board.board_matrix[old_y][old_x];
pthread_rwlock_unlock(&line_lock[old_y]);
send_to_all_clients(msg);
update_server_window(msg);
}
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
void* score_thread(void* arg){
struct move msg;
msg.type = SCORE_MESSAGE;
while(1){
sleep(60);
if(active_clients == 0)
continue;
printf("Sending score\n");
if(active_clients == 1)
set_lone_client_score();
pthread_rwlock_rdlock(&client_vector_mutex);
for(int i=0; i<total_clients; i++){
if(client_vector[i].client_fd > 0){
//Determina o id e o score de cada cliente
msg.place1.owner = client_vector[i].player_id;
msg.place2.owner = client_vector[i].score;
//Isto é para o client saber que vem a primeira mensagem
if(i == 0)
msg.place1.type = FIRST;
else
msg.place1.type = -FIRST;
for(int j=0; j<total_clients; j++){ //Aqui não se mete a send_to_all_clients porque essa faria lock outra vez
//Envia para todos os clientes
if(client_vector[j].client_fd > 0){
send(client_vector[j].client_fd, &msg, sizeof(struct move), 0);
}
}
}
}
pthread_rwlock_unlock(&client_vector_mutex);
}
}
////////////////////////////////////////////////////////////////////////////////
void increase_score(int player_id){
pthread_rwlock_wrlock(&client_vector_mutex);
for(int i=0; i<total_clients; i++){
if(client_vector[i].player_id == player_id){
client_vector[i].score++;
pthread_rwlock_unlock(&client_vector_mutex);
return;
}
}
pthread_rwlock_unlock(&client_vector_mutex);
}
////////////////////////////////////////////////////////////////////////////////
void swap_tiles(struct tile* place1, struct tile* place2){
struct tile aux;
aux.type = place2->type;
aux.tile_color = place2->tile_color;
aux.owner = place2->owner;
aux.super_bites = place2->super_bites;
place2->type = place1->type;
place2->tile_color = place1->tile_color;
place2->owner = place1->owner;
place2->super_bites = place1->super_bites;
place1->type = aux.type;
place1->tile_color = aux.tile_color;
place1->owner = aux.owner;
place1->super_bites = aux.super_bites;
}
////////////////////////////////////////////////////////////////////////////////
void deliver_or_trim_fruit(){
//Entra um novo cliente -> Põe frutas
//Sai um cliente -> Retira frutas
int current_fruit, fruit_needed;
struct move message;
message.type = REGULAR_MESSAGE;
current_fruit = board.max_fruit;
pthread_mutex_lock(&fruit_mutex);
board.max_fruit = ( active_clients - 1 ) * 2;
pthread_mutex_unlock(&fruit_mutex);
if(board.max_fruit < 0)
board.max_fruit = 0;
fruit_needed = board.max_fruit - current_fruit;
//Incrementa a fruta porque entra um novo cliente
if(fruit_needed > 0){
int free_x, free_y;
int fruit_type;
for(int i=0; i<fruit_needed; i++){
find_free_tile(&free_x, &free_y);
fruit_type = rand() % 2;
pthread_rwlock_wrlock(&line_lock[free_y]);
if(fruit_type == 0)
board.board_matrix[free_y][free_x].type = CHERRY;
else
board.board_matrix[free_y][free_x].type = LEMON;
message.place1 = board.board_matrix[free_y][free_x];
pthread_rwlock_unlock(&line_lock[free_y]);