2016-07-27 18:41:28 -07:00
using System ;
2016-09-16 10:02:31 -05:00
using System.IO ;
2019-07-14 20:22:40 -07:00
using System.Linq ;
using System.Collections.Generic ;
2016-07-27 18:41:28 -07:00
using Server ;
using Server.Items ;
using Server.Mobiles ;
2016-10-07 19:27:18 -07:00
using Server.Engines.CityLoyalty ;
2016-12-10 13:59:34 -07:00
using Server.Engines.VvV ;
2017-11-18 12:16:26 -07:00
using Server.Engines.ArenaSystem ;
2019-02-24 17:39:27 -07:00
using Server.Engines.SorcerersDungeon ;
2019-07-14 20:22:40 -07:00
using Server.Misc ;
2016-07-27 18:41:28 -07:00
namespace Server.Engines.Points
{
public enum PointsType
{
2016-10-07 19:27:18 -07:00
None = - 1 ,
2016-07-27 18:41:28 -07:00
QueensLoyalty ,
VoidPool ,
DespiseCrystals ,
ShameCrystals ,
2016-10-07 19:27:18 -07:00
CasinoData ,
2016-10-13 15:30:49 -07:00
//City Trading
2016-10-07 19:27:18 -07:00
CityTrading ,
2016-10-13 15:30:49 -07:00
// City Loyalty System
2016-10-07 19:27:18 -07:00
Moonglow ,
Britain ,
Jhelom ,
Yew ,
Minoc ,
Trinsic ,
SkaraBrae ,
NewMagincia ,
Vesper ,
2016-10-13 15:30:49 -07:00
2016-10-31 21:26:20 +03:00
Blackthorn ,
2016-12-10 13:59:34 -07:00
CleanUpBritannia ,
2017-04-06 20:32:08 -07:00
ViceVsVirtue ,
2017-11-14 20:47:02 -07:00
TreasuresOfKotlCity ,
2018-09-10 09:53:51 +03:00
PVPArena ,
2019-02-10 09:35:03 -07:00
Khaldun ,
2019-02-24 17:39:27 -07:00
Doom ,
2019-06-22 23:00:42 -07:00
SorcerersDungeon ,
2019-07-14 20:22:40 -07:00
RisingTide ,
GauntletPoints ,
TOT ,
VAS ,
2020-02-19 05:30:59 +03:00
FellowshipData ,
2016-07-27 18:41:28 -07:00
}
public abstract class PointsSystem
{
2016-09-16 10:02:31 -05:00
public static string FilePath = Path . Combine ( "Saves/PointsSystem" , "Persistence.bin" ) ;
2016-07-27 18:41:28 -07:00
2016-08-31 18:10:55 -07:00
public List < PointsEntry > PlayerTable { get ; set ; }
2016-07-27 18:41:28 -07:00
public abstract TextDefinition Name { get ; }
public abstract PointsType Loyalty { get ; }
public abstract bool AutoAdd { get ; }
public abstract double MaxPoints { get ; }
public virtual bool ShowOnLoyaltyGump { get { return true ; } }
public PointsSystem ( )
{
2016-08-31 18:10:55 -07:00
PlayerTable = new List < PointsEntry > ( ) ;
2016-07-27 18:41:28 -07:00
2016-10-07 19:27:18 -07:00
AddSystem ( this ) ;
}
private static void AddSystem ( PointsSystem system )
{
if ( Systems . FirstOrDefault ( s = > s . Loyalty = = system . Loyalty ) ! = null )
return ;
Systems . Add ( system ) ;
2016-07-27 18:41:28 -07:00
}
2019-04-29 19:56:47 -07:00
public virtual void ProcessKill ( Mobile victim , Mobile damager )
2016-07-27 18:41:28 -07:00
{
}
2019-03-15 21:50:39 -07:00
public virtual void ProcessQuest ( Mobile from , Type quest )
2016-07-27 18:41:28 -07:00
{
}
public virtual void ConvertFromOldSystem ( PlayerMobile from , double points )
{
2016-08-31 18:10:55 -07:00
PointsEntry entry = GetEntry ( from , false ) ;
if ( entry = = null )
2016-07-27 18:41:28 -07:00
{
if ( points > MaxPoints )
points = MaxPoints ;
2016-10-07 19:27:18 -07:00
AddEntry ( from , true ) ;
2016-08-31 18:10:55 -07:00
GetEntry ( from ) . Points = points ;
2016-07-27 18:41:28 -07:00
Utility . PushColor ( ConsoleColor . Green ) ;
Console . WriteLine ( "Converted {0} points for {1} to {2}!" , ( int ) points , from . Name , this . GetType ( ) . Name ) ;
Utility . PopColor ( ) ;
}
}
public virtual void AwardPoints ( Mobile from , double points , bool quest = false , bool message = true )
{
if ( ! ( from is PlayerMobile ) | | points < = 0 )
return ;
2016-08-31 18:10:55 -07:00
PointsEntry entry = GetEntry ( from ) ;
2016-07-27 18:41:28 -07:00
2016-08-31 18:10:55 -07:00
if ( entry ! = null )
2016-07-27 18:41:28 -07:00
{
2016-08-31 18:10:55 -07:00
double old = entry . Points ;
2016-07-27 18:41:28 -07:00
2019-06-05 21:58:34 -07:00
SetPoints ( ( PlayerMobile ) from , Math . Min ( MaxPoints , entry . Points + points ) ) ;
2016-11-03 00:21:53 +03:00
SendMessage ( ( PlayerMobile ) from , old , points , quest ) ;
2016-07-27 18:41:28 -07:00
}
}
public void SetPoints ( PlayerMobile pm , double points )
{
2016-08-31 18:10:55 -07:00
PointsEntry entry = GetEntry ( pm ) ;
if ( entry ! = null )
entry . Points = points ;
2016-07-27 18:41:28 -07:00
}
public virtual void SendMessage ( PlayerMobile from , double old , double points , bool quest )
{
if ( quest )
from . SendLocalizedMessage ( 1113719 , ( ( int ) points ) . ToString ( ) , 0x26 ) ; //You have received ~1_val~ loyalty points as a reward for completing the quest.
else
from . SendLocalizedMessage ( 1115920 , String . Format ( "{0}\t{1}" , Name . ToString ( ) , ( ( int ) points ) . ToString ( ) ) ) ; // Your loyalty to ~1_GROUP~ has increased by ~2_AMOUNT~;Original
}
public virtual bool DeductPoints ( Mobile from , double points , bool message = false )
{
2016-08-31 18:10:55 -07:00
PointsEntry entry = GetEntry ( from ) ;
if ( entry = = null | | entry . Points < points )
2016-07-27 18:41:28 -07:00
{
return false ;
}
else
{
2016-08-31 18:10:55 -07:00
entry . Points - = points ;
2016-07-27 18:41:28 -07:00
if ( message )
from . SendLocalizedMessage ( 1115921 , String . Format ( "{0}\t{1}" , Name . ToString ( ) , ( ( int ) points ) . ToString ( ) ) ) ; // Your loyalty to ~1_GROUP~ has decreased by ~2_AMOUNT~;Original
}
return true ;
}
public virtual void OnPlayerAdded ( PlayerMobile pm )
{
}
2016-10-07 19:27:18 -07:00
public virtual PointsEntry AddEntry ( PlayerMobile pm , bool existed = false )
2016-08-31 18:10:55 -07:00
{
PointsEntry entry = GetSystemEntry ( pm ) ;
if ( ! PlayerTable . Contains ( entry ) )
{
PlayerTable . Add ( entry ) ;
2016-10-07 19:27:18 -07:00
if ( ! existed )
OnPlayerAdded ( pm ) ;
2016-08-31 18:10:55 -07:00
}
return entry ;
}
2016-07-27 18:41:28 -07:00
public double GetPoints ( Mobile from )
{
2016-08-31 18:10:55 -07:00
PointsEntry entry = GetEntry ( from ) ;
if ( entry ! = null )
return entry . Points ;
2016-07-27 18:41:28 -07:00
return 0.0 ;
}
public virtual TextDefinition GetTitle ( PlayerMobile from )
{
return null ;
}
2016-08-31 18:10:55 -07:00
public PointsEntry GetEntry ( Mobile from , bool create = false )
{
2016-10-07 19:27:18 -07:00
PlayerMobile pm = from as PlayerMobile ;
if ( pm = = null )
2016-09-04 12:52:46 -07:00
return null ;
2016-10-07 19:27:18 -07:00
PointsEntry entry = PlayerTable . FirstOrDefault ( e = > e . Player = = pm ) ;
2016-08-31 18:10:55 -07:00
2016-10-07 19:27:18 -07:00
if ( entry = = null & & ( create | | AutoAdd ) )
entry = AddEntry ( pm ) ;
2016-08-31 18:10:55 -07:00
return entry ;
}
2016-10-07 19:27:18 -07:00
public TEntry GetPlayerEntry < TEntry > ( Mobile mobile , bool create = false ) where TEntry : PointsEntry
2016-08-31 18:10:55 -07:00
{
2016-10-07 19:27:18 -07:00
PlayerMobile pm = mobile as PlayerMobile ;
2016-08-31 18:10:55 -07:00
if ( pm = = null )
return null ;
2016-10-07 19:27:18 -07:00
TEntry e = PlayerTable . FirstOrDefault ( p = > p . Player = = pm ) as TEntry ;
if ( e = = null & & ( AutoAdd | | create ) )
e = AddEntry ( pm ) as TEntry ;
return e ;
2016-08-31 18:10:55 -07:00
}
/// <summary>
/// Override this if you are going to derive Points Entry into a bigger and badder class!
/// </summary>
/// <param name="pm"></param>
/// <returns></returns>
public virtual PointsEntry GetSystemEntry ( PlayerMobile pm )
{
return new PointsEntry ( pm ) ;
}
2018-07-15 22:51:21 -07:00
public int Version { get ; set ; }
2016-08-31 18:10:55 -07:00
2016-07-27 18:41:28 -07:00
public virtual void Serialize ( GenericWriter writer )
{
2018-07-15 22:51:21 -07:00
writer . Write ( ( int ) 2 ) ;
2016-07-27 18:41:28 -07:00
writer . Write ( PlayerTable . Count ) ;
2016-08-31 18:10:55 -07:00
PlayerTable . ForEach ( entry = >
{
writer . Write ( entry . Player ) ;
entry . Serialize ( writer ) ;
} ) ;
2016-07-27 18:41:28 -07:00
}
public virtual void Deserialize ( GenericReader reader )
{
2018-07-15 22:51:21 -07:00
Version = reader . ReadInt ( ) ;
2016-07-27 18:41:28 -07:00
2018-07-15 22:51:21 -07:00
switch ( Version )
2016-08-31 18:10:55 -07:00
{
2018-07-15 22:51:21 -07:00
case 2 : // added serialize/deserialize in all base classes. Poor implementation on my part, should have had from the get-go
2016-08-31 18:10:55 -07:00
case 1 :
2018-07-15 22:51:21 -07:00
case 0 :
2018-07-21 21:57:17 +01:00
{
int count = reader . ReadInt ( ) ;
for ( int i = 0 ; i < count ; i + + )
{
PlayerMobile player = reader . ReadMobile ( ) as PlayerMobile ;
PointsEntry entry = GetSystemEntry ( player ) ;
if ( Version > 0 )
entry . Deserialize ( reader ) ;
else
entry . Points = reader . ReadDouble ( ) ;
if ( player ! = null )
{
if ( ! PlayerTable . Contains ( entry ) )
{
PlayerTable . Add ( entry ) ;
}
}
}
}
2016-08-31 18:10:55 -07:00
break ;
}
2016-07-27 18:41:28 -07:00
}
#region Static Methods and Accessors
public static PointsSystem GetSystemInstance ( PointsType t )
{
return Systems . FirstOrDefault ( s = > s . Loyalty = = t ) ;
}
public static void OnSave ( WorldSaveEventArgs e )
{
Persistence . Serialize (
FilePath ,
writer = >
{
2017-02-06 17:48:59 -07:00
writer . Write ( ( int ) 2 ) ;
2016-07-27 18:41:28 -07:00
writer . Write ( Systems . Count ) ;
Systems . ForEach ( s = >
{
writer . Write ( ( int ) s . Loyalty ) ;
s . Serialize ( writer ) ;
} ) ;
} ) ;
}
public static void OnLoad ( )
2018-11-30 19:20:55 -07:00
{
Persistence . Deserialize (
FilePath ,
reader = >
{
int version = reader . ReadInt ( ) ;
2016-10-16 14:08:33 -07:00
2017-02-06 17:48:59 -07:00
if ( version < 2 )
reader . ReadBool ( ) ;
2016-10-16 14:08:33 -07:00
2018-11-30 19:20:55 -07:00
PointsType loaded = PointsType . None ;
2018-01-27 23:22:22 -07:00
2018-11-30 19:20:55 -07:00
int count = reader . ReadInt ( ) ;
for ( int i = 0 ; i < count ; i + + )
{
2018-01-27 23:22:22 -07:00
try
{
PointsType type = ( PointsType ) reader . ReadInt ( ) ;
2018-11-30 19:20:55 -07:00
loaded = type ;
2018-01-27 23:22:22 -07:00
PointsSystem s = GetSystemInstance ( type ) ;
s . Deserialize ( reader ) ;
}
2018-12-01 13:57:11 -07:00
catch
2018-01-27 23:22:22 -07:00
{
2018-11-30 19:20:55 -07:00
throw new Exception ( String . Format ( "Points System Failed Load: {0} Last Loaded..." , loaded . ToString ( ) ) ) ;
2018-01-27 23:22:22 -07:00
}
2018-11-30 19:20:55 -07:00
}
} ) ;
}
2016-07-27 18:41:28 -07:00
public static List < PointsSystem > Systems { get ; set ; }
public static QueensLoyalty QueensLoyalty { get ; set ; }
public static VoidPool VoidPool { get ; set ; }
public static DespiseCrystals DespiseCrystals { get ; set ; }
public static ShameCrystals ShameCrystals { get ; set ; }
public static CasinoData CasinoData { get ; set ; }
2016-10-12 21:38:34 +03:00
public static BlackthornData Blackthorn { get ; set ; }
2016-10-31 21:26:20 +03:00
public static CleanUpBritanniaData CleanUpBritannia { get ; set ; }
2016-12-10 13:59:34 -07:00
public static ViceVsVirtueSystem ViceVsVirtue { get ; set ; }
2017-04-06 20:32:08 -07:00
public static KotlCityData TreasuresOfKotlCity { get ; set ; }
2017-11-14 20:47:02 -07:00
public static PVPArenaSystem ArenaSystem { get ; set ; }
2018-09-10 09:53:51 +03:00
public static KhaldunData Khaldun { get ; set ; }
2019-02-10 09:35:03 -07:00
public static DoomData TreasuresOfDoom { get ; set ; }
2019-02-24 17:39:27 -07:00
public static SorcerersDungeonData SorcerersDungeon { get ; set ; }
2019-06-22 23:00:42 -07:00
public static RisingTide RisingTide { get ; set ; }
2019-07-14 20:22:40 -07:00
public static DoomGauntlet DoomGauntlet { get ; set ; }
public static TreasuresOfTokuno TreasuresOfTokuno { get ; set ; }
public static VirtueArtifactsSystem VirtueArtifacts { get ; set ; }
2020-02-19 05:30:59 +03:00
public static FellowshipData FellowshipData { get ; set ; }
2016-07-27 18:41:28 -07:00
public static void Configure ( )
{
EventSink . WorldSave + = OnSave ;
EventSink . WorldLoad + = OnLoad ;
2019-03-15 21:50:39 -07:00
EventSink . QuestComplete + = CompleteQuest ;
2019-04-29 19:56:47 -07:00
EventSink . OnKilledBy + = OnKilledBy ;
2016-07-27 18:41:28 -07:00
Systems = new List < PointsSystem > ( ) ;
QueensLoyalty = new QueensLoyalty ( ) ;
VoidPool = new VoidPool ( ) ;
DespiseCrystals = new DespiseCrystals ( ) ;
ShameCrystals = new ShameCrystals ( ) ;
CasinoData = new CasinoData ( ) ;
2016-10-12 21:38:34 +03:00
Blackthorn = new BlackthornData ( ) ;
2016-10-31 21:26:20 +03:00
CleanUpBritannia = new CleanUpBritanniaData ( ) ;
2016-12-10 13:59:34 -07:00
ViceVsVirtue = new ViceVsVirtueSystem ( ) ;
2017-04-06 20:32:08 -07:00
TreasuresOfKotlCity = new KotlCityData ( ) ;
2016-10-07 19:27:18 -07:00
CityLoyaltySystem . ConstructSystems ( ) ;
2017-11-18 12:16:26 -07:00
ArenaSystem = new PVPArenaSystem ( ) ;
2018-09-10 09:53:51 +03:00
Khaldun = new KhaldunData ( ) ;
2019-02-10 09:35:03 -07:00
TreasuresOfDoom = new DoomData ( ) ;
2019-02-24 17:39:27 -07:00
SorcerersDungeon = new SorcerersDungeonData ( ) ;
2019-06-22 23:00:42 -07:00
RisingTide = new RisingTide ( ) ;
2019-07-14 20:22:40 -07:00
DoomGauntlet = new DoomGauntlet ( ) ;
TreasuresOfTokuno = new TreasuresOfTokuno ( ) ;
VirtueArtifacts = new VirtueArtifactsSystem ( ) ;
2020-02-19 05:30:59 +03:00
FellowshipData = new FellowshipData ( ) ;
2016-07-27 18:41:28 -07:00
}
2019-04-29 19:56:47 -07:00
public static void OnKilledBy ( OnKilledByEventArgs e )
2016-07-27 18:41:28 -07:00
{
2019-04-29 19:56:47 -07:00
OnKilledBy ( e . Killed , e . KilledBy ) ;
}
public static void OnKilledBy ( Mobile victim , Mobile damager )
{
Systems . ForEach ( s = > s . ProcessKill ( victim , damager ) ) ;
2016-07-27 18:41:28 -07:00
}
2019-03-15 21:50:39 -07:00
public static void CompleteQuest ( QuestCompleteEventArgs e )
2016-07-27 18:41:28 -07:00
{
2019-03-15 21:50:39 -07:00
Systems . ForEach ( s = > s . ProcessQuest ( e . Mobile , e . QuestType ) ) ;
2016-07-27 18:41:28 -07:00
}
#endregion
}
2016-08-31 18:10:55 -07:00
public class PointsEntry
{
2019-05-22 19:44:13 -07:00
[CommandProperty(AccessLevel.GameMaster)]
public PlayerMobile Player { get ; private set ; }
[CommandProperty(AccessLevel.GameMaster)]
public double Points { get ; set ; }
2016-08-31 18:10:55 -07:00
public PointsEntry ( PlayerMobile pm )
{
Player = pm ;
}
public PointsEntry ( PlayerMobile pm , double points )
{
Player = pm ;
Points = points ;
}
2016-10-07 19:27:18 -07:00
public override bool Equals ( object o )
{
if ( o = = null | | ! ( o is PointsEntry ) )
{
return false ;
}
return ( ( PointsEntry ) o ) . Player = = Player ;
}
public override int GetHashCode ( )
{
if ( Player ! = null )
return Player . GetHashCode ( ) ;
return base . GetHashCode ( ) ;
}
2016-08-31 18:10:55 -07:00
public virtual void Serialize ( GenericWriter writer )
{
writer . Write ( 0 ) ;
writer . Write ( Player ) ;
writer . Write ( Points ) ;
}
public virtual void Deserialize ( GenericReader reader )
{
int version = reader . ReadInt ( ) ;
Player = reader . ReadMobile ( ) as PlayerMobile ;
Points = reader . ReadDouble ( ) ;
}
}
2018-09-10 09:53:51 +03:00
}