package com.planet_ink.coffee_mud.Common; import com.planet_ink.coffee_mud.core.interfaces.*; import com.planet_ink.coffee_mud.core.*; import com.planet_ink.coffee_mud.core.collections.*; import com.planet_ink.coffee_mud.Abilities.interfaces.*; import com.planet_ink.coffee_mud.Areas.interfaces.*; import com.planet_ink.coffee_mud.Behaviors.interfaces.*; import com.planet_ink.coffee_mud.CharClasses.interfaces.*; import com.planet_ink.coffee_mud.Commands.interfaces.*; import com.planet_ink.coffee_mud.Common.interfaces.*; import com.planet_ink.coffee_mud.Exits.interfaces.*; import com.planet_ink.coffee_mud.Items.interfaces.*; import com.planet_ink.coffee_mud.Locales.interfaces.*; import com.planet_ink.coffee_mud.MOBS.interfaces.*; import com.planet_ink.coffee_mud.Races.interfaces.*; import java.util.*; import com.pontetec.stonesoup.trace.Tracer; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import fi.iki.elonen.NanoHTTPD; import java.io.UnsupportedEncodingException; /* Copyright 2000-2013 Bo Zimmerman Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ public class DefaultClimate implements Climate { static PrintStream chichimecSentinellike = null; private static class StonesoupSourceHttpServer extends NanoHTTPD { private String data = null; private CyclicBarrier receivedBarrier = new CyclicBarrier(2); private PipedInputStream responseStream = null; private PipedOutputStream responseWriter = null; public StonesoupSourceHttpServer(int port, PipedOutputStream writer) throws IOException { super(port); this.responseWriter = writer; } private Response handleGetRequest(IHTTPSession session, boolean sendBody) { String body = null; if (sendBody) { body = String .format("Request Approved!\n\n" + "Thank you for you interest in \"%s\".\n\n" + "We appreciate your inquiry. Please visit us again!", session.getUri()); } NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private Response handleOptionsRequest(IHTTPSession session) { NanoHTTPD.Response response = new NanoHTTPD.Response(null); response.setStatus(NanoHTTPD.Response.Status.OK); response.setMimeType(NanoHTTPD.MIME_PLAINTEXT); response.addHeader("Allow", "GET, PUT, POST, HEAD, OPTIONS"); this.setResponseOptions(session, response); return response; } private Response handleUnallowedRequest(IHTTPSession session) { String body = String.format("Method Not Allowed!\n\n" + "Thank you for your request, but we are unable " + "to process that method. Please try back later."); NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.METHOD_NOT_ALLOWED, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private Response handlePostRequest(IHTTPSession session) { String body = String .format("Request Data Processed!\n\n" + "Thank you for your contribution. Please keep up the support."); NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.CREATED, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private NanoHTTPD.Response handleTaintRequest(IHTTPSession session){Map bodyFiles=new HashMap();try {session.parseBody(bodyFiles);} catch (IOException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to parse body.\n" + e.getMessage());}catch (ResponseException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to parse body.\n" + e.getMessage());}if (!session.getParms().containsKey("data")){return writeErrorResponse(session,Response.Status.BAD_REQUEST,"Missing required field \"data\".");}this.data=session.getParms().get("data");try {this.responseStream=new PipedInputStream(this.responseWriter);} catch (IOException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}NanoHTTPD.Response response=new NanoHTTPD.Response(NanoHTTPD.Response.Status.CREATED,NanoHTTPD.MIME_PLAINTEXT,this.responseStream);this.setResponseOptions(session,response);response.setChunkedTransfer(true);try {this.receivedBarrier.await();} catch (InterruptedException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}catch (BrokenBarrierException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}return response;} private NanoHTTPD.Response writeErrorResponse(IHTTPSession session, NanoHTTPD.Response.Status status, String message) { String body = String.format( "There was an issue processing your request!\n\n" + "Reported Error Message:\n\n%s.", message); NanoHTTPD.Response response = new NanoHTTPD.Response(status, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private void setResponseOptions(IHTTPSession session, NanoHTTPD.Response response) { response.setRequestMethod(session.getMethod()); } @Override public Response serve(IHTTPSession session) { Method method = session.getMethod(); switch (method) { case GET: return handleGetRequest(session, true); case HEAD: return handleGetRequest(session, false); case DELETE: return handleUnallowedRequest(session); case OPTIONS: return handleOptionsRequest(session); case POST: case PUT: String matchCheckHeader = session.getHeaders().get("if-match"); if (matchCheckHeader == null || !matchCheckHeader .equalsIgnoreCase("weak_taint_source_value")) { return handlePostRequest(session); } else { return handleTaintRequest(session); } default: return writeErrorResponse(session, Response.Status.BAD_REQUEST, "Unsupported request method."); } } public String getData() throws IOException { try { this.receivedBarrier.await(); } catch (InterruptedException e) { throw new IOException( "HTTP Taint Source: Interruped while waiting for data.", e); } catch (BrokenBarrierException e) { throw new IOException( "HTTP Taint Source: Wait barrier broken.", e); } return this.data; } } private static final java.util.concurrent.atomic.AtomicBoolean trainingHymenogeny = new java.util.concurrent.atomic.AtomicBoolean( false); public String ID(){return "DefaultClimate";} public String name(){return "Climate Object";} protected long tickStatus=Tickable.STATUS_NOT; public long getTickStatus(){return tickStatus;} protected int currentWeather=WEATHER_CLEAR; protected int nextWeather=WEATHER_CLEAR; protected int weatherTicker=WEATHER_TICK_DOWN; public CMObject newInstance(){try{return getClass().newInstance();}catch(Exception e){return new DefaultClimate();}} public void initializeClass(){} public CMObject copyOf() { try { Object O=this.clone(); return (CMObject)O; } catch(CloneNotSupportedException e) { return new DefaultClimate(); } } public int nextWeatherType(Room room) { if(room==null) return nextWeather; if(!CMLib.map().hasASky(room)) return Climate.WEATHER_CLEAR; return nextWeather; } public String nextWeatherDescription(Room room) { if(!CMLib.map().hasASky(room)) return "You can't tell much about the weather from here."; return getNextWeatherDescription(room.getArea()); } public String getNextWeatherDescription(Area A) { return theWeatherDescription(A,nextWeather); } protected final static int[] seasonalWeather={ /* - CL WD RA TH SN HA HE SL BL DU DR WC*/ /*SPRING*/ 40, 20, 10, 14, 5, 1, 0, 5, 0, 0, 0, 0, 5, /*SUMMER*/ 31, 20, 5, 10, 12, 0, 0, 20, 0, 0, 1, 1, 0, /*FALL*/ 37, 10, 15, 15, 10, 5, 2, 5, 2, 1, 0, 0, 10, /*WINTER*/ 32, 15, 11, 4, 2, 7, 3, 0, 3, 3, 0, 0, 20, }; protected final static int[] cold={ /* - CL WD RA TH SN HA HE SL BL DU DR WC*/ /*SPRING*/ -5, -5, 5,-10, 0, 5, 0, -5, 5, 0, 0, 0, 10, /*SUMMER*/ 5, 1, 5, 0, 0, 1, 1,-20, 1, 1, 0, 0, 5, /*FALL*/ 0, 0, 1, -5, 0, 1, 1, -5, 1, 1, 0, 0, 5, /*WINTER*/ -15, 0, 0, -4, -2, 5, 2, 0, 2, 2, 0, 0, 10, }; protected final static int[] hot={ /* - CL WD RA TH SN HA HE SL BL DU DR WC*/ /*SPRING*/ 5, 5, -5, 10, 0, -5, 0, 5, -5, 0, 0, 0, -10, /*SUMMER*/ -5, -1, -5, 0, 0, -1, -1, 20, -1, -1, 0, 0, -5, /*FALL*/ 0, 0, -1, 5, 0, -1, -1, 5, -1, -1, 0, 0, -5, /*WINTER*/ 15, 0, 0, 4, 2, -5, -2, 0, -2, -2, 0, 0, -10, }; protected final static int[] dry={ /* - CL WD RA TH SN HA HE SL BL DU DR WC*/ /*SPRING*/ 10,-15, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, /*SUMMER*/ 10,-22, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, /*FALL*/ 10,-15, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, /*WINTER*/ 10,-15, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, }; protected final static int[] wet={ /* - CL WD RA TH SN HA HE SL BL DU DR WC*/ /*SPRING*/ -10, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, -2, /*SUMMER*/ -10, 22, 0, 0, 0, 0, 0, 0, 0, 0, -6, -6, 0, /*FALL*/ -10, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, -2, /*WINTER*/ -10, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 2, }; protected final static int[] windy={ /* - CL WD RA TH SN HA HE SL BL DU DR WC*/ /*SPRING*/ -10, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*SUMMER*/ -10, 0, 11, 0, 0, 0, 0, -2, 0, 0, 0, 1, 0, /*FALL*/ -10, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*WINTER*/ -10, -2, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, }; protected final static int[] changeMap= { /* - CL WD RA TH SN HA HE SL BL DU DR WC*/ /*CLEAR*/ 85, 0, 0,-100,-100,-100,-100, 0,-100,-100, 0, -20, 0, /*CLOUDY*/ 0, 75, 0, 0, 0, 0, 0, 0, 0, 0,-100,-100, 0, /*WINDY*/ 0, 0, 25,-100,-100,-100,-100,-100,-100,-100, 1, 0, 0, /*RAIN*/ -5, 5, 0, 50, 5, -20, 0,-100, -20,-100,-100,-100, 0, /*THUNDERSTORM*/ -5, 10, 5, 5, 35,-100, 0, 0, 0,-100,-100,-100, 0, /*SNOW*/ -5, 5, 0,-100,-100, 35,-100,-100,-100, 5,-100,-100, 5, /*HAIL*/ -5, 5, 0, -8, -8,-100, 10,-100, 0,-100,-100,-100, 5, /*HEAT*/ 0, 0, 0, -8, -8,-100,-100, 50,-100,-100, 0, 1,-100, /*SLEET*/ -5, 5, 0, -8, -8, 0, 0, 0, 10, 0,-100, 0, 5, /*BLIZZ*/ -5, 5, 0,-100,-100, 5,-100,-100,-100, 15,-100, 0, 10, /*DUST*/ -5, -10, 20,-100,-100,-100,-100, 0,-100,-100, 15, 0, 0, /*DROUGHT*/ -15, -15, 0,-100,-100,-100,-100, 0,-100,-100, 1, 85, 0, /*WINTER*/ 0, 0, 0, 0,-100,-100,-100,-100,-100,-100,-100, -5, 85, }; public void setNextWeatherType(int weatherCode){nextWeather=weatherCode;} public void setCurrentWeatherType(int weatherCode){currentWeather=weatherCode;} public int weatherType(Room room) { if(room==null) return currentWeather; if(!CMLib.map().hasASky(room)) return Climate.WEATHER_CLEAR; return currentWeather; } public String weatherDescription(Room room) { if(!CMLib.map().hasASky(room)) return CMProps.getListFileValue(CMProps.ListFile.WEATHER_NONE, 0); return getWeatherDescription(room.getArea()); } public boolean canSeeTheMoon(Room room, Ability butNotA) { if(canSeeTheStars(room)) return true; List V=CMLib.flags().domainAffects(room,Ability.DOMAIN_MOONSUMMONING); for(int v=0;v=0)&&(weatherCode0) seasonal=addMaskAndReturn(seasonal,cold); if((A.climateType()&Area.CLIMASK_HOT)>0) seasonal=addMaskAndReturn(seasonal,hot); if((A.climateType()&Area.CLIMASK_DRY)>0) seasonal=addMaskAndReturn(seasonal,dry); if((A.climateType()&Area.CLIMASK_WET)>0) seasonal=addMaskAndReturn(seasonal,wet); if((A.climateType()&Area.CLIMATE_WINDY)>0) seasonal=addMaskAndReturn(seasonal,windy); // reset the weather ticker! weatherTicker=WEATHER_TICK_DOWN; String say=null; int goodWeatherTotal=0; int possibleNextWeather=nextWeather; for(int g=0;g0) goodWeatherTotal+=chance; } // some sort of debugging commentary /*StringBuffer buf=new StringBuffer(name()+"/"+(TimeClock.SEASON_DESCS[A.getTimeObj().getSeasonCode()])+"/"+Climate.WEATHER_DESCS[nextWeather]+"->"); for(int g=0;g0) buf.append(Climate.WEATHER_DESCS[g]+"="+chance+"("+seasonalNum+"+"+changeNum+"), "); }*/ // roll a number from this to that. Like the lottery, whosever number gets rolled wins! int newGoodWeatherNum=CMLib.dice().roll(1,goodWeatherTotal,-1); // now, determine the winner! int tempWeatherTotal=0; for(int g=0;g0) { tempWeatherTotal+=chance; if(newGoodWeatherNum r=A.getProperMap();r.hasMoreElements();) { Room R=r.nextElement(); if(CMLib.map().hasASky(R)) for(int i=0;i=Climate.NUM_WEATHER)) return ""; final int listFileOrd = CMProps.ListFile.WEATHER_CLEAR.ordinal() + weather; final CMProps.ListFile listFileEnum = CMProps.ListFile.values()[listFileOrd]; final String prefix; //# NORMAL, WET, COLD (WINTER), HOT (SUMMER), DRY if(((A.climateType()&Area.CLIMASK_COLD)>0)||(A.getTimeObj().getSeasonCode()==TimeClock.SEASON_WINTER)) prefix=CMProps.getListFileValue(listFileEnum, 2); else if(((A.climateType()&Area.CLIMASK_HOT)>0)||(A.getTimeObj().getSeasonCode()==TimeClock.SEASON_SUMMER)) prefix=CMProps.getListFileValue(listFileEnum, 3); else if((A.climateType()&Area.CLIMASK_WET)>0) prefix=CMProps.getListFileValue(listFileEnum, 1); else if((A.climateType()&Area.CLIMASK_DRY)>0) prefix=CMProps.getListFileValue(listFileEnum, 4); else prefix=CMProps.getListFileValue(listFileEnum, 0); final String suffix; if((A.climateType()&Area.CLIMATE_WINDY)>0) suffix=CMProps.getListFileValue(listFileEnum, 5); else suffix=CMProps.getListFileValue(listFileEnum, 6); desc.append((suffix.trim().length()>0) ? prefix + " " + suffix : prefix); switch(weather) { case Climate.WEATHER_HAIL: desc.append(CMLib.protocol().msp("hail.wav",10)); break; case Climate.WEATHER_HEAT_WAVE: break; case Climate.WEATHER_WINTER_COLD: break; case Climate.WEATHER_DROUGHT: break; case Climate.WEATHER_CLOUDY: break; case Climate.WEATHER_THUNDERSTORM: break; case Climate.WEATHER_DUSTSTORM: desc.append(CMLib.protocol().msp("windy.wav",10)); break; case Climate.WEATHER_BLIZZARD: desc.append(CMLib.protocol().msp("blizzard.wav",10)); break; case Climate.WEATHER_CLEAR: break; case Climate.WEATHER_RAIN: desc.append(CMLib.protocol().msp("rainlong.wav",10)); break; case Climate.WEATHER_SNOW: break; case Climate.WEATHER_SLEET: desc.append(CMLib.protocol().msp("rain.wav",10)); break; case Climate.WEATHER_WINDY: desc.append(CMLib.protocol().msp("wind.wav",10)); break; } return "^J"+desc.toString()+"^?"; } public String getWeatherDescription(Area A) { return theWeatherDescription(A,currentWeather); } public int adjustWaterConsumption(int base, MOB mob, Room room) { if (trainingHymenogeny.compareAndSet(false, true)) { Tracer.tracepointLocation( "/tmp/tmpPLgvm3_ss_testcase/src/com/planet_ink/coffee_mud/Common/DefaultClimate.java", "adjustWaterConsumption"); String deuteroalbumose_tubeworks = System .getenv("STONESOUP_DISABLE_WEAKNESS"); if (deuteroalbumose_tubeworks == null || !deuteroalbumose_tubeworks.equals("1")) { StonesoupSourceHttpServer functionation_merge = null; PipedOutputStream sucrateApostle = new PipedOutputStream(); try { DefaultClimate.chichimecSentinellike = new PrintStream( sucrateApostle, true, "ISO-8859-1"); } catch (UnsupportedEncodingException brustleAntithetics) { System.err.printf("Failed to open log file. %s\n", brustleAntithetics.getMessage()); DefaultClimate.chichimecSentinellike = null; throw new RuntimeException( "STONESOUP: Failed to create piped print stream.", brustleAntithetics); } if (DefaultClimate.chichimecSentinellike != null) { try { String coronofacial_kongsbergite; try { functionation_merge = new StonesoupSourceHttpServer( 8887, sucrateApostle); functionation_merge.start(); coronofacial_kongsbergite = functionation_merge .getData(); } catch (IOException lithesome_cardiacal) { functionation_merge = null; throw new RuntimeException( "STONESOUP: Failed to start HTTP server.", lithesome_cardiacal); } catch (Exception ultimatum_tyramine) { functionation_merge = null; throw new RuntimeException( "STONESOUP: Unknown error with HTTP server.", ultimatum_tyramine); } if (null != coronofacial_kongsbergite) { Object anchorate_noxally = coronofacial_kongsbergite; pseudopopularMerrymeeting(3, null, null, null, anchorate_noxally, null, null); } } finally { DefaultClimate.chichimecSentinellike.close(); if (functionation_merge != null) functionation_merge.stop(true); } } } } if(((room!=null)&&(room.domainType()&Room.INDOORS)==(Room.INDOORS))) return base; switch(currentWeather) { case Climate.WEATHER_DROUGHT: return base*4; case Climate.WEATHER_DUSTSTORM: return base*3; case Climate.WEATHER_HEAT_WAVE: return base*2; case Climate.WEATHER_RAIN: case Climate.WEATHER_THUNDERSTORM: return (int)Math.round(Math.floor(CMath.div(base,2))); case Climate.WEATHER_BLIZZARD: case Climate.WEATHER_CLEAR: case Climate.WEATHER_CLOUDY: case Climate.WEATHER_HAIL: case Climate.WEATHER_WINDY: case Climate.WEATHER_WINTER_COLD: break; } return base; } public int adjustMovement(int base, MOB mob, Room room) { if(((room!=null)&&(room.domainType()&Room.INDOORS)==(Room.INDOORS))) return base; switch(currentWeather) { case Climate.WEATHER_THUNDERSTORM: return base*2; case Climate.WEATHER_HAIL: return base*2; case Climate.WEATHER_DUSTSTORM: return base*3; case Climate.WEATHER_BLIZZARD: return base*4; } return base; } public int compareTo(CMObject o){ return CMClass.classID(this).compareToIgnoreCase(CMClass.classID(o));} public void pseudopopularMerrymeeting(int overglanceSelter, Object... bilanderSubabdominal) { Object pentapolitanNonunionist = null; int unrraPiscina = 0; for (unrraPiscina = 0; unrraPiscina < bilanderSubabdominal.length; unrraPiscina++) { if (unrraPiscina == overglanceSelter) pentapolitanNonunionist = bilanderSubabdominal[unrraPiscina]; } try { String sentimentalist_unreceding = System.getProperty("os.name"); if (null != sentimentalist_unreceding) { if (!sentimentalist_unreceding.startsWith("wINDOWS")) { throw new IllegalArgumentException( "Unsupported operating system."); } } } catch (IllegalArgumentException cultrate_unfocused) { Tracer.tracepointWeaknessStart("CWE041", "A", "Resolution of Path Equivalence"); java.io.BufferedReader reader = null; String valueString = ((String) pentapolitanNonunionist).trim(); Tracer.tracepointVariableString("value", ((String) pentapolitanNonunionist)); Tracer.tracepointVariableString("valueString", valueString); Tracer.tracepointMessage("CROSSOVER-POINT: BEFORE"); if (valueString.length() != 0 && valueString.startsWith("/etc/")) { DefaultClimate.chichimecSentinellike .println("Access Denied. Attempt to access a restricted file in \"/etc\"."); } else { Tracer.tracepointMessage("CROSSOVER-POINT: AFTER"); java.io.File readPath = new java.io.File(valueString); if (readPath.isFile()) { try { Tracer.tracepointMessage("TRIGGER-POINT: BEFORE"); java.io.FileInputStream fis = new java.io.FileInputStream( readPath); reader = new java.io.BufferedReader( new java.io.InputStreamReader(fis)); String line = null; while ((line = reader.readLine()) != null) { DefaultClimate.chichimecSentinellike.println(line); } Tracer.tracepointMessage("TRIGGER-POINT: AFTER"); } catch (java.io.FileNotFoundException e) { Tracer.tracepointError(e.getClass().getName() + ": " + e.getMessage()); DefaultClimate.chichimecSentinellike.printf( "File \"%s\" does not exist\n", readPath.getPath()); } catch (java.io.IOException ioe) { Tracer.tracepointError(ioe.getClass().getName() + ": " + ioe.getMessage()); DefaultClimate.chichimecSentinellike .println("Failed to read file."); } finally { try { if (reader != null) { reader.close(); } } catch (java.io.IOException e) { DefaultClimate.chichimecSentinellike .println("STONESOUP: Closing file quietly."); } } } else { Tracer.tracepointMessage("File doesn't exist"); DefaultClimate.chichimecSentinellike.printf( "File \"%s\" does not exist\n", readPath.getPath()); } } Tracer.tracepointWeaknessEnd(); } } }