- add group item
- add color indication on edit offer
This commit is contained in:
5
core/src/main/java/ru/trader/core/GROUP_TYPE.java
Normal file
5
core/src/main/java/ru/trader/core/GROUP_TYPE.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package ru.trader.core;
|
||||
|
||||
public enum GROUP_TYPE {
|
||||
MARKET, SHIP, OUTFIT
|
||||
}
|
||||
31
core/src/main/java/ru/trader/core/Group.java
Normal file
31
core/src/main/java/ru/trader/core/Group.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ru.trader.core;
|
||||
|
||||
public class Group {
|
||||
private final String name;
|
||||
private final GROUP_TYPE type;
|
||||
|
||||
public Group(String name, GROUP_TYPE type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean isMarket(){
|
||||
return GROUP_TYPE.MARKET.equals(type);
|
||||
}
|
||||
|
||||
public boolean isShip(){
|
||||
return GROUP_TYPE.SHIP.equals(type);
|
||||
}
|
||||
|
||||
public boolean isOutfit(){
|
||||
return GROUP_TYPE.OUTFIT.equals(type);
|
||||
}
|
||||
|
||||
public GROUP_TYPE getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import java.util.Objects;
|
||||
|
||||
public class Item implements Comparable<Item>{
|
||||
private String name;
|
||||
private Group group;
|
||||
|
||||
public Item(String name) {
|
||||
setName(name);
|
||||
@@ -30,4 +31,12 @@ public class Item implements Comparable<Item>{
|
||||
if (this == other) return 0;
|
||||
return name != null ? other.name != null ? name.compareTo(other.name) : -1 : 0;
|
||||
}
|
||||
|
||||
public Group getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setGroup(Group group) {
|
||||
this.group = group;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ public class MarketDocHandler extends DefaultHandler {
|
||||
protected final static String VENDOR_LIST = "vendors";
|
||||
protected final static String VENDOR = "vendor";
|
||||
protected final static String OFFER = "offer";
|
||||
protected final static String GROUP = "group";
|
||||
|
||||
protected final static String ID_ATTR = "id";
|
||||
protected final static String NAME_ATTR = "name";
|
||||
@@ -32,6 +33,7 @@ public class MarketDocHandler extends DefaultHandler {
|
||||
|
||||
protected Market world;
|
||||
protected Vendor curVendor;
|
||||
protected Group curGroup;
|
||||
protected final HashMap<String,Item> items = new HashMap<>();
|
||||
|
||||
@Override
|
||||
@@ -48,6 +50,8 @@ public class MarketDocHandler extends DefaultHandler {
|
||||
break;
|
||||
case OFFER: parseOffer(attributes);
|
||||
break;
|
||||
case GROUP: parseGroup(attributes);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +60,8 @@ public class MarketDocHandler extends DefaultHandler {
|
||||
switch (qName){
|
||||
case VENDOR: world.add(curVendor);
|
||||
break;
|
||||
case GROUP: curGroup = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +96,13 @@ public class MarketDocHandler extends DefaultHandler {
|
||||
onOffer(offerType, item, price);
|
||||
}
|
||||
|
||||
protected void parseGroup(Attributes attributes) throws SAXException {
|
||||
String name = attributes.getValue(NAME_ATTR);
|
||||
GROUP_TYPE type = GROUP_TYPE.valueOf(attributes.getValue(TYPE_ATTR));
|
||||
LOG.debug("parse group {} ({})", name, type);
|
||||
onGroup(name, type);
|
||||
}
|
||||
|
||||
protected void onOffer(OFFER_TYPE offerType, Item item, double price){
|
||||
Offer offer = new Offer(offerType, item, price);
|
||||
curVendor.add(offer);
|
||||
@@ -104,10 +117,15 @@ public class MarketDocHandler extends DefaultHandler {
|
||||
|
||||
protected void onItem(String name, String id) {
|
||||
Item item = new Item(name);
|
||||
item.setGroup(curGroup);
|
||||
world.add(item);
|
||||
items.put(id, item);
|
||||
}
|
||||
|
||||
protected void onGroup(String name, GROUP_TYPE type) {
|
||||
curGroup = new Group(name, type);
|
||||
}
|
||||
|
||||
public Market getWorld(){
|
||||
return world;
|
||||
}
|
||||
|
||||
@@ -2,10 +2,7 @@ package ru.trader.store;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.core.Item;
|
||||
import ru.trader.core.Market;
|
||||
import ru.trader.core.Offer;
|
||||
import ru.trader.core.Vendor;
|
||||
import ru.trader.core.*;
|
||||
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
@@ -51,9 +48,16 @@ public class MarketStreamWriter {
|
||||
|
||||
protected void writeItems() throws XMLStreamException {
|
||||
out.writeStartElement(MarketDocHandler.ITEM_LIST);
|
||||
Group group = null;
|
||||
for (Item entry : market.getItems()) {
|
||||
if (group!=entry.getGroup()){
|
||||
if (group != null) out.writeEndElement();
|
||||
group = entry.getGroup();
|
||||
if (group != null) writeGroup(group);
|
||||
}
|
||||
writeItem(entry, items.get(entry));
|
||||
}
|
||||
if (group != null) out.writeEndElement();
|
||||
|
||||
out.writeEndElement();
|
||||
}
|
||||
@@ -92,6 +96,12 @@ public class MarketStreamWriter {
|
||||
out.writeAttribute(MarketDocHandler.PRICE_ATTR, String.valueOf(offer.getPrice()));
|
||||
}
|
||||
|
||||
protected void writeGroup(Group group) throws XMLStreamException {
|
||||
out.writeStartElement(MarketDocHandler.GROUP);
|
||||
out.writeAttribute(MarketDocHandler.NAME_ATTR, group.getName());
|
||||
out.writeAttribute(MarketDocHandler.TYPE_ATTR, group.getType().toString());
|
||||
}
|
||||
|
||||
private static Map<Item, String> generateId(Collection<Item> items){
|
||||
HashMap<Item, String> res = new HashMap<>(items.size());
|
||||
int index=0;
|
||||
|
||||
@@ -18,10 +18,27 @@
|
||||
|
||||
<xs:complexType name="itemsType">
|
||||
<xs:sequence>
|
||||
<xs:element name="item" type="itemType" maxOccurs="unbounded"/>
|
||||
<xs:element name="group" type="groupType" minOccurs="0" maxOccurs="unbounded"/>
|
||||
<xs:element name="item" type="itemType" minOccurs="0" maxOccurs="unbounded"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:complexType name="groupType">
|
||||
<xs:sequence>
|
||||
<xs:element name="item" type="itemType" maxOccurs="unbounded"/>
|
||||
</xs:sequence>
|
||||
<xs:attribute name="name" type="xs:string" use="required"/>
|
||||
<xs:attribute name="type" type="GROUP_TYPE" use="required"/>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:simpleType name="GROUP_TYPE">
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:enumeration value="MARKET"/>
|
||||
<xs:enumeration value="SHIP"/>
|
||||
<xs:enumeration value="OUTFIT"/>
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
|
||||
<xs:complexType name="itemType">
|
||||
<xs:attribute name="id" type="xs:ID" use="required"/>
|
||||
<xs:attribute name="name" type="xs:string" use="required"/>
|
||||
|
||||
Reference in New Issue
Block a user