设计像 QuizUp 这样的多人游戏时使用的概念(在服务器上)是什么?
what are the concepts (on server) used in designing a multiplayer game like QuizUp?
我期待在 android 中设计像 QuizUp 这样的实时多人游戏。我理解 UI 和它的数据库部分,但我无法衡量编写服务器逻辑所涉及的复杂性。
所以我正在寻找的一些功能如下:
- 2 个注册用户在应用程序中随机相互连接
- 他们互相竞争在 1 分钟内回答一组 5 个问题
- 在更短的时间内回答得更准确的人可以获得一些分数
有人可以突出显示实现上述用例所需的服务器构建块吗?
我能想到的可能构建块/模块是:
- 身份验证
- 会话管理
请根据您的经验指出服务器而非数据库中任何缺失的块,UI 谢谢
虽然我从来没有玩过QuizUp,但我认为您在服务器中需要的是一个实时Web 应用程序框架。有了这些东西,你可以轻松处理你的业务逻辑。
下面的伪代码写在Cettia中。这是我编写的实时网络应用程序框架,但由于只提供 Java 服务器和 Java 脚本客户端,因此您暂时无法将其用于 Android。尽管如此,我认为它会帮助你粗略地编写你的业务逻辑并在评估你需要的功能方面找到这样的框架。
Server server = new DefaultServer();
Queue<ServerSocket> waitings = new ConcurrentLinkedQueue<>();
server.onsocket(socket -> {
// Find the counterpart from the waiting queue
// This is #1
ServerSocket counterpart = waitings.poll();
// If no one waits, adds the socket to the queue
// Remaining logic will be executed when other socket, the socket's counterpart, is connected
if (counterpart == null) {
waitings.offer(socket);
// Make sure evicts the socket when it's closed
socket.onclose(() -> waitings.remove(socket));
return;
}
// Now we have two registered users - socket and counterpart
// Find a quiz which is a set of 5 questions from database or somewhere
// Assumes quiz has a randomly generated id property
Quiz quiz = Quiz.random();
// Create a report to evaluate socket and counterpart's answer
Report report = new Report();
// Make a group for socket and counterpart for convenience
socket.tag(quiz.id());
counterpart.tag(quiz.id());
// Find a group whose name is quiz.id(), one we've just created
server.byTag(quiz.id())
// and send the quiz to sockets in the group
.send("quiz", quiz)
// and add an event listener which will be called by each client
// This is #2
.on("quiz", qa -> {
// Every time (total 5 times) client answers to each question,
// this listener will be executed and 'qa' contains information about question and answer
// Evaluates if answer is right
boolean right = Quiz.evaluate(qa.question(), qa.answer());
// Adds a final information to the report
report.add(qa.user(), qa.question(), qa.answer(), right);
});
// Initiate timeout timer
new Timer().schedule(() -> {
// Notifies socket and counterpart of timeout
server.byTag(quiz.id()).send("timeout");
// It's time to evalute the report which contains a result of this game
// Analyze who answers more accurately in less time and give him/her some points
// This is #3
report...
}, 60 * 1000).run();
});
我期待在 android 中设计像 QuizUp 这样的实时多人游戏。我理解 UI 和它的数据库部分,但我无法衡量编写服务器逻辑所涉及的复杂性。
所以我正在寻找的一些功能如下:
- 2 个注册用户在应用程序中随机相互连接
- 他们互相竞争在 1 分钟内回答一组 5 个问题
- 在更短的时间内回答得更准确的人可以获得一些分数
有人可以突出显示实现上述用例所需的服务器构建块吗?
我能想到的可能构建块/模块是:
- 身份验证
- 会话管理
请根据您的经验指出服务器而非数据库中任何缺失的块,UI 谢谢
虽然我从来没有玩过QuizUp,但我认为您在服务器中需要的是一个实时Web 应用程序框架。有了这些东西,你可以轻松处理你的业务逻辑。
下面的伪代码写在Cettia中。这是我编写的实时网络应用程序框架,但由于只提供 Java 服务器和 Java 脚本客户端,因此您暂时无法将其用于 Android。尽管如此,我认为它会帮助你粗略地编写你的业务逻辑并在评估你需要的功能方面找到这样的框架。
Server server = new DefaultServer();
Queue<ServerSocket> waitings = new ConcurrentLinkedQueue<>();
server.onsocket(socket -> {
// Find the counterpart from the waiting queue
// This is #1
ServerSocket counterpart = waitings.poll();
// If no one waits, adds the socket to the queue
// Remaining logic will be executed when other socket, the socket's counterpart, is connected
if (counterpart == null) {
waitings.offer(socket);
// Make sure evicts the socket when it's closed
socket.onclose(() -> waitings.remove(socket));
return;
}
// Now we have two registered users - socket and counterpart
// Find a quiz which is a set of 5 questions from database or somewhere
// Assumes quiz has a randomly generated id property
Quiz quiz = Quiz.random();
// Create a report to evaluate socket and counterpart's answer
Report report = new Report();
// Make a group for socket and counterpart for convenience
socket.tag(quiz.id());
counterpart.tag(quiz.id());
// Find a group whose name is quiz.id(), one we've just created
server.byTag(quiz.id())
// and send the quiz to sockets in the group
.send("quiz", quiz)
// and add an event listener which will be called by each client
// This is #2
.on("quiz", qa -> {
// Every time (total 5 times) client answers to each question,
// this listener will be executed and 'qa' contains information about question and answer
// Evaluates if answer is right
boolean right = Quiz.evaluate(qa.question(), qa.answer());
// Adds a final information to the report
report.add(qa.user(), qa.question(), qa.answer(), right);
});
// Initiate timeout timer
new Timer().schedule(() -> {
// Notifies socket and counterpart of timeout
server.byTag(quiz.id()).send("timeout");
// It's time to evalute the report which contains a result of this game
// Analyze who answers more accurately in less time and give him/her some points
// This is #3
report...
}, 60 * 1000).run();
});