Server funzionante

This commit is contained in:
Claudio Maggioni 2018-01-01 14:14:05 +01:00
parent e5ab042be3
commit e4296187ee
3 changed files with 51 additions and 11 deletions

View file

@ -15,9 +15,11 @@ gamedata_t* gamedata_new() {
perror("gamedata malloc() creation error: ");
}
srand(time(NULL));
self->chf = 0;
self->bitto = 500;
self->flour = 200;
self->bitto = 200;
self->flour = 400;
self->pizzoccheri = 0;
self->chf_unit = 0.5;
return self;
@ -33,11 +35,11 @@ void gamedata_print_status(gamedata_t *self, FILE* out) {
fprintf(out, "UNIT PRICE %.02f CHF, PROFIT %.02f CHF\n", self->chf_unit, self->chf);
}
#define Q_BITTO_FOR_PIZZOCCHERO 1
#define Q_FLOUR_FOR_PIZZOCCHERO 2
#define Q_BITTO_UNIT 1
#define Q_FLOUR_UNIT 2
bool gamedata_make_pizzoccheri(gamedata_t *self, unsigned int q) {
int q_flour = q * Q_FLOUR_FOR_PIZZOCCHERO;
int q_bitto = q * Q_BITTO_FOR_PIZZOCCHERO;
int q_flour = q * Q_FLOUR_UNIT;
int q_bitto = q * Q_BITTO_UNIT;
if(self->flour < q_flour || self->bitto < q_bitto) {
return false;
@ -75,3 +77,29 @@ bool gamedata_buy_flour(gamedata_t *self, unsigned int q) {
self->flour += q;
return true;
}
unsigned int gamedata_sell_pizzoccheri(gamedata_t *self) {
if(self->pizzoccheri == 0) {
return 0;
}
int sold = 5 * (-self->chf_unit + 1 + 2 * (double)rand() / (double)RAND_MAX);
if(sold <= 0) {
return 0;
}
if(sold > self->pizzoccheri) {
sold = self->pizzoccheri;
}
self->pizzoccheri -= sold;
self->chf += sold * self->chf_unit;
return sold;
}
bool gamedata_set_unit_price(gamedata_t *self, double price) {
if(price < 0) return false;
self->chf_unit = price;
return true;
}

View file

@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
typedef struct _gamedata gamedata_t;
@ -13,5 +14,7 @@ extern bool gamedata_buy_bitto(gamedata_t*, unsigned int);
extern bool gamedata_buy_flour(gamedata_t*, unsigned int);
extern void gamedata_print_status(gamedata_t*, FILE*);
extern bool gamedata_make_pizzoccheri(gamedata_t*, unsigned int);
extern unsigned int gamedata_sell_pizzoccheri(gamedata_t*);
extern bool gamedata_set_unit_price(gamedata_t*, double);
#endif

View file

@ -86,22 +86,31 @@ int main(int argc, char** argv) {
double d;
} arg1;
if(sscanf(command, "MAKE %u\n", &(arg1.u)) == 1) {
if(sscanf(command, "make %u\n", &(arg1.u)) == 1) {
bool status = gamedata_make_pizzoccheri(curr_data, arg1.u);
fprintf(out, status ? "OK Made.\n" : "KO 1 Insufficient resources.\n");
}
else if(sscanf(command, "BUY BITTO %u", &(arg1.u)) == 1) {
else if(prefix("sell", command)) {
unsigned int sold = gamedata_sell_pizzoccheri(curr_data);
fprintf(out, "OK %d sold.\n", sold);
}
else if(sscanf(command, "set price %lf\n", &(arg1.d)) == 1) {
bool status = gamedata_set_unit_price(curr_data, arg1.d);
fprintf(out, status ? "OK The price of one pizzocchero is now at %.02lf CHF.\n"
: "KO %.02lf CHF is not a valid price.\n", arg1.d);
}
else if(sscanf(command, "buy bitto %u", &(arg1.u)) == 1) {
bool status = gamedata_buy_bitto(curr_data, arg1.u);
fprintf(out, status ? "OK Purchased.\n" : "KO 2 Insufficient funds\n");
}
else if(sscanf(command, "BUY FLOUR %u", &(arg1.u)) == 1) {
else if(sscanf(command, "buy flour %u", &(arg1.u)) == 1) {
bool status = gamedata_buy_flour(curr_data, arg1.u);
fprintf(out, status ? "OK Purchased.\n" : "KO 3 Insufficient funds\n");
}
else if(prefix("STATUS", command)) {
else if(prefix("status", command)) {
gamedata_print_status(curr_data, out);
}
else if(prefix("EXIT", command)) {
else if(prefix("exit", command)) {
fprintf(out, "OK Closing...\n");
break;
}