Skip to content

Commit

Permalink
fix in kotlin, c#, cpp11, php, go
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisKolganov committed Sep 27, 2017
1 parent 31d4d05 commit d7bde81
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 44 deletions.
97 changes: 56 additions & 41 deletions clients/c_sharp_client/client/core/api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ public Elevator (JObject elevator)
this.id = (int)elevator.GetValue("id");
this.y = (float)elevator.GetValue("y");

this.passengers = new List<Passenger>();
foreach (Object passennger in (JArray)elevator.GetValue("passengers"))
{
passengers.Add(new Passenger((JObject)passennger));
}
this.passengers = Helpers.ParsePassengers(elevator, "passengers");

this.state = (int)elevator.GetValue("state");
this.speed = (float)elevator.GetValue("speed");
Expand Down Expand Up @@ -128,7 +124,7 @@ public int Id
}

public int Elevator
{
{
get { return elevator; }
}

Expand Down Expand Up @@ -166,19 +162,19 @@ public int State
}

public int TimeToAway
{
{
get { return timeToAway; }
}

public String Type
{
public String Type
{
get { return type; }
}

public int Floor
public int Floor
{
get { return floor; }
}
}

public List<JObject> Messages
{
Expand All @@ -190,12 +186,18 @@ public List<JObject> Messages
public Passenger (JObject passenger)
{
id = (int)passenger.GetValue ("id");
try {
elevator = (int)passenger.GetValue ("elevator");

JToken jToken = passenger.GetValue ("elevator");

if (jToken != null && jToken.Type == JTokenType.Integer)
{
elevator = (int)jToken;
}
catch (System.ArgumentException) {
else
{
elevator = -1;
}

x = (float)passenger.GetValue("x");
y = (float)passenger.GetValue("y");
weight = (float)passenger.GetValue("weight");
Expand Down Expand Up @@ -244,7 +246,7 @@ public DebugClass()

public List<JObject> Messages
{
get
get
{
List<JObject> messages = this.messages;
this.messages = new List<JObject>();
Expand Down Expand Up @@ -282,10 +284,10 @@ public class api
baseStrategy strategy = null;


public api()
public api()
{
debug = new DebugClass ();
try
try
{
strategy = new client.strategy ();
strategy.Debug = debug;
Expand All @@ -297,38 +299,22 @@ public api()

private Tuple<List<Passenger>, List<Elevator>, List<Passenger>, List<Elevator>> parseState (JObject state)
{
List<Passenger> myPassengers = new List<Passenger>();
List<Elevator> myElevators = new List<Elevator>(3);
List<Passenger> myPassengers = Helpers.ParsePassengers(state, "my_passengers");
List<Elevator> myElevators = Helpers.ParseElevators(state, "my_elevators");

foreach (Object passennger in (JArray) state.GetValue("my_passengers")) {
myPassengers.Add (new Passenger ((JObject)passennger));
}
foreach (Object elevator in (JArray) state.GetValue("my_elevators")) {
myElevators.Add (new Elevator ((JObject)elevator));
}

List<Passenger> enemyPassengers = new List<Passenger>();
List<Elevator> enemyElevators = new List<Elevator>(3);

foreach (Object passennger in (JArray)state.GetValue("enemy_passengers"))
{
enemyPassengers.Add(new Passenger((JObject)passennger));
}
foreach (Object elevator in (JArray)state.GetValue("enemy_elevators"))
{
enemyElevators.Add(new Elevator((JObject)elevator));
}
List<Passenger> enemyPassengers = Helpers.ParsePassengers(state, "enemy_passengers");
List<Elevator> enemyElevators = Helpers.ParseElevators(state, "enemy_elevators");


return new Tuple<List<Passenger>, List<Elevator>, List<Passenger>, List<Elevator>>(myPassengers, myElevators, enemyPassengers, enemyElevators);
}

public JArray turn (JObject state) {
Tuple<List<Passenger>, List<Elevator>, List<Passenger>, List<Elevator>> quadruple = parseState(state);
try
try
{
this.strategy.onTick (quadruple.Item1, quadruple.Item2, quadruple.Item3, quadruple.Item4);
} catch (Exception e)
} catch (Exception e)
{
debug.exception (e);
}
Expand All @@ -352,7 +338,36 @@ public JArray turn (JObject state) {

return resultArray;
}

}
}

static class Helpers
{
public static List<Passenger> ParsePassengers(JObject jObject, string propertyName)
{
var values = (JArray)jObject.GetValue(propertyName);

List<Passenger> passengers = new List<Passenger>(values.Count);

foreach (JObject passenger in values)
{
passengers.Add(new Passenger(passenger));
}

return passengers;
}

public static List<Elevator> ParseElevators(JObject jObject, string propertyName)
{
var values = (JArray)jObject.GetValue(propertyName);

List<Elevator> elevators = new List<Elevator>(values.Count);

foreach (JObject elevator in values)
{
elevators.Add(new Elevator(elevator));
}

return elevators;
}
}
}
4 changes: 3 additions & 1 deletion clients/cpp11_client/client/core/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Debug {
QString toString(double smth) { return QString::number(smth); }

QString toString(char smth) { return QString(smth); }
QString toString(const std::string &smth) { return QString::fromStdString(smth); }
QString toString(const char *smth) { return QString(smth); }
QString toString(QString smth) { return QString(smth); }
QString toString(Passenger& smth) { return "Passenger id=" + toString(smth.id); }
QString toString(Elevator& smth) { return "Elevator id=" + toString(smth.id); }
Expand Down Expand Up @@ -222,4 +224,4 @@ private slots:
void sendActions(QJsonArray);
};

#endif // API_H
#endif // API_H
2 changes: 1 addition & 1 deletion clients/go_client/client/src/client/core/API/API.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (a *API) parseState(state map[string]interface{}) ([]*Passenger, []*Elevato
}

func (a *API) Turn(state map[string]interface{}) []interface{}{
var messages []interface{}
messages := []interface{}{}

myPassengers, myElevators, enemyPassengers, enemyElevators := a.parseState(state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Passenger(passenger: JSONObject) : MessagesInterface{
private val messages = ArrayList<JSONObject>()
var x: Double = passenger["x"]?.toString()?.toDouble() ?: 0.0
var y: Double = passenger["y"]?.toString()?.toDouble() ?: 0.0
var weight: Double = passenger["weight"]?.toString()?.toDouble() ?: 0.0

override fun getMessages(): List<JSONObject> = messages

Expand Down
3 changes: 2 additions & 1 deletion clients/php7_client/client/core/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ public function parse_state($state) {
$p->from_floor,
$p->dest_floor,
$p->type,
$p->floor
$p->floor,
$p->weight
);
}, $enemy_passengers);

Expand Down

0 comments on commit d7bde81

Please sign in to comment.