Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions api/lib/src/event/process/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ Future<ServerResponse?> processClientEvent(
);
case ModeChangeRequest():
final location = event.location;
final mode = location == null
? null
: assetManager.getPack(location.namespace)?.getMode(location.id);
final mode = location == null ? null : assetManager.getModeItem(location);
return UpdateServerResponse.builder(
WorldInitialized.fromMode(mode, state),
channel,
Expand Down
22 changes: 12 additions & 10 deletions api/lib/src/event/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ final class WorldInitialized extends ServerWorldEvent
this.clearUserInterface = false,
});

factory WorldInitialized.fromMode(GameMode? mode, WorldState state) =>
WorldInitialized(
clearUserInterface: true,
info: state.info.copyWith(
teams: mode?.teams ?? {},
script: mode?.script,
),
table: mode?.tables[state.tableName] ?? GameTable(),
teamMembers: const {},
);
factory WorldInitialized.fromMode(
PackItem<GameMode>? mode,
WorldState state,
) => WorldInitialized(
clearUserInterface: true,
info: state.info.copyWith(
teams: mode?.item.teams ?? {},
script: mode?.location,
),
table: mode?.item.tables[state.tableName] ?? GameTable(),
teamMembers: const {},
);
}

@MappableClass()
Expand Down
8 changes: 8 additions & 0 deletions api/lib/src/models/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ final class SetonixConfig with SetonixConfigMappable {
final String? endpointSecret;
static const String defaultEndpointSecret = '';
static const String envEndpointSecret = 'SETONIX_ENDPOINT_SECRET';
final String? gameMode;
static const String defaultGameMode = '';
static const String envGameMode = 'SETONIX_GAME_MODE';

const SetonixConfig({
this.host,
Expand All @@ -55,6 +58,7 @@ final class SetonixConfig with SetonixConfigMappable {
this.accountRequired,
this.apiEndpoint,
this.endpointSecret,
this.gameMode,
});

static const defaultConfig = SetonixConfig(
Expand All @@ -70,6 +74,7 @@ final class SetonixConfig with SetonixConfigMappable {
accountRequired: defaultAccountRequired,
apiEndpoint: defaultApiEndpoint,
endpointSecret: defaultEndpointSecret,
gameMode: defaultGameMode,
);

static SetonixConfig fromEnvironment() {
Expand Down Expand Up @@ -128,6 +133,9 @@ final class SetonixConfig with SetonixConfigMappable {
defaultValue: defaultEndpointSecret,
)
: null,
gameMode: bool.hasEnvironment(envGameMode)
? String.fromEnvironment(envGameMode, defaultValue: defaultGameMode)
: null,
);
}

Expand Down
12 changes: 12 additions & 0 deletions api/lib/src/models/config.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ class SetonixConfigMapper extends ClassMapperBase<SetonixConfig> {
_$endpointSecret,
opt: true,
);
static String? _$gameMode(SetonixConfig v) => v.gameMode;
static const Field<SetonixConfig, String> _f$gameMode = Field(
'gameMode',
_$gameMode,
opt: true,
);

@override
final MappableFields<SetonixConfig> fields = const {
Expand All @@ -108,6 +114,7 @@ class SetonixConfigMapper extends ClassMapperBase<SetonixConfig> {
#accountRequired: _f$accountRequired,
#apiEndpoint: _f$apiEndpoint,
#endpointSecret: _f$endpointSecret,
#gameMode: _f$gameMode,
};

static SetonixConfig _instantiate(DecodingData data) {
Expand All @@ -124,6 +131,7 @@ class SetonixConfigMapper extends ClassMapperBase<SetonixConfig> {
accountRequired: data.dec(_f$accountRequired),
apiEndpoint: data.dec(_f$apiEndpoint),
endpointSecret: data.dec(_f$endpointSecret),
gameMode: data.dec(_f$gameMode),
);
}

Expand Down Expand Up @@ -202,6 +210,7 @@ abstract class SetonixConfigCopyWith<$R, $In extends SetonixConfig, $Out>
bool? accountRequired,
String? apiEndpoint,
String? endpointSecret,
String? gameMode,
});
SetonixConfigCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}
Expand All @@ -228,6 +237,7 @@ class _SetonixConfigCopyWithImpl<$R, $Out>
Object? accountRequired = $none,
Object? apiEndpoint = $none,
Object? endpointSecret = $none,
Object? gameMode = $none,
}) => $apply(
FieldCopyWithData({
if (host != $none) #host: host,
Expand All @@ -242,6 +252,7 @@ class _SetonixConfigCopyWithImpl<$R, $Out>
if (accountRequired != $none) #accountRequired: accountRequired,
if (apiEndpoint != $none) #apiEndpoint: apiEndpoint,
if (endpointSecret != $none) #endpointSecret: endpointSecret,
if (gameMode != $none) #gameMode: gameMode,
}),
);
@override
Expand All @@ -258,6 +269,7 @@ class _SetonixConfigCopyWithImpl<$R, $Out>
accountRequired: data.get(#accountRequired, or: $value.accountRequired),
apiEndpoint: data.get(#apiEndpoint, or: $value.apiEndpoint),
endpointSecret: data.get(#endpointSecret, or: $value.endpointSecret),
gameMode: data.get(#gameMode, or: $value.gameMode),
);

@override
Expand Down
24 changes: 24 additions & 0 deletions api/lib/src/models/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ class SetonixData extends ArchiveData<SetonixData> {
: identifier = identifier ?? createPackIdentifier(data),
super.fromBytes();

factory SetonixData.fromMode(ItemLocation? location, GameMode? mode) {
var data = SetonixData.empty().setInfo(
GameInfo(
packs: [?location?.namespace],
script: location,
teams: mode?.teams ?? const {},
),
);
for (final entry
in mode?.tables.entries ??
Iterable<MapEntry<String, GameTable>>.empty()) {
data = data.setTable(entry.value, entry.key);
}
return data;
}

GameTable? getTable([String name = '']) {
final data = getAsset('$kGameTablePath/$name.json');
if (data == null) return null;
Expand Down Expand Up @@ -305,6 +321,14 @@ class SetonixData extends ArchiveData<SetonixData> {
}
}

PackItem<GameMode>? getModeItem(String id, [String namespace = '']) =>
PackItem.wrap(
pack: this,
namespace: namespace,
id: id,
item: getMode(id),
);

Map<String, GameMode> getModesData() => Map.fromEntries(
getModes().map((e) {
final mode = getMode(e);
Expand Down
2 changes: 1 addition & 1 deletion api/lib/src/models/info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ part 'info.mapper.dart';
class GameInfo with GameInfoMappable {
final Map<String, GameTeam> teams;
final List<String> packs;
final String? script;
final ItemLocation? script;

const GameInfo({this.teams = const {}, this.packs = const [], this.script});
}
Expand Down
15 changes: 12 additions & 3 deletions api/lib/src/models/info.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class GameInfoMapper extends ClassMapperBase<GameInfo> {
if (_instance == null) {
MapperContainer.globals.use(_instance = GameInfoMapper._());
GameTeamMapper.ensureInitialized();
ItemLocationMapper.ensureInitialized();
}
return _instance!;
}
Expand All @@ -118,8 +119,8 @@ class GameInfoMapper extends ClassMapperBase<GameInfo> {
opt: true,
def: const [],
);
static String? _$script(GameInfo v) => v.script;
static const Field<GameInfo, String> _f$script = Field(
static ItemLocation? _$script(GameInfo v) => v.script;
static const Field<GameInfo, ItemLocation> _f$script = Field(
'script',
_$script,
opt: true,
Expand Down Expand Up @@ -200,7 +201,12 @@ abstract class GameInfoCopyWith<$R, $In extends GameInfo, $Out>
MapCopyWith<$R, String, GameTeam, GameTeamCopyWith<$R, GameTeam, GameTeam>>
get teams;
ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get packs;
$R call({Map<String, GameTeam>? teams, List<String>? packs, String? script});
ItemLocationCopyWith<$R, ItemLocation, ItemLocation>? get script;
$R call({
Map<String, GameTeam>? teams,
List<String>? packs,
ItemLocation? script,
});
GameInfoCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}

Expand All @@ -227,6 +233,9 @@ class _GameInfoCopyWithImpl<$R, $Out>
(v) => call(packs: v),
);
@override
ItemLocationCopyWith<$R, ItemLocation, ItemLocation>? get script =>
$value.script?.copyWith.$chain((v) => call(script: v));
@override
$R call({
Map<String, GameTeam>? teams,
List<String>? packs,
Expand Down
1 change: 1 addition & 0 deletions api/lib/src/models/kick.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:dart_mappable/dart_mappable.dart';

part 'kick.mapper.dart';

@MappableEnum()
enum KickReason {
kick,
ban,
Expand Down
63 changes: 63 additions & 0 deletions api/lib/src/models/kick.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,76 @@

part of 'kick.dart';

class KickReasonMapper extends EnumMapper<KickReason> {
KickReasonMapper._();

static KickReasonMapper? _instance;
static KickReasonMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = KickReasonMapper._());
}
return _instance!;
}

static KickReason fromValue(dynamic value) {
ensureInitialized();
return MapperContainer.globals.fromValue(value);
}

@override
KickReason decode(dynamic value) {
switch (value) {
case r'kick':
return KickReason.kick;
case r'ban':
return KickReason.ban;
case r'notWhitelisted':
return KickReason.notWhitelisted;
case r'notRegistered':
return KickReason.notRegistered;
case r'challengeFailed':
return KickReason.challengeFailed;
case r'pleaseLink':
return KickReason.pleaseLink;
default:
throw MapperException.unknownEnumValue(value);
}
}

@override
dynamic encode(KickReason self) {
switch (self) {
case KickReason.kick:
return r'kick';
case KickReason.ban:
return r'ban';
case KickReason.notWhitelisted:
return r'notWhitelisted';
case KickReason.notRegistered:
return r'notRegistered';
case KickReason.challengeFailed:
return r'challengeFailed';
case KickReason.pleaseLink:
return r'pleaseLink';
}
}
}

extension KickReasonMapperExtension on KickReason {
String toValue() {
KickReasonMapper.ensureInitialized();
return MapperContainer.globals.toValue<KickReason>(this) as String;
}
}

class KickMessageMapper extends ClassMapperBase<KickMessage> {
KickMessageMapper._();

static KickMessageMapper? _instance;
static KickMessageMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = KickMessageMapper._());
KickReasonMapper.ensureInitialized();
}
return _instance!;
}
Expand Down
2 changes: 0 additions & 2 deletions api/lib/src/models/mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ final class GameMode with GameModeMappable {
final String? script;

final Map<String, GameTable> tables;
final String tableName;
final Map<String, GameTeam> teams;

GameMode({
required this.script,
this.tables = const {},
this.tableName = '',
this.teams = const {},
});
}
13 changes: 0 additions & 13 deletions api/lib/src/models/mode.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ class GameModeMapper extends ClassMapperBase<GameMode> {
opt: true,
def: const {},
);
static String _$tableName(GameMode v) => v.tableName;
static const Field<GameMode, String> _f$tableName = Field(
'tableName',
_$tableName,
opt: true,
def: '',
);
static Map<String, GameTeam> _$teams(GameMode v) => v.teams;
static const Field<GameMode, Map<String, GameTeam>> _f$teams = Field(
'teams',
Expand All @@ -51,15 +44,13 @@ class GameModeMapper extends ClassMapperBase<GameMode> {
final MappableFields<GameMode> fields = const {
#script: _f$script,
#tables: _f$tables,
#tableName: _f$tableName,
#teams: _f$teams,
};

static GameMode _instantiate(DecodingData data) {
return GameMode(
script: data.dec(_f$script),
tables: data.dec(_f$tables),
tableName: data.dec(_f$tableName),
teams: data.dec(_f$teams),
);
}
Expand Down Expand Up @@ -133,7 +124,6 @@ abstract class GameModeCopyWith<$R, $In extends GameMode, $Out>
$R call({
String? script,
Map<String, GameTable>? tables,
String? tableName,
Map<String, GameTeam>? teams,
});
GameModeCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
Expand Down Expand Up @@ -170,21 +160,18 @@ class _GameModeCopyWithImpl<$R, $Out>
$R call({
Object? script = $none,
Map<String, GameTable>? tables,
String? tableName,
Map<String, GameTeam>? teams,
}) => $apply(
FieldCopyWithData({
if (script != $none) #script: script,
if (tables != null) #tables: tables,
if (tableName != null) #tableName: tableName,
if (teams != null) #teams: teams,
}),
);
@override
GameMode $make(CopyWithData data) => GameMode(
script: data.get(#script, or: $value.script),
tables: data.get(#tables, or: $value.tables),
tableName: data.get(#tableName, or: $value.tableName),
teams: data.get(#teams, or: $value.teams),
);

Expand Down
Loading