Search in sources :

Example 1 with Weapon

use of io.github.spugn.Sargo.Objects.Weapon in project S-argo by Expugn.

the class UserParser method readConfig.

/**
 * Reads the user file and saves the data to variables.
 */
private void readConfig() {
    InputStream in = null;
    XMLEventReader eventReader = null;
    try {
        /* CREATE XMLInputFactory AND XMLEventReader */
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        in = new FileInputStream(FILE_PATH);
        eventReader = inputFactory.createXMLEventReader(in);
        /* READ XML FILE */
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                /* GET AND SAVE MEMORY DIAMOND COUNT */
                if (event.asStartElement().getName().getLocalPart().equals(MEMORY_DIAMONDS)) {
                    event = eventReader.nextEvent();
                    memoryDiamonds = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE MONEY SPENT */
                if (event.asStartElement().getName().getLocalPart().equals(MONEY_SPENT)) {
                    event = eventReader.nextEvent();
                    moneySpent = Double.parseDouble(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE HACKING CRYSTAL COUNT */
                if (event.asStartElement().getName().getLocalPart().equals(HACKING_CRYSTALS)) {
                    event = eventReader.nextEvent();
                    hackingCrystals = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE COL BALANCE */
                if (event.asStartElement().getName().getLocalPart().equals(COL_BALANCE)) {
                    event = eventReader.nextEvent();
                    colBalance = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE TOTAL TICKET SCOUTS */
                if (event.asStartElement().getName().getLocalPart().equals(TOTAL_TICKET_SCOUT)) {
                    event = eventReader.nextEvent();
                    totalTicketScout = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE R2 EXCHANGE SWORDS */
                if (event.asStartElement().getName().getLocalPart().equals(R2_EXCHANGE_SWORDS)) {
                    event = eventReader.nextEvent();
                    r2ExchangeSwords = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE R3 EXCHANGE SWORDS */
                if (event.asStartElement().getName().getLocalPart().equals(R3_EXCHANGE_SWORDS)) {
                    event = eventReader.nextEvent();
                    r3ExchangeSwords = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE R4 EXCHANGE SWORDS */
                if (event.asStartElement().getName().getLocalPart().equals(R4_EXCHANGE_SWORDS)) {
                    event = eventReader.nextEvent();
                    r4ExchangeSwords = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE RAINBOW ESSENCES */
                if (event.asStartElement().getName().getLocalPart().equals(RAINBOW_ESSENCE)) {
                    event = eventReader.nextEvent();
                    rainbowEssence = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE UPGRADE CRYSTALS */
                if (event.asStartElement().getName().getLocalPart().equals(UPGRADE_CRYSTAL)) {
                    event = eventReader.nextEvent();
                    upgradeCrystal = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE MEMORY FRAGMENTS */
                if (event.asStartElement().getName().getLocalPart().equals(MEMORY_FRAGMENT)) {
                    event = eventReader.nextEvent();
                    memoryFragment = Integer.parseInt(event.asCharacters().getData());
                    continue;
                }
                /* GET AND SAVE BANNER INFO */
                if (event.asStartElement().getName().getLocalPart().equals(BANNER)) {
                    String bannerName = "";
                    int bannerData = 0;
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals(B_NAME)) {
                            bannerName = attribute.getValue();
                        }
                        if (attribute.getName().toString().equals(B_DATA)) {
                            bannerData = Integer.parseInt(attribute.getValue());
                        }
                    }
                    bannerInfo.put(bannerName, bannerData);
                }
                /* GET AND SAVE CHARACTER BOX */
                if (event.asStartElement().getName().getLocalPart().equals(CHARACTER)) {
                    Character character = new Character();
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals(CHARACTER_PREFIX)) {
                            character.setPrefix(attribute.getValue());
                        }
                        if (attribute.getName().toString().equals(CHARACTER_NAME)) {
                            character.setName(attribute.getValue());
                        }
                        if (attribute.getName().toString().equals(CHARACTER_RARITY)) {
                            character.setRarity(Integer.parseInt(attribute.getValue()));
                        }
                    }
                    /* GENERATE IMAGE FILE PATH */
                    character.setImagePath("images/System/Character_Placeholder.png");
                    /* ADD CHARACTER TO CHARACTER LIST */
                    characterBox.add(character);
                }
                /* GET AND SAVE WEAPON BOX */
                if (event.asStartElement().getName().getLocalPart().equals(WEAPON)) {
                    Weapon weapon = new Weapon();
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals(WEAPON_NAME)) {
                            weapon.setName(attribute.getValue());
                        }
                        if (attribute.getName().toString().equals(WEAPON_RARITY)) {
                            weapon.setRarity(Integer.parseInt(attribute.getValue()));
                        }
                        if (attribute.getName().toString().equals(WEAPON_COUNT)) {
                            weapon.setCount(Integer.parseInt(attribute.getValue()));
                        }
                    }
                    /* GENERATE IMAGE FILE PATH */
                    weapon.setImagePath("images/System/Weapon_Placeholder.png");
                    /* ADD CHARACTER TO CHARACTER LIST */
                    weaponBox.add(weapon);
                }
            }
        }
        /* GO THROUGH CHARACTER BOX AND COUNT CHARACTERS */
        if (!characterBox.isEmpty()) {
            for (Character c : characterBox) {
                if (c.getRarity() == 5) {
                    pC++;
                } else if (c.getRarity() == 4) {
                    gC++;
                } else if (c.getRarity() == 3) {
                    sC++;
                } else if (c.getRarity() == 2) {
                    cC++;
                }
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        /* IGNORED */
        }
        try {
            if (eventReader != null) {
                eventReader.close();
            }
        } catch (XMLStreamException e) {
        /* IGNORED */
        }
    }
}
Also used : Character(io.github.spugn.Sargo.Objects.Character) Weapon(io.github.spugn.Sargo.Objects.Weapon)

Example 2 with Weapon

use of io.github.spugn.Sargo.Objects.Weapon in project S-argo by Expugn.

the class BannerParser method tryRead.

private List<Banner> tryRead(String configFile) throws FailedToReadBannerFileException {
    List<Banner> banners = new ArrayList();
    goldBanners = new ArrayList();
    goldBannersv2 = new ArrayList();
    InputStream in;
    XMLEventReader eventReader;
    Banner banner = null;
    ArrayList<Character> characters = null;
    Character character;
    ArrayList<Weapon> weapons = null;
    Weapon weapon;
    /* CREATE XMLInputFactory AND XMLEventReader */
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {
        in = new FileInputStream(configFile);
    } catch (FileNotFoundException e) {
        throw new FailedToReadBannerFileException();
    }
    try {
        eventReader = inputFactory.createXMLEventReader(in);
    } catch (XMLStreamException e) {
        try {
            in.close();
        } catch (IOException ex) {
        /* IGNORED */
        }
        throw new FailedToReadBannerFileException();
    }
    /* READ XML FILE */
    while (eventReader.hasNext()) {
        XMLEvent event;
        try {
            event = eventReader.nextEvent();
        } catch (XMLStreamException e) {
            try {
                in.close();
            } catch (IOException ex) {
            /* IGNORED */
            }
            try {
                eventReader.close();
            } catch (XMLStreamException ex) {
            /* IGNORED */
            }
            throw new FailedToReadBannerFileException();
        }
        if (event.isStartElement()) {
            StartElement startElement = event.asStartElement();
            /* CREATE NEW BANNER AND CHARACTER LIST OBJECT */
            if (startElement.getName().getLocalPart().equals("Banner")) {
                banner = new Banner();
                characters = new ArrayList();
                weapons = new ArrayList<>();
                /* GET AND SAVE BANNER ID */
                Iterator<Attribute> attributes = startElement.getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("id")) {
                        banner.setBannerID(Integer.parseInt(attribute.getValue()));
                    }
                }
            }
            /* GET AND SAVE BANNER NAME */
            if (event.isStartElement()) {
                if (event.asStartElement().getName().getLocalPart().equals("name")) {
                    try {
                        event = eventReader.nextEvent();
                    } catch (XMLStreamException e) {
                        try {
                            in.close();
                        } catch (IOException ex) {
                        /* IGNORED */
                        }
                        try {
                            eventReader.close();
                        } catch (XMLStreamException ex) {
                        /* IGNORED */
                        }
                        throw new FailedToReadBannerFileException();
                    }
                    banner.setBannerName(event.asCharacters().getData());
                    continue;
                }
            }
            /* GET AND SAVE BANNER TYPE */
            if (event.asStartElement().getName().getLocalPart().equals("type")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadBannerFileException();
                }
                banner.setBannerType(Integer.parseInt(event.asCharacters().getData()));
                continue;
            }
            /* GET AND SAVE WEAPON BANNER TYPE */
            if (event.asStartElement().getName().getLocalPart().equals("wepType")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadBannerFileException();
                }
                banner.setBannerWepType(Integer.parseInt(event.asCharacters().getData()));
                continue;
            }
            /* GET AND SAVE IF BANNER IS INCLUDED WITH GOLD BANNERS/GOLD BANNERS V2 */
            if (event.asStartElement().getName().getLocalPart().equals("include")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadBannerFileException();
                }
                String includeType = event.asCharacters().getData();
                if (includeType.equals("GoldBanners")) {
                    try {
                        goldBanners.add(banner.getBannerID());
                    } catch (NullPointerException e) {
                    /* IGNORED */
                    }
                } else if (includeType.equals("GoldBannersv2")) {
                    try {
                        goldBannersv2.add(banner.getBannerID());
                    } catch (NullPointerException e) {
                    /* IGNORED */
                    }
                }
                continue;
            }
            /* GET AND SAVE CHARACTER */
            if (event.asStartElement().getName().getLocalPart().equals("Character")) {
                character = new Character();
                Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("prefix")) {
                        character.setPrefix(attribute.getValue());
                    }
                    if (attribute.getName().toString().equals("char")) {
                        character.setName(attribute.getValue());
                    }
                    if (attribute.getName().toString().equals("rarity")) {
                        character.setRarity(Integer.parseInt(attribute.getValue()));
                    }
                }
                /* GENERATE IMAGE FILE PATH*/
                String characterImage = character.getPrefix() + " " + character.getName();
                character.setImagePath("images/Characters/" + characterImage.replaceAll(" ", "_") + ".png");
                /* ADD CHARACTER TO CHARACTER LIST */
                characters.add(character);
            }
            /* GET AND SAVE WEAPON */
            if (event.asStartElement().getName().getLocalPart().equals("Weapon")) {
                weapon = new Weapon();
                Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("name")) {
                        weapon.setName(attribute.getValue());
                    }
                    if (attribute.getName().toString().equals("rarity")) {
                        weapon.setRarity(Integer.parseInt(attribute.getValue()));
                    }
                }
                /* GENERATE IMAGE FILE PATH*/
                weapon.setImagePath("images/Weapons/" + weapon.getName().replaceAll(" ", "_") + ".png");
                /* ADD WEAPON TO WEAPON LIST */
                weapons.add(weapon);
            }
        }
        /* END OF BANNER ELEMENT. FINALIZE CHARACTER LIST AND ADD OBJECT TO ArrayList */
        if (event.isEndElement()) {
            EndElement endElement = event.asEndElement();
            if (endElement.getName().getLocalPart().equals("Banner")) {
                banner.setCharacters(characters);
                banner.setWeapons(weapons);
                banners.add(banner);
            // System.out.println("added " + banner.getBannerName());
            }
        }
    }
    return banners;
}
Also used : FailedToReadBannerFileException(io.github.spugn.Sargo.Exceptions.FailedToReadBannerFileException) Character(io.github.spugn.Sargo.Objects.Character) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) Banner(io.github.spugn.Sargo.Objects.Banner) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Weapon(io.github.spugn.Sargo.Objects.Weapon) FileInputStream(java.io.FileInputStream) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with Weapon

use of io.github.spugn.Sargo.Objects.Weapon in project S-argo by Expugn.

the class UserParser method writeWeaponBox.

/**
 * Writes the weapon box using the List of Weapons from the class.
 *
 * @param eventWriter  Event writer.
 * @throws XMLStreamException  If there is an error when writing.
 */
private void writeWeaponBox(XMLEventWriter eventWriter) throws XMLStreamException {
    /* INITIALIZE VARIABLES */
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter weaponElement;
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    XMLEvent tab = eventFactory.createDTD("\t");
    /* WRITE weaponBox START NODE*/
    eventWriter.add(tab);
    StartElement sElement = eventFactory.createStartElement("", "", WEAPON_BOX);
    eventWriter.add(sElement);
    /* WRITE WEAPON DATA */
    for (Weapon weapon : weaponBox) {
        /* CREATE EMPTY ELEMENT */
        weaponElement = new StringWriter();
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(weaponElement);
        /* WRITE ELEMENT NAME, CHARACTER PREFIX, CHARACTER NAME, AND RARITY */
        writer.writeEmptyElement(WEAPON);
        writer.writeAttribute(WEAPON_NAME, weapon.getName());
        writer.writeAttribute(WEAPON_RARITY, String.valueOf(weapon.getRarity()));
        writer.writeAttribute(WEAPON_COUNT, String.valueOf(weapon.getCount()));
        writer.writeEndDocument();
        writer.flush();
        writer.close();
        /* START NEW LINE AND TAB TWICE */
        eventWriter.add(end);
        eventWriter.add(tab);
        eventWriter.add(tab);
        /* WRITE WEAPON ELEMENT TO FILE */
        eventWriter.add(eventFactory.createDTD(weaponElement.toString()));
    }
    /* NEW LINE AND ADD TAB */
    eventWriter.add(end);
    eventWriter.add(tab);
    /* CLOSE CHARACTER BOX ELEMENT */
    EndElement eElement = eventFactory.createEndElement("", "", WEAPON_BOX);
    eventWriter.add(eElement);
    eventWriter.add(end);
}
Also used : Weapon(io.github.spugn.Sargo.Objects.Weapon)

Aggregations

Weapon (io.github.spugn.Sargo.Objects.Weapon)3 Character (io.github.spugn.Sargo.Objects.Character)2 FailedToReadBannerFileException (io.github.spugn.Sargo.Exceptions.FailedToReadBannerFileException)1 Banner (io.github.spugn.Sargo.Objects.Banner)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Attribute (javax.xml.stream.events.Attribute)1 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1