private void OnMessageMarketBuyRes(TcpSession session, GameMessage message) { string name = message.Get <string>("itemname"); decimal price = message.Get <decimal>("price"); BConsole.WriteLine("market trade! item=", name, ", price=", price); }
private void OnMessageLoginReq(TcpSession session, GameMessage message) { string uid = message.Get <string>("uid"); string passcode = message.Get <string>("passcode"); var user = gamedb.Users.Select(uid, passcode); if (null == user) { session.Write(new GameMessage("error").With("message", "unknown uid or password mismatch")); return; } // session key string scode = session.ID; lock (sessions) sessions[scode] = uid; // login ok session.Write( new GameMessage("login.res") .With("scode", scode) .With("address", web4b.GetUserAddress(uid)) .With("poaUrl", poaUrl) .With("rpcUrl", rpcUrl) ); }
private void OnMessageLoginRes(TcpSession session, GameMessage message) { // session code & user address SessionKey = message.Get <string>("scode"); UserAddress = message.Get <string>("address"); string poaUrl = message.Get <string>("poaUrl"); string rpcUrl = message.Get <string>("rpcUrl"); // start bryllite api service PoA = new PoAHelper(poaUrl, OnPoARequest, rpcUrl); PoA.Start(Uid, UserAddress); BConsole.WriteLine("login success! session=", SessionKey, ", address=", UserAddress, ", poaUrl=", poaUrl, ", rpcUrl=", rpcUrl); }
private void OnMessageWithdrawRes(TcpSession session, GameMessage message) { // txid string txid = message.Get <string>("txid"); BConsole.WriteLine("txid=", txid); }
private void OnMessageShopListRes(TcpSession session, GameMessage message) { // items var items = message.Get <JArray>("items"); BConsole.WriteLine("shop=", items); }
private void OnMessageShopBuyRes(TcpSession session, GameMessage message) { // txid var item = message.Get <JObject>("item"); BConsole.WriteLine("item=", item); }
private void OnMessageMarketUnregisterReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } // order no. string order = message.Get <string>("order"); // sales var sales = gamedb.Market.Select(order); if (null == sales) { session.Write(new GameMessage("error").With("message", "unknown order")); return; } // owner? if (sales.Seller != uid) { session.Write(new GameMessage("error").With("message", "not owner")); return; } // remove sales from market if (!gamedb.Market.Delete(order)) { session.Write(new GameMessage("error").With("message", "not cancellable")); return; } // get back to user var inven = gamedb.Inventories.Select(uid); inven.Add(sales.ItemCode); gamedb.Inventories.Update(uid, inven); session.Write(new GameMessage("market.unregister.res").With("order", order)); }
private void OnMessageKeyExportTokenRes(TcpSession session, GameMessage message) { // AES decrypt with session key byte[] encrypted = Hex.ToByteArray(message.Get <string>("token")); if (Aes256.TryDecrypt(Encoding.UTF8.GetBytes(SessionKey), encrypted, out var plain)) { BConsole.WriteLine("key export token: ", Hex.ToString(plain)); } }
private void OnMessageTokenReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } // access token (string accessToken, string error) = web4b.GetPoAToken(uid, message.Get <string>("hash"), message.Get <string>("iv")); // access token session.Write( new GameMessage("token.res") .With("accessToken", accessToken) ); }
private void OnMessageWithdrawReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } string signer = web4b.GetUserKey(uid); string to = message.Get <string>("to"); decimal value = message.Get <decimal>("value"); // withdraw string txid = web4b.SendWithdrawAsync(signer, to, web4b.ToBeryl(value)).Result.txid; session.Write(new GameMessage("withdraw.res").With("txid", txid)); }
private void OnMessageMarketRegisterReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } // itemcode & price string itemcode = message.Get <string>("itemcode"); decimal price = message.Get <decimal>("price"); // item info var item = gamedb.Items.Select(itemcode); if (null == item) { session.Write(new GameMessage("error").With("message", "unknown item code")); return; } // remove item from user inventory var inven = gamedb.Inventories.Select(uid); if (!inven.Remove(itemcode)) { session.Write(new GameMessage("error").With("message", "not owned item")); return; } gamedb.Inventories.Update(uid, inven); // register market var sales = new Sales(uid, itemcode, item.Name, price); gamedb.Market.Insert(sales); session.Write(new GameMessage("market.register.res").With("order", sales.Order)); }
private void OnMessageInfoReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } // select user var user = gamedb.Users.Select(uid); if (ReferenceEquals(user, null)) { session.Write(new GameMessage("error").With("message", "user data not found")); return; } // user information JObject info = new JObject(); info.Put <string>("uid", uid); info.Put <string>("rdate", user.RegisterDate); // user balance & nonce string address = web4b.GetUserAddress(uid); ulong balance = web4b.GetBalanceAsync(address).Result.balance ?? 0; ulong nonce = web4b.GetNonceAsync(address).Result.nonce ?? 0; info.Put <string>("address", address); info.Put <decimal>("balance", Coin.ToCoin(balance)); info.Put <ulong>("nonce", nonce); // user inventory JArray inven = new JArray(); foreach (var code in gamedb.Inventories.Select(uid)) { var item = gamedb.Items.Select(code); inven.Add(JObject.Parse(item.ToString())); } info.Put <JArray>("inventory", inven); session.Write( new GameMessage("info.res") .With("info", info) ); }
private void OnMessageShopBuyReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } // item var item = gamedb.Items.Select(message.Get <string>("itemcode")); if (null == item) { session.Write(new GameMessage("error").With("message", "unknown item code")); return; } // user's balance -> shop address string signer = web4b.GetUserKey(uid); string txid = web4b.SendTransferAsync(signer, shopAddress, web4b.ToBeryl(item.Price)).Result.txid; if (string.IsNullOrEmpty(txid)) { session.Write(new GameMessage("error").With("message", "txid not found")); return; } // item to user inventory var inventory = gamedb.Inventories.Select(uid); inventory.Add(item.Code); gamedb.Inventories.Update(uid, inventory); session.Write(new GameMessage("shop.buy.res").With("item", JObject.FromObject(item))); }
private void OnMessageMarketListRes(TcpSession session, GameMessage message) { var sales = message.Get <JArray>("sales"); if (sales.Count == 0) { BConsole.WriteLine("no sales in market"); return; } foreach (var sale in sales) { BConsole.WriteLine(sale); } }
private void OnMessageLogoutReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } lock (sessions) sessions.Remove(scode); // logout ok session.Write( new GameMessage("logout.res") ); }
private void OnMessageMarketListReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } var sales = new JArray(); foreach (var sale in gamedb.Market.All) { sales.Add(JObject.Parse(sale.ToString())); } session.Write(new GameMessage("market.list.res").With("sales", sales)); }
private void OnMessageKeyExportTokenReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } Task.Run(async() => { (string token, string error) = await web4b.GetUserKeyExportTokenAsync(uid); BConsole.WriteLine("token: ", token, ", error: ", error); // AES encrypt with session key if (Aes256.TryEncrypt(Encoding.UTF8.GetBytes(scode), Hex.ToByteArray(token), out var encrypted)) { session.Write(new GameMessage("key.export.token.res").With("token", Hex.ToString(encrypted))); } }); }
private void OnMessageMarketUnregisterRes(TcpSession session, GameMessage message) { string order = message.Get <string>("order"); BConsole.WriteLine("market unregistered! order=", order); }
private void OnMessageInfoRes(TcpSession session, GameMessage message) { // info BConsole.WriteLine(message.Get <JObject>("info")); }
private void OnMessageTokenRes(TcpSession session, GameMessage message) { poaToken = message.Get <string>("accessToken"); }
private void OnMessageMarketBuyReq(TcpSession session, GameMessage message) { string scode = message.Get <string>("session"); string uid = GetUidBySession(scode); if (string.IsNullOrEmpty(uid) || scode != session.ID) { session.Write(new GameMessage("error").With("message", "unknown session key")); return; } Task.Run(async() => { // order no. string order = message.Get <string>("order"); var sales = gamedb.Market.Select(order); if (null == sales) { session.Write(new GameMessage("error").With("message", "unknown order")); return; } // payment = price - commission ulong price = Coin.ToBeryl(sales.Price); ulong commission = (ulong)(price * app.Commission); ulong payment = price - commission; BConsole.WriteLine("price=", price); BConsole.WriteLine("commission=", commission); BConsole.WriteLine("payment=", payment); string signer = web4b.GetUserKey(uid); string seller = web4b.GetUserAddress(sales.Seller); // send tx string txid = (await web4b.SendTransferAsync(signer, seller, payment, commission)).txid; if (string.IsNullOrEmpty(txid)) { session.Write(new GameMessage("error").With("message", "tx failed")); return; } // wait for tx confirm var receipt = await web4b.WaitTransactionReceiptAsync(txid); if (ReferenceEquals(receipt.receipt, null)) { session.Write(new GameMessage("error").With("message", "timeout")); return; } // remove sales gamedb.Market.Delete(order); // item to buyer var inven = gamedb.Inventories.Select(uid); inven.Add(sales.ItemCode); gamedb.Inventories.Update(uid, inven); session.Write(new GameMessage("market.buy.res").With("itemname", sales.ItemName).With("price", sales.Price)); }); }