Forum C# Common room code

Common room code

Postby azuanagames » May 23rd, 2020, 4:05 pm

Greetings,

I have a simple question... I would like to be able to have a common base class for my rooms, since there are many operations that are similar. However, if I attempt to add a class between my Game and my Room, PlayerIO complains.

Here is what I'm trying to do (Player is a BasePlayer):
Code: Select all
public class Room<P> : Game<P> where P : Player, new() {
    protected void Kick(Player player, string reason) {
        player.Kick(reason);

        // Give the client a half second to disconnect, if they don't then force it
        ScheduleCallback(() => {
            if (player.CurrentState != Player.State.Left) {
                player.Disconnect();
            }
        }, 500);
    }
}

In my game room I would do:
Code: Select all
    [RoomType("SomeRoom")]
    public class GameRoom : Room<Player> {
    }


As you can see the issue I have is that I need access to ScheduleCallback. I can write common routines in another class and use them that way but I would not have access to the Game functions, including things like BigDB.

Here is what PlayerIO says:
Code: Select all
Trying last loaded dll
- error: The Game class 'AzuanaGames.Room`1' does not have a [RoomType(...)] attribute, and is not an abstract base class.


Is there another solution I'm not considering?
azuanagames
 
Posts: 155
Joined: April 29th, 2010, 10:59 pm

Re: Common room code

Postby deetmo » May 25th, 2020, 1:45 pm

Actually... NO.

It was already asked for in my previous post:
https://playerio.com/forum/viewtopic.php?f=18&t=36664
deetmo
 
Posts: 3
Joined: May 5th, 2019, 10:30 pm

Re: Common room code

Postby azuanagames » July 12th, 2020, 6:16 pm

It is possible to share common room code, just not through inheritance.

I created a helper class that contains the following:

Code: Select all
using System;
using PlayerIO.GameLibrary;

namespace Example {
    class RoomHelper {
        private Action<Message> broadcast;
        private Action<Action, int> scheduleCallback;

        // Called by room to register the broadcast handler
        public void RegisterBroadcast(Action<Message> broadcastCB) {
            broadcast = broadcastCB;
        }

        // Called by room to register the timer handler
        public void RegisterCallback(Action<Action, int> schedueleCB) {
            scheduleCallback = schedueleCB;
        }

        // Loads common data from BigDB
        public void Load(BigDB bigDB, Action<DatabaseObject> cbLoaded, Action<PlayerIOError> cbError) {
            // Now load server info
            bigDB.Load("Server", "server", delegate (DatabaseObject server) {
                cbLoaded(server);
            }, delegate (PlayerIOError error) {
                cbError(error);
            });
        }

        public bool AllowUserJoin(IEnumerable<Player> Players, Player player) {
            return true;
        }

        public void UserJoined(Player player) {
            // common user joined code here
        }

        public void UserLeft(Player player) {
            // common user left code here
        }

        // Handles common messages
        public bool GotMessage(Player player, Message message) {
            // Switch on message type
            switch (message.Type) {
                case "chat":
                    broadcast.Invoke(Message.Create("chat", player.UID, message.GetString(0)));
                    return true;
            }
            return false;
        }
    }
}


And in my game room itself:
Code: Select all
using System;
using PlayerIO.GameLibrary;

namespace Example {
    [RoomType("Example")]
    public class ExampleRoom : Game<Player> {
        private readonly RoomHelper helper;

        public LobbyRoom() {
            helper = new RoomHelper();
            helper.RegisterBroadcast((Message message) => Broadcast(message));
            helper.RegisterCallback((Action cbDone, int dueTime) => ScheduleCallback(() => cbDone(), dueTime));
        }

        public override void GameStarted() {
            PreloadPlayerObjects = true;

            helper.Load(PlayerIO.BigDB, (DatabaseObject server) => {
                // do something with server here
            }, (PlayerIOError error) => {
                Console.WriteLine("Error loading server object: " + error.ToString());
            });
        }

        public override bool AllowUserJoin(Player player) {
            return helper.AllowUserJoin(Players, player);
        }

        public override void UserJoined(Player player) {
            helper.UserJoined(player);
        }

        public override void UserLeft(Player player) {
            helper.UserLeft(player);
        }

        public override void GotMessage(Player player, Message message) {
            // Check if the helper wants to handle this message
            if (helper.GotMessage(player, message)) {
                return;
            }

            // Other messages we need to handle go here
        }
    }
}


This may be obvious to some, but offering it here just in case someone searches for this answer...
azuanagames
 
Posts: 155
Joined: April 29th, 2010, 10:59 pm


Return to C#



cron