-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
14,596 additions
and
1,470 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
webpack.config.js | ||
jsonServer.js | ||
tsconfig.json | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.