Skip to content

Commit 6b2a3c8

Browse files
authoredMay 6, 2024··
Merge pull request #304 from asdawej/chore
refactor: check property & refactor `Cal` to `LadderCalculate`
2 parents 94be04c + 512b779 commit 6b2a3c8

File tree

8 files changed

+51
-60
lines changed

8 files changed

+51
-60
lines changed
 

‎logic/GameClass/GameClassLogging.cs

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
namespace GameClass.GameObj
55
{
6+
public static class GameObjLogger
7+
{
8+
public static readonly Logger logger = new("GameObj");
9+
}
10+
611
public static class LoggingFunctional
712
{
813
public static string ShipLogInfo(Ship ship)

‎logic/GameClass/GameObj/Areas/Resource.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GameClass.GameObj.Areas;
88
public class Resource(XY initPos)
99
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Resource)
1010
{
11-
public InVariableRange<long> HP { get; } = new InVariableRange<long>(GameData.ResourceHP);
11+
public InVariableRange<long> HP { get; } = new(GameData.ResourceHP);
1212
public override bool IsRigid => true;
1313
public override ShapeType Shape => ShapeType.Square;
1414
public AtomicInt ProduceNum { get; } = new AtomicInt(0);

‎logic/GameClass/GameObj/Areas/Wormhole.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace GameClass.GameObj.Areas;
77

88
public class Wormhole(List<WormholeCell> cells, int id)
99
{
10-
public InVariableRange<long> HP = new(GameData.WormholeHP);
10+
public InVariableRange<long> HP { get; } = new(GameData.WormholeHP);
1111
private readonly List<WormholeCell> cells = cells;
1212
public List<WormholeCell> Cells => cells;
1313
public AtomicInt RepairNum { get; } = new AtomicInt(0);

‎logic/GameClass/GameObj/Base.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,12 @@ public Base(Home home)
3737
}
3838
return false;
3939
},
40-
activator: (ship) =>
41-
{
42-
ship.CanMove.SetROri(true);
43-
ship.IsRemoved.SetROri(false);
44-
},
4540
inactivator: (ship) =>
4641
{
4742
ship.CanMove.SetROri(false);
4843
ship.IsRemoved.SetROri(true);
4944
});
45+
// 池初始化,但是由于服务器采用逐个添加船只的方式,因此这里不进行任何行为
5046
ShipPool.Initiate(ShipType.CivilShip, 0,
5147
() => new(GameData.ShipRadius, ShipType.CivilShip, MoneyPool));
5248
ShipPool.Initiate(ShipType.WarShip, 0,

‎logic/GameClass/GameObj/GameObj.cs

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
namespace GameClass.GameObj
99
{
10-
public static class GameObjLogger
11-
{
12-
public static readonly Logger logger = new("GameObj");
13-
}
1410
/// <summary>
1511
/// 一切游戏元素的总基类,与THUAI4不同,继承IMoveable接口(出于一切物体其实都是可运动的指导思想)——LHR
1612
/// </summary>

‎logic/GameClass/GameObj/Ship.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ShipStateType ShipState
4646
/// <summary>
4747
/// 子弹数上限, THUAI7为无穷
4848
/// </summary>
49-
public IntNumUpdateEachCD BulletNum => new(int.MaxValue, 1);
49+
public IntNumUpdateEachCD BulletNum { get; } = new(int.MaxValue, 1);
5050
/// <summary>
5151
/// 模块相关
5252
/// </summary>

‎logic/Preparation/Utility/Value/SafeValue/ObjPool.cs

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace Preparation.Utility.Value.SafeValue;
77
public class ObjPool<T, TType>(Func<T, TType> classfier,
88
Func<T, bool> idleChecker,
99
Func<T, bool> tryActivator,
10-
Action<T> activator,
1110
Action<T> inactivator)
1211
: IObjPool<T, TType>
1312
where T : class
@@ -18,7 +17,6 @@ public class ObjPool<T, TType>(Func<T, TType> classfier,
1817
private readonly Func<T, TType> classfier = classfier;
1918
private readonly Func<T, bool> idleChecker = idleChecker;
2019
private readonly Func<T, bool> tryActivator = tryActivator;
21-
private readonly Action<T> activator = activator;
2220
private readonly Action<T> inactivator = inactivator;
2321

2422
#region 属性

‎logic/Server/GameServer.cs

+42-46
Original file line numberDiff line numberDiff line change
@@ -133,73 +133,69 @@ protected async Task<double[]> GetLadderScore(double[] scores)
133133
// 解析 JSON 字符串
134134
var result = JsonConvert.DeserializeObject<List<ContestResult>>(jsonString);
135135
double[] org = (from r in result select (double)(r.score)).ToArray();
136-
double[] final = Cal(org, scores);
136+
double[] final = LadderCalculate(org, scores);
137137
return final;
138138
}
139139
catch (Exception e)
140140
{
141141
GameServerLogging.logger.ConsoleLog("No response from ladder URL!");
142142
GameServerLogging.logger.ConsoleLog(e.ToString());
143-
return new double[0];
143+
return [];
144144
}
145145
}
146146
else
147147
{
148148
GameServerLogging.logger.ConsoleLog("Null URL!");
149-
return new double[0];
149+
return [];
150150
}
151151
}
152152

153-
protected double[] Cal(double[] orgScore, double[] competitionScore)
153+
protected static double[] LadderCalculate(double[] oriScores, double[] competitionScores)
154154
{
155-
// 调整顺序,让第一个元素成为获胜者,便于计算
156-
bool reverse = false; // 记录是否需要调整
157-
if (competitionScore[0] < competitionScore[1])
155+
// 调整顺序,让第一项成为获胜者,便于计算
156+
bool scoresReverse = false; // 顺序是否需要交换
157+
if (competitionScores[0] < competitionScores[1]) // 第一项为落败者
158+
scoresReverse = true;
159+
else if (competitionScores[0] == competitionScores[1])// 平局
158160
{
159-
reverse = true;
160-
}
161-
else if (competitionScore[0] == competitionScore[1])
162-
{
163-
if (orgScore[0] == orgScore[1])
164-
{
161+
if (oriScores[0] == oriScores[1])
165162
// 完全平局,不改变天梯分数
166-
return orgScore;
167-
}
168-
if (orgScore[0] > orgScore[1])
169-
{
170-
// 本次游戏平局,但一方天梯分数高,另一方天梯分数低,需要将两者向中间略微靠拢,因此天梯分数低的定为获胜者
171-
reverse = true;
172-
}
163+
return oriScores;
164+
if (oriScores[0] > oriScores[1])
165+
// 本次游戏平局,但一方天梯分数高,另一方天梯分数低,
166+
// 需要将两者向中间略微靠拢,因此天梯分数低的定为获胜者
167+
scoresReverse = true;
173168
}
174-
if (reverse)
169+
if (scoresReverse)// 如果需要换,交换两者的顺序
175170
{
176-
// 如果需要换,换两者的顺序
177-
double t = competitionScore[1];
178-
competitionScore[1] = competitionScore[0];
179-
competitionScore[0] = t;
180-
t = orgScore[1];
181-
orgScore[1] = orgScore[0];
182-
orgScore[0] = t;
171+
(competitionScores[0], competitionScores[1]) = (competitionScores[1], competitionScores[0]);
172+
(oriScores[0], oriScores[1]) = (oriScores[1], oriScores[0]);
183173
}
174+
175+
const double normalDeltaThereshold = 1000.0; // 分数差标准化参数,同时也是大分数差阈值
176+
const double correctParam = normalDeltaThereshold * 1.2;// 修正参数
177+
const double winnerWeight = 9e-6; // 获胜者天梯得分权值
178+
const double loserWeight = 5e-6; // 落败者天梯得分权值
179+
const double scoreDeltaThereshold = 2100.0; // 极大分数差阈值
180+
184181
double[] resScore = [0, 0];
185-
double deltaWeight = 1000.0; // 差距悬殊判断参数
186-
double delta = (orgScore[0] - orgScore[1]) / deltaWeight;
187-
// 盈利者天梯得分权值、落败者天梯得分权值
188-
double firstnerGet = 9e-6;
189-
double secondrGet = 5e-6;
190-
double deltaScore = 2100.0; // 两队竞争分差超过多少时就认为非常大
191-
double correctRate = (orgScore[0] - orgScore[1]) / (deltaWeight * 1.2); // 订正的幅度,该值越小,则在势均力敌时天梯分数改变越大
192-
double correct = 0.5 * (Math.Tanh((competitionScore[0] - competitionScore[1] - deltaScore) / deltaScore - correctRate) + 1.0); // 一场比赛中,在双方势均力敌时,减小天梯分数的改变量
193-
resScore[0] = orgScore[0] + Math.Round(competitionScore[0] * competitionScore[0] * firstnerGet * (1 - Math.Tanh(delta)) * correct); // 胜者所加天梯分
194-
resScore[1] = orgScore[1] - Math.Round(
195-
(competitionScore[0] - competitionScore[1]) * (competitionScore[0] - competitionScore[1]) * secondrGet * (1 - Math.Tanh(delta)) * correct); // 败者所扣天梯分
196-
// 如果换过,再换回来
197-
if (reverse)
198-
{
199-
double t = resScore[1];
200-
resScore[1] = resScore[0];
201-
resScore[0] = t;
202-
}
182+
double oriDelta = oriScores[0] - oriScores[1]; // 原分数差
183+
double competitionDelta = competitionScores[0] - competitionScores[1]; // 本次比赛分数差
184+
double normalOriDelta = oriDelta / normalDeltaThereshold; // 标准化原分数差
185+
double correctRate = oriDelta / correctParam; // 修正率,修正方向为缩小分数差
186+
double correct = 0.5 * (Math.Tanh((competitionDelta - scoreDeltaThereshold) / scoreDeltaThereshold
187+
- correctRate)
188+
+ 1.0); // 分数修正
189+
resScore[0] = oriScores[0] + Math.Round(Math.Pow(competitionScores[0], 2)
190+
* winnerWeight
191+
* (1 - Math.Tanh(normalOriDelta))
192+
* correct); // 胜者所加天梯分
193+
resScore[1] = oriScores[1] - Math.Round(Math.Pow(competitionDelta, 2)
194+
* loserWeight
195+
* (1 - Math.Tanh(normalOriDelta))
196+
* correct); // 败者所扣天梯分
197+
if (scoresReverse)// 顺序换回
198+
(resScore[0], resScore[1]) = (resScore[1], resScore[0]);
203199
return resScore;
204200
}
205201

0 commit comments

Comments
 (0)
Please sign in to comment.