Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

javascript - 模块后端关注点分离[保持](separation of concerns in modules backend [on hold])

I know my code is correctly worry-free

(我知道我的代码正确无忧)

i don't know how i could improve it

(我不知道该如何改善)

my class player:

(我的班级球员:)

class Player {
    constructor(id, name, mmr, socket_id) {
      this.id = id;
      this.name = name;
      this.mmr = mmr;
      this.socket_id = socket_id;
    }
    // Player rest methods...
    // I.E: updateName...
  }

  module.exports = Player;

my class queue:

(我的课程队列:)

const Match = require('./Match');
// Queue class is a queue system with players.
class Queue {
  constructor() {
    this.players = [];
  }

  // addPlayers add multiple players into queue
  addPlayers(players) {

      this.players = this.players.concat(players);
  }

  // addPlayer adds a single player into queue
  addPlayer(player) {
    if(!this.players.find(p => p.id == player.id)){
      this.players.push(player);
    }
  }

  // removePlayers remove multiple players from queue
  removePlayers(players) {
    players.forEach(p => {
      this.removePlayer(p);
    });
  }

  // removePlayer removes a single player from queue
  removePlayer(player) {
    this.players = this.players.filter(p => p.id !== player.id)
  }

  // getPlayers return players that are currently on queue
  getPlayers() {
    return this.players;
  }

  searching(id) {
    const firstPLayer = this.players.find(p => p.id == id)
    const { mmr } = firstPLayer
    const secondPlayer = this.players.find((playerTwo) => playerTwo.mmr < (5 / 100) * mmr + mmr && playerTwo.mmr > mmr - ((5 / 100) * mmr) && playerTwo.id != firstPLayer.id);
    if(!secondPlayer){
      return null;
    }
    const matchedPlayers = [
      firstPLayer,
      secondPlayer
    ]
    // remove matched players from this.players
    this.removePlayers(matchedPlayers);
    // return new Match with matched players
    return matchedPlayers;
  }
}

module.exports = Queue;

my class match:

(我的班级比赛:)

class Matches {
    constructor() {
      this.matches = [];
    }

    addMatch(match) {
      if(match){
        this.matches.push(match);
      }
    }
    getMatch(id){
      if(id){      
        return this.matches.find((match) => match.id = match )
      }else{
        return null;
      }

    }


  }

  module.exports = Matches;

I really don't know how I could add a function to enable a match or disable a match according to my Match isActive

(我真的不知道如何根据我的Match isActive添加功能以启用匹配或禁用匹配)

I know I could have a function to match a match within one of my classes, but I get a little confused which way and what better way

(我知道我可以在一个课程中有一个匹配比赛的功能,但是我对哪种方式以及哪种更好的方式感到困惑)

how I could improve the design of my modules

(如何改进模块的设计)

  ask by gabriel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...