Skip to content

Commit

Permalink
init: set npm
Browse files Browse the repository at this point in the history
  • Loading branch information
dairinka committed Jan 15, 2023
1 parent f12211c commit f18164f
Show file tree
Hide file tree
Showing 11 changed files with 14,596 additions and 1,470 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
webpack.config.js
jsonServer.js
tsconfig.json
dist
23 changes: 23 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"plugins": ["prettier", "import", "@typescript-eslint"],
"extends": [
"plugin:prettier/recommended",
"prettier",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"env": {
"es6": true,
"browser": true,
"node": true
},
"rules": {
"no-debugger": "off",
"no-console": 0,
"@typescript-eslint/no-explicit-any": "warn"
}
}
86 changes: 85 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,86 @@
# Common settings that generally should always be used with your language specific settings

# Auto detect text files and perform LF normalization
* text=auto
* text=auto

#
# The above will handle all files NOT found below
#

# Documents
*.bibtex text diff=bibtex
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
*.md text diff=markdown
*.mdx text diff=markdown
*.tex text diff=tex
*.adoc text
*.textile text
*.mustache text
*.csv text
*.tab text
*.tsv text
*.txt text
*.sql text
*.epub diff=astextplain

# Graphics
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.tif binary
*.tiff binary
*.ico binary
# SVG treated as text by default.
*.svg text
# If you want to treat it as binary,
# use the following line instead.
# *.svg binary
*.eps binary

# Scripts
*.bash text eol=lf
*.fish text eol=lf
*.sh text eol=lf
*.zsh text eol=lf
*.zsh text eol=lf
*.js text eol=lf
*.ts text eol=lf
# These are explicitly windows files and should use crlf
*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf

# Serialisation
*.json text
*.toml text
*.xml text
*.yaml text
*.yml text

# Archives
*.7z binary
*.gz binary
*.tar binary
*.tgz binary
*.zip binary

# Text files where line endings should be preserved
*.patch -text

#
# Exclude files from exporting
#

.gitattributes export-ignore
.gitignore export-ignore
.gitkeep export-ignore
99 changes: 99 additions & 0 deletions jsonServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const jsonServer = require('json-server');

const db = {
garage: [
{
"name": "Tesla",
"color": "#e6e6fa",
"id": 1,
},
{
"name": "BMW",
"color": "#fede00",
"id": 2,
},
{
"name": "Mersedes",
"color": "#6c779f",
"id": 3,
},
{
"name": "Ford",
"color": "#ef3c40",
"id": 4,
},
],
winners: [
{
id: 1,
wins: 1,
time: 10,
}
]
};

const server = jsonServer.create();
const router = jsonServer.router(db);
const middlewares = jsonServer.defaults();

const PORT = 3000;

const state = { velocity: {}, blocked: {} };

server.use(middlewares);

server.patch('/engine', (req, res) => {
const { id, status } = req.query;

if (!id || !status || !/^(started)|(stopped)|(drive)$/.test(status)) {
return res.status(400).send('Wrong parameters: "id" should be any positive number, "status" should be "started", "stopped" or "drive"');
}

if (!db.garage.find(car => car.id === +id)) {
return res.status(404).send('Car with such id was not found in the garage.')
}

const distance = 500000;
if (status === 'drive') {
const velocity = state.velocity[id];

if (!velocity) return res.status(404).send('Engine parameters for car with such id was not found in the garage. Have you tried to set engine status to "started" before?');
if (state.blocked[id]) return res.status(429).send('Drive already in progress. You can\'t run drive for the same car twice while it\'s not stopped.');

state.blocked[id] = true;

const x = Math.round(distance / velocity);

if (new Date().getMilliseconds() % 3 === 0) {
setTimeout(() => {
delete state.velocity[id];
delete state.blocked[id];
res.header('Content-Type', 'application/json').status(500).send('Car has been stopped suddenly. It\'s engine was broken down.');
}, Math.random() * x ^ 0);
} else {
setTimeout(() => {
delete state.velocity[id];
delete state.blocked[id];
res.header('Content-Type', 'application/json').status(200).send(JSON.stringify({ success: true }));
}, x);
}
} else {
const x = req.query.speed ? +req.query.speed : Math.random() * 2000 ^ 0;

const velocity = status === 'started' ? Math.max(50, Math.random() * 200 ^ 0) : 0;

if (velocity) {
state.velocity[id] = velocity;
} else {
delete state.velocity[id];
delete state.blocked[id];
}

setTimeout(() => res.header('Content-Type', 'application/json').status(200).send(JSON.stringify({ velocity, distance })), x);
}
});

server.use(router);
server.listen(PORT, () => {
console.log('Server is running on port', PORT);
});
Loading

0 comments on commit f18164f

Please sign in to comment.