get/set prices for items

This commit is contained in:
Jacob Parker 2011-11-10 15:17:17 -05:00
parent 6868556ead
commit a859357adb
7 changed files with 43 additions and 5 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
a.out
db

View File

@ -70,6 +70,11 @@ void PosDb::acceptLogEntry(const LogEntry & l) {
break; break;
} }
case ET_PRICE: {
upc_to_price[l.PriceChange.upc] = l.PriceChange.price;
break;
}
case ET_REVERT: { case ET_REVERT: {
sto_it q = serial_to_object.find(l.Revert.revert_serial); sto_it q = serial_to_object.find(l.Revert.revert_serial);
if((q == serial_to_object.end()) || (q->second.type != ET_TRANS)) { if((q == serial_to_object.end()) || (q->second.type != ET_TRANS)) {
@ -172,7 +177,7 @@ uint64_t PosDb::deassociateHash(const SHA1Hash & hash) {
} else { } else {
LogEntry l; LogEntry l;
memset(&l, 0, sizeof(LogEntry)); memset(&l, 0, sizeof(LogEntry));
l.ts = (uint64_t)time(NULL); l.ts = (uint64_t)time(NULL);
l.type = ET_HASH; l.type = ET_HASH;
l.HashChange.uid = acct; l.HashChange.uid = acct;
l.HashChange.add = false; l.HashChange.add = false;
@ -184,6 +189,26 @@ uint64_t PosDb::deassociateHash(const SHA1Hash & hash) {
} }
} }
int32_t PosDb::getUPCPrice(UPC upc) {
uts_it it = upc_to_price.find(upc);
if (it == upc_to_price.end()) {
return 0;
}
return it->second;
}
uint64_t PosDb::setUPCPrice(UPC upc, int32_t price) {
LogEntry l;
memset(&l, 0, sizeof(LogEntry));
l.ts = (uint64_t)time(NULL);
l.type = ET_PRICE;
l.PriceChange.upc = upc;
l.PriceChange.price = price;
l.serial = log.nextSerial();
acceptLogEntry(l);
return log.writeEntry(l);
}
uint64_t PosDb::doTransaction(const SHA1Hash & hash, int32_t delta) { uint64_t PosDb::doTransaction(const SHA1Hash & hash, int32_t delta) {
// verify hash associated with something // verify hash associated with something
uint32_t acct = getAccountFromHash(hash); uint32_t acct = getAccountFromHash(hash);

View File

@ -23,6 +23,9 @@ public:
uint64_t associateHash(const SHA1Hash & hash, std::string account); uint64_t associateHash(const SHA1Hash & hash, std::string account);
uint64_t deassociateHash(const SHA1Hash & hash); uint64_t deassociateHash(const SHA1Hash & hash);
int32_t getUPCPrice(UPC upc);
uint64_t setUPCPrice(UPC upc, int32_t price);
uint64_t doTransaction(const SHA1Hash & hash, int32_t delta); uint64_t doTransaction(const SHA1Hash & hash, int32_t delta);
uint64_t doTransaction(const uint32_t account, int32_t delta); uint64_t doTransaction(const uint32_t account, int32_t delta);
uint64_t doTransaction(std::string account, int32_t delta); uint64_t doTransaction(std::string account, int32_t delta);
@ -38,6 +41,7 @@ private:
std::map<uint32_t, int32_t> account_to_balance; std::map<uint32_t, int32_t> account_to_balance;
std::map<UPC, int32_t, UPCLess> upc_to_stock; std::map<UPC, int32_t, UPCLess> upc_to_stock;
std::map<UPC, int32_t, UPCLess> upc_to_price;
std::map<uint64_t, LogEntry> serial_to_object; std::map<uint64_t, LogEntry> serial_to_object;

View File

@ -5,7 +5,7 @@
#include <string> #include <string>
#include <stdint.h> #include <stdint.h>
enum E_OBJ_TYPE { ET_TRANS, ET_HASH, ET_SALE, ET_REVERT }; enum E_OBJ_TYPE { ET_TRANS, ET_HASH, ET_SALE, ET_PRICE, ET_REVERT };
struct __attribute__ ((packed)) UPC { struct __attribute__ ((packed)) UPC {
uint64_t h, l; uint64_t h, l;
@ -43,6 +43,11 @@ struct __attribute__ ((aligned (64), packed)) LogEntry
int32_t delta; int32_t delta;
} StockChange; } StockChange;
struct __attribute__ ((packed)) {
UPC upc;
int32_t price;
} PriceChange;
struct __attribute__ ((packed)) { struct __attribute__ ((packed)) {
uint64_t revert_serial; uint64_t revert_serial;
} Revert; } Revert;

View File

@ -19,7 +19,6 @@ uint32_t NameServer::get_id (std::string name) {
std::cerr << "Doing LDAP lookup for \"" << name << "\".\n"; std::cerr << "Doing LDAP lookup for \"" << name << "\".\n";
struct passwd* pwd = getpwnam(name.c_str()); struct passwd* pwd = getpwnam(name.c_str());
if (!pwd) { if (!pwd) {
std::cerr << "Username " << name << " lookup fail!\n";
// TODO: oh fuck??? // TODO: oh fuck???
return 0; return 0;
} }

View File

@ -25,6 +25,8 @@ int main() {
UPC foo; UPC foo;
foo.l = 12345; foo.l = 12345;
foo.h = 67890; foo.h = 67890;
cout << db.setUPCPrice(foo, db.getUPCPrice(foo) + 1) << "\n";
cout << "price: " << db.getUPCPrice(foo) << "\n";
cout << db.doStockChange(foo, 100) << "\n"; cout << db.doStockChange(foo, 100) << "\n";
cout << db.getStock(foo) << "\n"; cout << db.getStock(foo) << "\n";
foo.l = 123; foo.l = 123;

View File

@ -75,7 +75,7 @@ void render () {
); );
static float x = 0; static float x = 0;
glTranslatef (-8,0,-20); glTranslatef (-8,0,-20);
glRotatef (x+=1.0, 0,1,0); glRotatef (x+=0.01, 0,1,0);
glRotatef (30.0, 1.0,0,0); glRotatef (30.0, 1.0,0,0);
GLint shader_tex_loc = glGetUniformLocation(shaders[0].prog_id, "texture"); GLint shader_tex_loc = glGetUniformLocation(shaders[0].prog_id, "texture");
@ -253,7 +253,7 @@ void GL_init (float w, float h) {
} }
int main () { int main () {
float scale = 3; float scale = 1;
float w = 480*scale; float w = 480*scale;
float h = 272*scale; float h = 272*scale;
if (!glfwInit () || !glfwOpenWindow (w, h, 8,8,8,0,32,0,GLFW_WINDOW)) { if (!glfwInit () || !glfwOpenWindow (w, h, 8,8,8,0,32,0,GLFW_WINDOW)) {