Skip to content

Instantly share code, notes, and snippets.

@timetocode
Created April 18, 2019 00:02
Show Gist options
  • Save timetocode/30472b3e536ab58ef3954300432c4225 to your computer and use it in GitHub Desktop.
Save timetocode/30472b3e536ab58ef3954300432c4225 to your computer and use it in GitHub Desktop.
nengi score system example,
import nengi from '../../nengi';
class Score {
constructor(score) {
this.entityId = score.entityId
this.kills = score.kills
this.assists = score.assists
this.damage = score.damage
this.deaths = score.deaths
}
}
Score.protocol = {
entityId: nengi.UInt16,
kills: nengi.UInt16,
assists: nengi.UInt16,
damage: nengi.UInt32,
deaths: nengi.UInt16
}
export default Score
import Score from '../common/message/Score'
import Scores from '../common/message/Scores'
const scoreTemplate = {
entityId: -1,
kills: 0,
assists: 0,
damage: 0,
deaths: 0
}
const scores = new Map()
const addClient = (client) => {
const score = Object.assign({}, scoreTemplate)
score.entityId = client.entity.id
score.weaponType = client.entity.weaponType
score.skinType = client.entity.skinType
scores.set(client, score)
}
const removeClient = (client) => {
scores.delete(client)
}
const _add = (client, prop, amount) => {
if (scores.has(client)) {
scores.get(client)[prop] += amount
return
}
console.log('scores unable to add', prop, amount)
}
const _set = (client, prop, value) => {
if (scores.has(client)) {
scores.get(client)[prop] = value
return
}
console.log('scores unable to set', prop, amount)
}
const updateLoadout = (client, config) => {
_set(client, 'weaponType', config.weaponType)
_set(client, 'skinType', config.skinType)
}
const addDamage = (client, amount) => {
_add(client, 'damage', amount)
}
const addKill = (client) => {
_add(client, 'kills', 1)
}
const addDeath = (client) => {
_add(client, 'deaths', 1)
}
const _killSort = (a, b) => {
if (a.kills < b.kills) {
return 1
} else {
return -1
}
}
const reset = () => {
scores.forEach(score => {
score.kills = 0
score.assists = 0
score.damage = 0
score.deaths = 0
})
}
const getRankings = () => {
return Array.from(scores.values()).sort(_killSort)
}
const toMessage = () => {
const arr = Array.from(scores.values()).sort(_killSort)
const scoresMessage = new Scores()
arr.forEach((scoreData) => {
scoresMessage.scores.push(new Score(scoreData))
})
return scoresMessage
}
export default {
addClient, removeClient, addDamage, addKill, addDeath, toMessage, getRankings, updateLoadout, reset
}
import nengi from '../../nengi'
import Score from './Score'
class Scores {
constructor() {
this.scores = []
}
}
Scores.protocol = {
scores: { type: Score, indexType: nengi.UInt8 }
}
export default Scores
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment