1 15 package org.apache.tapestry.wap.quiz; 16 17 import java.io.BufferedReader ; 18 import java.io.FileReader ; 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 import java.net.URL ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.servlet.ServletContext ; 28 29 import org.apache.hivemind.Resource; 30 import org.apache.tapestry.ApplicationServlet; 31 import org.apache.tapestry.IRequestCycle; 32 import org.apache.tapestry.engine.IPropertySource; 33 import org.apache.tapestry.request.RequestContext; 34 import org.apache.tapestry.resource.ContextResource; 35 import org.apache.tapestry.util.StringSplitter; 36 37 50 51 public class Global implements Serializable 52 { 53 private boolean init = false; 54 55 private int points[]; 56 private String names[]; 57 58 private static final List easyQuestions = new ArrayList (); 59 private static final List mediumQuestions = new ArrayList (); 60 private static final List hardQuestions = new ArrayList (); 61 62 public static final int EASY_QUESTIONS = 1; 63 public static final int MEDIUM_QUESTIONS = 2; 64 public static final int HARD_QUESTIONS = 3; 65 66 public static final String EASY_LEVEL = "easy"; 67 public static final String MEDIUM_LEVEL = "medium"; 68 public static final String HARD_LEVEL = "hard"; 69 70 public static final String QUESTION_KEY = "question"; 71 public static final String ANSWER_KEY = "answer"; 72 public static final String CHOICES_KEY = "choices"; 73 74 public List getHighscores() 75 { 76 List result = new ArrayList (); 77 for (int i = 0; i < 9; i++) 78 { 79 String name = names[i]; 80 if (!"nobody".equals(name)) 81 { 82 Map map = new HashMap (); 83 map.put("name", name); 84 map.put("score", new Integer (points[i])); 85 result.add(map); 86 } 87 } 88 return result; 89 } 90 91 94 public boolean isHighscore(int score) 95 { 96 return (score >= points[9]); 97 } 98 99 102 public boolean isNewHighscore(int score) 103 { 104 return (score > points[9]); 105 } 106 107 111 public int addHighscore(int score, String name) 112 { 113 int i = -1; 114 if (isNewHighscore(score)) 115 { 116 for (i = 0; i <= 9; i++) 117 { 118 if (score > points[i]) 119 break; 120 } 121 for (int j = 9; j >= i + 1; j--) 122 { 123 points[j] = points[j - 1]; 124 names[j] = names[j - 1]; 125 } 126 points[i] = score; 127 names[i] = name; 128 } 129 return i; 130 } 131 132 138 public int getQuestionSet(String level) 139 { 140 if (HARD_LEVEL.equals(level)) 141 return HARD_QUESTIONS; 142 if (MEDIUM_LEVEL.equals(level)) 143 return MEDIUM_QUESTIONS; 144 return EASY_QUESTIONS; 145 } 146 147 154 public Map getQuestion(int questionToBeAsked, int questionSet) 155 { 156 String line; 157 List list; 158 159 switch (questionSet) 160 { 161 case EASY_QUESTIONS : 162 list = easyQuestions; 163 break; 164 case MEDIUM_QUESTIONS : 165 list = mediumQuestions; 166 break; 167 default : 168 list = hardQuestions; 169 break; 170 } 171 line = (String ) list.get(questionToBeAsked); 172 StringSplitter splitter = new StringSplitter(';'); 173 String [] result = splitter.splitToArray(line); 174 HashMap map = new HashMap (); 175 map.put(QUESTION_KEY, result[0]); 176 int answer = Integer.parseInt(result[1]) - 1; 177 map.put(ANSWER_KEY, new Integer (answer)); 178 int length = result.length - 2; 179 String [] choices = new String [length]; 180 System.arraycopy(result, 2, choices, 0, length); 181 map.put(CHOICES_KEY, choices); 182 return map; 183 } 184 185 191 public int getNumberOfQuestions(int questionSet) 192 { 193 switch (questionSet) 194 { 195 case EASY_QUESTIONS : 196 return easyQuestions.size(); 197 case MEDIUM_QUESTIONS : 198 return mediumQuestions.size(); 199 default : 200 return hardQuestions.size(); 201 } 202 } 203 204 public void initialize(IRequestCycle cycle) 205 { 206 if (!init) 207 { 208 RequestContext requestContext = cycle.getRequestContext(); 209 ApplicationServlet servlet = requestContext.getServlet(); 210 ServletContext context = servlet.getServletContext(); 211 IPropertySource propertySource = cycle.getEngine().getPropertySource(); 212 213 Resource webInfLocation = new ContextResource(context, "/WEB-INF/"); 214 215 Resource webInfAppLocation = 216 webInfLocation.getRelativeResource(servlet.getServletName() + "/"); 217 218 readQuestions( 219 easyQuestions, 220 webInfAppLocation, 221 propertySource.getPropertyValue("easyquestionsfile")); 222 readQuestions( 223 mediumQuestions, 224 webInfAppLocation, 225 propertySource.getPropertyValue("mediumquestiosfile")); 226 readQuestions( 227 hardQuestions, 228 webInfAppLocation, 229 propertySource.getPropertyValue("hardquestionsfile")); 230 points = new int[10]; 231 names = new String [10]; 232 for (int i = 0; i <= 9; i++) 233 { 234 points[i] = 0; 235 names[i] = "nobody"; 236 } 237 init = true; 238 } 239 } 240 241 248 private void readQuestions(List questions, String questionsFile) throws IOException 249 { 250 BufferedReader reader = null; 251 String line; 252 reader = new BufferedReader (new FileReader (questionsFile)); 253 while ((line = reader.readLine()) != null) 254 { 255 if (line.trim().equals("")) 256 break; 257 questions.add(line); 258 } 259 } 260 261 private void readQuestions(List questions, Resource folder, String filename) 262 { 263 Resource resource = folder.getRelativeResource(filename); 264 265 URL url = resource.getResourceURL(); 266 267 try 268 { 269 readQuestions(questions, url.getFile()); 270 } 271 catch (IOException e) 272 { 273 e.printStackTrace(System.err); 274 } 275 } 276 277 } 278 | Popular Tags |