-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
123 lines (102 loc) · 2.64 KB
/
main2.cpp
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
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <ncurses.h>
#include <sys/types.h>
#include <signal.h>
#include "player.h"
int main(void)
{
initscr();
noecho();
cbreak();
curs_set(0);
nodelay(stdscr, TRUE);
timeout(500);
keypad(stdscr, TRUE);
Semaphore join("joinGame");
if (join.sem == NULL)
{
printw("Nie można się połączyć z serwerem, wciśnij cokolwiek żeby kontynuować");
refresh();
getchar();
return 1;
}
join.post();
int fdJoin = shm_open("joinShm", O_RDWR, 0666);
int *joinShm = (int *)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fdJoin, 0);
napms(1000);
join.wait();
if (*joinShm >= 4)
{
printw("Nie mozna dolaczyc - limit graczy osiagniety");
refresh();
getchar();
return 2;
}
int myNumber = *(joinShm);
char name[8];
snprintf(name, 8, "%s%d\0", "player", myNumber + 1); //tworzy nazwę player1, player2...
Semaphore communicateWithSerwer(name);
int fd = shm_open(name, O_RDWR, 0666);
Player *shMemory = (Player *)mmap(NULL, sizeof(Player), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Player me(shMemory);
shMemory->PID = me.PID;
communicateWithSerwer.post();
printw("Oczekiwanie na pozostalych graczy");
refresh();
napms(1000);
communicateWithSerwer.wait();
while (1)
{
me.copyToPlayer(shMemory);
me.mapDisplay();
int choice = getch();
switch (choice)
{
case KEY_UP:
shMemory->dir = UP;
communicateWithSerwer.post();
break;
case KEY_DOWN:
shMemory->dir = DOWN;
communicateWithSerwer.post();
break;
case KEY_LEFT:
shMemory->dir = LEFT;
communicateWithSerwer.post();
break;
case KEY_RIGHT:
shMemory->dir = RIGHT;
communicateWithSerwer.post();
break;
case 'q':
me.exit = true;
break;
case 'Q':
me.exit = true;
break;
default:
break;
}
if (me.exit)
{
break;
}
if (kill(me.serwerPID, 0) != 0)
{
mvprintw((MAP_SIZE) / 2, (MAP_SIZE) / 2, "Utracono polaczenie z serwerem... Zamykanie...");
refresh();
napms(3000);
shm_unlink(name);
endwin();
return 3;
}
communicateWithSerwer.wait();
}
shm_unlink(name);
endwin();
return 0;
}