设计QuizUp等多人游戏时使用的概念(在服务器上)是什么?

时间:2015-03-29 07:44:52

标签: android session real-time multiplayer

我期待在Android中设计像QuizUp这样的实时多人游戏。我理解UI及其数据库部分,但我无法衡量编写服务器逻辑所涉及的复杂性。

所以我正在寻找的一些功能如下:

  1. 2个注册用户在应用中随机连接
  2. 他们互相竞争,在1分钟内回答一组5个问题
  3. 在更短的时间内更准确地回答的人得到一些分数
  4. 有人可以突出显示服务器上实现上述用例所需的构建块吗?

    我能想到的可能的构建块/模块是:

    1. 验证
    2. 会话管理
    3. 请从您的经验中指出服务器上没有数据库和用户界面的任何缺失块谢谢

1 个答案:

答案 0 :(得分:2)

我认为您在服务器中需要的是一个实时的Web应用程序框架,尽管我从未玩过QuizUp。有了这些东西,您就可以轻松处理业务逻辑。

以下伪代码写在Cettia中。这是我编写的实时Web应用程序框架,但由于只提供了Java服务器和JavaScript客户端,所以现在不能将它用于Android。 Neverthelss,我认为它可以帮助您粗略地编写业务逻辑,并在评估您需要的功能方面找到这样的框架。

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();
});