Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/las.py
blob: 9f31b5761cde5e0e388f2ad54514f3d4e3a0dc65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/env python
import sys
import os
import dbus
import random
import time
import csnd
from dict import Dict
from dict import Word

# My class for key detection
class _Getch:
	"""Gets a single character from standard input.  Does not echo to the screen."""
	def __init__(self):
		try:
			self.impl = _GetchWindows()
		except ImportError:
			self.impl = _GetchUnix()

	def __call__(self): return self.impl()

class _GetchUnix:
	def __init__(self):
		import tty, sys

	def __call__(self):
		import sys, tty, termios
		fd = sys.stdin.fileno()
		old_settings = termios.tcgetattr(fd)
		try:
			tty.setraw(sys.stdin.fileno())
			ch = sys.stdin.read(1)
		finally:
			termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
		return ch

class _GetchWindows:
	def __init__(self):
		import msvcrt

	def __call__(self):
		import msvcrt
		return msvcrt.getch()

class Listenspell():

	def __init__(self, SQLiteDB):

		self.skill_level = 0
		self.level_threshold = 5
		self.points = 0
		self.words_played = 0
		self.words_correct = 0
		self.init = False
		self.dict_obj = Dict(SQLiteDB)	#Always intitiate first Dict object then Word object
		self.word_obj = Word()


	def start_speechd(self):  # Currently not in use have to manually start sugar-speechd
		pid = os.fork()
		if pid:
			return
		else:
			# we are the child
			os.popen("./sugar-speechd")

	def play_sound(self,event):
		if event == "begin":
			os.popen("aplay --quiet begin.wav")
		elif event == "correct":
			os.popen("aplay --quiet correct.wav")
		elif event == "incorrect":
			os.popen("aplay --quiet incorrect.wav")

	def set_skill_level(self,level):
		self.skill_level = level

	def get_skill_level(self):
		return self.skill_level

	def get_words_played(self):
		return self.words_played

	def get_words_correct(self):
		return self.words_correct

	def get_points(self):
		return self.points

	def ans_correct(self, wordid):
		self.play_sound("correct")
		self.points = self.points + self.skill_level
		self.words_correct = self.words_correct + 1
		self.word_obj.update_score(wordid)

	def ans_incorrect(self, wordid):
		self.play_sound("incorrect")
		self.word_obj.update_score(wordid, "incorrect")

	def clear_screen(self,numlines=100):
		"""Clear the console.
        numlines is an optional argument used only as a fall-back.
        """
		import os
		if os.name == "posix":
			# Unix/Linux/MacOS/BSD/etc
			os.system('clear')
		elif os.name in ("nt", "dos", "ce"):
			# DOS/Windows
			os.system('CLS')
		else:
			# Fallback for other operating systems.
			print '\n' * numlines 

	def load_wordid(self, num_words):
		temp_list = []
		self.wordid_list = []
		temp_list = self.dict_obj.get_random_wordid(length = self.skill_level,numwords = num_words)
		for(wordid, ) in temp_list:
			self.wordid_list.append(wordid)
		return self.wordid_list

	def get_word_info(self,wordid, attribute):
		if self.word_obj.get_wordid() != wordid:
			self.word_obj.__init__(identifier = "wordid", value= wordid)
			self.words_played = self.words_played + 1

		if attribute == "def":
			return self.word_obj.get_def()
		elif attribute == "usage":
			return self.word_obj.get_usage()
		elif attribute == "word":
			return self.word_obj.get_word()  
		elif attribute == "phnm":
			return self.word_obj.get_word()  # For the time being

		else: return "Invalid attribute"

	def say_text(self, text):
		if self.init == False:
			try:
				bus = dbus.SessionBus()
				self.espeak_object = bus.get_object('org.laptop.Speech','/org/laptop/Speech')
				self.init = True
			except dbus.exceptions.DBusException:
				print "Speech Server not turned on."
				return False
				
		text = str(text)
		self.espeak_object.SayText(text)

	def get_key(self):
		for longestinput in range(15):
			inkey = _Getch()
			for i in xrange(sys.maxint):
				k=inkey()
				if k<>'':break
				elif k == '\r': break
			return k

	def exit_game(self):
		self.say_text("goodbye")
		sys.exit()