Source for my MapleSolaxiaV2 (v83 MapleStory).
This commit is contained in:
ronancpl
2015-11-02 23:17:21 -02:00
parent 324982e94f
commit 972517e7b2
1675 changed files with 261831 additions and 0 deletions

View File

@@ -0,0 +1,325 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package scripting.event;
import java.io.File;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.script.ScriptException;
import net.server.world.MapleParty;
import net.server.world.MaplePartyCharacter;
import provider.MapleDataProviderFactory;
import server.TimerManager;
import server.expeditions.MapleExpedition;
import server.life.MapleMonster;
import server.maps.MapleMap;
import server.maps.MapleMapFactory;
import tools.DatabaseConnection;
import client.MapleCharacter;
/**
*
* @author Matze
*/
public class EventInstanceManager {
private List<MapleCharacter> chars = new ArrayList<>();
private List<MapleMonster> mobs = new LinkedList<>();
private Map<MapleCharacter, Integer> killCount = new HashMap<>();
private EventManager em;
private MapleMapFactory mapFactory;
private String name;
private Properties props = new Properties();
private long timeStarted = 0;
private long eventTime = 0;
private MapleExpedition expedition = null;
public EventInstanceManager(EventManager em, String name) {
this.em = em;
this.name = name;
mapFactory = new MapleMapFactory(MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Map.wz")), MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/String.wz")), (byte) 0, (byte) 1);//Fk this
mapFactory.setChannel(em.getChannelServer().getId());
}
public EventManager getEm() {
return em;
}
public void registerPlayer(MapleCharacter chr) {
if (chr == null || !chr.isLoggedin()){
return;
}
try {
chars.add(chr);
chr.setEventInstance(this);
em.getIv().invokeFunction("playerEntry", this, chr);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public void startEventTimer(long time) {
timeStarted = System.currentTimeMillis();
eventTime = time;
}
public boolean isTimerStarted() {
return eventTime > 0 && timeStarted > 0;
}
public long getTimeLeft() {
return eventTime - (System.currentTimeMillis() - timeStarted);
}
public void registerParty(MapleParty party, MapleMap map) {
for (MaplePartyCharacter pc : party.getMembers()) {
MapleCharacter c = map.getCharacterById(pc.getId());
registerPlayer(c);
}
}
public void registerExpedition(MapleExpedition exped) {
expedition = exped;
registerPlayer(exped.getLeader());
}
public void unregisterPlayer(MapleCharacter chr) {
chars.remove(chr);
chr.setEventInstance(null);
}
public int getPlayerCount() {
return chars.size();
}
public List<MapleCharacter> getPlayers() {
return new ArrayList<>(chars);
}
public void registerMonster(MapleMonster mob) {
if (!mob.getStats().isFriendly()) { //We cannot register moon bunny
mobs.add(mob);
mob.setEventInstance(this);
}
}
public void movePlayer(MapleCharacter chr) {
try {
em.getIv().invokeFunction("moveMap", this, chr);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public void monsterKilled(MapleMonster mob) {
mobs.remove(mob);
if (mobs.isEmpty()) {
try {
em.getIv().invokeFunction("allMonstersDead", this);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
}
public void playerKilled(MapleCharacter chr) {
try {
em.getIv().invokeFunction("playerDead", this, chr);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public boolean revivePlayer(MapleCharacter chr) {
try {
Object b = em.getIv().invokeFunction("playerRevive", this, chr);
if (b instanceof Boolean) {
return (Boolean) b;
}
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
return true;
}
public void playerDisconnected(MapleCharacter chr) {
try {
em.getIv().invokeFunction("playerDisconnected", this, chr);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
/**
*
* @param chr
* @param mob
*/
public void monsterKilled(MapleCharacter chr, MapleMonster mob) {
try {
Integer kc = killCount.get(chr);
int inc = ((Double) em.getIv().invokeFunction("monsterValue", this, mob.getId())).intValue();
if (kc == null) {
kc = inc;
} else {
kc += inc;
}
killCount.put(chr, kc);
if (expedition != null){
expedition.monsterKilled(chr, mob);
}
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public int getKillCount(MapleCharacter chr) {
Integer kc = killCount.get(chr);
if (kc == null) {
return 0;
} else {
return kc;
}
}
public void dispose() {
try {
em.getIv().invokeFunction("dispose", this);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
chars.clear();
mobs.clear();
killCount.clear();
mapFactory = null;
if (expedition != null) {
em.getChannelServer().getExpeditions().remove(expedition);
}
em.disposeInstance(name);
em = null;
}
public MapleMapFactory getMapFactory() {
return mapFactory;
}
public void schedule(final String methodName, long delay) {
TimerManager.getInstance().schedule(new Runnable() {
@Override
public void run() {
try {
em.getIv().invokeFunction(methodName, EventInstanceManager.this);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
}, delay);
}
public String getName() {
return name;
}
public void saveWinner(MapleCharacter chr) {
try {
try (PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("INSERT INTO eventstats (event, instance, characterid, channel) VALUES (?, ?, ?, ?)")) {
ps.setString(1, em.getName());
ps.setString(2, getName());
ps.setInt(3, chr.getId());
ps.setInt(4, chr.getClient().getChannel());
ps.executeUpdate();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public MapleMap getMapInstance(int mapId) {
MapleMap map = mapFactory.getMap(mapId);
if (!mapFactory.isMapLoaded(mapId)) {
if (em.getProperty("shuffleReactors") != null && em.getProperty("shuffleReactors").equals("true")) {
map.shuffleReactors();
}
}
return map;
}
public void setProperty(String key, String value) {
props.setProperty(key, value);
}
public Object setProperty(String key, String value, boolean prev) {
return props.setProperty(key, value);
}
public String getProperty(String key) {
return props.getProperty(key);
}
public Properties getProperties(){
return props;
}
public void leftParty(MapleCharacter chr) {
try {
em.getIv().invokeFunction("leftParty", this, chr);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public void disbandParty() {
try {
em.getIv().invokeFunction("disbandParty", this);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public void finishPQ() {
try {
em.getIv().invokeFunction("clearPQ", this);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public void removePlayer(MapleCharacter chr) {
try {
em.getIv().invokeFunction("playerExit", this, chr);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public boolean isLeader(MapleCharacter chr) {
return (chr.getParty().getLeader().getId() == chr.getId());
}
}

View File

@@ -0,0 +1,201 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package scripting.event;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.Invocable;
import javax.script.ScriptException;
import net.server.channel.Channel;
import net.server.world.MapleParty;
import server.TimerManager;
import server.expeditions.MapleExpedition;
import server.maps.MapleMap;
import client.MapleCharacter;
/**
*
* @author Matze
*/
public class EventManager {
private Invocable iv;
private Channel cserv;
private Map<String, EventInstanceManager> instances = new HashMap<String, EventInstanceManager>();
private Properties props = new Properties();
private String name;
private ScheduledFuture<?> schedule = null;
public EventManager(Channel cserv, Invocable iv, String name) {
this.iv = iv;
this.cserv = cserv;
this.name = name;
}
public void cancel() {
try {
iv.invokeFunction("cancelSchedule", (Object) null);
} catch (ScriptException ex) {
ex.printStackTrace();
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public void schedule(String methodName, long delay) {
schedule(methodName, null, delay);
}
public void schedule(final String methodName, final EventInstanceManager eim, long delay) {
schedule = TimerManager.getInstance().schedule(new Runnable() {
public void run() {
try {
iv.invokeFunction(methodName, eim);
} catch (ScriptException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}, delay);
}
public void cancelSchedule() {
schedule.cancel(true);
}
public ScheduledFuture<?> scheduleAtTimestamp(final String methodName, long timestamp) {
return TimerManager.getInstance().scheduleAtTimestamp(new Runnable() {
public void run() {
try {
iv.invokeFunction(methodName, (Object) null);
} catch (ScriptException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}, timestamp);
}
public Channel getChannelServer() {
return cserv;
}
public EventInstanceManager getInstance(String name) {
return instances.get(name);
}
public Collection<EventInstanceManager> getInstances() {
return Collections.unmodifiableCollection(instances.values());
}
public EventInstanceManager newInstance(String name) {
EventInstanceManager ret = new EventInstanceManager(this, name);
instances.put(name, ret);
return ret;
}
public void disposeInstance(String name) {
instances.remove(name);
}
public Invocable getIv() {
return iv;
}
public void setProperty(String key, String value) {
props.setProperty(key, value);
}
public String getProperty(String key) {
return props.getProperty(key);
}
public void setProperty(String key, int value) {
props.setProperty(key, value + "");
}
public int getIntProperty(String key) {
return Integer.parseInt(props.getProperty(key));
}
public String getName() {
return name;
}
//Expedition method: starts an expedition
public void startInstance(MapleExpedition exped) {
try {
EventInstanceManager eim = (EventInstanceManager) (iv.invokeFunction("setup", (Object) null));
eim.registerExpedition(exped);
exped.start();
} catch (ScriptException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
//Regular method: player
public void startInstance(MapleCharacter chr) {
try {
EventInstanceManager eim = (EventInstanceManager) (iv.invokeFunction("setup", (Object) null));
eim.registerPlayer(chr);
} catch (ScriptException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
//PQ method: starts a PQ
public void startInstance(MapleParty party, MapleMap map) {
try {
EventInstanceManager eim = (EventInstanceManager) (iv.invokeFunction("setup", (Object) null));
eim.registerParty(party, map);
} catch (ScriptException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
//non-PQ method for starting instance
public void startInstance(EventInstanceManager eim, String leader) {
try {
iv.invokeFunction("setup", eim);
eim.setProperty("leader", leader);
} catch (ScriptException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

View File

@@ -0,0 +1,90 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package scripting.event;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import net.server.channel.Channel;
import scripting.AbstractScriptManager;
/**
*
* @author Matze
*/
public class EventScriptManager extends AbstractScriptManager {
private class EventEntry {
public EventEntry(Invocable iv, EventManager em) {
this.iv = iv;
this.em = em;
}
public Invocable iv;
public EventManager em;
}
private Map<String, EventEntry> events = new LinkedHashMap<>();
public EventScriptManager(Channel cserv, String[] scripts) {
super();
for (String script : scripts) {
if (!script.equals("")) {
Invocable iv = getInvocable("event/" + script + ".js", null);
events.put(script, new EventEntry(iv, new EventManager(cserv, iv, script)));
}
}
}
public EventManager getEventManager(String event) {
EventEntry entry = events.get(event);
if (entry == null) {
return null;
}
return entry.em;
}
public void init() {
for (EventEntry entry : events.values()) {
try {
((ScriptEngine) entry.iv).put("em", entry.em);
entry.iv.invokeFunction("init", (Object) null);
} catch (Exception ex) {
Logger.getLogger(EventScriptManager.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error on script: " + entry.em.getName());
}
}
}
public void reload(){
cancel();
init();
}
public void cancel() {
for (EventEntry entry : events.values()) {
entry.em.cancel();
}
}
}