Source code: marauroa/game/RPScheduler.java
1 /* $Id: RPScheduler.java,v 1.8 2003/12/08 12:43:52 arianne_rpg Exp $ */
2 /***************************************************************************
3 * (C) Copyright 2003 - Marauroa *
4 ***************************************************************************
5 ***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
13 package marauroa.game;
14
15 import java.util.*;
16 import marauroa.marauroad;
17
18 /** This class represent a scheduler to deliver action by turns, so every action
19 * added to the scheduler is executed on the next turn.
20 * Each object can cast as many actions as it wants. */
21 public class RPScheduler
22 {
23 /** a HashMap<RPObject.ID,RPActionList> of entries for this turn */
24 private HashMap actualTurn;
25 /** a HashMap<RPObject.ID,RPActionList> of entries for next turn */
26 private HashMap nextTurn;
27 /** Turn we are executing now */
28 private int turn;
29
30 static class ActionInvalidException extends Exception
31 {
32 ActionInvalidException(String attribute)
33 {
34 super("Action is invalid: It lacks of mandatory attribute ["+attribute+"]");
35 }
36 }
37
38 /** Constructor */
39 public RPScheduler()
40 {
41 turn=0;
42 actualTurn=new HashMap();
43 nextTurn=new HashMap();
44 }
45
46 /** Add an RPAction to the scheduler for the next turn
47 * @param action the RPAction
48 * @throws ActionInvalidException if the action lacks of sourceid attribute.*/
49 public synchronized void addRPAction(RPAction action) throws ActionInvalidException
50 {
51 marauroad.trace("RPScheduler::addRPAction",">");
52 try
53 {
54 RPObject.ID id=new RPObject.ID(action);
55 marauroad.trace("RPScheduler::addRPAction","D","Add RPAction("+action+") from RPObject("+id+")");
56
57 if(nextTurn.containsKey(id))
58 {
59 RPActionList list=(RPActionList)nextTurn.get(id);
60 list.add(action);
61 }
62 else
63 {
64 RPActionList list=new RPActionList();
65 list.add(action);
66 nextTurn.put(id,list);
67 }
68 }
69 catch(Attributes.AttributeNotFoundException e)
70 {
71 marauroad.trace("RPScheduler::addRPAction","X","Action("+action+") has not requiered attributes");
72 throw new ActionInvalidException(e.getAttribute());
73 }
74 finally
75 {
76 marauroad.trace("RPScheduler::addRPAction","<");
77 }
78 }
79
80 /** For each action in the actual turn, make it to be run in the ruleProcessor
81 * Depending on the result the action needs to be added for next turn. */
82 public void visit(RPRuleProcessor ruleProcessor)
83 {
84 marauroad.trace("RPScheduler::visit",">");
85
86 try
87 {
88 Iterator it=actualTurn.entrySet().iterator();
89
90 while(it.hasNext())
91 {
92 Map.Entry val=(Map.Entry)it.next();
93
94 RPObject.ID id=(RPObject.ID)val.getKey();
95 RPActionList list=(RPActionList)val.getValue();
96
97 ruleProcessor.approvedActions(id,list);
98
99 Iterator action_it=list.iterator();
100 while(action_it.hasNext())
101 {
102 try
103 {
104 RPAction action=(RPAction)action_it.next();
105 RPAction.Status status=ruleProcessor.execute(id,action);
106
107 /* If state is incomplete add for next turn */
108 if(status.equals(RPAction.STATUS_INCOMPLETE))
109 {
110 addRPAction(action);
111 }
112 }
113 catch(Exception e)
114 {
115 marauroad.trace("RPScheduler::visit","X",e.getMessage());
116 }
117 }
118 }
119 }
120 catch(Exception e)
121 {
122 marauroad.trace("RPScheduler::visit","X",e.getMessage());
123 }
124 finally
125 {
126 marauroad.trace("RPScheduler::visit","<");
127 }
128 }
129
130 /** This method change the turn and delete all the actions in the actual turn */
131 public synchronized void nextTurn()
132 {
133 marauroad.trace("RPScheduler::nextTurn",">");
134
135 ++turn;
136 actualTurn.clear();
137 actualTurn=nextTurn;
138 nextTurn=new HashMap();
139
140 marauroad.trace("RPScheduler::nextTurn","<");
141 }
142 }