Commit f28de100 authored by Akhmad Fakhoni Listiyan Dede's avatar Akhmad Fakhoni Listiyan Dede
Browse files

Small fix in field function

parent 087aaa63
package HelperClasses;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Server {
public static void main(String[] args) {
try
{
int port = 8080;
String username = "abcdef";
String password = "abcdef";
Random rand = new Random(); // untuk random posisi awal player
System.out.println("Server is listening on localhost:" + port);
ServerSocket serverSocket = new ServerSocket(port);
int size = rand.nextInt(8) + 2;
while(true)
{
Socket connectionSocket = serverSocket.accept();
InputStream input = connectionSocket.getInputStream();
DataOutputStream output = new DataOutputStream(connectionSocket.getOutputStream());
byte[] client_bytes = new byte[4096];
int count = input.read(client_bytes);
String client_message = new String(client_bytes);
client_message = client_message.substring(0, count);
System.out.println("client_message: " + client_message);
JSONParser parser = new JSONParser();
JSONObject client_json = (JSONObject) parser.parse(client_message);
System.out.println("Client Request: " + client_json.toJSONString());
String method = (String) client_json.get("method");
JSONObject response = new JSONObject();
switch (method)
{
case "login":
{
if(client_json.get("username").equals(username) &&
client_json.get("password").equals(password))
{
response.put("status", "ok");
response.put("token", "47af9eb308ce2fc95bcc088add67b79e");
response.put("x", rand.nextInt(size-1));
response.put("y", rand.nextInt(size-1));
response.put("time", System.currentTimeMillis()/1000L);
}
else
{
response.put("status", "fail");
response.put("description", "username / password combination not found");
}
break;
}
case "signup":
{
if(!client_json.get("username").equals(username))
{
response.put("status", password);
}
else
{
response.put("status", "fail");
response.put("description", "username exists");
}
break;
}
case "map":
{
response.put("status", "ok");
response.put("name", "Akihabara");
response.put("width", size);
response.put("height", size);
break;
}
case "move":
{
if(rand.nextInt(100) <= 50)
{
/* response ok */
response.put("status", "ok");
response.put("time", System.currentTimeMillis()/1000L+5);
}
else
{
/* response fail */
response.put("status", "fail");
response.put("description", "FAILURE MESSAGE HERE");
}
break;
}
case "field":
{
if(rand.nextInt(100) <= 50)
{
/* response ok */
response.put("status", "ok");
response.put("item", rand.nextInt(3));
}
else
{
/* response fail */
response.put("status", "fail");
response.put("description", "FAILURE MESSAGE HERE");
}
break;
}
case "inventory":
{
response.put("status", "ok");
JSONArray array = new JSONArray();
for(int i=0; i<10; i++)
{
array.add(rand.nextInt(10));
}
response.put("inventory", array);
break;
}
case "mixitem" :
{
int item1 = Integer.parseInt(client_json.get("item1").toString());
int item2 = Integer.parseInt(client_json.get("item2").toString());
if(Math.abs(item1 - item2) == 1)
{
if(item1 + item2 == 7 || item1 + item2 == 13)
{
response.put("status", "fail");
response.put("description","error mixture");
} else
{
response.put("status", "ok");
response.put("item", 9);
}
}
else
{
response.put("status", "fail");
response.put("description","error mixture");
}
break;
}
case "offer" :
{
if(rand.nextInt(100) <= 50)
{
response.put("status", "ok");
}
else
{
response.put("status", "fail");
response.put("description", "insufficient");
}
break;
}
case "tradebox" :
{
response.put("status", "ok");
JSONArray offers = new JSONArray();
int jumlah_offer = rand.nextInt(50);
for(int i = 0; i < jumlah_offer; i++)
{
JSONArray offer = new JSONArray();
offer.add(rand.nextInt(10));
offer.add(rand.nextInt(10));
offer.add(rand.nextInt(10));
offer.add(rand.nextInt(10));
if(rand.nextInt(100) <= 50)
{
offer.add(false);
offer.add("0f5bb63592f716c05511cec90e171a89");
}
else
{
offer.add(true);
offer.add("a8179cde12f80a011874bce9d8a01483");
}
offers.add(offer);
}
response.put("offers", offers);
break;
}
case "sendfind" :
{
response.put("status", "ok");
JSONArray offers = new JSONArray();
int jumlah_offer = rand.nextInt(50);
for(int i = 0; i < jumlah_offer; i++)
{
JSONArray offer = new JSONArray();
offer.add(Integer.parseInt(client_json.get("item").toString()));
offer.add(rand.nextInt(10));
offer.add(rand.nextInt(10));
offer.add(rand.nextInt(10));
if(rand.nextInt(100) <= 50)
{
offer.add(false);
offer.add("0f5bb63592f716c05511cec90e171a89");
}
else
{
offer.add(true);
offer.add("a8179cde12f80a011874bce9d8a01483");
}
offers.add(offer);
}
response.put("offers", offers);
break;
}
case "fetchitem" :
{
if(rand.nextInt(100)<=50)
{
response.put("status", "ok");
}
else
{
response.put("status", "fail");
response.put("description", "FAILURE MESSAGE HERE");
}
break;
}
case "canceloffer" :
{
if(rand.nextInt(100)<=50)
{
response.put("status", "ok");
}
else
{
response.put("status", "fail");
response.put("description", "FAILURE MESSAGE HERE");
}
break;
}
case "sendaccept" :
{
if(rand.nextInt(100) <= 50)
{
response.put("status", "ok");
}
else
{
response.put("status", "fail");
response.put("description","FAILURE MESSAGE HERE");
}
break;
}
default:
{
response.put("status", "error");
break;
}
}
System.out.println("response: " + response);
output.write(response.toString().getBytes());
}
}
catch (IOException | ParseException ex)
{
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
......@@ -453,10 +453,10 @@ public class Message {
}
String [][] map = Store.getMap();
String item = map[x][y];
int item = Store.getItemIndex(x, y);
String response;
try {
if(Database.addItemToInventory(token, Store.getItemIndex(x, y)))
if(Database.addItemToInventory(token, item))
{
jsonObj.put("status", "ok");
jsonObj.put("item", item);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment