Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 创建一个类的集合(Create a collection of a class)

Hello I have a set interval which calls a function to select 2 index from an array and send per socket to the corresponding socket

(您好,我有一个设定的时间间隔,它调用一个函数从数组中选择2个索引,并将每个套接字发送到相应的套接字)

My function that takes 2 index from array and calls my function to send per socket

(我的函数从数组中获取2个索引,并调用我的函数以每个套接字发送)

  searching = () =>  {
    const firstPlayer = this.getRandomPlayer();
    const secondPlayer = this.players.find(
      playerTwo =>
        playerTwo.mmr < this.calculateLessThanPercentage(firstPlayer) &&
        playerTwo.mmr > this.calculateGreaterThanPercentage(firstPlayer) &&
        playerTwo.id != firstPlayer.id
    );
    if(secondPlayer){
    const matchedPlayers = [firstPlayer, secondPlayer];
    console.log(matchedPlayers);
    this.removePlayers(matchedPlayers);
    Match.configurePlayersForNewMatch(matchedPlayers);
    }
  }

and my socket send function that is in my class match

(和我的类中的套接字发送函数匹配)

const uuid = require("uuid");
const assert = require('assert');
const sessionMap = require('./SessionMap');
// Match class is a single game Match structure
class Match {
    constructor() {
      this.id = null
      this.firstPlayer = null;
      this.secondPlayer = null;
      this.isActive = false;;
    }
    addPlayer = (player) => {
     if(this.firstPlayer === null && player){
       this.firstPlayer = player;
     }else if(this.secondPlayer === null && player){
       this.secondPlayer = player;
     }
    }
    startMatch = () =>{
      this.isActive = true;
    }
    stopMatch = () => {
      this.isActive = false;
    }
    statusMatch = () => {
      return this.isActive;
    }

    getMatchConfigurationFor = player => {
        this.addPlayer(player);
    }
     configurePlayersForNewMatch = (matchedPlayers) => {
      matchedPlayers.forEach(player =>
      sessionMap.get(player.socketid)
          .broadcast.to(player.socketid)
          .emit('match',
                        this.getMatchConfigurationFor(player)));}
  }

  module.exports = new Match();

in this class I have my function that would send player / battle data per socket

(在这一节课中,我有我的功能,可以按插座发送玩家/战斗数据)

    getMatchConfigurationFor = player => {
        this.addPlayer(player);
    }
     configurePlayersForNewMatch = (matchedPlayers) => {
      matchedPlayers.forEach(player =>
      sessionMap.get(player.socketid)
          .broadcast.to(player.socketid)
          .emit('match',
                        this.getMatchConfigurationFor(player)));}
  }

and here I add the 2 players to my match

(在这里,我将2位玩家添加到我的比赛中)

addPlayer = (player) => {
 if(this.firstPlayer === null && player){
   this.firstPlayer = player;
 }else if(this.secondPlayer === null && player){
   this.secondPlayer = player;
 }
}

But then a problem came to me

(但是后来我遇到了一个问题)

I need to save these match's and return this match in my

(我需要保存这些匹配项,然后将其返回给我)

  getMatchConfigurationFor = player => {
        this.addPlayer (player);
    }

I would like help how could I do this to submit this created match and save it to the database using sequelize?

(我想要帮助,如何提交此创建的匹配项并使用sequelize将其保存到数据库中?)

  ask by Felipe translate from so

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

1 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...