Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <ncurses.h>
#include <vector>
#include <math.h>
std::vector<std::string> ids;
std::vector<std::string> names;
std::vector<std::string> prices;
int x = 0;
int y = 2;
int get_data(std::vector<std::string> data)
{
for (std::string x : data)
{
int counter = 0;
if (counter < 7)
{
std::string stat;
for(char e : x)
{
if (e == '=')
{
break;
}
else
{
stat = stat + e;
}
}
if (stat == "ID")
{
y = y + 2;
counter = 0;
std::string temp = "";
temp = temp + x[3];
if (x[4] == 0)
{}
else
{
temp = temp + x[4];
}
ids.push_back(temp);
}
else if (stat == "Description")
{
counter = counter + 1;
}
else if (stat == "Name")
{
int count = 5;
std::string iname = "";
do
{
iname = iname + x[count];
count = count + 1;
} while (x[count] != 0);
names.push_back(iname);
move(y, 30);
printw(x.c_str());
y = y + 1;
counter = counter + 1;
}
else if (stat == "Price")
{
int count = 6;
std::string iprice = "";
do
{
iprice = iprice + x[count];
count = count + 1;
}while (x[count] != 0);
prices.push_back(iprice);
move(y, 30);
printw(x.c_str());
y = y + 1;
counter = counter + 1;
}
else if (stat == "Equipped")
{
if (x[9] == '0')
{
move(y, 30);
printw("Equipped=No");
y = y + 1;
counter = counter + 1;
}
else if (x[9] == '1')
{
move(y, 30);
printw("Equipped=Yes");
y = y + 1;
counter = counter + 1;
}
}
else
{
move(y, 30);
printw(x.c_str());
y = y + 1;
counter = counter + 1;
}
}
}
int len = names.size();
if (len == 1)
{
names.push_back("empty");
names.push_back("empty");
names.push_back("empty");
}
else if (len == 2)
{
names.push_back("empty");
names.push_back("empty");
}
else if (len == 3)
{
names.push_back("empty");
}
else if (len > 3)
{}
}
int sell(int gold)
{
initscr();
noecho();
std::string s = "You have " + std::to_string(gold) + " gold.";
printw(s.c_str());
std::vector<std::string> item;
std::string query = "SELECT * FROM Items";
item = sql_call(query);
if (item.size() < 1)
{}
else
{
get_data(item);
}
y = 35;
mvprintw(y, 0, "Press 1, 2, 3 or 4 to sell an item or press ESC to exit the shop.");
int inp;
y = y + 2;
do{
inp = getch();
if (inp == 49)
{
//try{
if (names[0] == "sold")
{}
else if (names[0] == "empty")
{}
else
{
int total = gold + (std::stoi(prices[0]));
std::string msg = "Are you sure you want to sell your " + names[0] + "? You will be left with " + std::to_string(total) + " gold. Y or N.";
mvprintw(y, 0, msg.c_str());
int confirm = 0;
do
{
inp = getch();
if (inp == 110)
{
move(37, 0);
clrtoeol();
confirm = 1;
}
else if (inp == 121)
{
move(37, 0);
clrtoeol();
mvprintw(y, 0, ("You have sold your " + names[0]).c_str());
mvprintw(0, 0, ("You have " + std::to_string(total) + " gold.").c_str());
for (int i = 4; i < 9; i++)
{
move(i, 0);
clrtoeol();
}
std::string query = "DELETE FROM Items WHERE ID = " + ids[0];
sql_call(query);
names[0] = "sold";
confirm = 1;
}
}while (confirm != 1);
}
//} catch(){}
}
else if (inp == 50)
{
if (names[1] == "sold")
{}
else if (names[1] == "empty")
{}
else
{
int total = gold + (std::stoi(prices[1]));
std::string msg = "Are you sure you want to sell your " + names[1] + "? You will be left with " + std::to_string(total) + " gold. Y or N.";
mvprintw(y, 0, msg.c_str());
int confirm = 0;
do
{
inp = getch();
if (inp == 110)
{
move(37, 0);
clrtoeol();
confirm = 1;
}
else if (inp == 121)
{
move(37, 0);
clrtoeol();
mvprintw(y, 0, ("You have sold your " + names[1]).c_str());
mvprintw(0, 0, ("You have " + std::to_string(total) + " gold.").c_str());
for (int i = 11; i < 16; i++)
{
move(i, 0);
clrtoeol();
}
std::string query = "DELETE FROM Items WHERE ID = " + ids[1];
sql_call(query);
names[1] = "sold";
confirm = 1;
}
}while (confirm != 1);
}
}
else if (inp == 51)
{
if (names[2] == "sold")
{}
else if (names[2] == "empty")
{}
else
{
int total = gold + (std::stoi(prices[2]));
std::string msg = "Are you sure you want to sell your " + names[2] + "? You will be left with " + std::to_string(total) + " gold. Y or N.";
mvprintw(y, 0, msg.c_str());
int confirm = 0;
do
{
inp = getch();
if (inp == 110)
{
move(37, 0);
clrtoeol();
confirm = 1;
}
else if (inp == 121)
{
move(37, 0);
clrtoeol();
mvprintw(y, 0, ("You have sold your " + names[2]).c_str());
mvprintw(0, 0, ("You have " + std::to_string(total) + " gold.").c_str());
for (int i = 18; i < 23; i++)
{
move(i, 0);
clrtoeol();
}
std::string query = "DELETE FROM Items WHERE ID = " + ids[2];
sql_call(query);
names[2] = "sold";
confirm = 1;
}
}while (confirm != 1);
}
}
else if (inp == 52)
{
if (names[3] == "sold")
{}
else if (names[3] == "empty")
{}
else
{
int total = gold + (std::stoi(prices[3]));
std::string msg = "Are you sure you want to sell your " + names[3] + "? You will be left with " + std::to_string(total) + " gold. Y or N.";
mvprintw(y, 0, msg.c_str());
int confirm = 0;
do
{
inp = getch();
if (inp == 110)
{
move(37, 0);
clrtoeol();
confirm = 1;
}
else if (inp == 121)
{
move(37, 0);
clrtoeol();
mvprintw(y, 0, ("You have sold your " + names[3]).c_str());
mvprintw(0, 0, ("You have " + std::to_string(total) + " gold.").c_str());
for (int i = 25; i < 30; i++)
{
move(i, 0);
clrtoeol();
}
std::string query = "DELETE FROM Items WHERE ID = " + ids[3];
sql_call(query);
names[3] = "sold";
confirm = 1;
}
}while (confirm != 1);
}
}
}while (inp != 27);
endwin();
return 0;
}