Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
468 changes: 468 additions & 0 deletions GameServer.js

Large diffs are not rendered by default.

649 changes: 649 additions & 0 deletions GameServer.ts

Large diffs are not rendered by default.

100 changes: 97 additions & 3 deletions Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ var ffmpeg = require('fluent-ffmpeg')
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path
ffmpeg.setFfmpegPath(ffmpegPath)
const sgMail = require('@sendgrid/mail')
const { uniq } = require('lodash')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)

const imageMBLimit = 10
const audioMBLimit = 30
const imageMBLimit = 20
const audioMBLimit = 100
const defaultPostValues = {
state: 'active',
watermark: false,
Expand Down Expand Up @@ -965,6 +966,8 @@ const fullPostAttributes = [
'totalReposts',
'totalRatings',
'totalLinks',
'game',
'move',
]

// todo: replace all use cases with const fullPostAttributes above
Expand All @@ -984,6 +987,8 @@ function findFullPostAttributes(model, accountId) {
'totalReposts',
'totalRatings',
'totalLinks',
'game',
'move',
// accountLike('post', model, accountId),
// accountComment('post', model, accountId),
// accountLink('post', model, accountId),
Expand Down Expand Up @@ -1142,6 +1147,28 @@ function findPostInclude(accountId) {
},
},
},
{
model: Link,
as: 'Originals',
required: false,
where: { relationship: 'remix', state: 'active' },
include: {
model: Post,
as: 'Parent',
attributes: ['id', 'title', 'game', 'state'],
},
},
{
model: Link,
as: 'Remixes',
separate: true,
where: { relationship: 'remix', state: 'active' },
order: [['index', 'ASC']],
include: {
model: Post,
attributes: ['id', 'title', 'game', 'state'],
},
},
{
model: Reaction,
where: { creatorId: accountId, state: 'active' },
Expand Down Expand Up @@ -1683,6 +1710,50 @@ function addGBGPlayers(postId, creator, settings) {
})
}

async function addRemixes(accountId, game, postId) {
let originals = []
function findOriginals(steps) {
for (const step of steps) {
if (step.originalStep) {
originals.push(step.originalStep.gameId)
}
if (step.type === 'sequence') {
findOriginals(step.steps)
}
}
}
findOriginals(game.steps)
originals = uniq(originals)
const links = await Link.findAll({
attributes: ['itemAId'],
where: {
state: 'active',
itemAType: 'post',
itemBType: 'post',
itemAId: originals,
itemBId: postId,
relationship: 'remix',
},
})
for (const originalId of originals) {
if (links?.some((link) => link.itemAId === originalId)) {
continue
}
await Link.create({
creatorId: accountId,
state: 'active',
itemAType: 'post',
itemBType: 'post',
itemAId: originalId,
itemBId: postId,
relationship: 'remix',
totalLikes: 0,
totalComments: 0,
totalRatings: 0,
})
}
}

// todo:
// + check notifyMentions is adding the correct notification type
function createPost(data, files, accountId) {
Expand All @@ -1700,6 +1771,8 @@ function createPost(data, files, accountId) {
event,
poll,
glassBeadGame,
game,
move,
card,
color,
watermark,
Expand All @@ -1721,10 +1794,16 @@ function createPost(data, files, accountId) {
color: color || null,
watermark: !!watermark,
lastActivity: new Date(),
game,
move,
})

if (game) {
await addRemixes(accountId, game, post.id)
}

// todo: add the correct notification type
const notifyMentions = mentions.length
const notifyMentions = mentions?.length
? await new Promise(async (resolve) => {
const users = await User.findAll({
where: { id: mentions, state: 'active' },
Expand Down Expand Up @@ -1946,6 +2025,20 @@ function attachComment(comment, parent, accountId) {
totalComments: 0,
totalRatings: 0,
})
if (parent.relationship !== 'parent') {
await Link.create({
creatorId: accountId,
itemAId: parent.id,
itemAType: parent.type,
itemBId: comment.id,
itemBType: comment.type,
relationship: parent.relationship,
state: 'active',
totalLikes: 0,
totalComments: 0,
totalRatings: 0,
})
}
const addRootLink = await Link.create({
creatorId: accountId,
itemAId: rootPost.id,
Expand Down Expand Up @@ -2217,6 +2310,7 @@ module.exports = {
accountLink,
uploadFiles,
createPost,
addRemixes,
attachComment,
scheduleNextBeadDeadline,
pushNotification,
Expand Down
6 changes: 5 additions & 1 deletion ScheduledTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const { Op } = sequelize
const { User, Event, UserEvent, Notification, Post, Weave, GlassBeadGame } = require('./models')
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const io = require('./Socket')
const { initializeGameServerTasks } = require('./GameServer')

function scheduleEventNotification(data) {
async function scheduleEventNotification(data) {
const {
type,
postId,
Expand Down Expand Up @@ -344,6 +346,8 @@ async function initializeScheduledTasks() {
if (nextPlayer) scheduleGBGMoveJobs(id, nextPlayer, moveNumber, nextMoveDeadline)
}
})

await initializeGameServerTasks(io)
}

module.exports = {
Expand Down
3 changes: 3 additions & 0 deletions Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const socketServer = require('http').createServer()
const socketIo = require('socket.io')
const io = socketIo(socketServer, { cors: { origin: whitelist } })
// socket.io cheatsheet: https://socket.io/docs/v3/emit-cheatsheet/
const { registerGameServerEvents } = require('./GameServer')

const sockets = []
const rooms = [] // space, chat, post, or game + id: `space-58`
Expand Down Expand Up @@ -200,6 +201,8 @@ io.on('connection', (socket) => {
// gameRooms[roomId] = gameRooms[roomId].filter((users) => users.socketId !== socket.id)
// }
// })

registerGameServerEvents(socket, io)
})

socketServer.listen(5001)
Expand Down
4 changes: 4 additions & 0 deletions docs/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ sudo du -x -h / | sort -h | tail -40
# flush pm2 logs

pm2 flush

# deployment notes

Change dev script "concurrently -n node,ts \"nodemon Server.js\" \"tsc --watch\"" to "concurrently -n node,ts 'nodemon Server.js' 'tsc --watch'" to work on linux server
24 changes: 24 additions & 0 deletions migration-updates/add-game-to-posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.addColumn(
'Posts',
'game',
{
type: Sequelize.DataTypes.JSON,
},
{ transaction: t }
),
])
})
},

down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.removeColumn('Posts', 'game', { transaction: t }),
])
})
},
}
24 changes: 24 additions & 0 deletions migration-updates/add-move-to-posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.addColumn(
'Posts',
'move',
{
type: Sequelize.DataTypes.JSON,
},
{ transaction: t }
),
])
})
},

down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.removeColumn('Posts', 'move', { transaction: t }),
])
})
},
}
6 changes: 6 additions & 0 deletions migrations/create-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ module.exports = {
totalGlassBeadGames: {
type: Sequelize.INTEGER,
},
game: {
type: Sequelize.JSON,
},
move: {
type: Sequelize.JSON,
},
lastActivity: {
type: Sequelize.DATE,
},
Expand Down
5 changes: 5 additions & 0 deletions models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module.exports = (sequelize, DataTypes) => {
totalReposts: DataTypes.INTEGER,
totalRatings: DataTypes.INTEGER,
totalGlassBeadGames: DataTypes.INTEGER,
game: DataTypes.JSON,
move: DataTypes.JSON,
lastActivity: DataTypes.DATE,
},
{}
Expand All @@ -48,6 +50,9 @@ module.exports = (sequelize, DataTypes) => {
Post.hasMany(models.Link, { as: 'UrlBlocks', foreignKey: 'itemAId' })
Post.hasMany(models.Link, { as: 'ImageBlocks', foreignKey: 'itemAId' })
Post.hasMany(models.Link, { as: 'AudioBlocks', foreignKey: 'itemAId' })
Post.hasOne(models.Link, { as: 'Originals', foreignKey: 'itemBId' })
Post.hasMany(models.Link, { as: 'Remixes', foreignKey: 'itemAId' })
Post.hasMany(models.Link, { as: 'Submissions', foreignKey: 'itemAId' })
Post.hasOne(models.Link, { as: 'MediaLink', foreignKey: 'itemAId' })
// used for post map (todo: rethink...)
Post.hasMany(models.Link, { as: 'OutgoingPostLinks', foreignKey: 'itemAId' })
Expand Down
Loading