Anton Tarasenko
4 years ago
10 changed files with 215 additions and 249 deletions
@ -1,52 +0,0 @@
|
||||
/** |
||||
* `PlayerService`'s listener for events generated by 'ConnectionService'. |
||||
* Copyright 2019 Anton Tarasenko |
||||
*------------------------------------------------------------------------------ |
||||
* This file is part of Acedia. |
||||
* |
||||
* Acedia is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Acedia is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with Acedia. If not, see <https://www.gnu.org/licenses/>. |
||||
*/ |
||||
class ConnectionListener_Player extends ConnectionListenerBase; |
||||
|
||||
var LoggerAPI.Definition fatalNoPlayerService; |
||||
|
||||
static function ConnectionEstablished(ConnectionService.Connection connection) |
||||
{ |
||||
local PlayerService service; |
||||
service = PlayerService(class'PlayerService'.static.Require()); |
||||
if (service == none) |
||||
{ |
||||
__().logger.Auto(default.fatalNoPlayerService); |
||||
return; |
||||
} |
||||
service.RegisterPlayer(connection.controllerReference); |
||||
} |
||||
|
||||
static function ConnectionLost(ConnectionService.Connection connection) |
||||
{ |
||||
local PlayerService service; |
||||
service = PlayerService(class'PlayerService'.static.Require()); |
||||
if (service == none) |
||||
{ |
||||
__().logger.Auto(default.fatalNoPlayerService); |
||||
return; |
||||
} |
||||
service.UpdateAllPlayers(); |
||||
} |
||||
|
||||
defaultproperties |
||||
{ |
||||
relatedEvents = class'ConnectionEvents' |
||||
fatalNoPlayerService = (l=LOG_Fatal,m="Cannot start `PlayerService` service Acedia will not properly work from now on.") |
||||
} |
@ -1,51 +0,0 @@
|
||||
/** |
||||
* Event generator for 'ConnectionService'. |
||||
* Copyright 2019 Anton Tarasenko |
||||
*------------------------------------------------------------------------------ |
||||
* This file is part of Acedia. |
||||
* |
||||
* Acedia is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Acedia is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with Acedia. If not, see <https://www.gnu.org/licenses/>. |
||||
*/ |
||||
class PlayerEvents extends Events |
||||
abstract; |
||||
|
||||
static function CallPlayerConnected(APlayer newPlayer) |
||||
{ |
||||
local int i; |
||||
local array< class<Listener> > listeners; |
||||
listeners = GetListeners(); |
||||
for (i = 0; i < listeners.length; i += 1) |
||||
{ |
||||
class<PlayerListenerBase>(listeners[i]) |
||||
.static.PlayerConnected(newPlayer); |
||||
} |
||||
} |
||||
|
||||
static function CallPlayerDisconnected(APlayer lostPlayer) |
||||
{ |
||||
local int i; |
||||
local array< class<Listener> > listeners; |
||||
listeners = GetListeners(); |
||||
for (i = 0; i < listeners.length; i += 1) |
||||
{ |
||||
class<PlayerListenerBase>(listeners[i]) |
||||
.static.PlayerDisconnected(lostPlayer); |
||||
} |
||||
} |
||||
|
||||
defaultproperties |
||||
{ |
||||
relatedListener = class'PlayerListenerBase' |
||||
connectedServiceClass = class'PlayerService' |
||||
} |
@ -0,0 +1,99 @@
|
||||
/** |
||||
* API that provides functions for working player references (`APlayer`). |
||||
* Copyright 2021 Anton Tarasenko |
||||
*------------------------------------------------------------------------------ |
||||
* This file is part of Acedia. |
||||
* |
||||
* Acedia is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Acedia is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with Acedia. If not, see <https://www.gnu.org/licenses/>. |
||||
*/ |
||||
class PlayersAPI extends AcediaObject |
||||
dependson(Text); |
||||
|
||||
// Writer that can be used to write into this player's console |
||||
var private ConsoleWriter consoleInstance; |
||||
// Remember version to reallocate writer in case someone deallocates it |
||||
var private int consoleLifeVersion; |
||||
|
||||
protected function Constructor() |
||||
{ |
||||
local ConnectionService service; |
||||
service = ConnectionService(class'ConnectionService'.static.Require()); |
||||
service.OnConnectionEstablished(self).connect = MakePlayer; |
||||
} |
||||
|
||||
protected function Finalizer() |
||||
{ |
||||
local ConnectionService service; |
||||
service = ConnectionService(class'ConnectionService'.static.Require()); |
||||
service.OnConnectionEstablished(self).Disconnect(); |
||||
} |
||||
|
||||
private final function MakePlayer(ConnectionService.Connection newConnection) |
||||
{ |
||||
local APlayer newPlayer; |
||||
local Text textIdHash; |
||||
local PlayerService service; |
||||
// Make new player controller and link it to `newConnection` |
||||
newPlayer = APlayer(_.memory.Allocate(class'APlayer')); |
||||
service = PlayerService(class'PlayerService'.static.Require()); |
||||
service.RegisterPair(newConnection.controllerReference, newPlayer); |
||||
// Initialize new `APlayer` |
||||
textIdHash = _.text.FromString(newConnection.idHash); |
||||
newPlayer.Initialize(textIdHash); |
||||
textIdHash.FreeSelf(); |
||||
} |
||||
|
||||
/** |
||||
* Return `ConsoleWriter` that can be used to write into every player's |
||||
* console. |
||||
* |
||||
* Provided that returned object is never deallocated - returns the same object |
||||
* with each call, otherwise can allocate new instance of `ConsoleWriter`. |
||||
* |
||||
* @return `ConsoleWriter` that can be used to write into every player's |
||||
* console. Returned object should not be deallocated, but it is |
||||
* guaranteed to be valid for non-disconnected players. |
||||
*/ |
||||
public final function ConsoleWriter Console() |
||||
{ |
||||
if ( consoleInstance == none |
||||
|| consoleInstance.GetLifeVersion() != consoleLifeVersion) |
||||
{ |
||||
consoleInstance = _.console.ForAll(); |
||||
consoleLifeVersion = consoleInstance.GetLifeVersion(); |
||||
} |
||||
// Set everybody as a target in case someone messed with this setting |
||||
return consoleInstance.ForAll(); |
||||
} |
||||
|
||||
/** |
||||
* Fetches current array of all players. |
||||
* |
||||
* @return Current array of all players. |
||||
* Guaranteed to not contain `none` values. |
||||
*/ |
||||
public final function array<APlayer> GetPlayers() |
||||
{ |
||||
local PlayerService service; |
||||
local array<APlayer> emptyResult; |
||||
service = PlayerService(class'PlayerService'.static.GetInstance()); |
||||
if (service != none) { |
||||
return service.GetAllPlayers(); |
||||
} |
||||
return emptyResult; |
||||
} |
||||
|
||||
defaultproperties |
||||
{ |
||||
} |
@ -1,53 +0,0 @@
|
||||
/** |
||||
* Event generator for 'ConnectionService'. |
||||
* Copyright 2019 Anton Tarasenko |
||||
*------------------------------------------------------------------------------ |
||||
* This file is part of Acedia. |
||||
* |
||||
* Acedia is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Acedia is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with Acedia. If not, see <https://www.gnu.org/licenses/>. |
||||
*/ |
||||
class ConnectionEvents extends Events |
||||
dependson(ConnectionService) |
||||
abstract; |
||||
|
||||
static function CallConnectionEstablished( |
||||
ConnectionService.Connection connection) |
||||
{ |
||||
local int i; |
||||
local array< class<Listener> > listeners; |
||||
listeners = GetListeners(); |
||||
for (i = 0; i < listeners.length; i += 1) |
||||
{ |
||||
class<ConnectionListenerBase>(listeners[i]) |
||||
.static.ConnectionEstablished(connection); |
||||
} |
||||
} |
||||
|
||||
static function CallConnectionLost(ConnectionService.Connection connection) |
||||
{ |
||||
local int i; |
||||
local array< class<Listener> > listeners; |
||||
listeners = GetListeners(); |
||||
for (i = 0; i < listeners.length; i += 1) |
||||
{ |
||||
class<ConnectionListenerBase>(listeners[i]) |
||||
.static.ConnectionLost(connection); |
||||
} |
||||
} |
||||
|
||||
defaultproperties |
||||
{ |
||||
relatedListener = class'ConnectionListenerBase' |
||||
connectedServiceClass = class'ConnectionService' |
||||
} |
Loading…
Reference in new issue