Skip to content

Commit

Permalink
Merge pull request #4 from wizguin/develop
Browse files Browse the repository at this point in the history
1.5.0-beta
  • Loading branch information
wizguin authored Nov 7, 2022
2 parents d84d8b9 + 9d01c6e commit b871286
Show file tree
Hide file tree
Showing 53 changed files with 3,361 additions and 1,529 deletions.
16 changes: 10 additions & 6 deletions config/config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"password": "password",
"database": "yukon",
"dialect": "mysql",
"debug": false
"debug": false,
"logQueryParameters": false
},
"socketio": {
"https": false,
Expand All @@ -23,6 +24,13 @@
"cors": {
"origin": "http://localhost:8080"
},
"rateLimit": {
"enabled": true,
"addressConnectsPerSecond": 5,
"addressEventsPerSecond": 50,
"userEventsPerSecond": 10,
"ipAddressHeader": false
},
"worlds": {
"Login": {
"host": "localhost",
Expand All @@ -34,12 +42,8 @@
"maxUsers": 300
}
},
"rateLimit": {
"addressEventsPerSecond": 50,
"userEventsPerSecond": 10,
"ipAddressHeader": false
},
"game": {
"preferredSpawn": 0,
"iglooIdOffset": 2000
}
}
3,047 changes: 2,278 additions & 769 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yukon-server",
"version": "1.4.1-beta",
"version": "1.5.0-beta",
"description": "A server for https://github.com/wizguin/yukon",
"scripts": {
"dev": "babel-watch ./src/World.js Login Blizzard",
Expand All @@ -24,28 +24,29 @@
},
"homepage": "https://github.com/wizguin/yukon-server#readme",
"devDependencies": {
"@babel/cli": "^7.18.10",
"@babel/core": "^7.18.13",
"@babel/node": "^7.18.10",
"@babel/preset-env": "^7.18.10",
"@babel/cli": "^7.19.3",
"@babel/core": "^7.20.2",
"@babel/node": "^7.20.2",
"@babel/preset-env": "^7.20.2",
"babel-plugin-module-resolver": "^4.1.0",
"babel-watch": "^7.7.0",
"eslint": "^8.26.0",
"rimraf": "^3.0.2"
},
"dependencies": {
"bcrypt": "^5.0.1",
"fastest-validator": "^1.13.0",
"fs": "0.0.1-security",
"bcrypt": "^5.1.0",
"fastest-validator": "^1.15.0",
"fs": "0.0.2",
"jsonwebtoken": "^8.5.1",
"mysql2": "^2.3.3",
"pm2": "^5.2.0",
"rate-limiter-flexible": "^2.3.8",
"pm2": "^5.2.2",
"rate-limiter-flexible": "^2.4.1",
"sequelize": "^5.22.5",
"socket.io": "^4.5.1",
"socket.io": "^4.5.3",
"uuid": "^8.3.2"
},
"optionalDependencies": {
"bufferutil": "^4.0.6",
"utf-8-validate": "^5.0.9"
"bufferutil": "^4.0.7",
"utf-8-validate": "^5.0.10"
}
}
18 changes: 18 additions & 0 deletions src/database/BaseModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Sequelize from 'sequelize'


export default class BaseModel extends Sequelize.Model {

protectedAttributes = []

toJSON() {
let attributes = this.get()

for (let attribute of this.protectedAttributes) {
delete attributes[attribute]
}

return attributes
}

}
47 changes: 47 additions & 0 deletions src/database/Collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export default class Collection {

constructor(user, models, model, indexKey) {
this.user = user
this.indexKey = indexKey

this.db = user.db
this.model = user.db[model]
this.handler = user.handler

this.collection = {}

this.collect(models)
}

collect(models) {
for (let model of models) {
this.collection[model[this.indexKey]] = model
}
}

add(record) {
this.model.create(record)
.then((model) => {
this.collection[model[this.indexKey]] = model

})
.catch((error) => {
this.handler.error(error)
})
}

remove(key) {
this.collection[key].destroy()

delete this.collection[key]
}

includes(key) {
return key in this.collection
}

toJSON() {
return Object.keys(this.collection).map(key => parseInt(key))
}

}
111 changes: 35 additions & 76 deletions src/database/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ import fs from 'fs'
import path from 'path'
import Sequelize from 'sequelize'

const Op = Sequelize.Op


export default class Database {

constructor(config) {
this.sequelize = new Sequelize(
config.database,
config.user,
config.password, {
config.password,
{
host: config.host,
dialect: config.dialect,
logging: (config.debug) ? console.log : false
})
logging: (config.debug) ? console.log : false,
logQueryParameters: config.logQueryParameters
}
)

// Used to translate type id to string
this.slots = [ 'color', 'head', 'face', 'neck', 'body', 'hand', 'feet', 'flag', 'photo', 'award' ]

this.dir = `${__dirname}/models`
this.loadModels()

let models = this.loadModels()
this.loadAssociations(models)

this.usernameRegex = /[^ -~]/i
this.selectorRegex = /[^a-z0-9-]/i
Expand All @@ -37,14 +40,32 @@ export default class Database {
}

loadModels() {
let models = []

fs.readdirSync(this.dir).forEach(model => {
let modelImport = require(path.join(this.dir, model)).default
let modelObject = modelImport.init(this.sequelize, Sequelize)

let name = model.charAt(0).toLowerCase() + model.slice(1, -3)

this[name] = modelObject

models.push(modelObject)
})

return models
}

loadAssociations(models) {
for (let model of models) {
if (model.associate) {
model.associate(this)
}
}
}

buildUser() {
return new this.users({}, { isNewRecord: false })
}

async getUserByUsername(username) {
Expand All @@ -63,24 +84,14 @@ export default class Database {
})
}

async getAuthToken(userId, selector) {
if (this.selectorRegex.test(selector)) {
return null
}
async getUsername(userId) {
return await this.findOne('users', {
where: { id: userId },
attributes: ['username'],
raw: true

return await this.findOne('authTokens', {
where: { userId: userId, selector: selector }
})
}

async getActiveBan(userId) {
return await this.findOne('bans', {
where: {
userId: userId,
expires: {
[Op.gt]: Date.now()
}
}
}, null, (result) => {
return result.username
})
}

Expand All @@ -90,57 +101,6 @@ export default class Database {
})
}

async getBuddies(userId) {
return await this.findAll('buddies', {
where: { userId: userId },
attributes: ['buddyId']

}, [], (result) => {
return result.map(result => result.buddyId)
})
}

async getIgnores(userId) {
return await this.findAll('ignores', {
where: { userId: userId },
attributes: ['ignoreId']

}, [], (result) => {
return result.map(result => result.ignoreId)
})
}

async getInventory(userId) {
return await this.findAll('inventories', {
where: { userId: userId },
attributes: ['itemId']

}, [], (result) => {
return result.map(result => result.itemId)
})
}

async getIglooInventory(userId) {
return await this.findAll('iglooInventories', {
where: { userId: userId },
attributes: ['iglooId']

}, [], (result) => {
return result.map(result => result.iglooId)
})
}

async getFurnitureInventory(userId) {
return await this.findAll('furnitureInventories', {
where: { userId: userId },
attributes: ['itemId', 'quantity'],
raw: true

}, {}, (result) => {
return this.arrayToObject(result, 'itemId', 'quantity')
})
}

async getIgloo(userId) {
return await this.findOne('igloos', {
where: { userId: userId },
Expand All @@ -159,8 +119,7 @@ export default class Database {
raw: true

}, [], (result) => {
// Removes user id from all objects in furniture array
return result.map(({ userId, ...furnitures}) => furnitures)
return result.map(({ id, userId, ...furniture }) => furniture)
})
}

Expand Down
53 changes: 53 additions & 0 deletions src/database/collections/BuddyCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import Collection from '../Collection'


export default class BuddyCollection extends Collection {

constructor(user, models) {
super(user, models, 'buddies', 'buddyId')

this.usersById = this.handler.usersById
}

add(id) {
super.add({ userId: this.user.id, buddyId: id })
}

isOnline(id) {
return id in this.usersById
}

sendOnline(id) {
let user = this.usersById[id]

user.send('buddy_online', { id: this.user.id })
}

sendOffline() {
for (let buddy in this.collection) {
if (this.isOnline(buddy)) {
let user = this.usersById[buddy]

user.send('buddy_offline', { id: this.user.id })
}
}
}

toJSON() {
let buddies = []

for (let buddy in this.collection) {
let online = this.isOnline(buddy)
let username = this.collection[buddy].user.username

buddies.push({ id: parseInt(buddy), username: username, online: online })

if (online) {
this.sendOnline(buddy)
}
}

return buddies
}

}
Loading

0 comments on commit b871286

Please sign in to comment.