/*
 * Copyright (c) 2017, Adam <Adam@sigterm.info>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.runelite.http.service.updatecheck;

import com.google.common.base.Suppliers;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.worlds.WorldResult;
import net.runelite.http.service.worlds.WorldsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/update-check")
public class UpdateCheckService
{
	private static final Logger logger = LoggerFactory.getLogger(UpdateCheckService.class);

	private static final int PORT = 43594;
	private static final int WORLD_OFFSET = 300;
	private static final byte HANDSHAKE_TYPE = 15;

	private static final int RESPONSE_OK = 0;
	private static final int RESPONSE_OUTDATED = 6;

	private final WorldsService worldsService;
	private final Supplier<Boolean> updateAvailable = Suppliers.memoizeWithExpiration(this::checkUpdate, 1, TimeUnit.MINUTES);

	@Autowired
	public UpdateCheckService(WorldsService worldsService)
	{
		this.worldsService = worldsService;
	}

	@RequestMapping
	public Boolean check()
	{
		return updateAvailable.get();
	}

	private boolean checkUpdate()
	{
		int nextWorld = randomWorld();
		if (nextWorld == -1)
		{
			return false;
		}

		InetAddress address;
		try
		{
			String url = "oldschool" + nextWorld + ".runescape.com";
			address = InetAddress.getByName(url);
		}
		catch (UnknownHostException ex)
		{
			logger.warn(null, ex);
			return false;
		}

		try (Socket socket = new Socket(address, PORT))
		{
			ByteBuffer buffer = ByteBuffer.allocate(5);
			buffer.put(HANDSHAKE_TYPE);
			buffer.putInt(RuneLiteAPI.getRsVersion());

			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
			os.write(buffer.array());

			int reply = is.read();

			if (reply == RESPONSE_OUTDATED)
			{
				return true;
			}

			if (reply != RESPONSE_OK)
			{
				logger.debug("Non-ok response for handshake: {}", reply);
			}
		}
		catch (IOException ex)
		{
			logger.warn(null, ex);
		}

		return false; // no update
	}

	private int randomWorld()
	{
		try
		{
			WorldResult worlds = worldsService.listWorlds();
			int size = worlds.getWorlds().size();
			Random rand = new Random();
			int worldNumber = worlds.getWorlds().get(rand.nextInt(size)).getId();
			return worldNumber - WORLD_OFFSET;
		}
		catch (IOException ex)
		{
			logger.warn(null, ex);
			return -1;
		}
	}
}

@@ -1662,24 +1662,47 @@
 ByteBuffer;%0A
+import java.util.List;%0A
 import java.
@@ -1830,16 +1830,59 @@
 iteAPI;%0A
+import net.runelite.http.api.worlds.World;%0A
 import n
@@ -2441,54 +2441,8 @@
 94;%0A
-%09private static final int WORLD_OFFSET = 300;%0A
 %09pri
@@ -2977,19 +2977,21 @@
 ()%0A%09%7B%0A%09%09
-int
+World
  nextWor
@@ -3029,18 +3029,20 @@
 orld == 
--1
+null
 )%0A%09%09%7B%0A%09%09
@@ -3114,50 +3114,30 @@
 l = 
-%22oldschool%22 + nextWorld + %22.runescape.com%22
+nextWorld.getAddress()
 ;%0A%09%09
@@ -3863,19 +3863,21 @@
 private 
-int
+World
  randomW
@@ -3916,17 +3916,22 @@
 lt world
-s
+Result
  = world
@@ -3960,24 +3960,34 @@
 %0A%09%09%09
-int size
+List%3CWorld%3E worlds
  = world
 s.ge
@@ -3974,33 +3974,38 @@
 %3E worlds = world
-s
+Result
 .getWorlds().siz
@@ -4004,15 +4004,8 @@
 ds()
-.size()
 ;%0A%09%09
@@ -4040,44 +4040,21 @@
 %0A%09%09%09
-int worldNumber = worlds.getWorlds()
+return worlds
 .get
@@ -4071,60 +4071,23 @@
 Int(
+worlds.
 size
+(
 ))
-.getId();%0A%09%09%09return worldNumber - WORLD_OFFSET
+)
 ;%0A%09%09
@@ -4157,10 +4157,12 @@
 urn 
--1
+null
 ;%0A%09%09

Use the address returned by the worldlist api instead of building our own