+++ /dev/null
-
-BIN := ../../cokebank.so
-OBJ := main.o
-
-CPPFLAGS :=
-CFLAGS := -Wall -Werror -g -fPIC
-LDFLAGS := -shared -Wl,-soname,cokebank.so
-
-.PHONY: all clean
-
-all: $(BIN)
-
-clean:
- $(RM) $(BIN) $(OBJ)
-
-$(BIN): $(OBJ)
- $(CC) $(LDFLAGS) -o $(BIN) $(OBJ)
-
-%.o: %.c
- $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
+++ /dev/null
-/*
- * OpenDispense 2
- * UCC (University [of WA] Computer Club) Electronic Accounting System
- *
- * cokebank.c - Coke-Bank management
- *
- * This file is licenced under the 3-clause BSD Licence. See the file COPYING
- * for full details.
- *
- * TODO: Make this a Dynamic Library and load it at runtime
- */
-#include <stdlib.h>
-#include <stdio.h>
-
-// === PROTOTYPES ===
-void Init_Cokebank(void);
- int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
- int GetBalance(int User);
-char *GetUserName(int User);
- int GetUserID(const char *Username);
- int GetUserAuth(const char *Username, const char *Password);
-
-// === CODE ===
-/**
- * \brief Load the cokebank database
- */
-void Init_Cokebank(void)
-{
-
-}
-
-/**
- * \brief Transfers money from one user to another
- * \param SourceUser Source user
- * \param DestUser Destination user
- * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
- * \param Reason Reason for the transfer (essentially a comment)
- * \return Boolean failure
- */
-int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
-{
- return 0;
-}
-
-/**
- * \brief Get the balance of the passed user
- */
-int GetBalance(int User)
-{
- return 0;
-}
-
-/**
- * \brief Return the name the passed user
- */
-char *GetUserName(int User)
-{
- return NULL;
-}
-
-/**
- * \brief Get the User ID of the named user
- */
-int GetUserID(const char *Username)
-{
- return -1;
-}
-
-/**
- * \brief Authenticate a user
- * \return User ID, or -1 if authentication failed
- */
-int GetUserAuth(const char *Username, const char *Password)
-{
- return -1;
-}
-
--- /dev/null
+
+BIN := ../../cokebank.so
+OBJ := main.o
+
+CPPFLAGS :=
+CFLAGS := -Wall -Werror -g -fPIC
+LDFLAGS := -shared -Wl,-soname,cokebank.so
+
+.PHONY: all clean
+
+all: $(BIN)
+
+clean:
+ $(RM) $(BIN) $(OBJ)
+
+$(BIN): $(OBJ)
+ $(CC) $(LDFLAGS) -o $(BIN) $(OBJ)
+
+%.o: %.c
+ $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
--- /dev/null
+/*
+ * OpenDispense 2
+ * UCC (University [of WA] Computer Club) Electronic Accounting System
+ * - Cokebank (Basic Version)
+ *
+ * bank.c - Actual bank database
+ *
+ * This file is licenced under the 3-clause BSD Licence. See the file COPYING
+ * for full details.
+ */
+#include <stdio.h>
+
+enum {
+ FLAG_TYPEMASK = 0x03,
+ USER_FLAG_NORMAL = 0x00,
+ USER_FLAG_COKE = 0x01,
+ USER_FLAG_WHEEL = 0x02,
+ USER_FLAG_GOD = 0x03
+};
+
+// === CODE ===
+int Bank_GetUserByUnixID(int UnixUID)
+{
+ // Expensive search :(
+ for( i = 0; i < giBank_NumUsers; i ++ )
+ {
+ if( gaBank_Users[i].UnixID == UnixID )
+ return i;
+ }
+
+ return -1;
+}
+
+int Bank_GetUserBalance(int ID)
+{
+ if( ID < 0 || ID >= giBank_NumUsers )
+ return INT_MIN;
+
+ return gaBank_Users[ID].Balance;
+}
+
+int Bank_AlterUserBalance(int ID, int Delta)
+{
+ // Sanity
+ if( ID < 0 || ID >= giBank_NumUsers )
+ return -1;
+
+ // Update
+ gaBank_Users[ID].Balance += Delta;
+
+ // Commit
+ fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
+ fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
+
+ return 0;
+}
+
+int Bank_SetUserBalance(int ID, int Value)
+{
+ // Sanity
+ if( ID < 0 || ID >= giBank_NumUsers )
+ return -1;
+
+ // Update
+ gaBank_Users[ID].Balance = Value;
+
+ // Commit
+ fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
+ fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
+
+ return 0;
+}
+
+int Bank_GetMinAllowedBalance(int ID)
+{
+ if( ID < 0 || ID >= giBank_NumUsers )
+ return -1;
+
+ switch( gaBank_Users[ID].Flags & FLAG_TYPEMASK )
+ {
+ case USER_TYPE_NORMAL: return 0;
+ case USER_TYPE_COKE: return -2000;
+ case USER_TYPE_WHEEL: return -10000;
+ case USER_TYPE_GOD: return INT_MIN;
+ default: return 0;
+ }
+}
+
+/**
+ * \brief Create a new user in our database
+ */
+int Bank_AddUser(int UnixID)
+{
+ void *tmp;
+
+ // Can has moar space plz?
+ tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0]));
+ if( !tmp ) return -1;
+ gaBank_Users = tmp;
+
+ // Crete new user
+ gaBank_Users[giBank_NumUsers].UnixID = UnixID;
+ gaBank_Users[giBank_NumUsers].Balance = 0;
+ gaBank_Users[giBank_NumUsers].Flags = 0;
+
+ // Commit to file
+ fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET);
+ fwrite(gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File);
+
+ // Increment count
+ giBank_NumUsers ++;
+
+ return 0;
+}
--- /dev/null
+/*
+ * OpenDispense 2
+ * UCC (University [of WA] Computer Club) Electronic Accounting System
+ *
+ * cokebank.c - Coke-Bank management
+ *
+ * This file is licenced under the 3-clause BSD Licence. See the file COPYING
+ * for full details.
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <pwd.h>
+#include <string.h>
+
+// === IMPORTS ===
+ int Bank_GetMinAllowedBalance(int ID);
+ int Bank_GetUserBalance(int ID);
+ int Bank_AlterUserBalance(int ID, int Delta);
+ int Bank_GetUserByUnixID(int UnixID);
+ int Bank_GetUserByName(const char *Name);
+ int Bank_AddUser(int UnixID);
+
+// === PROTOTYPES ===
+void Init_Cokebank(void);
+ int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
+ int GetBalance(int User);
+char *GetUserName(int User);
+ int GetUserID(const char *Username);
+ int GetUserAuth(const char *Username, const char *Password);
+
+// === CODE ===
+/**
+ * \brief Load the cokebank database
+ */
+void Init_Cokebank(void)
+{
+
+}
+
+/**
+ * \brief Transfers money from one user to another
+ * \param SourceUser Source user
+ * \param DestUser Destination user
+ * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
+ * \param Reason Reason for the transfer (essentially a comment)
+ * \return Boolean failure
+ */
+int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
+{
+ if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
+ return 1;
+ if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) )
+ return 1;
+ Bank_AlterUserBalance(DestUser, Ammount);
+ Bank_AlterUserBalance(SourceUser, -Ammount);
+ return 0;
+}
+
+/**
+ * \brief Get the balance of the passed user
+ */
+int GetBalance(int User)
+{
+ return 0;
+}
+
+/**
+ * \brief Return the name the passed user
+ */
+char *GetUserName(int User)
+{
+ return NULL;
+}
+
+/**
+ * \brief Get the User ID of the named user
+ */
+int GetUserID(const char *Username)
+{
+ struct passwd *pwd;
+ int ret;
+
+ // Get user ID
+ pwd = getpwnam(Username);
+ if( !pwd ) {
+ return -1;
+ }
+
+ // Get internal ID (or create new user)
+ ret = Bank_GetUserByUnixID(pwd->pw_uid);
+ if( ret == -1 ) {
+ ret = Bank_AddUser(pwd->pw_uid);
+ }
+
+ return ret;
+}
+
+/**
+ * \brief Authenticate a user
+ * \return User ID, or -1 if authentication failed
+ */
+int GetUserAuth(const char *Username, const char *Password)
+{
+ if( strcmp(Username, "test") == 0 )
+ return Bank_GetUserByName("test");
+ return -1;
+}
+
--- /dev/null
+
+BIN := ../../cokebank.so
+OBJ := main.o
+
+CPPFLAGS :=
+CFLAGS := -Wall -Werror -g -fPIC
+LDFLAGS := -shared -Wl,-soname,cokebank.so
+
+.PHONY: all clean
+
+all: $(BIN)
+
+clean:
+ $(RM) $(BIN) $(OBJ)
+
+$(BIN): $(OBJ)
+ $(CC) $(LDFLAGS) -o $(BIN) $(OBJ)
+
+%.o: %.c
+ $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
--- /dev/null
+/*
+ * OpenDispense 2
+ * UCC (University [of WA] Computer Club) Electronic Accounting System
+ *
+ * cokebank.c - Coke-Bank management
+ *
+ * This file is licenced under the 3-clause BSD Licence. See the file COPYING
+ * for full details.
+ *
+ * TODO: Make this a Dynamic Library and load it at runtime
+ */
+#include <stdlib.h>
+#include <stdio.h>
+
+// === PROTOTYPES ===
+void Init_Cokebank(void);
+ int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
+ int GetBalance(int User);
+char *GetUserName(int User);
+ int GetUserID(const char *Username);
+ int GetUserAuth(const char *Username, const char *Password);
+
+// === CODE ===
+/**
+ * \brief Load the cokebank database
+ */
+void Init_Cokebank(void)
+{
+
+}
+
+/**
+ * \brief Transfers money from one user to another
+ * \param SourceUser Source user
+ * \param DestUser Destination user
+ * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
+ * \param Reason Reason for the transfer (essentially a comment)
+ * \return Boolean failure
+ */
+int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
+{
+ return 0;
+}
+
+/**
+ * \brief Get the balance of the passed user
+ */
+int GetBalance(int User)
+{
+ return 0;
+}
+
+/**
+ * \brief Return the name the passed user
+ */
+char *GetUserName(int User)
+{
+ return NULL;
+}
+
+/**
+ * \brief Get the User ID of the named user
+ */
+int GetUserID(const char *Username)
+{
+ return -1;
+}
+
+/**
+ * \brief Authenticate a user
+ * \return User ID, or -1 if authentication failed
+ */
+int GetUserAuth(const char *Username, const char *Password)
+{
+ return -1;
+}
+