Based on ChatGPT launched the "You Say I Guess" mini game

Summary

How can AIGC, GPT, and casual games be combined?
The combination of AIGC, GPT and mini games brings new possibilities to the gaming experience. AIGC (Artificial Intelligence Game Content), as an artificial intelligence technology, can automatically generate game elements such as tasks, plots, and character dialogues, and inject rich and diverse content into mini-games. As a powerful natural language processing model, GPT (Generative Pre-trained Transformer) assumes the role of generating various game content in the AIGC system, such as dialogues, tasks and maps. GPT can learn from a large amount of text data, generate logical and coherent game elements, and inject novel, interesting and personalized elements into small games.

1 Introduction

Since ChatGPT was launched last year, various applications based on ChatGPT have emerged in an endless stream. The author has also written many articles about ChatGPT before, helping many beginners to use ChatGPT correctly. It has received a lot of positive feedback from readers. Recently, some readers have given feedback on whether ChatGPT can be used to make the interactive game "You Say I Guess" for live broadcast rooms such as Douyin, Kuaishou, and Video Account.

Keywords: ChatGPT, AIGC, GPT-4, large model, RTC real-time audio and video, casual games

1.jpg

2. Introduction to the tools used in the demo

Before introducing the implementation process of AIGC mini-games, I will first introduce the tools used in this demo, the AI ​​efficient production tools headed by GPT and the live broadcast SDK.

  1. Live product: RTC SDK Android Java real-time audio and video implementation process- Developer Center
  2. GPT4.0:New bing
  3. GPT3.5:ChatGPT

3. AIGC live room casual games

Since the third-party live broadcast platform does not open the barrage interface, here we use the Instant RTC platform to access the mini game, that is, the official RTC development document . If readers need to access other live broadcast platforms (such as Douyin, Kuaishou, Station B) etc.), you can search for the relevant bullet chat interface by yourself and replace the bullet chat interface in this article.

Based on practical experience, there are many benefits to using instant RTC real-time audio and video services . The full-link self-developed audio and video engine used in instant RTC products has a minimum delay of 79ms. What is the concept of 79ms? The user experience is beyond the human body. perceived. Multi-platform development is the current trend. I have been using instant RTC to realize the live broadcast room. The access process is simple, convenient and easy to use, which helps our project go online quickly and seize market traffic many times. According to the official website, instant real-time audio and video services can adapt to as many as 15,000+ terminal devices and are compatible with 25 kinds of development platforms. One-time development and multi-terminal launch have saved me a lot of development costs and enabled us to focus on the business itself. There are also industry-leading Qos strategies and powerful 3A processing capabilities, which are recommended to independent developers and entrepreneurial teams. Developers can quickly realize real-time voice capabilities through instant SDK in games, which is suitable for quickly verifying the feasibility of projects.

Let’s first look at the final effect of this article (gif compression is powerful, readers can directly run the attached code or private message the author to get the demo): left:
homeowner’s perspective right: player’s perspective (viewer’s perspective)
2.gif

4 Game rule settings

Closer to home, first set up the game interaction process. First create a login screen where users can enter their room number and nickname. Next, the user can click the [Enter Room] and [Create Room] buttons.

  • If you click the enter room button, you need to verify whether the room exists, if it exists, enter, if it does not exist, prompt the user that the room does not exist.
  • If you click to create a room, you need to verify whether the room exists. If it exists, it prompts that the creation failed. If it does not exist, create the room directly and enter.

In the room:

  • If you are a homeowner, you can set guessing words, such as "water cup". Then assemble the prompt words: "Please describe the water cup, within 10 characters, do not appear 2 words of water cup".
  • After getting a reply from ChatGPT, use the content of the reply as the "reminder of this round" in the live broadcast room.
  • The homeowner can click the refresh button every time, send the same prompt word to request ChatGPT reply, and refresh the "current round prompt"
  • The audience guesses the words according to the "reminders of this round", and the avatars of the four users who guess the words the fastest will appear on the big screen in the live broadcast room, enjoying the honor of the winners.

Viewer Perspective (Player Perspective)
3.jpg

The game login interface and word guessing interface are shown above. Astute readers may find a bug: if the first user answers correctly and gets posted on the wall, subsequent users can plagiarize. There is indeed this problem. The main reason here is that the author has not built a server, so it is not easy to directly control the user's barrage. The correct approach should be to replace the correct answer with an asterisk (*) to prevent the answer from being plagiarized. It is left to the reader to solve this little problem here. The entire game logic flow is as follows:

4.png

5 code implementation

5.1 Docking with ChatGPT

The implementation code for docking with ChatGPT is in the front ** "Everyone can use ChatGPT4.0 to do Avatar virtual human live broadcast" **The article describes how to call in detail, so I won’t go into details here. After downloading the source code in the attachment, execute the following command to install the necessary libraries:

npm install

Next, start the http service:

node main.js

Still use ** "Everyone can use ChatGPT4.0 to do Avatar virtual human live broadcast" ** The interface packaged in the article (including chatGPT3.5 and BingGPT4.0), here we use OpenAI's ChatGPT3.5. As shown below, after introducing the chatGPT interface, import Express to start the Http service.

var chatGPT = require("./robot/robot").chatGPT
var express = require('express');
var app = express(); 
app.post('/ask', function (req, res) {
    
    
    let ask = req.query["ask"];  
    chatGPT(ask, function (succ, txt) {
    
    
        if (succ) {
    
    
            res.send({
    
     state: 0, txt });
        } else {
    
    
            res.send({
    
     state: -1, txt: "" });
        }
    })
})

var server = app.listen(8888, function () {
    
    
    var port = server.address().port
    console.log("应用实例,访问地址为 http://%s:%s", '0.0.0.0', port)

})

It only contains a POST interface /ask, the user only needs to pass in the prompt word, internally pass it to ChatGPT, and return the content of ChatGPT reply to the user.

5.2 Access to Architectural RTC

Instant RTC is a real-time audio and video SDK with a minimum delay of 79ms and smooth video calls under 70% packet loss. It can provide us with stable, multi-platform interoperable real-time audio and video capabilities. On the basis of audio and video calls, it provides advanced audio and video processing such as super-resolution, background segmentation, and scene-based noise reduction. Virtual world solutions.
It covers users all over the world and serves more than 3 billion minutes per day. For more details, please go to the official development document of Jigou . Next, we will use the powerful capabilities of Instant RTC to develop real-time voice and in-room barrage synchronization capabilities. The following is the general process of accessing Instant RTC.
5.png

Download the latest SDK from the official website , and after introducing the SDK into the project, create an instant RTC engine:

public class RTCMngr implements RTCHandler.IRTCEventListener {
    
    

    private static final String TAG = "RTCMngr";
    private ZegoExpressEngine mRTCEngine;
    private static RTCMngr mInstance;
    private RTCHandler mRTCHandler = new RTCHandler(this);
    private ZegoVideoConfig videoCfg = null;
    private IListener msgListener;
    private static Map<String, String> roomInfo = new HashMap<>();

    private RTCMngr(Application app) {
    
    
        mRTCEngine = createRTCEngine(app, mRTCHandler);
    }

    public static RTCMngr getInstance(Application app) {
    
    
        if (null == mInstance) {
    
    
            synchronized (RTCMngr.class) {
    
    
                if (null == mInstance) {
    
    
                    mInstance = new RTCMngr(app);
                }
            }
        }
        return mInstance;
    }

    private ZegoExpressEngine createRTCEngine(Application app, IZegoEventHandler handler) {
    
    
        ZegoEngineProfile profile = new ZegoEngineProfile();
        profile.appID = KeyCenter.APP_ID;
        profile.scenario = ZegoScenario.GENERAL;  // 通用场景接入
        profile.application = app;
        ZegoExpressEngine engine = ZegoExpressEngine.createEngine(profile, handler);
        return engine;
    }

login room

Next, log in to the room. You first need to obtain the login room Token, and Tokenthe method of obtaining is encapsulated in RunOnServerthe class.

It is worth noting that RunOnServerall functions in the class require the reader to move them to the server to run, otherwise there will be a security risk!

After having Tokenit, the loginRoom function called next will judge whether the login is successful ZegoExpressEnginethrough the parameters in the callback function error. If error=0not, the login is successful, otherwise, you need to go to the official website to check the specific cause of the error.

//登录房间
public void loginRoom(User user, CB cb) {
    
    

    ZegoUser zuser = new ZegoUser(user.userId, user.userName);
    ZegoRoomConfig config = new ZegoRoomConfig();
    config.token = RunOnServer.getToken(user.userId, user.roomId); // 请求开发者服务端获取
    config.isUserStatusNotify = true;
    mRTCEngine.loginRoom(user.roomId, zuser, config, (int error, JSONObject extendedData) -> {
    
    
        if (error == 0) {
    
    
            Log.e(TAG, "登录房间:" + user.roomId);
            String hostId = null;
            try {
    
    
                hostId = extendedData.getString("hostId");
            } catch (JSONException e) {
    
    
                hostId = "";
            }
            cb._complete(error == 0, hostId);
        } else {
    
    
            Log.e(TAG, "Login Error, errorCode=" + error);
            cb._complete(error == 0, "login room error code:" + error);
        }
    });

}

exit the room

If there is a login, there will be a logout. Before exiting the room, stop streaming. If there is streaming, you need to stop streaming. Then call ZegoExpressEnginethe logoutRoomfunction.

    //离开房间
    public void leaveRoom(String roomId, CB cb) {
    
    
        mRTCEngine.stopPreview();
        mRTCEngine.stopPublishingStream();
        mRTCEngine.logoutRoom(roomId, new IZegoRoomLogoutCallback() {
    
    
            @Override
            public void onRoomLogoutResult(int errorCode, JSONObject extendedData) {
    
    
                cb._complete(errorCode == 0, "");
            }
        });
    }

Send bullet chatting
In the previous part, we realized the login and exit from the room. With the concept of the room, then we can send the bullet chat message to the room, so that everyone in the room can receive the bullet chat message, just call the function ZegoExpressEnginedirectly sendBarrageMessage:

    /**
     * 发送弹幕消息
     */
    public void sendMsg(Msg msg, CB cb) {
    
     
        mRTCEngine.sendBarrageMessage(msg.toUID,
                msg.decMsg, new IZegoIMSendBarrageMessageCallback() {
    
    
                    @Override
                    public void onIMSendBarrageMessageResult(int errorCode, String messageID) {
    
    
                        cb._complete(errorCode == 0, messageID);
                    }
                });

    }

    //其他相关代码略.....
}

The above code creates functions such as RTC engine, login room, leave room, send barrage, push stream, etc. Based on these basic capabilities, we can assemble more complex functions. Next, the streaming capability is encapsulated. Since the real-time voice screen is 4a user avatar, there is no need for real-time video streaming. Therefore, you only need to use the real-time voice function. When pulling the stream, canvaspass in the function that needs to use the parameter null. When streaming, call muteVideoPublishthe function to disable video streaming. In addition, before calling startPreview, call to enableCameraclose the camera to avoid SDK requesting camera permission. Or simply do not request camera permissions to ensure that the camera will not be called to push the real-time images.

//推流
public void pushStream(String streamId) {
    
    
    Log.e(TAG, "push streamID" + streamId);
    mRTCEngine.startPublishingStream(streamId);
    mRTCEngine.startPreview(null);

}

5.3 Homeowner setting the topic

The homeowner has the right to create questions, and the rhythm of the entire game is controlled by the homeowner, such as: changing questions, refreshing reminders, etc. The most important interface is to connect with the ChatGPT service. The general process is as follows:

6.png

The specific code is as follows:

private void reqGPT() {
    
    
    if (wordAns.length() <= 0) return;
    chatGPT.ask("请描述" + wordAns + ",10个字以内,不要出现" + wordAns + wordAns.length() + "个字", new CB() {
    
    
        @Override
        public void onResult(boolean succ, String res) {
    
    
            if (!succ) {
    
    
                toast(res);
            } else {
    
    
                Msg msg = Msg.newRoundMsg(mUser.roomId, res, mUser.userId);
                rtcMngr.sendMsg(msg, new CB() {
    
    
                    @Override
                    public void onResult(boolean succ, String err) {
    
    
                        onlineIds.clear();
                        onNewRound(msg);
                    }
                });
            }
        }
    });
}

Notice that in the third line of code, the prompt words of ChatGPT are assembled, and a limit of 10 words is added to prevent ChatGPT from returning a large description; in addition, the keyword xx is added to prevent ChatGPT from guessing in the returned content. Words are revealed.

The answer is currently saved by the homeowner. After receiving the barrage from the audience, the homeowner compares them one by one. If the answer is correct and there are no more than 4 users who have answered correctly, the user's avatar will be displayed on the screen. Here, since we do not have a separate server connected to Instant RTC, the homeowner sends a special barrage message containing the user information on the screen. After receiving this special barrage message, everyone parses and displays the user data on the screen.

5.4 Audience Guess Questions

The audience guessing logic is relatively simple. After the user directly enters the guessed answer, he or she can directly send a barrage message. Click the send button, and there are two different logics for homeowners and viewers:

/**
 * 点击发送按钮
 */
public void onClkSendMsgBtn(View view) {
    
    
    String txt = sendET.getText().toString().trim();
    if (txt.length() <= 0) return;
    Msg msg = Msg.newRoomMsg(mUser.roomId, txt, mUser.userId, mUser.userName);
    if (mUser.isCreator) {
    
    //房主重新设置猜测词
        newRound(txt);
    } else {
    
    //观众发送弹幕
        rtcMngr.sendMsg(msg, new CB() {
    
    
            @Override
            public void onResult(boolean succ, String err) {
    
    
                addBarrageItem(msg);
            }
        });
    }
    sendET.setText("");
}

6. Instant RTC social game solution

Now more and more companies are trying to introduce mini-games into pan-entertainment social products to improve the fun of user social interaction and product stickiness, and at the same time to verify the new ideas of product commercialization. For small and medium-sized enterprises with small budgets to self-develop mini-games or companies verifying mini-game projects. The author recommends that you can choose to connect to a third-party audio and video SDK with mini games, such as constructing RTC real-time audio and video.

It is understood that Jigo has reached cooperation with a number of leading game studios and launched a social + small game solution. Currently, it supports many popular casual games such as billiards, bumper battle, who is undercover, and guess what you draw. It is applied to common business scenarios in the pan-entertainment industry, such as video live broadcast, one-on-one dating, voice radio, and chat rooms. At the same time, it also supports localized games such as Ludo, Domino, and UNO, providing new market competition for Chinese companies going overseas. help.

In addition, the instant mini game library is continuously expanding. Developers only need to access it once to continuously update and launch new games. It is suitable for teams or developers who want to quickly verify user preferences at low cost. Even if you don’t want to use Instant RTC, you are strongly recommended to check out the relevant demos, which may bring you a lot of entrepreneurial inspiration. Official website Demo list URL

7. Mini-game extension

This article mainly implements the "You Say I Guess" mini-game through the homeowner setting the topic, GPT providing the corresponding description, and the audience guessing the logic. The description of GPT has the stimulation of opening the blind box, which is the highlight of this game.

Of course, gameplay can vary. Bloggers offer several ideas, such as:

  1. GPT questions, no need for homeowners, all audiences guess the questions
  2. Audience poses questions, GPT guesses words

In addition, it is not only a small game of "you say I guess", but also some more complex language games, such as: who is undercover, werewolf killing and other popular games nowadays; in addition, it can also be combined with AI painting to realize "you draw I guess" mini-game. It only needs a few lines of code to connect to build RTC capability, and almost all real-time games can be easily connected.

Guess you like

Origin blog.csdn.net/RTC_SDK_220704/article/details/131723075