From: John Hodge Date: Wed, 5 Jan 2011 08:36:17 +0000 (+0800) Subject: More cleanup, implementing `dispense give` X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=7b65f9ffa8bf79403c71bb540e9a252826451903;p=tpg%2Fopendispense2.git More cleanup, implementing `dispense give` --- diff --git a/src/cokebank_basic/bank.c b/src/cokebank_basic/bank.c index d78f90f..dce089f 100644 --- a/src/cokebank_basic/bank.c +++ b/src/cokebank_basic/bank.c @@ -24,7 +24,7 @@ enum { }; // === PROTOTYPES === -static int GetUnixID(const char *Name); +static int GetUnixID(const char *Username); // === GLOBALS === tUser *gaBank_Users; @@ -32,13 +32,16 @@ tUser *gaBank_Users; FILE *gBank_File; // === CODE === -int Bank_GetUserByUnixID(int UnixID) +int Bank_GetUserByName(const char *Username) { - int i; + int i, uid; + + uid = GetUnixID(Username); + // Expensive search :( for( i = 0; i < giBank_NumUsers; i ++ ) { - if( gaBank_Users[i].UnixID == UnixID ) + if( gaBank_Users[i].UnixID == uid ) return i; } diff --git a/src/server/common.h b/src/server/common.h index 2704696..280cd4e 100644 --- a/src/server/common.h +++ b/src/server/common.h @@ -76,6 +76,7 @@ extern char *mkstr(const char *Format, ...); // --- Dispense --- extern int DispenseItem(int User, tItem *Item); +extern int DispenseGive(int SrcUser, int DestUser, int Ammount, const char *ReasonGiven); // --- Logging --- extern void Log_Error(const char *Format, ...); diff --git a/src/server/dispense.c b/src/server/dispense.c index ae6d42e..d574a1e 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -47,11 +47,29 @@ int DispenseItem(int User, tItem *Item) } // And log that it happened - Log_Info("Dispensed %s (%s:%i) for %s [cost %i, balance %i cents]", - Item->Name, handler->Name, Item->ID, - username, Item->Price, GetBalance(User) + Log_Info("%s dispensed %s (%s:%i) [cost %i, balance %i cents]", + username, Item->Name, handler->Name, Item->ID, + Item->Price, GetBalance(User) ); free( username ); return 0; // 0: EOK } + +/** + * \brief Give money from one user to another + */ +int DispenseGive(int SrcUser, int DestUser, int Ammount, const char *ReasonGiven) +{ + int ret; + if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch + + ret = Transfer( SrcUser, DestUser, Ammount, ReasonGiven ); + if(ret) return 2; // No Balance + + Log_Info("%s gave %i to %s (%s)", + GetUserName(SrcUser), Ammount, GetUserName(DestUser), ReasonGiven + ); + + return 0; +} diff --git a/src/server/server.c b/src/server/server.c index d7466a9..35f16eb 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -379,7 +379,6 @@ char *Server_Cmd_AUTOAUTH(tClient *Client, char *Args) */ char *Server_Cmd_ENUMITEMS(tClient *Client, char *Args) { -// int nItems = giNumItems; int retLen; int i; char *ret; @@ -513,12 +512,14 @@ char *Server_Cmd_GIVE(tClient *Client, char *Args) if( iAmmount <= 0 ) return strdup("407 Invalid Argument, ammount must be > zero\n"); // Do give - switch( Transfer(Client->UID, uid, iAmmount, reason) ) + switch( DispenseGive(Client->UID, uid, iAmmount, reason) ) { case 0: return strdup("200 Give OK\n"); - default: + case 2: return strdup("402 Poor You\n"); + default: + return strdup("500 Unknown error\n"); } }