235 lines
7.7 KiB
C#
235 lines
7.7 KiB
C#
using Assets.MyGame.Scripts.Net;
|
|
using Assets.MyGame.Scripts.Net.WebResponses;
|
|
using Assets.MyGame.Scripts.Net.WebResponses.ChineseChess;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Text;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public partial class WebSocketSendList
|
|
{
|
|
public static string MakeKillFishSign(
|
|
string roomId,
|
|
string userId,
|
|
string fishNumber,
|
|
string fishId,
|
|
long gold,
|
|
long timespan,
|
|
string bulletId,
|
|
long timestamp,
|
|
string signKey
|
|
)
|
|
{
|
|
string rawStr = $"{roomId}|{userId}|{fishNumber}|{fishId}|{gold}|{timespan}|{bulletId}|{timestamp}";
|
|
return Sign(rawStr, signKey);
|
|
}
|
|
|
|
public static string MakeFireCannonSign(string roomId, string userId, long gold, int count, long timestamp, string signKey)
|
|
{
|
|
string rawStr = $"{roomId}|{userId}|{gold}|{count}|{timestamp}";
|
|
Debug.Log($"生成签名的原始字符串:{rawStr}");
|
|
return Sign(rawStr, signKey);
|
|
}
|
|
|
|
|
|
|
|
public static string Sign(string rawStr, string signKey)
|
|
{
|
|
string input = rawStr + "|" + signKey;
|
|
|
|
using (var md5 = System.Security.Cryptography.MD5.Create())
|
|
{
|
|
byte[] bytes = Encoding.UTF8.GetBytes(input);
|
|
byte[] hash = md5.ComputeHash(bytes);
|
|
|
|
// 转成 hex 字符串(前 8 位)
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < hash.Length; i++)
|
|
{
|
|
sb.Append(hash[i].ToString("x2"));
|
|
}
|
|
|
|
return sb.ToString().Substring(0, 8);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取房间信息
|
|
/// </summary>
|
|
public static void GetRoomListFish(int leave,int district)
|
|
{
|
|
WebSocketManager.Instance.Send(SocketType.room, "{\"type\":1,\"target\":\"GetRoomList\",\"invocationId\":\"1\",\"arguments\":[\"" + GameManager.Instance.user.id + "\"," + (int)GameManager.Instance.gameData.currGameType + "," + leave + ","+ district + "]}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打中鱼
|
|
/// </summary>
|
|
/// <param name="roomId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="fishNumber">配置id</param>
|
|
/// <param name="fishId">唯一id</param>
|
|
public static void KillFish(
|
|
string roomId,
|
|
string userId,
|
|
string fishNumber,
|
|
string fishId,
|
|
int paoBei,
|
|
string bulletId,
|
|
string signKey
|
|
)
|
|
{
|
|
long timespan = BeijingTimeManager.Instance.CurrentTimestampMs; // 毫秒
|
|
long timestamp = timespan / 1000; // 秒级
|
|
|
|
// count 固定为 1
|
|
string sign = MakeKillFishSign(roomId, userId, fishNumber, fishId, paoBei, timespan, bulletId, timestamp, signKey);
|
|
|
|
var sendObj = new WebSocketSendList(
|
|
"KillFish",
|
|
roomId,
|
|
userId,
|
|
fishNumber,
|
|
fishId,
|
|
paoBei,
|
|
timespan,
|
|
bulletId,
|
|
timestamp,
|
|
sign
|
|
);
|
|
|
|
WebSocketManager.Instance.Send(SocketType.game, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
/// <summary>
|
|
/// 发射子弹
|
|
/// </summary>
|
|
/// <param name="roomId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="gold">单价</param>
|
|
/// <param name="count">数量</param>
|
|
/// <summary>
|
|
/// 发射子弹(与 KillFish 同结构版本)
|
|
/// </summary>
|
|
public static void FireCannon(
|
|
string roomId,
|
|
string userId,
|
|
long gold,
|
|
int count,
|
|
object extra,
|
|
string signKey
|
|
)
|
|
{
|
|
long timespan = BeijingTimeManager.Instance.CurrentTimestampMs; // 毫秒
|
|
long timestamp = timespan / 1000; // 秒级
|
|
|
|
// FireCannon 的签名规则与 KillFish 一样:roomId|userId|gold|count|timestamp
|
|
string sign = MakeFireCannonSign(roomId, userId, gold, count, timestamp, signKey);
|
|
|
|
var sendObj = new WebSocketSendList(
|
|
"FireCannon",
|
|
roomId,
|
|
userId,
|
|
gold,
|
|
count,
|
|
timestamp,
|
|
sign,
|
|
extra
|
|
);
|
|
|
|
WebSocketManager.Instance.Send(SocketType.game, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发射子弹
|
|
/// </summary>
|
|
/// <param name="roomId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="gold">单价</param>
|
|
/// <param name="count">数量</param>
|
|
public static void ShootBullet(string roomId, string userId, int gold, int count)
|
|
{
|
|
var sendObj = new WebSocketSendList("ShootBullet", roomId, userId, gold, count);
|
|
WebSocketManager.Instance.Send(SocketType.game, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
/// <summary>
|
|
/// 链接登录界面ws
|
|
/// </summary>
|
|
/// <param name="userId"></param>
|
|
public static void Bind(string userId)
|
|
{
|
|
var sendObj = new WebSocketSendList("Bind", userId);
|
|
WebSocketManager.Instance.Send(SocketType.login, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
/// <summary>
|
|
/// 加入房间
|
|
/// </summary>
|
|
/// <param name="roomId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="level">房间等级</param>
|
|
public static void JoinRoom(string roomId, string userId, int sitID)
|
|
{
|
|
var sendObj = new WebSocketSendList("JoinRoom", roomId, userId, sitID);
|
|
WebSocketManager.Instance.Send(SocketType.game, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
/// <summary>
|
|
/// 获取房间信息
|
|
/// </summary>
|
|
/// <param name="roomId">房间id</param>
|
|
public static void GetRoomInfo(string roomId)
|
|
{
|
|
var sendObj = new WebSocketSendList("GetRoomInfo", roomId);
|
|
WebSocketManager.Instance.Send(SocketType.room, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
/// <summary>
|
|
/// 使用道具
|
|
/// </summary>
|
|
/// <param name="roomId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="propNumber">道具id</param>
|
|
/// <param name="diamond">道具消耗</param>
|
|
public static void UseProp(string roomId, string userId, string propNumber, int diamond)
|
|
{
|
|
var sendObj = new WebSocketSendList("UseProp", roomId, userId, propNumber, diamond);
|
|
WebSocketManager.Instance.Send(SocketType.game, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
/// <summary>
|
|
/// 离开房间
|
|
/// </summary>
|
|
/// <param name="roomId"></param>
|
|
/// <param name="userId"></param>
|
|
public static void LeaveRoom(string roomId, string userId)
|
|
{
|
|
WebSocketManager.Instance.isGame=false;
|
|
var sendObj = new WebSocketSendList("LeaveRoom", roomId, userId);
|
|
WebSocketManager.Instance.Send(SocketType.game, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
/// <summary>
|
|
/// 更改炮倍
|
|
/// </summary>
|
|
/// <param name="roomId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="multiple"></param>
|
|
public static void ChangeGunMultiple(string roomId, string userId,int multiple)
|
|
{
|
|
var sendObj = new WebSocketSendList("ChangeGunMultiple", roomId, userId, multiple);
|
|
WebSocketManager.Instance.Send(SocketType.game, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据同步
|
|
/// </summary>
|
|
public static void SyncGameData(object obj)
|
|
{
|
|
WebSocketSendList sendObj = new WebSocketSendList
|
|
{
|
|
type = 1,
|
|
invocationId = "1",
|
|
target = "SyncGameData",
|
|
arguments = new System.Collections.Generic.List<object>()
|
|
};
|
|
|
|
sendObj.arguments.Add(GameManager.Instance.gameData.currRoomData.roomId);
|
|
sendObj.arguments.Add(obj);
|
|
WebSocketManager.Instance.Send(SocketType.game, JsonConvert.SerializeObject(sendObj));
|
|
}
|
|
}
|