Skip to content

Commit

Permalink
Naming changes and express server ready
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Mar 23, 2021
1 parent 02b4aa4 commit c2d28ed
Show file tree
Hide file tree
Showing 26 changed files with 178 additions and 114 deletions.
39 changes: 0 additions & 39 deletions app/src/account/storage.ts

This file was deleted.

8 changes: 3 additions & 5 deletions app/src/account/account.ts → app/src/accounts/account.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { SteamCommunity, TradeOfferManager, SteamUser } from '../untyped';
import SteamTotp from 'steam-totp';
import TradeProcessor from '../trading/processor';
import TradeProcessor from '../transactions/processor';
import { ICurrency, getCurrency } from '../steam/currency';
import { Offer } from '../trading/types';
import Storage from './storage';
import { Offer } from '../transactions/types';
import Logger from './logger';

const language = 'en';
Expand Down Expand Up @@ -32,10 +31,9 @@ export default class Account {
readonly manager = new TradeOfferManager({ steam: this.client, community: this.community, language });
readonly logger = Logger(this);
readonly trader = new TradeProcessor(this);
readonly storage = new Storage(this);

constructor(readonly options: AccountOptions) {
this.logger.info(`The account was created, waiting for login...`);
this.logger.info(`'${options.login.username}' was created, waiting for login...`);
}

login() {
Expand Down
69 changes: 69 additions & 0 deletions app/src/accounts/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Account, { AccountOptions } from './account';

const accounts = new Map<string, Account>();

export function getAll() {
return [true, [...accounts.values()].map((acc) => acc.options)];
}

export function getByName(name: string) {
return [true, accounts.get(name)];
}

export function create(options: AccountOptions) {
const { username } = options.login;
if (accounts.has(username)) {
return [false, 'Account already exists'];
} else {
const account = new Account(options);
accounts.set(username, account);
return [true, 'Created.'];
}
}

export function edit(name: string, { status, trading }: AccountOptions) {
const account = accounts.get(name);
if (!account) {
return [false, `Account doesn't exist`];
} else {
account.options.status = { ...account.options.status, ...status };
account.options.trading = { ...account.options.trading, ...trading };
account.logoff();
return [true, 'Modified.'];
}
}

export function remove(name: string) {
const account = accounts.get(name);
if (!account) {
return [false, `Account doesn't exist`];
} else {
account.logoff();
accounts.delete(name);
return [true, 'Deleted.'];
}
}

// export function add(acc: Account) {
// const exists = contains(acc.options.login.username);
// accounts.set(acc.options.login.username, acc);
// return exists;
// }

// export function remove(username: string): Account | undefined {
// const acc = get(username);
// acc && accounts.delete(username);
// return acc;
// }

// export function get(username: string) {
// return accounts.get(username);
// }

// export function getAll() {
// return [...accounts.values()];
// }

// export function contains(username: string) {
// return accounts.has(username);
// }
File renamed without changes.
16 changes: 12 additions & 4 deletions app/src/ads.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
function log(...msg: string[]) {
msg.forEach(msg => console.log(msg));
async function log(...msg: string[]) {
msg.forEach((msg) => console.log(msg));
console.log();
}

export function startup() {
log(
'This app was developed by Arthur Fiorette',
'Visit us on GitHub!',
'https://github.com/ArthurFiorette/steam-trader',
'',
'Loading...'
);
}

export function listening(port: any) {
log(
`We started at port '${port}'`,
'If you are using docker-compose you only need',
'to wait the web service starts itself.',
'If not, you are able to start it now.'
);
}
25 changes: 0 additions & 25 deletions app/src/controller/accounts.ts

This file was deleted.

1 change: 0 additions & 1 deletion app/src/controller/index.ts

This file was deleted.

19 changes: 3 additions & 16 deletions app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import config from '../config.json';
import * as Ads from './ads';
import Account from './account/account';
import app from './server';

import express from 'express';
import cors from 'cors';

import { Accounts } from './routes';
const PORT = process.env.PORT || 1228;

Ads.startup();

// const account = new Account(config);

// account.login();

const app = express();

app.use(cors());
app.use('/users', Accounts());

app.listen(process.env.PORT);
app.listen(PORT, () => Ads.listening(PORT));
15 changes: 0 additions & 15 deletions app/src/routes/accounts.ts

This file was deleted.

1 change: 0 additions & 1 deletion app/src/routes/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions app/src/routes/responses.ts

This file was deleted.

23 changes: 23 additions & 0 deletions app/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import express from 'express';
import cors from 'cors';
import setRoutes from './routes';

const app = express();

app.use(cors());
app.use((req, res, next) => {
const oldSend = res.send;
res.send = function ([status, response]: [boolean, any]) {
res.send = oldSend;
return res.send({
status: status ? 'Success' : 'Failure',
timestamp: new Date().toLocaleString(),
response
});
};
next();
});

setRoutes(app);

export default app;
8 changes: 8 additions & 0 deletions app/src/server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Express } from 'express';
import users from './users';
import ping from './ping';

export default function apply(app: Express) {
app.use('/users', users);
app.use('/ping', ping);
}
9 changes: 9 additions & 0 deletions app/src/server/routes/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router, json } from 'express';

const router = Router();

router.use(json());

router.use('/', (req, res) => res.send([true, 'Pong']));

export default router;
18 changes: 18 additions & 0 deletions app/src/server/routes/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Router, json } from 'express';
import { getAll, getByName, create, edit, remove } from '../../accounts/controller';

const router = Router();

router.use(json());

router.get('/', (_req, res) => res.send(getAll()));

router.post('/', (req, res) => res.send(create(req.body)));

router.get('/:name', (req, res) => res.send(getByName(req.params.name)));

router.put('/:name', (req, res) => res.send(edit(req.params.name, req.body)));

router.delete('/:name', (req, res) => res.send(remove(req.params.name)));

export default router;
2 changes: 1 addition & 1 deletion app/src/steam/market.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { ItemPrice, Item } from '../trading/types';
import { ItemPrice, Item } from '../transactions/types';
import Currency from './currency';

const { parse: cleanPrice } = Currency.DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextFunction } from '../../util/middleware';
import { getAllItemsPrice } from '../../steam/market';
import { AccountOptions } from '../../account/account';
import { AccountOptions } from '../../accounts/account';
import { ItemPrice, OfferContext } from '../types';
import { Reason } from '../processor';

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Account from '../account/account';
import Account from '../accounts/account';
import { Offer, OfferContext } from './types';
import logic from './logic';
import Pipeline from '../util/middleware';
import serialize from './serializer'

export type Processor = (offer: Offer) => Promise<Offer>;

Expand Down Expand Up @@ -32,15 +33,15 @@ export default class TradeProcessor {
}

async accept(offer: OfferContext, reason: Reason) {
const { storage, logger } = this.account;
const { logger } = this.account;
logger.info(`${reason}. Accepting trade ${offer.offer.id}...`);
await offer.offer.accept((err) => {
if (err) {
logger.error(`Catch an error while trying to accept the trade ${offer.offer.id}`, err);
return;
}
logger.info(`Accepted trade ${offer.offer.id}`);
storage.saveTransaction(offer, reason);
serialize(offer, reason);
});
}
}
Expand Down
27 changes: 27 additions & 0 deletions app/src/transactions/serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Item, OfferContext } from '../transactions/types';
import fs from 'fs';
import path from 'path';

const PATH = path.resolve(__dirname, '../../output/trades/');

export default function save(context: OfferContext, reason: string) {
const { account } = context.processor;
const name = account.options.login.username;
const { id } = context.offer;
const filePath = path.resolve(PATH, `${name}/${id}`);
account.logger.debug(`Saving transaction ${id}`);
fs.writeFileSync(filePath, stringifyTransaction(name, context, reason));
}

function stringifyTransaction(account: string, { offer, profit }: OfferContext, reason: string) {
const mapItems = (items: Item[]) => items.map((item) => item.market_hash_name);
return JSON.stringify({
account,
partner: offer.partner.getSteamID64(),
offerId: offer.id,
profit,
ourItems: mapItems(offer.itemsToGive),
theirItems: mapItems(offer.itemsToReceive),
reason
});
}
File renamed without changes.

0 comments on commit c2d28ed

Please sign in to comment.