package foundation.capacity;
|
|
import java.util.Iterator;
|
|
import foundation.json.IJSONProvider;
|
import foundation.json.IJSONWriter;
|
import foundation.json.JSONReader;
|
import foundation.util.MapList;
|
import foundation.util.Util;
|
|
public class ActorsRuntime implements Iterable<ActorTarget>, IJSONProvider {
|
|
private MapList<String, ActorTarget> actors;
|
private CurrentActor current;
|
private ActorTarget defalut;
|
|
|
public ActorsRuntime() {
|
actors = new MapList<String, ActorTarget>();
|
current = new CurrentActor();
|
}
|
|
public void loadOne(ActorTarget actor) {
|
//1. 添加 Actor
|
String id = actor.getId();
|
|
if (!actors.contains(id)) {
|
actors.add(id, actor);
|
}
|
|
if (actor.isDefault()) {
|
defalut = actor;
|
}
|
}
|
|
public void setCurrentActor() {
|
ActorTarget defaultCurrent = getCurrent(null);
|
boolean isSetCurrent = true;
|
|
if (defaultCurrent != null) {
|
String actorId = defaultCurrent.getId().toLowerCase();
|
|
if (actors.get(actorId) != null) {
|
isSetCurrent = false;
|
}
|
}
|
|
if (isSetCurrent) {
|
current.setDefault(defalut == null ? actors.get(0) : defalut);
|
}
|
}
|
|
public ActorTarget changeCurrent(String onlineCode, String targetId) {
|
if (actors == null || actors.size() == 0) {
|
return null;
|
}
|
|
ActorTarget result = defalut == null ? actors.get(0) : defalut;
|
|
if (!Util.isEmpty(targetId)) {
|
result = actors.get(targetId);
|
}
|
current.set(onlineCode, result);
|
|
return result;
|
}
|
|
public ActorTarget getCurrent(String onlineCode) {
|
return current.get(onlineCode);
|
}
|
|
public ActorTarget get(String actorId) {
|
return actors.get(actorId);
|
}
|
|
public void saveChanges(JSONReader changes) {
|
//1. translate changes to capacity list
|
MapList<String, Actor> actors = new MapList<String, Actor>();
|
ActorBucket actorBucket = ActorBucket.getInstance();
|
|
for (JSONReader reader: changes) {
|
String id = reader.getString("id");
|
|
if (Util.isEmpty(id)) {
|
return;
|
}
|
|
Actor actor = actorBucket.get(id);
|
|
if (actor == null) {
|
continue;
|
}
|
|
actors.add(id, actor);
|
}
|
|
/*
|
//2. add new
|
for (Actor actor: itemList) {
|
String id = actor.getId();
|
|
if (!contains(id)) {
|
add(id, actor);
|
}
|
}
|
|
//3. delete
|
for (Actor actor: itemList) {
|
String id = actor.getId();
|
|
if (!actors.contains(id)) {
|
remove(id);
|
|
if (isEmpty()) {
|
current = null;
|
}
|
}
|
}
|
*/
|
}
|
|
public boolean isVerified() {
|
return current.isVerified();
|
}
|
|
public void setVetified(boolean vetify) throws Exception {
|
current.setVerified(vetify);
|
}
|
|
public void clear() {
|
actors.clear();
|
}
|
|
@Override
|
public Iterator<ActorTarget> iterator() {
|
return actors.iterator();
|
}
|
|
@Override
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginArray();
|
|
for (ActorTarget actor: actors) {
|
actor.writeJSON(writer);
|
}
|
|
writer.endArray();
|
}
|
}
|