source: trunk/game/tests/test_dialogue.py @ 287

Revision 287, 4.2 KB checked in by orlandov, 10 years ago (diff)

Ticket #74: Patch by or1andov, Integrate dialogue GUI and engine into game.

It should now be possible to engage in a conversation with NPC characters by
selecting talk from their context menu. fixes[s:trac, t:74]

Known Issues:


  • the GUI is terrible. instead of a dialog window, we could use a better looking background image and fixed position widgets
  • requires PyYAML to be installed sudo apt-get install python-yaml easy_install PyYAML
  • we have to create a simple state model for the PC can remember tasks
  • need to make NPC's be able to remember things - need a model in NPCs for this
  • no way currently to give or take items from npc's; again, more model work. this could be done by adding another callback similar to start_quest. may be necessary for npc's to have an inventory for this
  • The dialogue engine is not hooked up to specific NPC's... it currently always loads dialogue/sample.yaml - need to have the dialogue looked up from some XML attribute
  • dialogue is not broken off if the PC moves away from the NPC
  • npc image is hardcoded, it should use the image specified in the yaml file
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18import sys
19import unittest
20from scripts.dialogue import DialogueEngine
21
22class TestDialogue(unittest.TestCase):
23    def setUp(self):
24        self.tree = {
25            'START': 'main',
26            'SECTIONS': {
27                'main': [
28                    { "say": "Greetings stranger" },
29                    { "responses": [
30                        ["Hi, can you tell me where I am?", "friendly"],
31                        ["Watch your words", "aggro"],
32                        ["This one toggles", "toggles", "show == True"],
33                        ["Always display this one", "display", "True and True"],
34                        ["response3", "stop"],
35                    ] }
36                ],
37                'friendly': [
38                    { "say": "You sure are lost" },
39                    { "responses": [
40                        ["Thanks, I know", "thanks"],
41                        ["Wait what did you say before?", "back"],
42                    ] }
43                ],
44                'thanks': [
45                    { "say": "We haven't seen one of your kind in ages" },
46                    { "responses": [
47                        ["Blah blah blah", "foo"],
48                        ["Say the other thing again", "back"],
49                    ] }
50                ],
51            }
52        }
53        # record actions in test_vars
54        test_vars = { "say": None, "responses": [] }
55
56        def say_cb(state, text):
57            state["say"] = text
58
59        self.replies = ["resp1", "back", "stop"]
60
61        def responses_cb(state, responses):
62            state['responses'] = responses
63
64        callbacks = {
65            "say": say_cb,
66            "responses": responses_cb
67        }
68
69        self.dialogue = DialogueEngine(self.tree, callbacks, test_vars)
70
71    def assert_say(self, text):
72        self.assertEqual(text, self.dialogue.state['say'])
73
74    def assert_responses(self, responses):
75        self.assertEqual(responses, self.dialogue.state['responses'])
76
77    def test_simple(self):
78        """Test basic dialogue interaction"""
79        self.dialogue.state['show'] = False
80        self.dialogue.run()
81
82        self.assert_say('Greetings stranger')
83        self.assert_responses([
84            ["Hi, can you tell me where I am?", "friendly"],
85            ["Watch your words", "aggro"],
86            ["Always display this one", "display", "True and True"],
87            ["response3", "stop"],
88        ])
89        self.dialogue.reply(0)
90
91        self.assert_say('You sure are lost')
92        self.assert_responses([
93            ["Thanks, I know", "thanks"],
94            ["Wait what did you say before?", "back"]
95        ])
96        self.dialogue.state['show'] = True
97        self.dialogue.reply(1)
98
99        self.assert_say('Greetings stranger')
100        self.assert_responses([
101            ["Hi, can you tell me where I am?", "friendly"],
102            ["Watch your words", "aggro"],
103            ["This one toggles", "toggles", "show == True"],
104            ["Always display this one", "display", "True and True"],
105            ["response3", "stop"],
106        ])
107        self.dialogue.reply(0)
108
109        self.assert_say('You sure are lost')
110        self.dialogue.reply(1)
111
112        self.assert_say('Greetings stranger')
113        self.dialogue.reply(0)
114
115        self.assert_say('You sure are lost')
116        self.dialogue.reply(0)
117
118        self.assert_say("We haven't seen one of your kind in ages")
119        self.dialogue.reply(1)
120
121        self.assert_say('You sure are lost')
122        self.dialogue.reply(1)
123
124        self.assert_say('Greetings stranger')
125
126if __name__ == "__main__":
127    unittest.main()
Note: See TracBrowser for help on using the repository browser.