Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.58 KB | None | 0 0
  1. using MessagesCommunication;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10.  
  11. namespace GameMaster
  12. {
  13.     class GameMaster
  14.     {
  15.         public static Socket master;
  16.         public static string name;
  17.         public static string id;
  18.         private static GameBoard _gameboard;
  19.         private static Location _location;
  20.         private static Player[] _players;
  21.         private static GoalField[] _goalField;
  22.         private static Piece[] _piece;
  23.         private static TaskField[] _taskfield;
  24.         static void Main(string[] args)
  25.         {
  26.             Console.Title = "Game Master";
  27.             Console.Write("Enter your name: ");
  28.             name = Console.ReadLine();
  29.  
  30.             //UWAGA!! GOTO deklaracja!!
  31.             A: Console.Clear();
  32.  
  33.             Console.WriteLine("Enter hostname: ");
  34.             string HOST = Console.ReadLine();
  35.  
  36.             master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  37.  
  38.             //IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ip), 4242);
  39.             try
  40.             {
  41.                 master.Connect(HOST, 4242);
  42.             }
  43.             catch
  44.             {
  45.                 Console.WriteLine("Could not connect to the host!");
  46.                 Thread.Sleep(1000);
  47.                 goto A;
  48.             }
  49.             //generuje ranodmowe wartosci
  50.             GenerateRandomVariable();
  51.  
  52.             Thread t = new Thread(Data_IN);
  53.             t.Start();
  54.  
  55.             for (;;)
  56.             {
  57.                 Console.ForegroundColor = ConsoleColor.Cyan;
  58.                 Console.Write(name + "@GameMaster:~$");
  59.                 TestFunction();
  60.                 //string input = Console.ReadLine();
  61.                 //Packet p = new Packet(input);
  62.  
  63.                 //Packet p = new Packet(PacketType.Chat, id);
  64.                 //p.Gdata.Add(name);
  65.                 //p.Gdata.Add(input);
  66.  
  67.                 //master.Send(p.ToBytes());
  68.             }
  69.         }
  70.  
  71.         static void GenerateRandomVariable()
  72.         {
  73.             _gameboard = new GameBoard();
  74.             _gameboard.goalsHeight = 20;
  75.             _gameboard.tasksHeight = 20;
  76.             _gameboard.width = 20;
  77.             _location = new Location();
  78.             _location.x = 1;
  79.             _location.y = 1;
  80.             _players = new Player[1];
  81.             _players[0] = new Player();
  82.             _players[0].id = 1;
  83.             _players[0].team = TeamColour.blue;
  84.             _players[0].type = PlayerType.player;
  85.             _goalField = new GoalField[1];
  86.             _goalField[0] = new GoalField();
  87.             _goalField[0].playerId = 1;
  88.             _goalField[0].playerIdSpecified = true;
  89.             _goalField[0].team = TeamColour.blue;
  90.             _goalField[0].timestamp = DateTime.Now;
  91.             _goalField[0].type = GoalFieldType.goal;
  92.             _goalField[0].x = 1;
  93.             _goalField[0].y = 1;
  94.             _piece = new Piece[1];
  95.             _piece[0] = new Piece();
  96.             _piece[0].id = 1;
  97.             _piece[0].playerId = 1;
  98.             _piece[0].playerIdSpecified = true;
  99.             _piece[0].timestamp = DateTime.Now;
  100.             _piece[0].type = PieceType.normal;
  101.             _taskfield = new TaskField[1];
  102.             _taskfield[0] = new TaskField();
  103.             _taskfield[0].pieceId = 1;
  104.             _taskfield[0].pieceIdSpecified = true;
  105.             _taskfield[0].playerId = 1;
  106.             _taskfield[0].distanceToPiece = 10;
  107.             _taskfield[0].playerIdSpecified = true;
  108.             _taskfield[0].timestamp = DateTime.Now;
  109.             _taskfield[0].x = 1;
  110.             _taskfield[0].y = 1;
  111.         }
  112.  
  113.         static void Data_IN()
  114.         {
  115.             byte[] Buffer;
  116.             int readBytes;
  117.  
  118.             for (;;)
  119.             {
  120.                 try
  121.                 {
  122.                     Buffer = new byte[master.SendBufferSize];
  123.                     readBytes = master.Receive(Buffer);
  124.  
  125.                     if (readBytes > 0)
  126.  
  127.                     {
  128.                         //Packet p = new Packet();
  129.                         //p.PacketXml(Buffer);
  130.                         string text = Messages.ChangeType.ToString(Buffer);
  131.                         //DataManager(text);
  132.  
  133.                         int i = 0;
  134.                         switch (i)
  135.                         {
  136.                             //ConfirmGameRegistration
  137.                             case 1:
  138.                                 {
  139.                                     break;
  140.                                 }
  141.                             //JoinGame
  142.                             case 2:
  143.                                 {
  144.                                     break;
  145.                                 }
  146.                             //data
  147.                             case 3:
  148.                                 {
  149.  
  150.                                     break;
  151.                                 }
  152.                             case 4:
  153.                                 {
  154.  
  155.                                     break;
  156.                                 }
  157.                             case 5:
  158.                                 {
  159.  
  160.                                     break;
  161.                                 }
  162.                             case 6:
  163.                                 {
  164.  
  165.                                     break;
  166.                                 }
  167.                         }
  168.                     }
  169.                 }
  170.                 catch (SocketException ex)
  171.                 {
  172.                     Console.WriteLine("The server disconnected!");
  173.                     Console.ReadLine();
  174.                     Environment.Exit(0);
  175.                 }
  176.             }
  177.         }
  178.  
  179.         static void DataManager(string text)
  180.         {
  181.             Console.ForegroundColor = ConsoleColor.Cyan;
  182.             Console.WriteLine(text.Trim('\0'));
  183.  
  184.             //switch (p.packetType)
  185.             //{
  186.             //    case PacketType.Registration:
  187.             //        //Console.WriteLine("Recieved a packet for reistration! Responding...");
  188.             //        id = p.Gdata[0];
  189.             //        break;
  190.             //    case PacketType.Chat:
  191.             //        Console.WriteLine(p.Gdata[0] + ": " + p.Gdata[1]);
  192.             //        Console.ForegroundColor = ConsoleColor.Cyan;
  193.             //        break;
  194.             //}
  195.         }
  196.  
  197.  
  198.  
  199.         private static void Send(byte[] dataBuf)
  200.         {
  201.             //Packet p = new Packet();
  202.             //p.PacketXml(dataBuf);
  203.             master.Send(dataBuf);
  204.         }
  205.         //testing functions, I think we can create the class libary with them
  206.         private static void TestFunction()
  207.         {
  208.             int i;
  209.             if (Int32.TryParse(System.Console.ReadLine(), out i))
  210.                 switch (i)
  211.                 {
  212.                     //register game
  213.                     case 1:
  214.                         {
  215.                             Console.WriteLine("Register Game");
  216.                             int blueTeam, redTeam;
  217.                             string Name;
  218.                             Console.WriteLine("Give a name for a game:");
  219.                             Name = (System.Console.ReadLine()).ToLower();
  220.                             Console.WriteLine("Choose Game:");
  221.                             Console.WriteLine("Number of blue team players:");
  222.                             while (!Int32.TryParse(System.Console.ReadLine(), out blueTeam))
  223.                                 Console.WriteLine("Number of blue team players:");
  224.                             Console.WriteLine("Number of red team players:");
  225.                             while (!Int32.TryParse(System.Console.ReadLine(), out redTeam))
  226.                                 Console.WriteLine("Number of red team players:");
  227.                             RegisterGame(blueTeam, redTeam, Name);
  228.                             break;
  229.                         }
  230.                     //ConfirmJoiningGame
  231.                     case 2:
  232.                         {
  233.                             Console.WriteLine("Confirm Joining Game");
  234.                             int gameId, playerId;
  235.                             string privateGuid;
  236.  
  237.                             Console.WriteLine("Game id:");
  238.                             while (!Int32.TryParse(System.Console.ReadLine(), out gameId)) ;
  239.  
  240.                             Console.WriteLine("Player id:");
  241.  
  242.                             while (!Int32.TryParse(System.Console.ReadLine(), out playerId)) ;
  243.  
  244.                             Console.WriteLine("Give a privateGuid:");
  245.                             privateGuid = (System.Console.ReadLine()).ToLower();
  246.  
  247.                             ConfirmJoiningGame(gameId, playerId, privateGuid, TeamColour.blue, PlayerType.player);
  248.  
  249.                             break;
  250.                         }
  251.  
  252.                     //Game
  253.                     case 3:
  254.                         {
  255.                             Console.WriteLine("Game");
  256.                             Game game = new Game();
  257.                             game.Board = _gameboard;
  258.                             game.playerId = 1;
  259.                             game.PlayerLocation = _location;
  260.                             game.Players = _players;
  261.                             Send(Messages.ChangeType.ToByteArrayObject(game));
  262.                             break;
  263.                         }
  264.                     //Data
  265.                     case 4:
  266.                         {
  267.                             Console.WriteLine("Data");
  268.                             Data data = new Data();
  269.                             data.gameFinished = false;
  270.                             data.GoalFields = _goalField;
  271.                             data.Pieces = _piece;
  272.                             data.playerId = 1;
  273.                             data.PlayerLocation = _location;
  274.                             data.TaskFields = _taskfield;
  275.                             Send(Messages.ChangeType.ToByteArrayObject(data));
  276.                             break;
  277.                         }
  278.                 }
  279.         }
  280.  
  281.         private static void RegisterGame(int blueTeam, int redTeam, string gameName)
  282.         {
  283.             MessagesCommunication.RegisterGame rG = Messages.CreateMessage.RegisterGame(blueTeam, redTeam, gameName);
  284.             Send(Messages.ChangeType.ToByteArrayObject(rG));
  285.         }
  286.  
  287.         private static void ConfirmJoiningGame(int gameId, int playerId, string privateGuid, MessagesCommunication.TeamColour colour, MessagesCommunication.PlayerType type)
  288.         {
  289.             MessagesCommunication.ConfirmJoiningGame cGR = Messages.CreateMessage.ConfirmJoiningGame(gameId, playerId, privateGuid, colour, type);
  290.             Send(Messages.ChangeType.ToByteArrayObject(cGR));
  291.         }
  292.  
  293.         private static void Game(int playerId, int locx, int locy, MessagesCommunication.Player[] players, int goalsHeight, int tasksHeight, int width)
  294.         {
  295.             MessagesCommunication.Game game = Messages.CreateMessage.Game(playerId, locx, locy, players, goalsHeight, tasksHeight, width);
  296.             Send(Messages.ChangeType.ToByteArrayObject(game));
  297.         }
  298.  
  299.         private static void Data(int playerId, bool gameFinished, int locx, int locy, MessagesCommunication.Piece[] pieces, MessagesCommunication.TaskField[] taskFields, MessagesCommunication.GoalField[] goalFields)
  300.         {
  301.             MessagesCommunication.Data data = Messages.CreateMessage.Data(playerId, gameFinished, locx, locy, pieces, taskFields, goalFields);
  302.             Send(Messages.ChangeType.ToByteArrayObject(data));
  303.         }
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement