Пример #1
0
        public Kernel(dynamic state, ActorMeth meth)
        {
            // TODO clean up this mess and provide a better root actor
            var pid = this.Spawn(state, meth);

            this.Send(pid, new Mail(Symbol.Init, null));
        }
Пример #2
0
        public static ActorMeth Shop()
        {
            List <Shop.Item> ItemShop = new List <Shop.Item>();

            ItemShop.Add(new Shop.Item("Gladius", "A mighty Sword", 100));
            ItemShop.Add(new Shop.Item("Gladius", "A mighty Sword", 100));
            ItemShop.Add(new Shop.Item("Gladius", "A mighty Sword", 100));

            ActorMeth behaviour = (rt, self, _, msg) =>
            {
                switch (msg.mtype)
                {
                case Symbol.Items:
                    StoreItems items = new StoreItems()
                    {
                        MailType = "items",
                        Items    = ItemShop
                    };
                    string json = JsonSerializer.Serialize(items);
                    WebSocketClient.SendMessage(rt.GetWebSocket(new PID(long.Parse(msg.content.PId))), json);
                    break;

                case Symbol.Buy:

                    break;
                }
                return(null);
            };

            return(behaviour);
        }
Пример #3
0
        private Future(Runtime runtime, Func <dynamic, Either <dynamic, dynamic> > fn, bool link)
        {
            ActorMeth meth = (rt, self, state, mail) =>
            {
                switch (mail.mtype)
                {
                case Symbol.Continue:
                    ((Either <dynamic, dynamic>)state).MatchDo(
                        Left: r => { },
                        Right: r => { rt.Send(self, new Mail(Symbol.Continue, null)); }
                        );
                    return(state);

                case Symbol.FutureGet:
                    ((Either <dynamic, dynamic>)state).MatchDo(
                        Left: r => { rt.Send(mail.content, new Mail(Symbol.FutureResult, new Some <dynamic>(r))); },
                        Right: _ => { rt.Send(mail.content, new Mail(Symbol.Continue, new None <dynamic>())); }
                        );
                    return(state);

                default:
                    return(state);
                }
            };

            if (!link)
            {
                this.self = runtime.Spawn(new None <dynamic>(), meth);
            }
            else
            {
                this.self = runtime.SpawnLink(new None <dynamic>(), meth);
            }
            this.runtime = runtime;
        }
Пример #4
0
        // Spawn a new actor with the given state and method
        public PID Spawn(dynamic state, ActorMeth meth)
        {
            PID     pid     = this.NewPID();
            Runtime runtime = new Runtime(this, pid);

            this.actors.Add(pid, new Actor(runtime, pid, state, meth));
            Console.WriteLine($"spawn: {pid}");
            return(pid);
        }
Пример #5
0
        public static ActorMeth ClientProxy()
        {
            ActorMeth behaviour = (rt, self, _, msg) =>
            {
                return(null);
            };

            return(behaviour);
        }
Пример #6
0
        public static ActorMeth Log()
        {
            ActorMeth behaviour = (rt, self, state, msg) =>
            {
                return(null);
            };

            return(behaviour);
        }
Пример #7
0
        // Spawn a new linked actor with the given state and method
        public PID SpawnLink(PID parent, dynamic state, ActorMeth meth)
        {
            PID     pid     = this.NewPID();
            Runtime runtime = new Runtime(this, pid);

            this.actors.Add(pid, new Actor(runtime, pid, parent, state, meth));
            this.actors[parent].children.Add(pid);
            Console.WriteLine($"spawn link: {pid}");
            return(pid);
        }
Пример #8
0
 public Actor(Runtime runtime, PID pid, PID parent, dynamic state, ActorMeth meth)
 {
     this.pid      = pid;
     this.meth     = meth;
     this.children = new List <PID>();
     this.mqueue   = new Queue <Mail>();
     this.parent   = parent;
     this.state    = state;
     this.runtime  = runtime;
 }
Пример #9
0
        public static ActorMeth Echo()
        {
            ActorMeth behaviour = (rt, self, _, msg) =>
            {
                if (msg.mtype == Symbol.Echo)
                {
                    byte[] buffer;
                    string json = JsonSerializer.Serialize <JsonPID>(msg.content);
                    buffer = Encoding.UTF8.GetBytes(json);
                    PID       webSocketKey = new PID(long.Parse(msg.content.pId));
                    WebSocket socket       = rt.GetWebSocket(webSocketKey);
                    socket.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
                }
                return(null);
            };

            return(behaviour);
        }
Пример #10
0
        public static ActorMeth UserDb()
        {
            var userManager = new Usermanager(new EditorContext());

            ActorMeth behaviour = (rt, self, _, msg) =>
            {
                DbMail mail = msg.content;
                switch (mail.Action)
                {
                case DbAction.Create:
                    //if()
                    break;

                default:
                    break;
                }
                return(null);
            };

            return(behaviour);
        }
Пример #11
0
        public static ActorMeth DatabaseManager()
        {
            PID userDb_PId = new PID();

            ActorMeth behaviour = (rt, self, _, msg) =>
            {
                DbMail mail = msg.content;
                switch (msg.mtype)
                {
                case Symbol.Init:
                    userDb_PId = rt.Spawn(null, UserDb());
                    break;

                case Symbol.Normal:
                    //mailtype
                    switch (mail.Type)
                    {
                    case DbType.User:
                        rt.Send(userDb_PId, new Mail(Symbol.Normal, mail));
                        break;

                    case DbType.Lanista:
                        break;

                    case DbType.Gladiator:
                        break;

                    case DbType.Item:
                        break;

                    default:
                        break;
                    }
                    break;
                }
                return(null);
            };

            return(behaviour);
        }
Пример #12
0
        public static ActorMeth Login()
        {
            ActorMeth behaviour = (rt, self, _, msg) =>
            {
                switch (msg.mtype)
                {
                case Symbol.Authorize:
                    //check if user exist
                    //check if password matches.
                    //add socket to socketDictionary.
                    break;

                case Symbol.CreateUser:

                    break;

                default:
                    break;
                }
                return(null);
            };

            return(behaviour);
        }
Пример #13
0
        private Broadcaster(Runtime runtime, bool link)
        {
            ActorMeth meth = (rt, self, state, mail) =>
            {
                switch (mail.mtype)
                {
                case Symbol.Killed:
                    rt.Die();
                    return(state);

                case Symbol.Broadcast:
                    foreach (PID to in state)
                    {
                        rt.Send(to, mail.content);
                    }
                    return(state);

                case Symbol.Subscribe:
                    state.Add(mail.content);
                    return(state);

                default:
                    return(state);
                }
            };

            this.runtime = runtime;
            if (!link)
            {
                this.self = runtime.Spawn(new List <PID>(), meth);
            }
            else
            {
                this.self = runtime.SpawnLink(new List <PID>(), meth);
            }
        }
Пример #14
0
        public static ActorMeth GameManager(PID pid)
        {
            BattleGladiator gladiatorOne = new BattleGladiator();

            gladiatorOne.Name = "gladiatorOne";
            BattleGladiator gladiatorTwo = new BattleGladiator();

            gladiatorTwo.Name = "gladiatorTwo";

            int turnCount = 0;
            PID parent    = pid;
            PID playerOne = new PID();
            PID playerTwo = new PID();

            bool isFinished = false;


            ActorMeth behaviour = (rt, self, _, msg) =>
            {
                GameReturn pOneMsg = new GameReturn();
                GameReturn pTwoMsg = new GameReturn();


                switch (msg.mtype)
                {
                case Symbol.Init:
                    playerOne = msg.content[0];
                    playerTwo = msg.content[1];

                    var startOneMsg = GameManagerService.GetStartMessage(gladiatorOne, gladiatorTwo, playerOne);
                    GameManagerService.SendStartMessage(rt.GetWebSocket(playerOne), startOneMsg);

                    var startTwoMsg = GameManagerService.GetStartMessage(gladiatorTwo, gladiatorOne, playerOne);
                    GameManagerService.SendStartMessage(rt.GetWebSocket(playerTwo), startTwoMsg);
                    break;

                case Symbol.GameAction:
                    GameAction gAction = msg.content;
                    var        skills  = new SkillRepository();

                    if ((turnCount & 1) == 0)    // gladiator a
                    {
                        if (new PID(long.Parse(gAction.PId)).ToString() == playerOne.ToString())
                        {
                            skills.UseSkill(gAction.Action, gladiatorOne, gladiatorTwo);
                            //deactive then remove buff if the turns == zero
                            if (gladiatorOne.Buffs.Count > 0)
                            {
                                foreach (Buff buff in gladiatorOne.Buffs)
                                {
                                    buff.Turns--;
                                    if (buff.Turns <= 0)
                                    {
                                        buff.DeActivate(gladiatorOne);
                                    }
                                }
                                gladiatorOne.Buffs.RemoveAll(x => x.Turns <= 0);
                            }
                        }
                        else
                        {
                            //send back msg to sync?
                        }
                    }
                    else if ((turnCount & 1) != 0)     // gladiator b
                    {
                        if (new PID(long.Parse(gAction.PId)).ToString() == playerTwo.ToString())
                        {
                            skills.UseSkill(gAction.Action, gladiatorTwo, gladiatorOne);
                            if (gladiatorTwo.Buffs.Count > 0)
                            {
                                foreach (Buff buff in gladiatorTwo.Buffs)
                                {
                                    buff.Turns--;
                                    if (buff.Turns <= 0)
                                    {
                                        buff.DeActivate(gladiatorTwo);
                                    }
                                }
                                gladiatorTwo.Buffs.RemoveAll(x => x.Turns <= 0);
                            }
                        }
                        else
                        {
                            //send back msg to sync?
                        }
                    }
                    turnCount++;
                    //send synced userdata back to user
                    pOneMsg = GameManagerService.GetReturnMessage(gladiatorOne, turnCount);
                    pTwoMsg = GameManagerService.GetReturnMessage(gladiatorTwo, turnCount);
                    if ((turnCount & 1) == 0)
                    {
                        pOneMsg.Turn       = playerOne.ToString();
                        pTwoMsg.Turn       = playerOne.ToString();
                        pOneMsg.GOneHealth = gladiatorOne.Health.ToString();
                        pTwoMsg.GOneHealth = gladiatorTwo.Health.ToString();
                        pOneMsg.GTwoHealth = gladiatorTwo.Health.ToString();
                        pTwoMsg.GTwoHealth = gladiatorOne.Health.ToString();
                    }
                    else if ((turnCount & 1) != 0)
                    {
                        pOneMsg.Turn       = playerTwo.ToString();
                        pTwoMsg.Turn       = playerTwo.ToString();
                        pOneMsg.GOneHealth = gladiatorOne.Health.ToString();
                        pTwoMsg.GOneHealth = gladiatorTwo.Health.ToString();
                        pOneMsg.GTwoHealth = gladiatorTwo.Health.ToString();
                        pTwoMsg.GTwoHealth = gladiatorOne.Health.ToString();
                    }
                    if (!isFinished)
                    {
                        if (gladiatorOne.Health <= 0)
                        {
                            pOneMsg.Winner = gladiatorTwo.Name;
                            pTwoMsg.Winner = gladiatorTwo.Name;
                            isFinished     = true;
                        }
                        if (gladiatorTwo.Health <= 0)
                        {
                            pOneMsg.Winner = gladiatorOne.Name;
                            pTwoMsg.Winner = gladiatorOne.Name;
                            isFinished     = true;
                        }
                    }

                    GameManagerService.SendReturnMessage(rt.GetWebSocket(playerOne), pOneMsg);
                    GameManagerService.SendReturnMessage(rt.GetWebSocket(playerTwo), pTwoMsg);

                    if (isFinished)
                    {
                        PID[] players = new PID[] { playerOne, playerTwo };
                        rt.Send(parent, new Mail(Symbol.Killed, players));
                        rt.Die();
                    }
                    break;

                default:
                    break;
                }

                return(null);
            };

            return(behaviour);
        }
Пример #15
0
        public static ActorMeth SessionManager()
        {
            Queue <PID>           playQueue     = new Queue <PID>();
            Dictionary <PID, PID> clientToGames = new Dictionary <PID, PID>();

            ActorMeth behaviour = (rt, self, _, msg) =>
            {
                switch (msg.mtype)
                {
                case Symbol.QueueGame:
                    PID key = new PID(long.Parse(msg.content.PId));
                    if (!playQueue.Contains(key))
                    {
                        playQueue.Enqueue(key);
                        Response response = new Response()
                        {
                            MailType     = "queuegame",
                            MailResponse = "inqueue"
                        };
                        string json   = JsonSerializer.Serialize(response);
                        byte[] buffer = Encoding.UTF8.GetBytes(json);
                        rt.GetWebSocket(key).SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
                    }
                    else if (playQueue.Contains(key))
                    {
                        Response response = new Response()
                        {
                            MailType     = "queuegame",
                            MailResponse = "allreadyinqueue"
                        };
                        string json   = JsonSerializer.Serialize(response);
                        byte[] buffer = Encoding.UTF8.GetBytes(json);
                        rt.GetWebSocket(key).SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
                    }
                    if (playQueue.Count > 1)
                    {
                        var   playerOne       = playQueue.Dequeue();
                        var   playerTwo       = playQueue.Dequeue();
                        PID[] players         = new PID[] { playerOne, playerTwo };
                        var   gameManager_pid = rt.SpawnLink(null, GameManager(self));
                        clientToGames.Add(playerOne, gameManager_pid);
                        clientToGames.Add(playerTwo, gameManager_pid);
                        rt.Send(gameManager_pid, new Mail(Symbol.Init, players));
                    }
                    break;

                case Symbol.GameAction:
                    if (clientToGames.ContainsKey(new PID(long.Parse(msg.content.PId))))
                    {
                        var to = clientToGames[new PID(long.Parse(msg.content.PId))];
                        rt.Send(to, msg);
                    }
                    break;

                case Symbol.Killed:
                    foreach (PID pid in msg.content)
                    {
                        clientToGames.Remove(pid);
                    }
                    break;

                default:
                    break;
                }
                return(null);
            };

            return(behaviour);
        }
Пример #16
0
 public PID Spawn(dynamic state, ActorMeth meth)
 {
     return(kernel.Spawn(state, meth));
 }
Пример #17
0
 public PID SpawnLink(dynamic state, ActorMeth meth)
 {
     return(kernel.SpawnLink(self, state, meth));
 }