-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstan.c
195 lines (159 loc) · 5.08 KB
/
stan.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <math.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <sys/shm.h>
/*typy komunikatow w kolejkach KOM_GR1 i KOM_GR2*/
#define ATAK 1
#define TWORZ 2
#define STAN 3
#define KONIEC 4
#define ZAKONCZ 5
#define NIEDOST_SUROWCE 6
#define BLEDNY_ATAK 7
#define PORAZKA_ATAK 8
#define PORAZKA_OBRONA 9
#define SKUTECZNY_ATAK 10
#define SKUTECZNA_OBRONA 11
#define STRATY_ATAK 12
#define STRATY_OBRONA 13
#define PODDAJSIE 14
#define ZAPYTANIE 16
#define ODPOWIEDZ 17
/* typy komunikatow miedzy klientem a outputem */
#define ID 15
/*typy komunikatow w kolejce ID_KOM_INIC*/
#define ROZPOCZNIJ 1
#define AKCEPTUJ 2
/* key kolejek */
#define GR1_QUEUE_KEY 1
#define GR2_QUEUE_KEY 2
#define SIGKILL 9
/* struktury */
typedef struct Game_data_struct{
int light_infantry;
int heavy_infantry;
int cavalry;
int workers;
int stocks;
int victory_points;
int winner;
}Game_data_struct;
typedef struct Init_data_struct {
int id_kolejki_kom;
int id_gracza;
}Init_data_struct;
typedef struct Game_message {
long mtype;
struct Game_data_struct game_data;
}Game_message;
typedef struct Init_message {
long mtype;
struct Init_data_struct init_data;
}Init_message;
/* koniec struktur */
/* funkcje */
void show_player(Game_data_struct player){
printf("Statystyki gracza\n");
printf("Lekka piechota: %d\t",player.light_infantry);
printf("Ciezka piechota: %d\n",player.heavy_infantry);
printf("Jazda: %d\t\t",player.cavalry);
printf("Robotnicy: %d\n",player.workers);
printf("Surowce: %d\t\t",player.stocks);
printf("Punkty zwyciestwa: %d\n",player.victory_points);
printf("\n");
}
void show_casualties(Game_data_struct player){
printf("\n");
printf("Lekka piechota: %d\n",player.light_infantry);
printf("Ciezka piechota: %d\n",player.heavy_infantry);
printf("Jazda: %d\n",player.cavalry);
printf("\n");
}
void koniec(int sleep_time,int queue_id){
msgctl(queue_id,IPC_RMID,0);
sleep(sleep_time);
kill(0,SIGKILL);
}
/* koniec funkcji */
int main(int args, char* argv[]){
if(args<2){
printf("NIEWLASCIWA LICZBA ARGUMENTOW\n");
exit(1);
}
int game_queue_key=atoi(argv[1]);
int game_queue_id=msgget(game_queue_key,IPC_CREAT|0664);
if(game_queue_id==-1){
perror("BLAD PRZY TWORZENIU KOLEJKI GRY");
exit(1);
}
Game_message message;
Init_message init_message;
msgrcv(game_queue_id,&init_message, sizeof(init_message.init_data),ID,0);
int id_gracza=init_message.init_data.id_gracza;
while(1){
msgrcv(game_queue_id,&message, sizeof(message.game_data),0,0);
if(message.mtype==STAN){
show_player(message.game_data);
}
else if(message.mtype==BLEDNY_ATAK){
printf("\033[2J\033[1;1H");
printf("WYKONALES BLEDNY ATAK\n");
}
else if(message.mtype==NIEDOST_SUROWCE){
printf("\033[2J\033[1;1H");
printf("NIE MASZ WYSTARCZAJACEJ ILOSCI SUROWCOW\n");
}
else if(message.mtype==SKUTECZNY_ATAK){
printf("\033[2J\033[1;1H");
printf("PRZEPROWADZONO SKUTECZNY ATAK\n");
}
else if(message.mtype==SKUTECZNA_OBRONA){
printf("\033[2J\033[1;1H");
printf("PRZEPROWADZONO SKUTECZNA OBRONE\n");
}
else if(message.mtype==PORAZKA_ATAK){
printf("\033[2J\033[1;1H");
printf("ARMIA ZOSTALA ROZGROMIONA W TRAKCIE ATAKU\n");
}
else if(message.mtype==PORAZKA_OBRONA){
printf("\033[2J\033[1;1H");
printf("ARMIA ZOSTALA ROZGROMIONA W TRAKCIE OBRONY\n");
}
else if(message.mtype==STRATY_ATAK){
printf("\033[2J\033[1;1H");
printf("W CZASIE ATAKU ODNIESIONO NASTEPUJACE STRATY:\n");
show_casualties(message.game_data);
}
else if(message.mtype==STRATY_OBRONA){
printf("\033[2J\033[1;1H");
printf("W CZASIE OBRONY ODNIESIONO NASTEPUJACE STRATY:\n");
show_casualties(message.game_data);
}
else if(message.mtype==KONIEC){
printf("\033[2J\033[1;1H");
printf("SERWER NAGLE PRZESTAL DZIALAC\nPROGRAM ZAKONCZY SIE ZA 3 SEKUNDY\n");
koniec(3,game_queue_id);
}
else if(message.mtype==ZAKONCZ){
printf("\033[2J\033[1;1H");
if(message.game_data.winner==id_gracza)
printf("WYGRANA\n");
else
printf("PRZEGRANA\n");
printf("PROGRAM ZAKONCZY SIE ZA 3 sekundy\n");
koniec(3,game_queue_id);
}
else if(message.mtype==PODDAJSIE){
printf("\033[2J\033[1;1H");
printf("PODDALES SIE\nPROGRAM ZAKONCZY SIE ZA 3 SEKUNDY\n");
koniec(3,game_queue_id);
}
}
}