Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
131 commits
Select commit Hold shift + click to select a range
01f3b39
Update app.js
douglasdriving Oct 26, 2022
e2a3214
added db connection
douglasdriving Oct 26, 2022
e9e8195
Update app.js
douglasdriving Oct 26, 2022
bf6adfc
Update app.js
douglasdriving Oct 26, 2022
8408eef
some installs might help???
douglasdriving Oct 26, 2022
8b7e677
Update app.js
douglasdriving Oct 26, 2022
496641b
Update app.js
douglasdriving Oct 26, 2022
bd1d8e5
db initialization functions
douglasdriving Oct 26, 2022
62aac14
more test routes
douglasdriving Oct 26, 2022
7235dfd
Update app.js
douglasdriving Oct 26, 2022
151baf3
Create dev branch
douglasdriving Oct 27, 2022
d02d5ab
some file structure
douglasdriving Oct 27, 2022
4255890
connect to local db if not in heroku
douglasdriving Oct 27, 2022
5a1a94b
dotENV added
douglasdriving Oct 27, 2022
0130846
user paths done
douglasdriving Oct 27, 2022
377e2d6
Added create room route
douglasdriving Oct 28, 2022
968c74f
Join room route
douglasdriving Oct 28, 2022
a926543
progress on add scenario route
douglasdriving Oct 28, 2022
871256a
upload scenario route progress
douglasdriving Oct 28, 2022
96d97a8
scenario posting
douglasdriving Oct 31, 2022
3eeb2bf
scenario posting accounts for ends
douglasdriving Oct 31, 2022
e8da14a
get available rooms route
douglasdriving Oct 31, 2022
94a9768
get user room route
douglasdriving Nov 1, 2022
110b47c
get archive route
douglasdriving Nov 1, 2022
c7057fa
Cleanup
douglasdriving Nov 1, 2022
c914c3c
get room info route - back alive
douglasdriving Nov 1, 2022
fc4c1b5
refactoring route functions progress
douglasdriving Nov 1, 2022
b4009be
more cleanups
douglasdriving Nov 1, 2022
8afb10a
transaction func update
douglasdriving Nov 2, 2022
9688102
room router cleanup
douglasdriving Nov 2, 2022
4c024a5
cleanup
douglasdriving Nov 2, 2022
4bd9130
clean
douglasdriving Nov 2, 2022
dd0565c
Update userRouter.js
douglasdriving Nov 2, 2022
bc87f86
query return data fixes
douglasdriving Nov 4, 2022
71c1f90
login routes and jwt implemented
douglasdriving Nov 8, 2022
b043139
password hashin
douglasdriving Nov 8, 2022
33c7728
auto login
douglasdriving Nov 8, 2022
daa3c17
hotfix on create room path
douglasdriving Nov 8, 2022
e2aad6b
another hotfix
douglasdriving Nov 8, 2022
a343fc6
backend route fix for token requests
douglasdriving Nov 10, 2022
3c80968
bugfix
douglasdriving Nov 10, 2022
5dfd095
better error responses from server
douglasdriving Nov 10, 2022
bdf6533
Update scenarioRouter.js
douglasdriving Nov 10, 2022
fe964a8
error handling on create user
douglasdriving Nov 10, 2022
54f58fb
improved backend checks create user
douglasdriving Nov 10, 2022
254689b
push notifications added
douglasdriving Nov 11, 2022
b091de1
Merge branch 'main' into dev
douglasdriving Nov 11, 2022
af80f93
bugfix
douglasdriving Nov 16, 2022
4319c30
push notification fix
douglasdriving Nov 16, 2022
148e6b5
updated available rooms query
douglasdriving Nov 17, 2022
5066526
turn change on deadline
douglasdriving Nov 17, 2022
e872b43
Merge branch 'dev'
douglasdriving Nov 17, 2022
0ea560d
strike system implemented
douglasdriving Nov 18, 2022
53ef4fc
Merge branch 'dev'
douglasdriving Nov 18, 2022
52a4128
strikes returned with room data
douglasdriving Nov 18, 2022
c37d8aa
notification data fix
douglasdriving Nov 19, 2022
4e6898c
notification hotfixing
douglasdriving Nov 19, 2022
7f5574d
Add a check to make sure really is full
douglasdriving Nov 21, 2022
e3c85bc
sätt turn end rätt
douglasdriving Nov 21, 2022
20f0a27
Update roomRouter.js
douglasdriving Nov 21, 2022
1897c3d
room correction fix for new rooms
douglasdriving Nov 22, 2022
6c8a3ad
Update roomRouter.js
douglasdriving Dec 1, 2022
63a243b
added route for fetching user chars
douglasdriving Dec 3, 2022
58c1370
begin work on input validation
douglasdriving Dec 9, 2022
01659a5
input validation
douglasdriving Dec 11, 2022
670f538
correct feedback on room creation length
douglasdriving Dec 11, 2022
2bf856d
added test job to schedule in heroky
douglasdriving Dec 12, 2022
64e2aad
created auto room deadline clean function
douglasdriving Dec 12, 2022
d70d893
bugfix to deadline schedule function
douglasdriving Dec 12, 2022
4335b8c
input validation added dots
douglasdriving Dec 12, 2022
c911993
start on password reset flow
douglasdriving Dec 12, 2022
9f8b5b0
revert changes for email var
douglasdriving Dec 13, 2022
3b60050
Kick new players if they dont write in 30
douglasdriving Dec 13, 2022
6deb593
user request bugs, might fix?
douglasdriving Dec 13, 2022
fe291d8
possible fix to user fetch backend
douglasdriving Dec 13, 2022
83647cf
quickfix for writing user query correctly
douglasdriving Dec 13, 2022
614c0e3
Merge branch 'main' into dev
douglasdriving Dec 13, 2022
b4f2f95
prevent user from getting rooms that they are kicked from
douglasdriving Dec 13, 2022
916aafd
fixed bugg: players are immediately kicked when joining a new room
douglasdriving Dec 13, 2022
d175504
allow - character
douglasdriving Dec 14, 2022
f1206bf
corrected deadline passed function
douglasdriving Dec 23, 2022
beae49f
scenario add fix
douglasdriving Dec 23, 2022
2c41d17
fixed passed turn bug
douglasdriving Dec 23, 2022
a96b2a5
create ending works
douglasdriving Dec 23, 2022
27a6473
Update scenarioRouter.js
douglasdriving Dec 23, 2022
caf1db7
remove 500 char limit on start scenario
douglasdriving Dec 25, 2022
ddb8a41
room deadline request rout
douglasdriving Jan 16, 2023
ae1c42c
+ ongoing rooms query
douglasdriving Jan 23, 2023
c2518c7
Added Feed Rout
douglasdriving Jan 24, 2023
aaae9af
added a random prompt route
douglasdriving Jan 27, 2023
3bcaa2d
added user stat route
douglasdriving Feb 1, 2023
7d8ed80
fixed db function for new db node structure
douglasdriving Feb 3, 2023
ed72709
Updated routes to work when nodes can be empty
douglasdriving Feb 3, 2023
d9f2ac7
getting prompts back on room data request
douglasdriving Feb 3, 2023
fed5a5e
worked on new node post route
douglasdriving Feb 4, 2023
bdd7c7c
färdigställe post node route (buggfix)
douglasdriving Feb 6, 2023
87b9218
scenario add route: checks done
douglasdriving Feb 6, 2023
86a2c5f
create node route fix. now returns prompt
douglasdriving Feb 6, 2023
448296c
post scenario transaction done
douglasdriving Feb 6, 2023
cf9915b
add scenario route now works
douglasdriving Feb 6, 2023
d22f7f8
feed timestamp correction
douglasdriving Feb 6, 2023
8ab0703
new route to get camp data
douglasdriving Feb 6, 2023
2540f30
get active camps route
douglasdriving Feb 6, 2023
6efe1ef
get player camps route
douglasdriving Feb 6, 2023
d4bd202
create camp query, began work
douglasdriving Feb 6, 2023
8df982e
finalized camp post route
douglasdriving Feb 8, 2023
a6a9de9
camp routes done
douglasdriving Feb 8, 2023
1f72f4f
get stats route updated for new node db structure
douglasdriving Feb 8, 2023
6fe3701
old room function fix
douglasdriving Feb 8, 2023
76527d4
added new archive route
douglasdriving Feb 9, 2023
0eabd3b
bugs in node posting order
douglasdriving Feb 10, 2023
b776a88
fix player stats query
douglasdriving Feb 10, 2023
fc51054
added route to retrieve latest news
douglasdriving Feb 13, 2023
83f1fd9
new notifications when someone uploads a scenario
douglasdriving Feb 14, 2023
8d0cbbd
tog bort dubblering av expo tokens vid push
douglasdriving Feb 14, 2023
bda162d
like/dislike routes added
douglasdriving Feb 15, 2023
a9825b4
get node likes with camp data
douglasdriving Feb 15, 2023
fa8e8a9
new login route using google token
douglasdriving Feb 17, 2023
7eeaa9f
added route to set display name
douglasdriving Feb 17, 2023
fd8a9cf
route for updating expo token
douglasdriving Feb 21, 2023
0bb6f8f
auth middleware uppdaterad on tillagd
douglasdriving Feb 21, 2023
c72b73c
expo token check buggfix
douglasdriving Feb 21, 2023
1d6ab36
Update userRouter.js
douglasdriving Feb 22, 2023
00136d8
bugfixes with user id in auth
douglasdriving Feb 24, 2023
64d299b
stamp logins in db
douglasdriving Feb 24, 2023
77b242d
fixed route for fetching player rooms
douglasdriving Feb 28, 2023
b6fa2bd
player stats only count finished nodes
douglasdriving Feb 28, 2023
9eaff16
my camps route fix
douglasdriving Feb 28, 2023
0ac010b
open rooms route fix
douglasdriving Feb 28, 2023
18c68dd
accepts camps without a description
douglasdriving Mar 8, 2023
21eecee
bugfix when uploading camp without description
douglasdriving Mar 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
node_modules
node_modules
.env
.env.test
50 changes: 36 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
const express = require('express'); // import express module (simplifies routing/requests, among other things)
const app = express(); // create an instance of the express module (app is the conventional variable name used)
const fetch = require('node-fetch'); // import node-fetch (enables the fetch API to be used server-side)
const PORT = process.env.PORT || 5000; // use either the host env var port (PORT) provided by Heroku or the local port (5000) on your machine
//import libs
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;
require('dotenv').config()

app.get('/', (req, res) => { // send a get request to root directory ('/' is this file (app.js))
fetch('https://www.boredapi.com/api/activity') // fetch activity from bored API - https://www.boredapi.com/about
.then(res => res.json()) // return a promise containing the response
.then(json => res.send(`<h1>Today's Activity: ${json.activity}!</h1>`)) // extract the JSON body content from the response (specifically the activity value) and sends it to the client
.catch(function(err){ // catch any errors
console.log(err); // log errors to the console
})
//logging middleware
const morgan = require('morgan');
app.use(morgan('tiny'));

//error handling for dev environment
if (process.env.NODE_ENV === 'development') {
const errorhandler = require('errorhandler');
app.use(errorhandler());
}

//import routers
const roomRouter = require('./routers/roomRouter'); //legacy
const scenarioRouter = require('./routers/scenarioRouter'); //legacy

const userRouter = require('./routers/userRouter');
const nodeRouter = require('./routers/nodeRouter');
const campRouter = require('./routers/campRouter');
const infoRouter = require('./routers/infoRouter');

//mount routers
app.use('/room', roomRouter); //legacy
app.use('/scenario', scenarioRouter); //legacy
app.use('/user', userRouter);
app.use('/node', nodeRouter);
app.use('/camp', campRouter);
app.use('/info', infoRouter);

//start server
app.listen(PORT, () => {
console.log(`App is running on ${PORT}`)
})

app.listen(PORT, () => { // start server and listen on specified port
console.log(`App is running on ${PORT}`) // confirm server is running and log port to the console
})
//test
13 changes: 13 additions & 0 deletions appInfo/balancing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const numbers = {
maxPlayersForQueueTop: 3,
titleMinChars: 3,
titleMaxChars: 50,
descriptionMinChars: 3,
descriptionMaxChars: 200,
scenarioMinCharacter: 3,
scenarioMaxCharacters: 600,
}

module.exports = {
numbers
}
107 changes: 107 additions & 0 deletions database/dbChecks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//CHECKS STUFF IN THE DATABASE AND THROWS ERRORS IF THEY ARE FALSE
const db = require('./dbConnect.js');

//ERROR CHECKS
async function CanAddNode(campId, userId) {

const lastFinishedNodeQ = await db.query(
`
SELECT
camps.finished,
nodes_0.creator_id AS last_scenario_creator_id
FROM camps
JOIN nodes_0 on nodes_0.camp_id = camps.id
WHERE camps.id = $1
AND nodes_0.finished_at IS NOT NULL
ORDER BY nodes_0.id DESC
LIMIT 1;
`,
[campId]
);

if (lastFinishedNodeQ.rowCount == 0) throw new Error('there is no camp with that id');
const { finished, last_scenario_creator_id } = lastFinishedNodeQ.rows[0];
if (finished) throw new Error('Cant add node, story is already finished');
if (last_scenario_creator_id == userId) throw new Error('Cant add node, because user added the last scenario');

const lastNodeQ = await db.query(
`
SELECT
nodes_0.created_at AS last_node_posted_at,
nodes_0.finished_at AS last_node_finished_at
FROM nodes_0
WHERE nodes_0.camp_id = $1
ORDER BY nodes_0.id DESC
LIMIT 1;
`,
[campId]
);

const { last_node_posted_at, last_node_finished_at } = lastNodeQ.rows[0];

//check if someone else is writing
if (last_node_finished_at) return;
const last_node_time = (new Date(last_node_posted_at)).getTime();
const current_time = (new Date()).getTime();
const diff = current_time - last_node_time;
const diffInMinutes = diff / 1000 / 60;
if (diffInMinutes < 20) {
throw new Error('Cant add node. Another player is currently writing');
}

}

async function CanAddScenario(campId, userId, isEnd) {

const q = await db.query(
`
SELECT
nodes_0.creator_id AS node_creator_id,
finished_at AS node_finish_time,
scenario,
camps.finished AS camp_finished
FROM nodes_0
LEFT JOIN scenarios_0 ON scenarios_0.node_id = nodes_0.id
JOIN camps ON camps.id = nodes_0.camp_id
WHERE camp_id = $1
ORDER BY nodes_0.id;
`,
[campId]
);

const nodeCount = q.rowCount;
const lastNode = q.rows[q.rows.length - 1];
const lastPosterId = lastNode.node_creator_id;
const lastNodeEmpty = (lastNode.node_finish_time == null)
const scenario = lastNode.scenario;
const campFinished = lastNode.camp_finished;

if (!lastNodeEmpty) throw new Error('Cant add scenario, because the last node is marked as finished');
if (lastPosterId != userId) throw new Error('Cant add scenario, because someone else owns the node');
if (isEnd && nodeCount < 30) throw new Error('Cant add end, because the story is not long enough. Current nodecount: ', nodeCount);
if (!isEnd && nodeCount == 40) throw new Error('Cant add scenario. Must add end, because the story has reached its max length: ', nodeCount);
if (campFinished) throw new Error('cant add scenario, story marked as finished');
if (scenario) throw new Error('Cant add scenario, there already exist one linked to the last node');

}
async function GoogleTokenExists(googleToken) {

const tokenQ = await db.query(
`
SELECT COUNT(*)
FROM users
WHERE google_token = $1
`,
[googleToken]
);

if (tokenQ.rows[0].count == 0) return false;
else return true;

}

module.exports = {
CanAddNode,
CanAddScenario,
GoogleTokenExists
};
29 changes: 29 additions & 0 deletions database/dbConnect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const Pool = require('pg').Pool

const pool = (

process.env.DATABASE_URL ?

//heroku
new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
})

:

//local
new Pool({
user: 'postgres',
host: 'localhost',
database: 'unwritten',
password: 'postgres',
port: 5432,
})

)


module.exports = pool;
Loading