Skip to content

Commit

Permalink
update server to v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Yvees committed Jul 11, 2018
1 parent d1f4476 commit 57fbf5e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 63 deletions.
5 changes: 3 additions & 2 deletions src/sdk/JustRush/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ static void Main(string[] args)
{
string input = string.Empty;

//ServerHelper.Initialize("http://106.75.33.221:6000/");
ServerHelper.Initialize("http://localhost:6000/");
ServerHelper.Initialize("http://106.75.33.221:6000/");
//ServerHelper.Initialize("http://localhost:6000/");

map = MapHelper.GetMap("RectSmall");
game = new Game(map.Rows.Count, map.Rows[0].Count);
Expand Down Expand Up @@ -155,6 +155,7 @@ static void RushAttack()
else if (game.State > 1)
{
Console.WriteLine("Game over");

break;
}

Expand Down
63 changes: 35 additions & 28 deletions src/server/MagCore.Core/ActionLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,49 @@ public static int Calc(Model.Action action, Cell target, Player sender)
{
if (target == null || sender == null)
{
throw new ArgumentNullException();
return Int32.MinValue;
}
switch (action)
{
case Model.Action.Attack:
if (target.State == CellState.Empty)
return 1000;
else if (target.State == CellState.Occupied)
{
int time = 1000;
if (target.Owner != sender
&& DateTime.Now > target.OccupiedTime)
{
//attack other
var duration = (DateTime.Now - target.OccupiedTime).Value.TotalSeconds;
if (duration > 60)
time = 3000;
else if (duration > 30)
time = 6000;
else if (duration > 15)
time = 9000;
else if (duration > 5)
time = 12000;
else
time = 15000;
}

//Console.WriteLine("Sleep time:" + time.ToString());
return time;
}
else
return 0;
return _calcInternal(action, target, sender);
default:
return 0;
}

}

internal static int _calcInternal(Model.Action action, Cell target, Player sender)
{
if (target.State == CellState.Occupied)
{
int time = 1000;
if (target.Owner != sender
&& DateTime.Now > target.OccupiedTime)
{
//attack other
var ts = DateTime.Now - target.OccupiedTime;
if (ts != null && ts.HasValue)
{
var duration = ts.Value.TotalSeconds;
if (duration > 60)
time = 3000;
else if (duration > 30)
time = 6000;
else if (duration > 15)
time = 9000;
else if (duration > 5)
time = 12000;
else
time = 15000;
}
}

//Console.WriteLine("Sleep time:" + time.ToString());
return time;
}
else
return 1000;
}
}
}
86 changes: 53 additions & 33 deletions src/server/MagCore.Core/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,34 @@ public Game(IMap map)
else
Thread.Sleep(1000);
}
while (_state != GameState.Done)
try
{
if (_commands.IsEmpty || _state != GameState.Playing)
while (_state != GameState.Done)
{
Thread.Sleep(100);
continue;
}
else if (_commands.TryDequeue(out var cmd))
{
switch (cmd.Action)
if (_commands.IsEmpty || _state != GameState.Playing)
{
case Model.Action.Attack:
ProcessAttack(cmd, _map);
break;
default:
break;
Thread.Sleep(100);
continue;
}
else if (_commands.TryDequeue(out var cmd))
{
switch (cmd.Action)
{
case Model.Action.Attack:
ProcessAttack(cmd, _map);
break;
default:
break;
}
}
}
ProcessVictory();
ProcessVictory();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}).ContinueWith((task) => {
//Recycling
Expand All @@ -100,33 +108,45 @@ public Game(IMap map)

private void ProcessAttack(Command cmd, IMap map)
{
var cell = map.Locate(cmd.Target);
var player = Core.Players.Get(cmd.Sender);
int time = ActionLogic.Calc(cmd.Action, cell, player);

if (cell.CanAttack(player))
int time = 0;
try
{
Task<bool>.Factory.StartNew(() =>
{
return cell.BeginChangeOwner(player, time);
}).ContinueWith((task) =>
var cell = map.Locate(cmd.Target);
var player = Core.Players.Get(cmd.Sender);

if (cell != null && cell.CanAttack(player))
{
if (task.Result)
return cell.EndChangeOwner(player);
else
return false;
}).ContinueWith((task) => {
if (task.Result)
ProcessAttackResult(player, cell);
time = ActionLogic.Calc(cmd.Action, cell, player);
Task<bool>.Factory.StartNew(() =>
{
return cell.BeginChangeOwner(player, time);
}).ContinueWith((task) =>
{
if (task.Result)
return cell.EndChangeOwner(player);
else
return false;
}).ContinueWith((task) => {
if (task.Result)
ProcessAttackResult(player, cell);
ClearLostCells();
});
ClearLostCells();
});
}
}
catch (Exception ex)
{
Console.WriteLine("cell pos:" + cmd.Target.ToString());
Console.WriteLine("sender:" + cmd.Sender);
Console.WriteLine("calced time:" + time.ToString());
throw ex;
}
}

private void ProcessVictory()
{
int iCount = 0;

foreach (Player player in Players.Values)
{
if (player.State == PlayerState.Playing)
Expand Down

0 comments on commit 57fbf5e

Please sign in to comment.