Skip to content
Permalink
Browse files
Fix adding games bug
Implemented new platforms and categories into the adding games and individual game pages, as well as the add_data.js script. Cleaned up db.js and wrote the getCategories method.
  • Loading branch information
drumevp committed Nov 30, 2019
1 parent b0698a9 commit 8d2141a7af062b9a9b3891b469ea9cd148b491ae
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 49 deletions.
@@ -1,3 +1,4 @@
/* eslint-disable max-lines-per-function */
/* eslint-disable complexity */
'use strict'

@@ -8,16 +9,14 @@ const db = require(path.join(__dirname, '/../db'))
const dbctx = new db.SqliteDbContext(path.join(__dirname, '/../app.db'))

//Automatically inserted data
// eslint-disable-next-line max-lines-per-function
request('https://api.steampowered.com/ISteamApps/GetAppList/v2/', (error, response, body) => {
request('https://api.steampowered.com/ISteamApps/GetAppList/v2/', async(error, response, body) => {
const allGames = JSON.parse(body)
let i = 800
const gameAmount = 1100 //change according to how many games you want to add
let i = 1
const gameAmount = 10 //change according to how many games you want to add
for(i; i<gameAmount; i++) {
const gameid = allGames.applist.apps[i].appid //getting the appID to insert into next URL as a string
gameid.toString()
// eslint-disable-next-line max-lines-per-function
request(`https://store.steampowered.com/api/appdetails/?appids=${gameid}` , (error, response, body) => {
request(`https://store.steampowered.com/api/appdetails/?appids=${gameid}` , async(error, response, body) => {
//console.log(gameid)
const statusInt = 200
if (response.statusCode !== statusInt) {
@@ -43,23 +42,23 @@ request('https://api.steampowered.com/ISteamApps/GetAppList/v2/', (error, respon
return
}
//Creating Game Object
const game = new Game(
let game = new Game(
gameData.name,
'38', //Most Steam games are on Windows
gameData.short_description,
gameData.about_the_game.replace(/<[^>]*>?/gm, ''),
gameData.release_date.date,
gameData.developers[0], //not every game has developers listed so it repeats the publisher
gameData.publishers[0],
'admin',
1,
'yes',
gameData.header_image,
gameData.screenshots[0].path_thumbnail)
dbctx.addGame(game)
//console.log(gameData)
game.categories = gameData.categories
game = await dbctx.createGame(game)
const platform = await dbctx.getPlatform(38)
await dbctx.linkGamePlatform(game, platform)
} catch(err) {
console.log(body)
throw TypeError(`Game: https://store.steampowered.com/api/appdetails/?appids=${gameid} ${err}`)
throw new Error(`Game: https://store.steampowered.com/api/appdetails/?appids=${gameid} ${err}`)
}
})
}
@@ -106,6 +106,30 @@ INSERT INTO 'platforms' VALUES (40, 'iOS');
INSERT INTO 'platforms' VALUES (41, 'Google Stadia');
INSERT INTO 'platforms' VALUES (42, 'Linux');

INSERT INTO 'categories' ('name') VALUES ('Action');
INSERT INTO 'categories' ('name') VALUES ('Platformer');
INSERT INTO 'categories' ('name') VALUES ('Shooter');
INSERT INTO 'categories' ('name') VALUES ('Fighting');
INSERT INTO 'categories' ('name') VALUES ('Stealth');
INSERT INTO 'categories' ('name') VALUES ('Survival');
INSERT INTO 'categories' ('name') VALUES ('Sandbox');
INSERT INTO 'categories' ('name') VALUES ('Rhythm');
INSERT INTO 'categories' ('name') VALUES ('Horror');
INSERT INTO 'categories' ('name') VALUES ('Visual Novel');
INSERT INTO 'categories' ('name') VALUES ('RPG');
INSERT INTO 'categories' ('name') VALUES ('MMORPG');
INSERT INTO 'categories' ('name') VALUES ('Action RPG');
INSERT INTO 'categories' ('name') VALUES ('Rougelike');
INSERT INTO 'categories' ('name') VALUES ('Tower Defence');
INSERT INTO 'categories' ('name') VALUES ('Racing');
INSERT INTO 'categories' ('name') VALUES ('Puzzle');
INSERT INTO 'categories' ('name') VALUES ('Indie');
INSERT INTO 'categories' ('name') VALUES ('Adventure');
INSERT INTO 'categories' ('name') VALUES ('Sports');
INSERT INTO 'categories' ('name') VALUES ('Co-op');



INSERT INTO
`gamePlatforms`
VALUES
@@ -119,5 +143,19 @@ VALUES
(4, 40),
(4, 41);

INSERT INTO
`gameCategories`
VALUES
(1, 3),
(1, 4),
(2, 5),
(2, 1),
(3, 3),
(3, 7),
(4, 4),
(4, 3),
(4, 7);


INSERT INTO `users` (`username`, `hash`, `isAdmin`) VALUES ('admin', '$2b$12$niVK8DnXKSyYzAIOUun2C.PZ51waVc2NU/e7DQ9cYM6zxNwUiiOCG', 'yes');
INSERT INTO `users` (`username`, `hash`, `isAdmin`) VALUES ('user', '$2b$12$niVK8DnXKSyYzAIOUun2C.PZ51waVc2NU/e7DQ9cYM6zxNwUiiOCG', 'no');
@@ -11,8 +11,9 @@ adding.get('/game', authenticateUser, async ctx => {
const a = ctx.session.userID
const user = await ctx.db.getUser(a)
const platformnames = await ctx.db.getAllPlatforms()
const categories = await ctx.db.getCategories()
await ctx.render('addingGames.hbs', {platforms: platformnames, user: ctx.session.userID,
admin: await ctx.db.isUserAdmin(ctx.session.userID)})
categories: categories, admin: await ctx.db.isUserAdmin(ctx.session.userID)})

if(!user) {
console.log('you are not allowed to add any games')
@@ -24,12 +25,19 @@ adding.get('/game', authenticateUser, async ctx => {
adding.post('/game', authenticateUser, async ctx => {
const body = ctx.request.body
body.submittedBy = ctx.session.userID
const platforms = body.platforms
if(Array.isArray(platforms)) { //if only one platform is selected 'platforms' is a string and not a list
body.platforms = platforms.join(',')
}
body.approved = 'no'
await ctx.db.addGame(body)
//createGame expects a list of objects for body.categories and body.platforms
for(let i=0; i<body.categories.length; i++) {
const categoryId = parseInt(body.categories[i])
const category = await ctx.db.getCategory(categoryId)
body.categories[i] = category
}
for(let i=0; i<body.platforms.length; i++) {
const platformId = parseInt(body.platforms[i])
const platform = await ctx.db.getPlatform(platformId)
body.platforms[i] = platform
}
await ctx.db.createGame(body)
return ctx.redirect('/adding/game')
})
module.exports = adding
@@ -9,7 +9,7 @@ const {authenticateUser} = require('./middleware/auth')
// eslint-disable-next-line max-lines-per-function
approval.get('/games', authenticateUser, async ctx => {
try {
if(await ctx.db.isUserAdmin(await ctx.session.userID) !== true) {
if(await ctx.db.isUserAdmin(ctx.session.userID) !== true) {
return await ctx.render('error', {message: 'Session not authorised'})
}

@@ -18,6 +18,7 @@ approval.get('/games', authenticateUser, async ctx => {
//changing submittedBy from an ID to the username
let game
for(game of unapproved) {
//console.log(game.submittedBy)
const user = await ctx.db.getUser(game.submittedBy)
game.submittedBy = user.username
if(game.poster.startsWith('http')) {
@@ -15,6 +15,7 @@ game.get('/game:id', async ctx => {
throw new Error('game ID must be a number')
}
const gamedata = await ctx.db.getGame(Number(ctx.params.id))
console.log(gamedata)
const reviews = (await ctx.db.getReviewsForGame(Number(gamedata.gameID))).reverse()
const reviewCount = reviews.length
const platforms = gamedata.platforms
@@ -34,7 +35,8 @@ game.get('/game:id', async ctx => {
const sliceInt = 3
await ctx.render('game', {review: reviews.slice(0, sliceInt), expandedReview: reviews, thisgame: gamedata,
reviewNo: reviewCount, platforms: platforms, avgScore: avgScore, user: ctx.session.authorised,
admin: await ctx.db.isUserAdmin(ctx.session.userID), hasReviews: hasReviews})
admin: await ctx.db.isUserAdmin(ctx.session.userID), hasReviews: hasReviews,
categories: gamedata.categories})
} else {
ctx.redirect('/list')
}
41 db.js
@@ -68,7 +68,7 @@ class DbContext {

// eslint-disable-next-line no-unused-vars
async createGame(game) {
throw new NotImplemented('addGame is not implemented')
throw new NotImplemented('createGame is not implemented')
}

/**
@@ -292,31 +292,6 @@ class DbContext {
throw new NotImplemented('postComment is not implemented')
}

async getCategories() {
throw new NotImplemented('getCategories is not implemented')
}

// eslint-disable-next-line no-unused-vars
async getCategory(id) {
throw new NotImplemented('getCategory is not implemented')
}

// eslint-disable-next-line no-unused-vars
async deleteCategory(id) {
throw new NotImplemented('deleteCategory is not implemented')
}

// eslint-disable-next-line no-unused-vars
async createCategory(category) {
throw new NotImplemented('createCategory is not implemented')
}

// eslint-disable-next-line no-unused-vars
async updateCategory(category) {
throw new NotImplemented('updateCategory is not implemented')
}

// eslint-disable-next-line no-unused-vars
async execute(query) {
throw new NotImplemented('execute is not implemented')
}
@@ -551,6 +526,18 @@ class SqliteDbContext extends DbContext {
return this.getGame(lastID)
}

/**
* Gets all Categories.
* @abstract
* @returns {Promise<Category[]>}
*/
async getCategories() {
const sqlite = await this.sqlitePromise

const categories = await sqlite.all('SELECT * FROM `categories`;')
return categories.map(x => Object.assign(new Category(), x))
}

/**
* @param {number|string} id - Name or ID of the Category
* @returns {Promise<Category>}
@@ -834,7 +821,7 @@ class SqliteDbContext extends DbContext {
query = 'SELECT * FROM `games` WHERE `approved` != ?'
}
const games = await sqlite.all(query, 'yes')
return games
return games.map(x => Object.assign(new Game(), x))
}
//selects all the platforms from the database
async getAllPlatforms() {
@@ -21,6 +21,7 @@
"koa-session": "^5.12.3",
"koa-static": "^5.0.0",
"koa-views": "^6.2.1",
"request": "^2.88.0",
"sqlite": "^3.0.3"
},
"devDependencies": {
@@ -16,9 +16,13 @@
<form method= "post">
<input type="text" placeholder="Title:" name="title" required/>
<h1>Platforms:</h1>
{{#each platforms}}
{{#each platforms}}
<input type="checkbox" name="platforms" value="{{this.id}}" />{{this.name}}
{{/each}}
<h1>Categories:</h1>
{{#each categories}}
<input type="checkbox" name="categories" value="{{this.id}}" />{{this.name}}
{{/each}}
<input type="text" placeholder="Slugline" name="slugline" required/>
<input type= "text" placeholder="Summary" name="summary" required/>

@@ -54,6 +54,10 @@
<th>Publisher</th>
<td>{{thisgame.publisher}}</td>
</tr>
<tr>
<th>Categories</th>
<td>{{#each categories}}{{this.name}}{{#unless @last}}, {{/unless}}{{/each}}</td>
</tr>
</tbody>
</table>
</section>

0 comments on commit 8d2141a

Please sign in to comment.