简体   繁体   English

使用C#消费测验Web服务

[英]Consuming a Quiz web service with C#

I am currently making a Quiz web service using C# in Unity 3D. 我目前正在Unity 3D中使用C#制作Quiz Web服务。 I am relatively new to C#. 我对C#比较陌生。

I've ran into a problem that I cannot seem to fix on my own. 我遇到了一个问题,我似乎无法自己解决。 I am able to retrieve all of the questions and possible answers from the database at once. 我能够一次从数据库中检索所有问题和可能的答案。 My problem is that I cannot split them up and display one question and set of answers at a time. 我的问题是我无法拆分它们,一次只能显示一个问题和一组答案。 I am not sure how to go about this! 我不确定该怎么做!

Here is my method which calls the web service: 这是我调用Web服务的方法:

public void retrieveQuizObject(){

    QuizService service = new QuizService();

    quizModel q = new quizModel();

    quizModel[] quizArray = new quizModel[3];

    List<quizModel> quizList = new List<quizModel>();

    //consuming web service
    object[] qArray = quizList.ToArray();
    qArray = service.playQuiz();


    foreach(object element in qArray)
        {
        quizArray[n] = (quizModel)element;
        Debug.Log ("Array Size: " + quizArray.Length);
        Debug.Log ("Questions: " + quizArray[n].question);//just checking to see questions returned
        }
  }

Here is the web service SQL: 这是Web服务SQL:

"SELECT * from quiz ORDER BY RAND() LIMIT 3"

The quiz table has the following columns: QuizId, Question, Answer A, Answer B, Answer C, Answer D. Answer A will always be the right answer. 测验表包含以下列:QuizId,问题,答案A,答案B,答案C,答案D。答案A永远是正确的答案。 I plan on shuffling them later! 我打算稍后改组它们!

Using a GUI interface, I want to display each question and the four possible answers one at a time instead of all at once (like WHO WANTS TO BE A MILLIONAIRE). 我想使用GUI界面一次显示每个问题和四个可能的答案,而不是一次显示所有问题(例如谁想要成为百万富翁)。 When the user chooses their option, a new question should appear. 当用户选择他们的选项时,将出现一个新问题。 I have tried many variations of loops and tried to iterate through the loop only when a GUI button is pressed but could not figure it out. 我尝试了多种循环,并尝试仅在按下GUI按钮但无法弄清楚时才遍历循环。

How should I approach this? 我应该如何处理? Which loop should I use and how do I pause it? 我应该使用哪个循环以及如何暂停它?

I would really appreciate the help! 我非常感谢您的帮助! Thank you! 谢谢!

I am not sure if the above code is your goal in a pseudo form, or if you actually have the service/client working, if not, seems you need to setup a framework first before you get into the specifics of Table Design and Game Mechanics. 我不确定上面的代码是否是以伪形式实现的目标,或者您是否确实在运行服务/客户端(如果没有),看来您需要首先设置框架,然后才能了解表设计和游戏机制的细节。 。

I would start building a framework around Web API, they are pretty straight forward if you follow a few good tutorials. 我将开始围绕Web API构建框架,如果您遵循一些不错的教程,它们将非常简单。

http://www.asp.net/web-api http://www.asp.net/web-api

Then for persistency, you can use use Entity Framework if you are going with SQL, or a NoSQL solution might make more sense such as Redis: 然后为了保持持久性,如果要使用SQL,则可以使用Use Entity Framework,或者使用NoSQL解决方案可能更有意义,例如Redis:

http://www.d80.co.uk/post/2011/05/12/Redis-Tutorial-with-ServiceStackRedis.aspx http://www.d80.co.uk/post/2011/05/12/Redis-Tutorial-with-ServiceStackRedis.aspx

Once you have your end points exposed, you will use WWW to consume it in Unity3D 暴露了端点之后,您将使用WWW在Unity3D中使用它

The following is what I use to instantiate a game object as a packet, which will self dispose when its finished, you pass in a callback function that receives a string so you can handle your response asynchronously. 以下是我用来将游戏对象实例化为数据包的方法,该数据包在完成时会自行处理,您传入一个接收字符串的回调函数,以便可以异步处理响应。

class CoroutineServiceAsync : IServiceAsync

{

    public void SendRequest(string url, Action<string> callback, string data)

    {

        Debug.Log("Sending service call with data: " + data);

        GameObject obj = new GameObject("ServiceCall: " + data);

        CoRoutineRequest packet = obj.AddComponent<CoRoutineRequest>();

        packet.SendRequest(url, callback, data);

    }

}



class CoRoutineRequest : MonoBehaviour

{

    void Start()

    {

        DontDestroyOnLoad(gameObject);

    }



    public void SendRequest(string url, Action<string> callback, string data)

    {

        StartCoroutine(StartSendRequest(url, callback, data));

    }



    IEnumerator StartSendRequest(string url, Action<string> callback, string data)

    {

        WWW www = new WWW(url + "/" + data);

        yield return www;



        if (callback != null)

            callback(www.text);



        Destroy(gameObject);

    }

}

You would get a Question and Answer row after the player answers or starts. 玩家回答或开始后,您将获得“问答”行。 If you don't want the answer options to pop up all at once, then as you are looping/iterating through the answer options, you would have a delay. 如果您不想一次全部弹出答案选项,那么当您循环/迭代答案选项时,将会有一个延迟。

I think the only loop you need is for displaying the answer options, the rest is event driven based on player input (when they select an answer) and when the Question and Answer row packet comes in with your call back (that will trigger the data to be displayed in your GUI). 我认为您唯一需要的循环是显示答案选项,其余的则是基于玩家的输入(当他们选择答案时)以及当您的回叫出现问与答行数据包时触发事件驱动的(这将触发数据)在您的GUI中显示)。

Depending on what king of security you want, you might want to handle some of the logic server side, and thus would require managing sessions. 根据所需的安全性之王,您可能需要处理某些逻辑服务器端,因此需要管理会话。 I would start simple and assume the player is playing nice. 我会从简单开始,假设玩家的表现很好。

Hope I understood the question correctly and that lends some help. 希望我正确理解了这个问题,并能提供一些帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM