c9aa6b36dc7259a1883f39ff5d5ab6484f7a44fd
[tpg/opendispense2.git] / src / cokebank_basic / bank.c
1 /*
2  * OpenDispense 2
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  * - Cokebank (Basic Version)
5  *
6  * bank.c - Actual bank database
7  *
8  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
9  * for full details.
10  */
11 #include <stdio.h>
12
13 enum {
14         FLAG_TYPEMASK    = 0x03,
15         USER_FLAG_NORMAL = 0x00,
16         USER_FLAG_COKE   = 0x01,
17         USER_FLAG_WHEEL  = 0x02,
18         USER_FLAG_GOD    = 0x03
19 };
20
21 // === CODE ===
22 int Bank_GetUserByUnixID(int UnixUID)
23 {
24         // Expensive search :(
25         for( i = 0; i < giBank_NumUsers; i ++ )
26         {
27                 if( gaBank_Users[i].UnixID == UnixID )
28                         return i;
29         }
30
31         return -1;
32 }
33
34 int Bank_GetUserBalance(int ID)
35 {
36         if( ID < 0 || ID >= giBank_NumUsers )
37                 return INT_MIN;
38
39         return gaBank_Users[ID].Balance;
40 }
41
42 int Bank_AlterUserBalance(int ID, int Delta)
43 {
44         // Sanity
45         if( ID < 0 || ID >= giBank_NumUsers )
46                 return -1;
47
48         // Update
49         gaBank_Users[ID].Balance += Delta;
50
51         // Commit
52         fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
53         fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
54         
55         return 0;
56 }
57
58 int Bank_SetUserBalance(int ID, int Value)
59 {
60         // Sanity
61         if( ID < 0 || ID >= giBank_NumUsers )
62                 return -1;
63
64         // Update
65         gaBank_Users[ID].Balance = Value;
66         
67         // Commit
68         fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
69         fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
70         
71         return 0;
72 }
73
74 int Bank_GetMinAllowedBalance(int ID)
75 {
76         if( ID < 0 || ID >= giBank_NumUsers )
77                 return -1;
78
79         switch( gaBank_Users[ID].Flags & FLAG_TYPEMASK )
80         {
81         case USER_TYPE_NORMAL:  return     0;
82         case USER_TYPE_COKE:    return  -2000;
83         case USER_TYPE_WHEEL:   return -10000;
84         case USER_TYPE_GOD:     return INT_MIN;
85         default:        return 0;
86         }
87 }
88
89 /**
90  * \brief Create a new user in our database
91  */
92 int Bank_AddUser(int UnixID)
93 {
94         void    *tmp;
95
96         // Can has moar space plz?
97         tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0]));
98         if( !tmp )      return -1;
99         gaBank_Users = tmp;
100
101         // Crete new user
102         gaBank_Users[giBank_NumUsers].UnixID = UnixID;
103         gaBank_Users[giBank_NumUsers].Balance = 0;
104         gaBank_Users[giBank_NumUsers].Flags = 0;
105         
106         // Commit to file
107         fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET);
108         fwrite(gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File);
109
110         // Increment count
111         giBank_NumUsers ++;
112
113         return 0;
114 }

UCC git Repository :: git.ucc.asn.au