1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package aigames.soccer.application;
21
22 import aigames.soccer.Move;
23
24 import org.dom4j.Document;
25 import org.dom4j.DocumentHelper;
26 import org.dom4j.Element;
27
28 import java.text.DateFormat;
29 import java.text.SimpleDateFormat;
30
31 import java.util.Date;
32 import java.util.Iterator;
33
34
35 /***
36 * Class for creating log messages for observer clients.
37 * @version $Id: LogMessageBuilder.java,v 1.5 2004/05/08 21:55:28 mwerla Exp $
38 */
39 public class LogMessageBuilder {
40 private Element logElement;
41 private DateFormat df = new SimpleDateFormat("HH:mm:ss");
42
43 /***
44 * Class constructor - creates class instance and initiates log structure.
45 */
46 public LogMessageBuilder() {
47 logElement = DocumentHelper.createElement("message");
48 logElement.addAttribute("type", "log");
49 logElement.addAttribute("id", "NOT_SET");
50 }
51
52 /***
53 * Adds move to log.
54 * @param m Move to add.
55 * @param date Date at which move mwas made.
56 */
57 public void addMove(final Move m, final Date date) {
58 Element moveElement = DocumentHelper.createElement("move");
59 moveElement.addAttribute("time", df.format(date));
60
61 for (Iterator iter = m.getSteps().iterator(); iter.hasNext();) {
62 Integer step = (Integer) iter.next();
63 moveElement.addElement("type").setText(step.toString());
64 }
65
66 logElement.add(moveElement);
67 }
68
69 /***
70 * Adds player name to log.
71 * @param name Player name.
72 */
73 public void addName(final String name) {
74 final String nodeName = "name";
75 addTextElement(nodeName, name);
76 }
77
78 /***
79 * Adds map informations to log.
80 * @param mapWidth Field width.
81 * @param mapHeight Field height.
82 * @param goalHeight Goal height.
83 */
84 public void addMap(final int mapWidth, final int mapHeight,
85 final int goalHeight) {
86 Element mapElement = DocumentHelper.createElement("map");
87 mapElement.addAttribute("width", String.valueOf(mapWidth));
88 mapElement.addAttribute("height", String.valueOf(mapHeight));
89 mapElement.addAttribute("goal", String.valueOf(goalHeight));
90 logElement.add(mapElement);
91 }
92
93 /***
94 * Adds to log information about player which starts game.
95 * @param name Player name.
96 */
97 public void addBegin(final String name) {
98 final String nodeName = "begin";
99 addTextElement(nodeName, name);
100 }
101
102 /***
103 * Adds to log information about game winner.
104 * @param name
105 */
106 public void addWinner(final String name) {
107 final String nodeName = "winner";
108 addTextElement(nodeName, name);
109 }
110
111 /***
112 * Adds to log information about error which occured during game.
113 * @param who User name which message casued error.
114 * @param reason Message explaining error cause.
115 */
116 public void addError(final String who, final String reason) {
117 Element errorElement = DocumentHelper.createElement("error");
118 errorElement.addAttribute("name", who);
119 errorElement.setText(reason);
120 logElement.add(errorElement);
121 }
122
123 /***
124 * Helper method, which adds text element to log.
125 * @param nodeName Name of the element.
126 * @param value Element value.
127 */
128 public void addTextElement(final String nodeName, final String value) {
129 Element beginElement = DocumentHelper.createElement(nodeName);
130 beginElement.setText(value);
131 logElement.add(beginElement);
132 }
133
134 /***
135 * Returns document containing entire log.
136 * @return Document containing entire log.
137 */
138 public Document getMessage() {
139 return DocumentHelper.createDocument((Element) logElement.clone());
140 }
141 }