我正在通过电报创建一个游戏,目前我有一个关于同时处理多个更新的问题。我正在使用node.js
例如我有这个代码
var TelegramBot = require('node-telegram-bot-api'),
bot = new TelegramBot("MY_TOKEN", {polling: true});
bot.onText(/^\/createroom/, function (res, match) {
//Here i have some logic, to check whether if the room already created or not
service.checkIfRoomExist(res) // this service here, will always return false, because of the simultaneously chat
.then (function(isExist) {
if (isExist === false) {
service.createRoom(res)
.then (function() {
});
}
});
//it works fine, if player type "/createroom" not simultaneously
//but if more than 1 player type "/createroom" simultaneously, my logic here doesn't work, it will create multiple room
}
有任何想法解决这个问题吗?
非常感谢,任何帮助都将不胜感激
答案 0 :(得分:1)
您需要将唯一的聊天/用户ID链接到您的数据库,以防止此类冲突。请参阅下面的代码以及有关如何执行此操作的注释。
var TelegramBot = require('node-telegram-bot-api'),
bot = new TelegramBot("MY_TOKEN", {
polling: true
});
bot.onText(/^\/createroom/, function (res, match) {
//use res.chat.id for groups and res.user.id for individuals
service.checkIfRoomExist(res.chat.id).then(function (isExist) {
if (isExist === false) {
service.createRoom(res.chat.id).then(function () {
bot.sendMessage(res.chat.id, 'Initializing game!')
// send game content here
});
}
bot.sendMessage(res.chat.id, 'A game has already started in this group!')
})
});
function checkIfRoomExist(id) {
// Your logic here that checks in database if game has been created
}