Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MessagesCommunication;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace GameMaster
- {
- class GameMaster
- {
- public static Socket master;
- public static string name;
- public static string id;
- private static GameBoard _gameboard;
- private static Location _location;
- private static Player[] _players;
- private static GoalField[] _goalField;
- private static Piece[] _piece;
- private static TaskField[] _taskfield;
- static void Main(string[] args)
- {
- Console.Title = "Game Master";
- Console.Write("Enter your name: ");
- name = Console.ReadLine();
- //UWAGA!! GOTO deklaracja!!
- A: Console.Clear();
- Console.WriteLine("Enter hostname: ");
- string HOST = Console.ReadLine();
- master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- //IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ip), 4242);
- try
- {
- master.Connect(HOST, 4242);
- }
- catch
- {
- Console.WriteLine("Could not connect to the host!");
- Thread.Sleep(1000);
- goto A;
- }
- //generuje ranodmowe wartosci
- GenerateRandomVariable();
- Thread t = new Thread(Data_IN);
- t.Start();
- for (;;)
- {
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write(name + "@GameMaster:~$");
- TestFunction();
- //string input = Console.ReadLine();
- //Packet p = new Packet(input);
- //Packet p = new Packet(PacketType.Chat, id);
- //p.Gdata.Add(name);
- //p.Gdata.Add(input);
- //master.Send(p.ToBytes());
- }
- }
- static void GenerateRandomVariable()
- {
- _gameboard = new GameBoard();
- _gameboard.goalsHeight = 20;
- _gameboard.tasksHeight = 20;
- _gameboard.width = 20;
- _location = new Location();
- _location.x = 1;
- _location.y = 1;
- _players = new Player[1];
- _players[0] = new Player();
- _players[0].id = 1;
- _players[0].team = TeamColour.blue;
- _players[0].type = PlayerType.player;
- _goalField = new GoalField[1];
- _goalField[0] = new GoalField();
- _goalField[0].playerId = 1;
- _goalField[0].playerIdSpecified = true;
- _goalField[0].team = TeamColour.blue;
- _goalField[0].timestamp = DateTime.Now;
- _goalField[0].type = GoalFieldType.goal;
- _goalField[0].x = 1;
- _goalField[0].y = 1;
- _piece = new Piece[1];
- _piece[0] = new Piece();
- _piece[0].id = 1;
- _piece[0].playerId = 1;
- _piece[0].playerIdSpecified = true;
- _piece[0].timestamp = DateTime.Now;
- _piece[0].type = PieceType.normal;
- _taskfield = new TaskField[1];
- _taskfield[0] = new TaskField();
- _taskfield[0].pieceId = 1;
- _taskfield[0].pieceIdSpecified = true;
- _taskfield[0].playerId = 1;
- _taskfield[0].distanceToPiece = 10;
- _taskfield[0].playerIdSpecified = true;
- _taskfield[0].timestamp = DateTime.Now;
- _taskfield[0].x = 1;
- _taskfield[0].y = 1;
- }
- static void Data_IN()
- {
- byte[] Buffer;
- int readBytes;
- for (;;)
- {
- try
- {
- Buffer = new byte[master.SendBufferSize];
- readBytes = master.Receive(Buffer);
- if (readBytes > 0)
- {
- //Packet p = new Packet();
- //p.PacketXml(Buffer);
- string text = Messages.ChangeType.ToString(Buffer);
- //DataManager(text);
- int i = 0;
- switch (i)
- {
- //ConfirmGameRegistration
- case 1:
- {
- break;
- }
- //JoinGame
- case 2:
- {
- break;
- }
- //data
- case 3:
- {
- break;
- }
- case 4:
- {
- break;
- }
- case 5:
- {
- break;
- }
- case 6:
- {
- break;
- }
- }
- }
- }
- catch (SocketException ex)
- {
- Console.WriteLine("The server disconnected!");
- Console.ReadLine();
- Environment.Exit(0);
- }
- }
- }
- static void DataManager(string text)
- {
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine(text.Trim('\0'));
- //switch (p.packetType)
- //{
- // case PacketType.Registration:
- // //Console.WriteLine("Recieved a packet for reistration! Responding...");
- // id = p.Gdata[0];
- // break;
- // case PacketType.Chat:
- // Console.WriteLine(p.Gdata[0] + ": " + p.Gdata[1]);
- // Console.ForegroundColor = ConsoleColor.Cyan;
- // break;
- //}
- }
- private static void Send(byte[] dataBuf)
- {
- //Packet p = new Packet();
- //p.PacketXml(dataBuf);
- master.Send(dataBuf);
- }
- //testing functions, I think we can create the class libary with them
- private static void TestFunction()
- {
- int i;
- if (Int32.TryParse(System.Console.ReadLine(), out i))
- switch (i)
- {
- //register game
- case 1:
- {
- Console.WriteLine("Register Game");
- int blueTeam, redTeam;
- string Name;
- Console.WriteLine("Give a name for a game:");
- Name = (System.Console.ReadLine()).ToLower();
- Console.WriteLine("Choose Game:");
- Console.WriteLine("Number of blue team players:");
- while (!Int32.TryParse(System.Console.ReadLine(), out blueTeam))
- Console.WriteLine("Number of blue team players:");
- Console.WriteLine("Number of red team players:");
- while (!Int32.TryParse(System.Console.ReadLine(), out redTeam))
- Console.WriteLine("Number of red team players:");
- RegisterGame(blueTeam, redTeam, Name);
- break;
- }
- //ConfirmJoiningGame
- case 2:
- {
- Console.WriteLine("Confirm Joining Game");
- int gameId, playerId;
- string privateGuid;
- Console.WriteLine("Game id:");
- while (!Int32.TryParse(System.Console.ReadLine(), out gameId)) ;
- Console.WriteLine("Player id:");
- while (!Int32.TryParse(System.Console.ReadLine(), out playerId)) ;
- Console.WriteLine("Give a privateGuid:");
- privateGuid = (System.Console.ReadLine()).ToLower();
- ConfirmJoiningGame(gameId, playerId, privateGuid, TeamColour.blue, PlayerType.player);
- break;
- }
- //Game
- case 3:
- {
- Console.WriteLine("Game");
- Game game = new Game();
- game.Board = _gameboard;
- game.playerId = 1;
- game.PlayerLocation = _location;
- game.Players = _players;
- Send(Messages.ChangeType.ToByteArrayObject(game));
- break;
- }
- //Data
- case 4:
- {
- Console.WriteLine("Data");
- Data data = new Data();
- data.gameFinished = false;
- data.GoalFields = _goalField;
- data.Pieces = _piece;
- data.playerId = 1;
- data.PlayerLocation = _location;
- data.TaskFields = _taskfield;
- Send(Messages.ChangeType.ToByteArrayObject(data));
- break;
- }
- }
- }
- private static void RegisterGame(int blueTeam, int redTeam, string gameName)
- {
- MessagesCommunication.RegisterGame rG = Messages.CreateMessage.RegisterGame(blueTeam, redTeam, gameName);
- Send(Messages.ChangeType.ToByteArrayObject(rG));
- }
- private static void ConfirmJoiningGame(int gameId, int playerId, string privateGuid, MessagesCommunication.TeamColour colour, MessagesCommunication.PlayerType type)
- {
- MessagesCommunication.ConfirmJoiningGame cGR = Messages.CreateMessage.ConfirmJoiningGame(gameId, playerId, privateGuid, colour, type);
- Send(Messages.ChangeType.ToByteArrayObject(cGR));
- }
- private static void Game(int playerId, int locx, int locy, MessagesCommunication.Player[] players, int goalsHeight, int tasksHeight, int width)
- {
- MessagesCommunication.Game game = Messages.CreateMessage.Game(playerId, locx, locy, players, goalsHeight, tasksHeight, width);
- Send(Messages.ChangeType.ToByteArrayObject(game));
- }
- private static void Data(int playerId, bool gameFinished, int locx, int locy, MessagesCommunication.Piece[] pieces, MessagesCommunication.TaskField[] taskFields, MessagesCommunication.GoalField[] goalFields)
- {
- MessagesCommunication.Data data = Messages.CreateMessage.Data(playerId, gameFinished, locx, locy, pieces, taskFields, goalFields);
- Send(Messages.ChangeType.ToByteArrayObject(data));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement