In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use SignalR to fight landlords in .net Core". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use SignalR to fight landlords in .net Core.
Direct access to practical information, the first is the data model:
/ user information / public class Customer {/ unique ID / public string? ID {get; set;} / nickname / public string? NickName {get; set;} / Card / public List Card {get; set;}} / public class Room {/ Room name / public string Name {get; set } / homeowner id / public string Masterid {get; set;} / current licensee / public int Curr {get; set;} / current card / public List CurrCard {get; set;} = new List () / the current card presenter / public string ExistingCardClient {get; set;} / public List Customers {get; set;} = new List ();}
Tips: it's only designed for Dou D. the commercial version certainly can't do this. Please use it carefully for reference.
With the data model, you can't do without CRUD:
/ user actions / public static class CustomerAction {/ user list / private static List cusList = new List () / public static void Create (Customer customer) {Customer curr = null; if (cusList.Count > 0) curr = cusList.Where (x = > x.ID = = customer.ID) .FirstOrDefault () If (curr is null) cusList.Add (customer); else {curr.NickName = customer.NickName; Up4ID (curr) }} / user list / public static List GetList () {return cusList } / get a single / public static Customer GetOne (string id) {return cusList.Where (x = > x.ID = = id) .FirstOrDefault () } / delete user / public static void Delete (string id) {cusList.RemoveAll (x = > x.ID = = id) Add cards / public static void InCard (string id, List cards) {Customer customer = cusList.Where (x = > x.ID = = id). FirstOrDefault (); if (customer.Card is null) customer.Card = cards Else customer.Card.AddRange (cards); Up4ID (customer) } / public static bool OutCard (string id, List cards, Room group) {Customer client = cusList.Where (x = > x.ID = = id). FirstOrDefault () If (client is null) return false; / / Card mismatch Direct failure if (client.Card.Where (x = > cards.Contains (x)). ToList (). Count! = cards.Count) return false / / failure of if (! new Game.WithCard () .Rule (group.CurrCard, cards, group.ExistingCardClient is null | | group.ExistingCardClient = = id)) return false; foreach (var item in cards) {client.Card.Remove (item);} group.CurrCard = cards Group.ExistingCardClient = id; Up4ID (client); RoomAction.Up4Name (group); return true Update (according to ID) / public static bool Up4ID (Customer customer) {if (cusList.Count = = 0) return false; cusList.RemoveAll (x = > x.ID = = customer.ID); cusList.Add (customer) Return true;}} / Room Operation / public static class RoomAction {/ Room list / private static List roomList = new List () / / add a new room / do not add / public static void Create (Room group) {if (! roomList.Where (x = > x.Name = = group.Name). Any () roomList.Add (group) if the room already exists. } / get the list / public static List GetList () {return roomList } / get a single / owner id / room name / public static Room GetOne (string masterid = null, string roomName = null) {if (roomList.Count = = 0) return null If (masterid! = null) return roomList.Where (x = > x.Masterid = = masterid). FirstOrDefault (); if (roomName! = null) return roomList.Where (x = > x.Name = = roomName). FirstOrDefault (); return null Join the room / public static bool Join (Customer client, string roomName) {if (roomList.Count = = 0) return false; var room = roomList.Where (x = > x.Name = = roomName) .FirstOrDefault () If (room is null) return false; if (room.Customers.Count = = 3) return false; room.Customers.Add (client); Up4Name (room); return true Delete the room / the owner id public static bool Delete (string masterid) {if (roomList.Count = = 0) return false; var room = roomList.Where (x = > x.Masterid = = masterid) .FirstOrDefault () If (room = = null) return false; roomList.Remove (room); return true } / public static bool Up4Name (Room room) {if (roomList.Count = = 0) return false; roomList.RemoveAll (x = > x.Name = = room.Name); roomList.Add (room) Return true;} / update the current player / if it is passed in, you will be forced to modify it. If you do not pass, follow the rule public static Customer ChangeCurr (string roomName, int index =-1) {var room = roomList.Where (x = > x.Name = = roomName). FirstOrDefault () If (index! =-1) room.Curr = index; else room.Curr = (room.Curr + 1) 3; Up4Name (room); return room.Customers [room.Curr];}}
Because all the data is saved through static properties, most of it is linq operations (forgive me for my limited linq level).
Then there is the game logic:
/ public class WithCard {/ spades-S, hearts-H, clubs-C, diamonds-D / BG King, SG Wang, 14Mel A. 15-2 / / readonly List Cards = new List () {"Smur14", "Smur15", "Smur3", "Smur4", "Smur5", "Smur6", "Smur7", "Smur8", "Smur9", "Smur10", "Smur11", "Smur12", "Smur13", "HMu14", "HMu15" "Hmur3", "Hmur4", "Hmur5", "Hmur6", "Hmur7", "Hmur8", "Hmur9", "Hmur10", "Hmur11", "Hmur12", "Hmur13", "Cmur14", "Cmur15", "Cmur3", "Cmur4", "Cmur5", "CMuE6", "CMuE7", "Cmel8", "Cmel9", "CMue 10" "Cmur11", "Dmur12", "Cmur13", "Dmur14", "Dmur15", "Dmur3", "Dmur4", "Dmur5", "Dmur6", "Dmur7", "Dmur8", "Dmur9", "Dmur10", "Dmur11", "Dmur12", "Dmur13", "BG-99", "SG-88"} / license / public List DrawCard () {List a = new List (); List b = new List (); List c = new List (); Random ran = new Random (); / / 3 cards left for (int I = 0; I
< 51; i++) { //随机抽取一张牌 string item = Cards[ran.Next(Cards.Count)]; switch (i % 3) { case 0: a.Add(item); break; case 1: b.Add(item); break; case 2: c.Add(item); break; } Cards.Remove(item); } return new List() { a,b,c,Cards }; } /// /// 规则 /// /// /// /// /// public bool Rule(List existingCard, List newCard, bool isSelf) { //现有牌号 List existingCardNo = existingCard.Select(x =>Convert.ToInt32 (x.Split ('-') [1]). ToList (). OrderBy (x = > x). ToList (); / / newly issued brand List newCardNo = newCard.Select (x = > Convert.ToInt32 (x.Split ('-') [1]). ToList (). OrderBy (x = > x). ToList () / / the last hand is Wang Fei, forbidding others to play if (existingCardNo.All (x = > x > 50) & & existingCardNo.Count = = 2) {if (isSelf) return true; else return false } / / Wang Fei Max if (newCardNo.All (x = > x > 50) & & newCard.Count = = 2) return true; / / single if (newCardNo.Count = = 1) {if (existingCardNo.Count = = 0) return true If ((existingCardNo.Count = = 1 & & newCardNo [0] > existingCardNo [0]) | | isSelf) return true } / / pair / three if (newCardNo.Count = = 2 | | newCardNo.Count = = 3) {if (existingCardNo.Count = = 0 & & newCardNo.All (x = > x = = newCardNo [0])) return true If (newCardNo.All (x = > x = = newCardNo [0]) & & (isSelf | | newCardNo.Count = = existingCardNo.Count & & newCardNo [0] > existingCardNo [0])) return true } if (newCard.Count = = 4) {/ / Fry if (newCardNo.All (x = > x = = newCardNo [0])) {if (existingCardNo.Count = = 0 | | isSelf) return true If (existingCardNo.All (x = > x = = existingCardNo [0]) & & existingCardNo.Count = = 4) {if (newCardNo [0] > existingCardNo [0]) return true;} return true } / / three with one {List flagA = newCardNo.Distinct () .ToList (); / / more than 2 cards failed directly if (flagA.Count > 2) return false / / there is no previous hand, or the last hand is self-played if (existingCardNo.Count = = 0 | | isSelf) return true; int newCardFlag = 0 If (newCardNo.Where (x = > x = = flagA [0]). ToList (). Count () > 1) {newCardFlag = flagA [0];} else newCardFlag = flagA [1]; List flagB = existingCardNo.Distinct () .ToList () / / the previous hand is not three bands and one if (flagB.Count > 2) return false; int existingCardFlag = 0 If (existingCardNo.Where (x = > x = = flagB [0]). ToList (). Count () > 1) {existingCardFlag = flagB [0];} else existingCardFlag = flagB [1] If (newCardFlag > existingCardFlag) return true;}} if (newCard.Count > = 5) {bool flag = true; for (int I = 0; I
< newCardNo.Count - 1; i++) { if (newCardNo[i] + 1 != newCardNo[i + 1]) { flag = false; break; } } //顺子 if (flag) { if (existingCardNo.Count == 0 || (newCardNo[0] >ExistingCardNo [0] & & newCardNo.Count = = existingCardNo.Count) | | isSelf) return true;}} return false;}}
The single rule is the same as the ordinary fighting D master (except Wang 2 is the largest, followed by A), multiple rules are currently supported: Wang fried, pair, three, Shunzi, three belt one. At present, we can only do this. Students can take it back and expand it on their own.
Put up some operation diagrams. The homeowner builds the house and joins:
New players join:
After the room is full, the landlord starts the game and assigns the landlord randomly:
Special effects of playing cards:
Game settlement:
At this point, I believe you have a deeper understanding of "how to use SignalR to fight landlords in .net Core". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.